Skip to content

Commit 5bf209f

Browse files
committed
增加dllplugin配置
1 parent 50d3410 commit 5bf209f

13 files changed

+378
-185
lines changed

build/utils.js

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin')
66
const packageConfig = require('../package.json')
77
const entriePaths = getEntries()
88
const HtmlWebpackPlugin = require('html-webpack-plugin')
9+
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')
10+
const dllConfigs = require('../config/dll.conf')
11+
912

1013
exports.assetsPath = function (_path) {
1114
const assetsSubDirectory = process.env.NODE_ENV === 'production'
@@ -125,6 +128,17 @@ exports.htmlPlugins = function() {
125128
}))
126129
})
127130

131+
if (dllConfigs.isUsed && process.env.NODE_ENV === 'production') {
132+
arr.push(new AddAssetHtmlPlugin([
133+
{
134+
filepath: dllConfigs.filepath,
135+
outputPath: dllConfigs.outputPath,
136+
publicPath: dllConfigs.publicPath,
137+
includeSourcemap: false
138+
}
139+
]))
140+
}
141+
128142
return arr
129143
}
130144

@@ -133,6 +147,9 @@ function getPath(...args) {
133147
return path.join(path.resolve(__dirname, '../src'), ...args);
134148
}
135149

150+
/*
151+
* 获取页面入口
152+
* */
136153
function getEntries() {
137154
const files = glob.sync(`${config.base.pagesRoot}/*/*.js`)
138155
let extries = {}

build/webpack.base.conf.js

+3-66
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,9 @@
22
const path = require('path')
33
const utils = require('./utils')
44
const config = require('../config')
5-
const vueLoaderConfig = require('./vue-loader.conf')
5+
const webpackConfig = require('../config/prod.webpack')
66
const webpack = require('webpack')
77

8-
function resolve (dir) {
9-
return path.join(__dirname, '..', dir)
10-
}
11-
12-
const createLintingRule = () => ({
13-
test: /\.(js|vue)$/,
14-
loader: 'eslint-loader',
15-
enforce: 'pre',
16-
include: [resolve('src'), resolve('test')],
17-
options: {
18-
formatter: require('eslint-friendly-formatter'),
19-
emitWarning: !config.dev.showEslintErrorsInOverlay
20-
}
21-
})
22-
238
module.exports = {
249
context: path.resolve(__dirname, '../'),
2510
entry: utils.entries(),
@@ -30,58 +15,10 @@ module.exports = {
3015
? config.build.assetsPublicPath
3116
: config.dev.assetsPublicPath
3217
},
33-
resolve: {
34-
extensions: ['.js', '.vue', '.json'],
35-
alias: {
36-
'vue$': 'vue/dist/vue.esm.js',
37-
'@': resolve('src'),
38-
}
39-
},
40-
module: {
41-
rules: [
42-
...(config.dev.useEslint ? [createLintingRule()] : []),
43-
{
44-
test: /\.vue$/,
45-
loader: 'vue-loader',
46-
options: vueLoaderConfig
47-
},
48-
{
49-
test: /\.js$/,
50-
loader: 'babel-loader',
51-
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
52-
},
53-
{
54-
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
55-
loader: 'url-loader',
56-
options: {
57-
limit: 10000,
58-
name: utils.assetsPath('img/[name].[hash:7].[ext]')
59-
}
60-
},
61-
{
62-
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
63-
loader: 'url-loader',
64-
options: {
65-
limit: 10000,
66-
name: utils.assetsPath('media/[name].[hash:7].[ext]')
67-
}
68-
},
69-
{
70-
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
71-
loader: 'url-loader',
72-
options: {
73-
limit: 10000,
74-
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
75-
}
76-
}
77-
]
78-
},
18+
resolve: webpackConfig.resolvesConfig,
19+
module: webpackConfig.modulesConfig,
7920
node: {
80-
// prevent webpack from injecting useless setImmediate polyfill because Vue
81-
// source contains it (although only uses it if it's native).
8221
setImmediate: false,
83-
// prevent webpack from injecting mocks to Node native modules
84-
// that does not make sense for the client
8522
dgram: 'empty',
8623
fs: 'empty',
8724
net: 'empty',

build/webpack.dll.conf.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Created by [email protected] on 2018/3/13.
3+
*/
4+
5+
const webpack = require('webpack')
6+
const path = require('path')
7+
const ExtractTextPlugin = require('extract-text-webpack-plugin')
8+
const config = require('../config')
9+
const webpackConfig = require('../config/prod.webpack')
10+
const dllConfigs = require('../config/dll.conf')
11+
const AssetsPlugin = require('assets-webpack-plugin')
12+
13+
module.exports = {
14+
output: {
15+
path: dllConfigs.devPath,
16+
filename: '[name].[hash:8].js',
17+
library: '[name]_libs', // 注意与DllPlugin的name参数保持一致 [name]为entry的key
18+
},
19+
entry: {
20+
[dllConfigs.fileName]: dllConfigs.vendor
21+
},
22+
plugins: [
23+
new webpack.optimize.ModuleConcatenationPlugin(),
24+
new webpack.DllPlugin({
25+
path: 'manifest.json', // DllReferencePlugin使用
26+
name: '[name]_libs', // 确保output.library一致
27+
context: __dirname, // 建议设置为项目根目录
28+
}),
29+
new webpack.optimize.UglifyJsPlugin({
30+
compress: {
31+
warnings: true
32+
}
33+
}),
34+
new AssetsPlugin({
35+
filename: `${dllConfigs.fileName}.json`,
36+
path: dllConfigs.devPath
37+
}),
38+
new ExtractTextPlugin('[name].[hash:8].css'),
39+
],
40+
resolve: webpackConfig.resolvesConfig,
41+
module: webpackConfig.modulesConfig,
42+
};

build/webpack.dll.config.js

-32
This file was deleted.

build/webpack.prod.conf.js

+2-75
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ const webpack = require('webpack')
55
const config = require('../config')
66
const merge = require('webpack-merge')
77
const baseWebpackConfig = require('./webpack.base.conf')
8-
const CopyWebpackPlugin = require('copy-webpack-plugin')
9-
const ExtractTextPlugin = require('extract-text-webpack-plugin')
10-
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
11-
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
12-
8+
const prodWepbackConfig = require('../config/prod.webpack')
139
const env = require('../config/prod.env')
1410

1511
const webpackConfig = merge(baseWebpackConfig, {
@@ -26,76 +22,7 @@ const webpackConfig = merge(baseWebpackConfig, {
2622
filename: utils.assetsPath('js/[name].[chunkhash:8].js'),
2723
chunkFilename: utils.assetsPath('js/[id].[chunkhash:8].js')
2824
},
29-
plugins: [
30-
// http://vuejs.github.io/vue-loader/en/workflow/production.html
31-
new webpack.DefinePlugin({
32-
'process.env': env
33-
}),
34-
new UglifyJsPlugin({
35-
uglifyOptions: {
36-
compress: {
37-
warnings: false
38-
}
39-
},
40-
sourceMap: config.build.productionSourceMap,
41-
parallel: true
42-
}),
43-
// extract css into its own file
44-
new ExtractTextPlugin({
45-
filename: utils.assetsPath('css/[name].[contenthash:8].css'),
46-
// Setting the following option to `false` will not extract CSS from codesplit chunks.
47-
// Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
48-
// It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
49-
// increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
50-
allChunks: true,
51-
}),
52-
// Compress extracted CSS. We are using this plugin so that possible
53-
// duplicated CSS from different components can be deduped.
54-
new OptimizeCSSPlugin({
55-
cssProcessorOptions: config.build.productionSourceMap
56-
? { safe: true, map: { inline: false } }
57-
: { safe: true }
58-
}),
59-
60-
// keep module.id stable when vendor modules does not change
61-
new webpack.HashedModuleIdsPlugin(),
62-
// enable scope hoisting
63-
new webpack.optimize.ModuleConcatenationPlugin(),
64-
65-
// copy custom static assets
66-
new CopyWebpackPlugin([
67-
{
68-
from: path.resolve(__dirname, '../static'),
69-
to: config.build.assetsSubDirectory,
70-
ignore: ['.*']
71-
}
72-
]),
73-
74-
new webpack.optimize.CommonsChunkPlugin({
75-
name: 'vendor',
76-
minChunks (module) {
77-
return (
78-
module.resource &&
79-
/\.js$/.test(module.resource) &&
80-
module.resource.indexOf(
81-
path.join(__dirname, '../node_modules')
82-
) === 0
83-
)
84-
}
85-
}),
86-
87-
new webpack.optimize.CommonsChunkPlugin({
88-
name: 'manifest',
89-
minChunks: Infinity
90-
}),
91-
92-
new webpack.optimize.CommonsChunkPlugin({
93-
name: 'app',
94-
async: 'vendor-async',
95-
children: true,
96-
minChunks: 3
97-
})
98-
].concat(utils.htmlPlugins())
25+
plugins: prodWepbackConfig.pluginsConfig()
9926
})
10027

10128
if (config.build.productionGzip) {

config/dll.conf.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Created by [email protected] on 2018/3/13.
3+
*/
4+
const path = require('path')
5+
const fileName = 'vendor_dll'
6+
const dllProdName = require(`../src/units/libs/${fileName}.json`)
7+
module.exports = {
8+
isUsed: true, //是否开启DllPlugins
9+
fileName: fileName,
10+
devPath: path.resolve(__dirname, '../src/units/libs'),
11+
filepath: path.resolve(__dirname, `../src/units/libs/${dllProdName[fileName].js}`),
12+
outputPath: 'js',
13+
publicPath: '/js/',
14+
vendor: [
15+
'vue',
16+
'vue-router'
17+
]
18+
}

config/index.js

-8
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,5 @@ module.exports = {
7878
base: {
7979
"isMultiPage": true,
8080
"pagesRoot": path.resolve(__dirname, '../src/pages')
81-
},
82-
dll: {
83-
isOk: false,
84-
path: path.resolve(__dirname, '../src/units/libs'),
85-
libs: [
86-
'vue',
87-
'vue-router'
88-
]
8981
}
9082
}

0 commit comments

Comments
 (0)