Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit 870f37f

Browse files
insinevilebottnawi
authored andcommitted
refactor: Webpack 4 (only) compatibility (#123)
BREAKING CHANGE: drop support webpack@1, webpack@2, webpack@3
1 parent de97e6a commit 870f37f

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@
5151
"webpack": "^3.0.0"
5252
},
5353
"peerDependencies": {
54-
"webpack": "^1.0.0 || ^2.0.0 || >= 3.0.0-rc.0 || ^3.0.0"
54+
"webpack": "^4.0.0"
5555
}
5656
}

src/plugin.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,27 @@ function NpmInstallPlugin(options) {
3838
NpmInstallPlugin.prototype.apply = function(compiler) {
3939
this.compiler = compiler;
4040

41+
var plugin = { name: "NpmInstallPlugin" };
42+
4143
// Recursively install missing dependencies so primary build doesn't fail
42-
compiler.plugin("watch-run", this.preCompile.bind(this));
44+
compiler.hooks.watchRun.tapAsync(plugin, this.preCompile.bind(this));
4345

4446
// Install externals that wouldn't normally be resolved
4547
if (Array.isArray(compiler.options.externals)) {
4648
compiler.options.externals.unshift(this.resolveExternal.bind(this));
4749
}
4850

49-
compiler.plugin("after-resolvers", function(compiler) {
51+
compiler.hooks.afterResolvers.tap(plugin, (compiler) => {
5052
// Install loaders on demand
51-
compiler.resolvers.loader.plugin("module", this.resolveLoader.bind(this));
53+
compiler.resolverFactory.hooks.resolver.tap("loader", plugin, (resolver) => {
54+
resolver.hooks.module.tapAsync("NpmInstallPlugin", this.resolveLoader.bind(this));
55+
});
5256

5357
// Install project dependencies on demand
54-
compiler.resolvers.normal.plugin("module", this.resolveModule.bind(this));
55-
}.bind(this))
58+
compiler.resolverFactory.hooks.resolver.tap("normal", plugin, (resolver) => {
59+
resolver.hooks.module.tapAsync("NpmInstallPlugin", this.resolveModule.bind(this));
60+
});
61+
});
5662
};
5763

5864
NpmInstallPlugin.prototype.install = function(result) {
@@ -128,27 +134,20 @@ NpmInstallPlugin.prototype.resolve = function(resolver, result, callback) {
128134
var version = require("webpack/package.json").version;
129135
var major = version.split(".").shift();
130136

131-
if (major === "1") {
132-
return this.compiler.resolvers[resolver].resolve(
133-
result.path,
134-
result.request,
135-
callback
136-
);
137-
}
138-
139-
if (major === "2" || major === "3") {
140-
return this.compiler.resolvers[resolver].resolve(
137+
if (major === "4") {
138+
return this.compiler.resolverFactory.get(resolver).resolve(
141139
result.context || {},
142140
result.path,
143141
result.request,
142+
{},
144143
callback
145144
);
146145
}
147146

148147
throw new Error("Unsupported Webpack version: " + version);
149148
}
150149

151-
NpmInstallPlugin.prototype.resolveLoader = function(result, next) {
150+
NpmInstallPlugin.prototype.resolveLoader = function(result, resolveContext, next) {
152151
// Only install direct dependencies, not sub-dependencies
153152
if (result.path.match("node_modules")) {
154153
return next();
@@ -172,7 +171,7 @@ NpmInstallPlugin.prototype.resolveLoader = function(result, next) {
172171
}.bind(this));
173172
};
174173

175-
NpmInstallPlugin.prototype.resolveModule = function(result, next) {
174+
NpmInstallPlugin.prototype.resolveModule = function(result, resolveContext, next) {
176175
// Only install direct dependencies, not sub-dependencies
177176
if (result.path.match("node_modules")) {
178177
return next();

0 commit comments

Comments
 (0)