Skip to content

Commit afe8fee

Browse files
committed
Improve webpack 4 support. Deprecate node 4 and level up scripts.
1 parent 661af13 commit afe8fee

File tree

4 files changed

+126
-112
lines changed

4 files changed

+126
-112
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ node_js:
33
- '9'
44
- '8'
55
- '6'
6-
- '4'
76

87
env:
98
- WEBPACK_VERSION=2 EXTRACT_PLUGIN_VERSION=2
109
- WEBPACK_VERSION=3 EXTRACT_PLUGIN_VERSION=3.0.2
11-
- WEBPACK_VERSION=4 EXTRACT_PLUGIN_VERSION=3.0.2
10+
- WEBPACK_VERSION=4 EXTRACT_PLUGIN_VERSION=4.0.0-alpha.0
1211

1312
install:
1413
- npm install

lib/plugin.js

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var path = require('path');
2-
var fse = require('fs-extra');
3-
var _ = require('lodash');
4-
var mutexify = require('mutexify');
1+
const path = require('path');
2+
const fse = require('fs-extra');
3+
const _ = require('lodash');
4+
const mutexify = require('mutexify');
55

6-
var lock = mutexify();
6+
const lock = mutexify();
77

88
function ManifestPlugin(opts) {
99
this.opts = _.assign({
@@ -16,40 +16,38 @@ function ManifestPlugin(opts) {
1616
map: null,
1717
generate: null,
1818
sort: null,
19-
serialize: function(manifest) {
20-
return JSON.stringify(manifest, null, 2);
21-
},
19+
serialize: manifest => JSON.stringify(manifest, null, 2),
2220
}, opts || {});
2321
}
2422

2523
ManifestPlugin.prototype.getFileType = function(str) {
2624
str = str.replace(/\?.*/, '');
27-
var split = str.split('.');
28-
var ext = split.pop();
25+
const split = str.split('.');
26+
let ext = split.pop();
2927
if (this.opts.transformExtensions.test(ext)) {
3028
ext = split.pop() + '.' + ext;
3129
}
3230
return ext;
3331
};
3432

3533
ManifestPlugin.prototype.apply = function(compiler) {
36-
var seed = this.opts.seed || {};
37-
var moduleAssets = {};
34+
const seed = this.opts.seed || {};
35+
const moduleAssets = {};
3836

39-
var moduleAsset = function (module, file) {
37+
const moduleAsset = (module, file) => {
4038
moduleAssets[file] = path.join(
4139
path.dirname(file),
4240
path.basename(module.userRequest)
4341
);
4442
};
4543

46-
var emit = function(compilation, compileCallback) {
47-
var publicPath = compilation.options.output.publicPath;
48-
var stats = compilation.getStats().toJson();
44+
const emit = (compilation, compileCallback) => {
45+
const publicPath = compilation.options.output.publicPath;
46+
const stats = compilation.getStats().toJson();
4947

50-
var files = compilation.chunks.reduce(function(files, chunk) {
51-
return chunk.files.reduce(function (files, path) {
52-
var name = chunk.name ? chunk.name : null;
48+
let files = compilation.chunks.reduce((files, chunk) => {
49+
return chunk.files.reduce((files, path) => {
50+
let name = chunk.name ? chunk.name : null;
5351

5452
if (name) {
5553
name = name + '.' + this.getFileType(path);
@@ -70,13 +68,13 @@ ManifestPlugin.prototype.apply = function(compiler) {
7068
isAsset: false,
7169
isModuleAsset: false
7270
});
73-
}.bind(this), files);
74-
}.bind(this), []);
71+
}, files);
72+
}, []);
7573

7674
// module assets don't show up in assetsByChunkName.
7775
// we're getting them this way;
78-
files = stats.assets.reduce(function (files, asset) {
79-
var name = moduleAssets[asset.name];
76+
files = stats.assets.reduce((files, asset) => {
77+
const name = moduleAssets[asset.name];
8078
if (name) {
8179
return files.concat({
8280
path: asset.name,
@@ -88,7 +86,7 @@ ManifestPlugin.prototype.apply = function(compiler) {
8886
});
8987
}
9088

91-
var isEntryAsset = asset.chunks.length > 0;
89+
const isEntryAsset = asset.chunks.length > 0;
9290
if (isEntryAsset) {
9391
return files;
9492
}
@@ -103,28 +101,26 @@ ManifestPlugin.prototype.apply = function(compiler) {
103101
});
104102
}, files);
105103

106-
files = files.filter(function (file) {
107-
// Don't add hot updates to manifest
108-
return file.path.indexOf('hot-update') === -1;
109-
});
104+
// Don't add hot updates to manifest
105+
files = files.filter(file => !file.path.includes('hot-update'));
110106

111107
// Append optional basepath onto all references.
112108
// This allows output path to be reflected in the manifest.
113109
if (this.opts.basePath) {
114-
files = files.map(function(file) {
110+
files = files.map(file => {
115111
file.name = this.opts.basePath + file.name;
116112
return file;
117-
}.bind(this));
113+
});
118114
}
119115

120116
if (publicPath) {
121117
// Similar to basePath but only affects the value (similar to how
122118
// output.publicPath turns require('foo/bar') into '/public/foo/bar', see
123119
// https://github.com/webpack/docs/wiki/configuration#outputpublicpath
124-
files = files.map(function(file) {
120+
files = files.map(file => {
125121
file.path = publicPath + file.path;
126122
return file;
127-
}.bind(this));
123+
});
128124
}
129125

130126
files = files.map(file => {
@@ -145,21 +141,21 @@ ManifestPlugin.prototype.apply = function(compiler) {
145141
files = files.sort(this.opts.sort);
146142
}
147143

148-
var manifest;
144+
let manifest;
149145
if (this.opts.generate) {
150146
manifest = this.opts.generate(seed, files);
151147
} else {
152-
manifest = files.reduce(function (manifest, file) {
148+
manifest = files.reduce((manifest, file) => {
153149
manifest[file.name] = file.path;
154150
return manifest;
155151
}, seed);
156152
}
157153

158-
var output = this.opts.serialize(manifest);
154+
const output = this.opts.serialize(manifest);
159155

160-
var outputFolder = compilation.options.output.path;
161-
var outputFile = path.resolve(compilation.options.output.path, this.opts.fileName);
162-
var outputName = path.relative(outputFolder, outputFile);
156+
const outputFolder = compilation.options.output.path;
157+
const outputFile = path.resolve(compilation.options.output.path, this.opts.fileName);
158+
const outputName = path.relative(outputFolder, outputFile);
163159

164160
compilation.assets[outputName] = {
165161
source: function() {
@@ -177,27 +173,34 @@ ManifestPlugin.prototype.apply = function(compiler) {
177173
// NOTE: make sure webpack is not writing multiple manifests simultaneously
178174
lock(function(release) {
179175
if (compiler.hooks) {
180-
compiler.hooks.afterEmit.tap('ManifestPlugin', function(compilation) {
176+
compiler.hooks.afterEmit.tap('ManifestPlugin', compilation => {
181177
release();
182178
});
179+
180+
compilation.hooks.webpackManifestPluginAfterEmit.call(manifest, compileCallback);
183181
} else {
184-
compiler.plugin('after-emit', function(compilation, cb) {
182+
compiler.plugin('after-emit', (compilation, cb) => {
185183
release();
186184
cb();
187185
});
188186

189187
compilation.applyPluginsAsync('webpack-manifest-plugin-after-emit', manifest, compileCallback);
190188
}
191189
});
192-
}.bind(this);
190+
};
193191

194192
if (compiler.hooks) {
195-
compiler.hooks.compilation.tap('ManifestPlugin', function (compilation) {
193+
compiler.hooks.compilation.tap('ManifestPlugin', compilation => {
194+
195+
const SyncWaterfallHook = require('tapable').SyncWaterfallHook;
196+
197+
compilation.hooks.webpackManifestPluginAfterEmit = new SyncWaterfallHook(['manifest', 'compileCallback']);
198+
196199
compilation.hooks.moduleAsset.tap('ManifestPlugin', moduleAsset);
197200
});
198201
compiler.hooks.emit.tap('ManifestPlugin', emit);
199202
} else {
200-
compiler.plugin('compilation', function (compilation) {
203+
compiler.plugin('compilation', compilation => {
201204
compilation.plugin('module-asset', moduleAsset);
202205
});
203206
compiler.plugin('emit', emit);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"author": "Dane Thurber <[email protected]>",
1010
"license": "MIT",
1111
"engines": {
12-
"node": ">=4"
12+
"node": ">=6.5.0"
1313
},
1414
"peerDependencies": {
1515
"webpack": "2 || 3 || 4"
@@ -41,7 +41,8 @@
4141
"dependencies": {
4242
"fs-extra": "^0.30.0",
4343
"lodash": ">=3.5 <5",
44-
"mutexify": "1.0.1"
44+
"mutexify": "1.0.1",
45+
"tapable": "^1.0.0"
4546
},
4647
"nyc": {
4748
"reporter": [

0 commit comments

Comments
 (0)