天天看點

webStorm 打包Vue項目

webStorm打包vue項目

之前在網上看到過好多打包vue項目的方法,感覺不是很全面,最後自己總結了一下。

首先,如果需要打包的話,你需要改下資源的路徑

1、打開build下面的utils.js檔案,添加publicPath:“…/…/”

if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        publicPath:"../../",
        fallback: 'vue-style-loader'
           

2、打開config下面的index.js檔案,找到build下的assetsSubDirectory和assetsPublicPath,将它們修改為如下代碼所示

build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: './static',
    assetsPublicPath: './',
           

3、在指令行中輸入 npm run build,指令行中出現如下所示即可

Hash: 978ffe2a97c464742abd
Version: webpack 3.12.0
Time: 79400ms
                                                  Asset       Size  Chunks                    Chunk Names
                              static/images/eyeopen.png    4.04 kB          [emitted]         
                             static/img/w16.89bac29.jpg    18.4 kB          [emitted]   
                             ................................
  Build complete.

  Tip: built files are meant to be served over an HTTP server.
  Opening index.html over file:// won't work.
           

4、執行完上一步後,目錄中會出現一個dist目錄,到此就打包完成了

webStorm 打包Vue項目

5、有可能打開index.html頁面中圖檔路徑會報錯,可以将頁面中引入的路徑改為"./static…"即可,改完之後必須重新打包一次。

打包完成之後,代碼中有任何的修改都必須重新打包