在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 在vue-cli中如何將所有js以及css打包成一個js文件

在vue-cli中如何將所有js以及css打包成一個js文件

在vue-cli中如何將所有js以及css打包成一個js文件,如果做不到,如何將app.js manifest.js vendor.js打包成一個文件

回答
編輯回答
刮刮樂

為什么會有這樣的需求?
如果是為了優(yōu)化性能,這樣做得不償失,雖然減少url請求會有一定的性能優(yōu)化,但這樣會導致你每次修改代碼會生成一個帶有不同hash的js文件,這樣的話瀏覽器的緩存作用會微乎其微
vue-cli 會把你的所有js打包成3個js,
app.js 自己寫的代碼
manifest.js 一個映射文件
vendor.js 你使用的庫打包后的文件(jq、vue之類的)
使用這種方式自己更改代碼后manifest.js、app.js會重新打包生成新的文件,vendor.js文件不會重新打包,這樣vendor.js就會從緩存中讀取

2017年3月17日 10:54
編輯回答
吢涼

這樣的需求不是很合理啊。具體參考樓上回答。
但非要實現(xiàn)也可以。
1、再怎么樣,我也建議提取css建議。不然等待JS加載完,才顯示樣式。用戶明顯可以看出樣式缺失,然后才有樣式。
提取使用的插件是用的extract-text-webpack-plugin。
2、從vue-cli @2.9.x版本的build/webpack.prod.conf.js 來看,會提取公共的JS代碼到mainfest.js,vendor.js,vendor-async.js。如果不需要就注釋即可。
剛好之前寫了篇分析vue-cli構建的文章,分析vue-cli@2.9.3 搭建的webpack項目工程,附上一些代碼和注釋可供參考。

// 提取公共代碼
new webpack.optimize.CommonsChunkPlugin({
  name: 'vendor',
  minChunks (module) {
    // any required modules inside node_modules are extracted to vendor
    return (
      module.resource &&
      /\.js$/.test(module.resource) &&
      module.resource.indexOf(
        path.join(__dirname, '../node_modules')
      ) === 0
    )
  }
}),
// extract webpack runtime and module manifest to its own file in order to
// prevent vendor hash from being updated whenever app bundle is updated
// 提取公共代碼
new webpack.optimize.CommonsChunkPlugin({
  // 把公共的部分放到 manifest 中
  name: 'manifest',
  // 傳入 `Infinity` 會馬上生成 公共chunk,但里面沒有模塊。
  minChunks: Infinity
}),
// This instance extracts shared chunks from code splitted chunks and bundles them
// in a separate chunk, similar to the vendor chunk
// see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
// 提取動態(tài)組件
new webpack.optimize.CommonsChunkPlugin({
  name: 'app',
  // 如果設置為 `true`,一個異步的  公共chunk 會作為 `options.name` 的子模塊,和 `options.chunks` 的兄弟模塊被創(chuàng)建。
  // 它會與 `options.chunks` 并行被加載??梢酝ㄟ^提供想要的字符串,而不是 `true` 來對輸出的文件進行更換名稱。
  async: 'vendor-async',
  // 如果設置為 `true`,所有  公共chunk 的子模塊都會被選擇
  children: true,
  // 最小3個,包含3,chunk的時候提取
  minChunks: 3
}),
2017年12月16日 04:07