Skip to content

Commit a14b426

Browse files
committed
Update build tools 🚀
- adding _standard_ poi plugins
1 parent a23ad51 commit a14b426

File tree

7 files changed

+272
-27
lines changed

7 files changed

+272
-27
lines changed

Diff for: build/plugins/clean-out-dir.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
exports.name = 'clean-out-dir';
4+
5+
exports.apply = (api, options = {}) => {
6+
api.hook('createWebpackChain', config => {
7+
if (!api.isProd || !api.config.output.clean) return;
8+
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
9+
options.verbose = Boolean(api.args.options.debug);
10+
config
11+
.plugin('clean-out-dir')
12+
.use(CleanWebpackPlugin, [options]);
13+
});
14+
};

Diff for: build/plugins/html-version-spec.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const { execFileSync } = require('child_process');
4+
5+
const execSync = (cmd, args, opts) => execFileSync(cmd, args, opts).toString().trim();
6+
const forEach = (obj, cb) => Object.keys(obj).forEach(key => cb(obj[key], key));
7+
const isObject = arg => arg !== null && typeof arg === 'object';
8+
9+
exports.name = 'html-version-spec';
10+
11+
exports.apply = ({ config, pkg }) => {
12+
const meta = {
13+
get version() {
14+
return getVersion(pkg);
15+
}
16+
};
17+
if (isObject(config.pages)) {
18+
return forEach(config.pages, page => Object.assign(page, { meta }));
19+
}
20+
const { output } = config;
21+
output.html = output.html || {};
22+
Object.assign(output.html, { meta });
23+
};
24+
25+
function getVersion(pkg) {
26+
const { version } = pkg.data;
27+
try {
28+
const rev = execSync('git', ['rev-parse', '--short', 'HEAD']);
29+
return `${version}-rev-${rev}`;
30+
} catch (err) {
31+
console.error(err);
32+
}
33+
return version;
34+
}

Diff for: build/plugins/output-filenames.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
exports.name = 'output-filenames';
4+
5+
const prefix = `${exports.name}-plugin:`;
6+
7+
exports.apply = (api, { normal, vendor } = {}) => {
8+
const defaultFilenames = api.config.output.fileNames;
9+
normal = Object.assign({}, defaultFilenames, normal);
10+
vendor = Object.assign({}, defaultFilenames, vendor);
11+
12+
api.logger.debug(prefix, 'normal output filenames', normal);
13+
api.logger.debug(prefix, 'vendor output filenames', vendor);
14+
15+
api.hook('createWebpackChain', config => {
16+
setOutputFilename(config.module.rule('font'), {
17+
normal: normal.font,
18+
vendor: vendor.font
19+
});
20+
setOutputFilename(config.module.rule('svg'), {
21+
normal: normal.image,
22+
vendor: vendor.image
23+
});
24+
});
25+
};
26+
27+
function setOutputFilename(rule, { normal, vendor } = {}) {
28+
const generic = rule.use('file-loader');
29+
const loader = generic.get('loader');
30+
const options = generic.get('options');
31+
32+
rule.uses.store.clear();
33+
34+
rule.oneOf('vendor')
35+
.include
36+
.add(filepath => /node_modules/.test(filepath))
37+
.end()
38+
.use(generic.name)
39+
.loader(loader)
40+
.options({ ...options, name: vendor || options.name });
41+
42+
rule.oneOf('normal')
43+
.use(generic.name)
44+
.loader(loader)
45+
.options({ ...options, name: normal || options.name });
46+
}

Diff for: build/plugins/stats.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const { writeFileSync } = require('fs');
4+
5+
exports.name = 'stats';
6+
7+
exports.cli = api => {
8+
api.command.option(
9+
'--print-stats',
10+
'Output webpack stats to `stats.json`',
11+
{ default: false, type: [Boolean] }
12+
);
13+
};
14+
15+
class StatsPlugin {
16+
constructor({ logger, path = 'stats.json' } = {}) {
17+
this.logger = logger;
18+
this.path = path;
19+
}
20+
21+
apply(compiler) {
22+
compiler.hooks.done.tap('write-stats', stats => {
23+
this.logger.log('\nGenerating webpack stats file');
24+
writeFileSync(this.path, JSON.stringify(stats.toJson()), 'utf8');
25+
this.logger.done(`Location: ${this.path}`);
26+
});
27+
}
28+
}
29+
30+
exports.apply = api => {
31+
api.hook('createWebpackChain', config => {
32+
if (!api.cli.options.printStats) return;
33+
const options = { logger: api.logger };
34+
config.plugin('generate-stats').use(StatsPlugin, [options]);
35+
});
36+
};

Diff for: package-lock.json

+95
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"private": true,
55
"scripts": {
66
"dev:client": "poi --serve",
7+
"build": "del-cli node_modules/.cache/vue-loader && poi --prod",
8+
"clean": "del-cli dist/* !dist/.gitkeep",
9+
"webpack:inspect-config": "poi --inspect-webpack",
10+
"webpack:bundle-report": "poi --prod --bundle-report",
11+
"webpack:stats": "poi --prod --print-stats",
712
"dev:server": "nodemon -r dotenv/config ./server | bunyan",
813
"db": "node ./server/script/sequelize",
914
"db:reset": "npm run db drop && npm run db create && npm run db migrate",
@@ -14,9 +19,7 @@
1419
"lint": "npm run lint:js; npm run lint:scss",
1520
"debug": "nodemon --inspect-brk -r dotenv/config ./server | bunyan",
1621
"start": "NODE_ENV=production node -r dotenv/config ./server",
17-
"build": "poi --prod",
1822
"bundle-report": "poi --prod -- --bundle-report",
19-
"clean": "del-cli dist/* !dist/.gitkeep",
2023
"test": "echo \"Error: no test specified\" && exit 1"
2124
},
2225
"repository": {
@@ -31,9 +34,12 @@
3134
"license": "MIT",
3235
"devDependencies": {
3336
"@babel/core": "^7.5.5",
37+
"@poi/plugin-bundle-report": "^12.1.1",
3438
"@poi/plugin-eslint": "^12.0.0",
39+
"clean-webpack-plugin": "^3.0.0",
3540
"dargs": "^7.0.0",
3641
"del-cli": "^2.0.0",
42+
"dotenv-webpack": "^1.7.0",
3743
"emoji-strip": "^1.0.1",
3844
"eslint": "^6.1.0",
3945
"eslint-config-semistandard": "^14.0.0",
@@ -55,8 +61,7 @@
5561
"stylelint-config-standard": "^18.3.0",
5662
"stylelint-order": "^3.0.1",
5763
"stylelint-scss": "^3.9.2",
58-
"vue-template-compiler": "^2.6.10",
59-
"webpack-bundle-analyzer": "^3.4.1"
64+
"vue-template-compiler": "^2.6.10"
6065
},
6166
"dependencies": {
6267
"@mdi/font": "^3.0.39",

0 commit comments

Comments
 (0)