Skip to content

Commit 7fc8768

Browse files
'优化webpack打包'
1 parent 3a87753 commit 7fc8768

23 files changed

+2705
-39
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.DS_Store
22
node_modules
33
/dist
4-
4+
/web-serve-until/node_modules
55
# local env files
66
.env.local
77
.env.*.local

build/build.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
require('./check-versions')();
3+
4+
process.env.OPT_TYPE = 'deploy';
5+
process.env.NODE_ENV = 'production';
6+
7+
const ora = require('ora');
8+
const rm = require('rimraf');
9+
const path = require('path');
10+
const chalk = require('chalk');
11+
const webpack = require('webpack');
12+
const config = require('../config');
13+
const webpackConfig = require('./webpack.prod.conf');
14+
15+
const spinner = ora('building for production...');
16+
spinner.start();
17+
18+
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
19+
if (err) throw err;
20+
webpack(webpackConfig, (err, stats) => {
21+
spinner.stop();
22+
if (err) throw err;
23+
process.stdout.write(stats.toString({
24+
colors: true,
25+
modules: false,
26+
children: false,
27+
chunks: false,
28+
chunkModules: false,
29+
}) + '\n\n');
30+
31+
if (stats.hasErrors()) {
32+
console.log(chalk.red(' Build failed with errors.\n'));
33+
process.exit(1);
34+
}
35+
36+
console.log(chalk.cyan(' Build complete.\n'));
37+
console.log(chalk.yellow(
38+
' Tip: built files are meant to be served over an HTTP server.\n' +
39+
' Opening index.html over file:// won\'t work.\n',
40+
));
41+
});
42+
});

build/check-version.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
const chalk = require('chalk');
3+
const semver = require('semver');
4+
const packageConfig = require('../package.json');
5+
const shell = require('shelljs');
6+
7+
function exec (cmd) {
8+
return require('child_process').execSync(cmd).toString().trim();
9+
}
10+
11+
const versionRequirements = [
12+
{
13+
name: 'node',
14+
currentVersion: semver.clean(process.version),
15+
versionRequirement: packageConfig.engines.node,
16+
},
17+
];
18+
19+
if (shell.which('npm')) {
20+
versionRequirements.push({
21+
name: 'npm',
22+
currentVersion: exec('npm --version'),
23+
versionRequirement: packageConfig.engines.npm,
24+
});
25+
}
26+
27+
module.exports = function () {
28+
const warnings = [];
29+
30+
for (let i = 0; i < versionRequirements.length; i++) {
31+
const mod = versionRequirements[i];
32+
33+
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
34+
warnings.push(mod.name + ': ' +
35+
chalk.red(mod.currentVersion) + ' should be ' +
36+
chalk.green(mod.versionRequirement),
37+
);
38+
}
39+
}
40+
41+
if (warnings.length) {
42+
console.log('');
43+
console.log(chalk.yellow('To use this template, you must update following to modules:'));
44+
console.log();
45+
46+
for (let i = 0; i < warnings.length; i++) {
47+
const warning = warnings[i];
48+
console.log(' ' + warning);
49+
}
50+
51+
console.log();
52+
process.exit(1);
53+
}
54+
};

build/thread-loader.conf

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = {
2+
options: {
3+
// 产生的 worker 的数量,默认是 (cpu 核心数 - 1)
4+
// 或者,在 require('os').cpus() 是 undefined 时回退至 1
5+
// workers: require('os').cpus() - 1,
6+
7+
// 一个 worker 进程中并行执行工作的数量
8+
// 默认为 20
9+
workerParallelJobs: 20,
10+
11+
// 额外的 Node.js 参数
12+
workerNodeArgs: ['--max-old-space-size=4096'],
13+
14+
// Allow to respawn a dead worker pool
15+
// respawning slows down the entire compilation
16+
// and should be set to false for development
17+
poolRespawn: false,
18+
19+
// 闲置时定时删除 worker 进程
20+
// 默认为 500ms
21+
// 可以设置为无穷大, 这样在监视模式(--watch)下可以保持 worker 持续存在
22+
poolTimeout: 500,
23+
24+
// 池(pool)分配给 worker 的工作数量
25+
// 默认为 200
26+
// 降低这个数值会降低总体的效率,但是会提升工作分布更均一
27+
poolParallelJobs: 200,
28+
29+
},
30+
};

build/utils.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'use strict';
2+
const path = require('path');
3+
const config = require('../config');
4+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
5+
const packageConfig = require('../package.json');
6+
7+
exports.assetsPath = function (_path) {
8+
const assetsSubDirectory = process.env.NODE_ENV === 'production'
9+
? config.build.assetsSubDirectory
10+
: config.dev.assetsSubDirectory;
11+
12+
return path.posix.join(assetsSubDirectory, _path);
13+
};
14+
15+
exports.cssLoaders = function (options) {
16+
options = options || {};
17+
18+
const cssLoader = {
19+
loader: 'css-loader',
20+
options: {
21+
sourceMap: options.sourceMap,
22+
},
23+
};
24+
25+
const postcssLoader = {
26+
loader: 'postcss-loader',
27+
options: {
28+
sourceMap: options.sourceMap,
29+
},
30+
};
31+
32+
// generate loader string to be used with extract text plugin
33+
function generateLoaders (loader, loaderOptions) {
34+
const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader];
35+
36+
if (loader) {
37+
loaders.push({
38+
loader: loader + '-loader',
39+
options: Object.assign({}, loaderOptions, {
40+
sourceMap: options.sourceMap,
41+
}),
42+
});
43+
}
44+
45+
// Extract CSS when that option is specified
46+
// (which is the case during production build)
47+
if (options.extract) {
48+
return [MiniCssExtractPlugin.loader].concat(loaders);
49+
} else {
50+
return ['vue-style-loader'].concat(loaders);
51+
}
52+
}
53+
54+
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
55+
return {
56+
css: generateLoaders(),
57+
postcss: generateLoaders(),
58+
less: generateLoaders('less', {
59+
javascriptEnabled: true,
60+
}),
61+
sass: generateLoaders('sass', { indentedSyntax: true }),
62+
scss: generateLoaders('sass'),
63+
stylus: generateLoaders('stylus'),
64+
styl: generateLoaders('stylus'),
65+
};
66+
};
67+
68+
// Generate loaders for standalone style files (outside of .vue)
69+
exports.styleLoaders = function (options) {
70+
const output = [];
71+
const loaders = exports.cssLoaders(options);
72+
73+
for (const extension in loaders) {
74+
const loader = loaders[extension];
75+
output.push({
76+
test: new RegExp('\\.' + extension + '$'),
77+
use: loader,
78+
});
79+
}
80+
81+
return output;
82+
};
83+
84+
exports.createNotifierCallback = () => {
85+
const notifier = require('node-notifier');
86+
87+
return (severity, errors) => {
88+
if (severity !== 'error') return;
89+
90+
const error = errors[0];
91+
const filename = error.file && error.file.split('!').pop();
92+
93+
notifier.notify({
94+
title: packageConfig.name,
95+
message: severity + ': ' + error.name,
96+
subtitle: filename || '',
97+
icon: path.join(__dirname, 'logo.png'),
98+
});
99+
};
100+
};

0 commit comments

Comments
 (0)