-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
219 lines (195 loc) · 6.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const path = require('path');
const {
baseDllPath,
log,
isInstallOf,
forEachObj,
isFunctionAndCall
} = require('./service/helper');
const Dll = require('./service/dll.js');
module.exports = (api, options) => {
const webpack = require('webpack');
const dllConfig = (options.pluginOptions && options.pluginOptions.dll) || {};
const dll = new Dll(api.resolveWebpackConfig(), dllConfig);
api.chainWebpack(config => {
if (!dll.isOpen || dll.isCommand === true) return;
const referenceArgs = dll.resolveDllReferenceArgs();
config.when(referenceArgs.length !== 0, config => {
// add DllReferencePlugins
referenceArgs.forEach(args => {
config
.plugin(`dll-reference-${args.manifest.name}`)
.use(webpack.DllReferencePlugin, [args]);
});
// auto inject
if (dll.inject) {
config
.plugin('dll-add-asset-html')
.use(
require('add-asset-html-webpack-plugin'),
dll.resolveAddAssetHtmlArgs()
);
// add copy agrs
config.plugin('copy').tap(args => {
// 解析最后一部分
const lastPath = path.basename(dll.outputPath);
// console.log('lastPath' + lastPath);
args[0][0].ignore.push(`${lastPath}/**`);
args[0][0].ignore.push(`${lastPath}/*.manifest.json`);
args[0].push({
from: dll.outputPath,
toType: 'dir',
ignore: ['*.js', '*.css', '*.manifest.json']
});
// console.log('args', args, args[0]);
// console.log('args', path.dirname(dll.outputPath));
// console.log(process.cwd(), dll.outputPath);
// console.log(path.relative(process.cwd(), dll.outputPath));
return args;
});
// console.log(config);
}
});
});
api.registerCommand(
'dll',
{
description: 'build dll',
usage: 'vue-cli-service dll',
options: {}
},
async function (args) {
dll.callCommand();
// entry parameter can not be empty
if (!dll.validateEntry()) {
throw Error('"entry" parameter no found, more config url:');
}
const FileNameCachePlugin = require('./service/fileNameCachePlugin');
api.chainWebpack(config => {
const extractOptions = Object.assign({
filename: `${baseDllPath}/css/[name].[contenthash:8].css`,
chunkFilename: `${baseDllPath}/css/[name].[contenthash:8].css`
}, {});
config
.plugin('dll')
.use(webpack.DllPlugin, dll.resolveDllArgs())
.end()
.plugin('file-list-plugin')
.use(FileNameCachePlugin);
config.optimization.delete('splitChunks');
config.optimization.delete('runtimeChunk');
config.devtool(false);
// console.log('config', config);
// fonts images media
const staticRes = ['fonts', 'images', 'media'];
staticRes.forEach((type) => genUrlLoaderOptions(type));
function genUrlLoaderOptions(type) {
config.module
.rule(type)
.use('url-loader')
.loader('url-loader')
.tap((options) => {
// options.fallback loader -> file-loader
options.fallback.options['name'] = `${baseDllPath}/${type === 'images' ? 'img' : type}/[name].[hash:8].[ext]`;
return options;
})
.end();
}
// svg
config.module
.rule('svg')
.use('file-loader')
.loader('file-loader')
.tap((options) => {
options['name'] = `${baseDllPath}/img/[name].[hash:8].[ext]`;
return options;
})
.end();
// 样式文件里的引用的静态资源地址,如 image,fonts...
const langs = ['css', 'postcss', 'scss', 'sass', 'less', 'stylus'];
const matches = ['vue-modules', 'vue', 'normal-modules', 'normal'];
langs.forEach(lang =>
matches.forEach(match =>
config.module
.rule(lang)
.oneOf(match)
.use('extract-css-loader')
.loader(require('mini-css-extract-plugin').loader)
.options({
publicPath: '../../'
})
)
);
// 这种方式也可以
// const types = ['vue-modules', 'vue', 'normal-modules', 'normal'];
// types.forEach(type => addStyleResource(config.module.rule('css').oneOf(type)));
//
// function addStyleResource(rule) {
// rule.use('extract-css-loader')
// .loader(require('mini-css-extract-plugin').loader)
// .options({
// publicPath: './'
// }).end();
// }
// mini-css-extract-plugin
config
.plugin('extract-css')
.use(require('mini-css-extract-plugin'), [extractOptions]);
// set output
forEachObj(dll.resolveOutput(), (fnName, value) => {
// console.log('fnName, value', fnName, value);
isFunctionAndCall(
config.output[fnName],
config.output,
value
);
});
});
let webpackConfig = api.resolveWebpackConfig();
let {VueLoaderPlugin} = require('vue-loader');
let DefinePlugin = require('webpack/lib/DefinePlugin');
let FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
let NamedChunksPlugin = require('webpack/lib/NamedChunksPlugin');
let MiniCssExtractPlugin = require('mini-css-extract-plugin');
let fs = require('fs-extra');
// filter plugins
webpackConfig.plugins = webpackConfig.plugins.filter(i =>
isInstallOf(
i,
VueLoaderPlugin,
DefinePlugin,
FriendlyErrorsWebpackPlugin,
NamedChunksPlugin,
MiniCssExtractPlugin,
webpack.DllPlugin,
FileNameCachePlugin
)
);
// console.log('webpack', JSON.stringify(webpackConfig.plugins));
// console.log('webpack', webpackConfig.plugins[2]);
// webpackConfig.plugins[2].options.filename = '[name].[contenthash:8].css';
// webpackConfig.plugins[2].options.chunkFilename = '[name].[contenthash:8].css';
// reset entry
webpackConfig.entry = dll.resolveEntry();
// webpackConfig.output.publicPath = './';
// console.log('webpackConfig', JSON.stringify(webpackConfig, null, 2));
// remove dir
fs.remove(dll.outputPath);
log('Starting build dll...');
return new Promise((resolve, reject) => {
webpack(webpackConfig, (err, stats) => {
if (err) {
return reject(err);
} else if (stats.hasErrors()) {
return reject(new Error('Build failed with errors.'));
}
log('Build complete.');
resolve();
});
});
}
);
};
module.exports.defaultModes = {
dll: 'production'
};