Skip to content

Commit b146776

Browse files
committed
Add a plugin to warn if the source type is ambiguous
1 parent f6e366b commit b146776

File tree

14 files changed

+126
-0
lines changed

14 files changed

+126
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Florent Cailhol @ooflorent
4+
*/
5+
"use strict";
6+
7+
const WebpackError = require("./WebpackError");
8+
9+
class WarnAmbiguousSourceTypePlugin {
10+
apply(compiler) {
11+
compiler.hooks.compilation.tap(
12+
"WarnAmbiguousSourceTypePlugin",
13+
(compilation, { normalModuleFactory }) => {
14+
normalModuleFactory.hooks.parser
15+
.for("javascript/auto")
16+
.tap("WarnAmbiguousSourceTypePlugin", parser => {
17+
function flagAsDynamic() {
18+
const module = parser.state.module;
19+
if (!module.buildInfo) module.buildInfo = {};
20+
module.buildInfo.dynamic = true;
21+
}
22+
parser.hooks.call
23+
.for("define")
24+
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
25+
parser.hooks.call
26+
.for("require")
27+
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
28+
parser.hooks.call
29+
.for("require.resolve")
30+
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
31+
parser.hooks.expression
32+
.for("module")
33+
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
34+
parser.hooks.expression
35+
.for("exports")
36+
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
37+
});
38+
compilation.hooks.finishModules.tap(
39+
"WarnAmbiguousSourceTypePlugin",
40+
modules => {
41+
for (const module of modules) {
42+
if (module.type === "javascript/auto") {
43+
if (
44+
module.buildMeta &&
45+
module.buildMeta.exportsType &&
46+
module.buildInfo &&
47+
module.buildInfo.dynamic
48+
) {
49+
compilation.warnings.push(
50+
new AmbiguousSourceTypeWarning(module)
51+
);
52+
}
53+
}
54+
}
55+
}
56+
);
57+
}
58+
);
59+
}
60+
}
61+
62+
class AmbiguousSourceTypeWarning extends WebpackError {
63+
constructor(module) {
64+
super();
65+
66+
this.name = "AmbiguousSourceTypeWarning";
67+
this.message = "Source type is ambiguous";
68+
this.origin = this.module = module;
69+
70+
Error.captureStackTrace(this, this.constructor);
71+
}
72+
}
73+
74+
module.exports = WarnAmbiguousSourceTypePlugin;

lib/webpack.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ exportPlugins(exports, {
117117
SourceMapDevToolPlugin: () => require("./SourceMapDevToolPlugin"),
118118
Stats: () => require("./Stats"),
119119
UmdMainTemplatePlugin: () => require("./UmdMainTemplatePlugin"),
120+
WarnAmbiguousSourceTypePlugin: () =>
121+
require("./WarnAmbiguousSourceTypePlugin"),
120122
WatchIgnorePlugin: () => require("./WatchIgnorePlugin")
121123
});
122124
exportPlugins((exports.optimize = {}), {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "./empty";
2+
exports.type = "ambiguous";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("./empty");
2+
export default "ambiguous";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require.resolve("./empty");
2+
export default "ambiguous";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "./empty";
2+
require("./empty");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./empty";
2+
3+
Object.assign({}, module.exports, {
4+
type: "ambiguous"
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./empty";
2+
3+
Object.assign({}, exports, {
4+
type: "ambiguous"
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require("./empty");
2+
require.resolve("./empty");
3+
4+
module.exports = {};
5+
exports.x = true;

test/configCases/parsing/ambiguous/cases/empty.js

Whitespace-only changes.

0 commit comments

Comments
 (0)