mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-02-17 00:21:20 -05:00
build: replace handmade VersionPlugin with emit-file-webpack-plugin
This commit is contained in:
parent
ba40df0a02
commit
678ad55ab6
3 changed files with 32 additions and 28 deletions
11
package-lock.json
generated
11
package-lock.json
generated
|
@ -49,6 +49,7 @@
|
|||
"copy-webpack-plugin": "6.4.1",
|
||||
"css-loader": "5.2.7",
|
||||
"email-validator": "2.0.4",
|
||||
"emit-file-webpack-plugin": "2.0.1",
|
||||
"enzyme": "3.10.0",
|
||||
"enzyme-adapter-react-16": "1.14.0",
|
||||
"eslint": "5.16.0",
|
||||
|
@ -9776,6 +9777,16 @@
|
|||
"node": ">4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/emit-file-webpack-plugin": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/emit-file-webpack-plugin/-/emit-file-webpack-plugin-2.0.1.tgz",
|
||||
"integrity": "sha512-OCE+944bpMq2FuSRC+LPLfBh8TiBP2QuyBQE2yhvtmhHd9F6c+DcrFXMV9geC4MORFMZB+di2spm2ZOx8kPZlw==",
|
||||
"dev": true,
|
||||
"peerDependencies": {
|
||||
"webpack": "*",
|
||||
"webpack-sources": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/emoji-regex": {
|
||||
"version": "7.0.3",
|
||||
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz",
|
||||
|
|
|
@ -84,6 +84,7 @@
|
|||
"copy-webpack-plugin": "6.4.1",
|
||||
"css-loader": "5.2.7",
|
||||
"email-validator": "2.0.4",
|
||||
"emit-file-webpack-plugin": "2.0.1",
|
||||
"enzyme": "3.10.0",
|
||||
"enzyme-adapter-react-16": "1.14.0",
|
||||
"eslint": "5.16.0",
|
||||
|
|
|
@ -6,6 +6,7 @@ const webpack = require('webpack');
|
|||
// Plugins
|
||||
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
|
||||
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
||||
const EmitFilePlugin = require('emit-file-webpack-plugin');
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
||||
const TerserPlugin = require('terser-webpack-plugin');
|
||||
|
@ -24,35 +25,23 @@ if (process.env.NODE_ENV !== 'production') {
|
|||
routes = routes.filter(route => !process.env.VIEW || process.env.VIEW === route.view);
|
||||
|
||||
const pageRoutes = routes.filter(route => !route.redirect);
|
||||
const VersionPlugin = function (options) {
|
||||
this.options = options || {};
|
||||
return this;
|
||||
};
|
||||
|
||||
VersionPlugin.prototype.apply = function (compiler) {
|
||||
const addVersion = function (compilation, versionId, callback) {
|
||||
compilation.assets['version.txt'] = {
|
||||
source: function () {
|
||||
return versionId;
|
||||
},
|
||||
size: function () {
|
||||
return versionId.length;
|
||||
/**
|
||||
* Retrieve a version ID string for the current build, to be emitted into `version.txt`.
|
||||
* @returns {Promise<string>} A promise that resolves to a version ID string.
|
||||
*/
|
||||
const getVersionId = () => {
|
||||
if (process.env.WWW_VERSION) {
|
||||
return Promise.resolve(process.env.WWW_VERSION);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
gitsha({length: 5}, (err, sha) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(sha);
|
||||
}
|
||||
};
|
||||
callback();
|
||||
};
|
||||
const options = this.options;
|
||||
|
||||
compiler.plugin('emit', (compilation, callback) => {
|
||||
const sha = process.env.WWW_VERSION;
|
||||
if (!sha) { // eslint-disable-line no-negated-condition
|
||||
gitsha(options, (err, _sha) => {
|
||||
if (err) return callback(err);
|
||||
return addVersion(compilation, _sha, callback);
|
||||
});
|
||||
} else {
|
||||
return addVersion(compilation, sha, callback);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -205,7 +194,10 @@ module.exports = {
|
|||
plugins: [
|
||||
new MiniCssExtractPlugin(),
|
||||
new HtmlWebpackBackwardsCompatibilityPlugin(),
|
||||
new VersionPlugin({length: 5})
|
||||
new EmitFilePlugin({
|
||||
filename: 'version.txt',
|
||||
content: getVersionId
|
||||
})
|
||||
].concat(pageRoutes
|
||||
.map(route => new HtmlWebpackPlugin(defaults({}, {
|
||||
title: route.title,
|
||||
|
|
Loading…
Reference in a new issue