Skip to content

Commit 926c027

Browse files
committed
use webpack 4 api
1 parent 43356a1 commit 926c027

File tree

2 files changed

+65
-68
lines changed

2 files changed

+65
-68
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
"polyfill-library": "^3.26.0-0",
2828
"webpack-sources": "^1.0.0"
2929
},
30+
"peerDependencies": {
31+
"webpack": "^4.0.0"
32+
},
3033
"devDependencies": {
3134
"chai": "^4.1.2",
3235
"chai-as-promised": "^7.1.1",

src/plugin.js

Lines changed: 62 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,93 +10,87 @@ class PolyfillInjectorPlugin {
1010
}
1111

1212
apply(compiler) {
13-
compiler.plugin('invalid', () => {
13+
compiler.hooks.invalid.tap('webpack-polyfill-injector', () => {
1414
compiler.__WebpackPolyfillInjectorInWatchRun = true;
1515
});
1616

17-
compiler.plugin('this-compilation', (compilation) => {
17+
compiler.hooks.thisCompilation.tap('webpack-polyfill-injector', (compilation) => {
1818
const pluginState = new PluginState(compilation, this.options);
1919
compilation.__WebpackPolyfillInjector = pluginState;
2020

21-
compilation.plugin('additional-assets', async (callback) => {
22-
try {
23-
if (compiler.__WebpackPolyfillInjectorInWatchRun) {
24-
// Nothing to do for successive compilations
25-
callback();
26-
return;
27-
}
21+
compilation.hooks.additionalAssets.tapPromise('webpack-polyfill-injector', async () => {
22+
if (compiler.__WebpackPolyfillInjectorInWatchRun) {
23+
// Nothing to do for successive compilations
24+
return;
25+
}
2826

29-
if (!pluginState.hasLoader) {
30-
throw new Error('[webpack-polyfill-injector] The plugin must be used together with the loader!');
31-
}
27+
if (!pluginState.hasLoader) {
28+
throw new Error('[webpack-polyfill-injector] The plugin must be used together with the loader!');
29+
}
3230

33-
// Create the additional assets
34-
await pluginState.iteratePolyfillSets(
35-
async ({polyfills, singleFile, banner}, filename) => {
36-
// Load all polyfill sources and meta data
37-
const tasks = polyfills.map(polyfill => pluginState.getPolyfillSource(polyfill));
38-
tasks.push(...polyfills.map(polyfill => pluginState.getPolyfillMeta(polyfill)));
31+
// Create the additional assets
32+
await pluginState.iteratePolyfillSets(
33+
async ({polyfills, singleFile, banner}, filename) => {
34+
// Load all polyfill sources and meta data
35+
const tasks = polyfills.map(polyfill => pluginState.getPolyfillSource(polyfill));
36+
tasks.push(...polyfills.map(polyfill => pluginState.getPolyfillMeta(polyfill)));
3937

40-
// Run all tasks and split the results into their appropriate arrays
41-
const polyfillSources = await Promise.all(tasks);
42-
const polyfillMetas = polyfillSources.splice(polyfills.length);
38+
// Run all tasks and split the results into their appropriate arrays
39+
const polyfillSources = await Promise.all(tasks);
40+
const polyfillMetas = polyfillSources.splice(polyfills.length);
4341

44-
// Collect internal dependencies (i.e. polyfills whose names start with underscore)
45-
const dependencySources = {};
46-
async function resolveDependencies(dependencies, fetched) {
47-
const deps = dependencies.filter(d => d.startsWith('_') && !fetched.includes(d));
48-
const tasks = deps.map(dep => pluginState.getPolyfillSource(dep));
49-
tasks.push(...deps.map(dep => pluginState.getPolyfillMeta(dep)));
50-
const depsSources = await Promise.all(tasks);
51-
const depsMetas = depsSources.splice(deps.length);
52-
deps.forEach((dep, i) => {
53-
dependencySources[dep] = depsSources[i];
54-
});
55-
fetched.push(...deps);
56-
const recursiveDeps = await Promise.all(
57-
depsMetas.map(meta => resolveDependencies(meta.dependencies || [], fetched))
58-
);
59-
return [].concat(deps, ...recursiveDeps);
60-
}
61-
const polyfillDependencies = await Promise.all(
62-
polyfillMetas.map(meta => resolveDependencies(meta.dependencies || [], []))
42+
// Collect internal dependencies (i.e. polyfills whose names start with underscore)
43+
const dependencySources = {};
44+
async function resolveDependencies(dependencies, fetched) {
45+
const deps = dependencies.filter(d => d.startsWith('_') && !fetched.includes(d));
46+
const tasks = deps.map(dep => pluginState.getPolyfillSource(dep));
47+
tasks.push(...deps.map(dep => pluginState.getPolyfillMeta(dep)));
48+
const depsSources = await Promise.all(tasks);
49+
const depsMetas = depsSources.splice(deps.length);
50+
deps.forEach((dep, i) => {
51+
dependencySources[dep] = depsSources[i];
52+
});
53+
fetched.push(...deps);
54+
const recursiveDeps = await Promise.all(
55+
depsMetas.map(meta => resolveDependencies(meta.dependencies || [], fetched))
6356
);
57+
return [].concat(deps, ...recursiveDeps);
58+
}
59+
const polyfillDependencies = await Promise.all(
60+
polyfillMetas.map(meta => resolveDependencies(meta.dependencies || [], []))
61+
);
6462

65-
if (singleFile) {
66-
// Create one file containing all requested polyfills
67-
compilation.assets[filename] = constructFile(
68-
polyfills.length > 1,
63+
if (singleFile) {
64+
// Create one file containing all requested polyfills
65+
compilation.assets[filename] = constructFile(
66+
polyfills.length > 1,
67+
banner,
68+
polyfills,
69+
polyfillSources,
70+
polyfillMetas.map(meta => meta.detectSource),
71+
polyfillDependencies,
72+
dependencySources
73+
);
74+
addAsChunk(filename, compilation);
75+
} else {
76+
// Create one file for each possible subset of polyfills
77+
const choices = Math.pow(2, polyfills.length);
78+
for (let choiceId = 1; choiceId < choices; choiceId++) {
79+
const choice = choiceId.toString(2).padStart(polyfills.length, '0');
80+
const outputFile = filename.replace(/\.js$/, '.') + choice + '.js';
81+
compilation.assets[outputFile] = constructFileWithoutTests(
6982
banner,
7083
polyfills,
7184
polyfillSources,
72-
polyfillMetas.map(meta => meta.detectSource),
7385
polyfillDependencies,
74-
dependencySources
86+
dependencySources,
87+
choice
7588
);
76-
addAsChunk(filename, compilation);
77-
} else {
78-
// Create one file for each possible subset of polyfills
79-
const choices = Math.pow(2, polyfills.length);
80-
for (let choiceId = 1; choiceId < choices; choiceId++) {
81-
const choice = choiceId.toString(2).padStart(polyfills.length, '0');
82-
const outputFile = filename.replace(/\.js$/, '.') + choice + '.js';
83-
compilation.assets[outputFile] = constructFileWithoutTests(
84-
banner,
85-
polyfills,
86-
polyfillSources,
87-
polyfillDependencies,
88-
dependencySources,
89-
choice
90-
);
91-
addAsChunk(outputFile, compilation);
92-
}
89+
addAsChunk(outputFile, compilation);
9390
}
9491
}
95-
);
96-
callback(); // eslint-disable-line callback-return
97-
} catch (error) {
98-
callback(error); // eslint-disable-line callback-return
99-
}
92+
}
93+
);
10094
});
10195
});
10296
}

0 commit comments

Comments
 (0)