move to WarnNonEsmSourceTypePlugin

This commit is contained in:
Ivan Kopeykin 2021-09-15 18:39:49 +03:00
parent 34389a932a
commit d0ee75b255
10 changed files with 139 additions and 74 deletions

View File

@ -0,0 +1,7 @@
/*
* This file was automatically generated.
* DO NOT MODIFY BY HAND.
* Run `yarn special-lint-fix` to update
*/
export type WarnNonEsmSourceTypePlugin = RegExp | string;

View File

@ -0,0 +1,24 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Ivan Kopeykin @vankop
*/
"use strict";
const WebpackError = require("./WebpackError");
const makeSerializable = require("./util/makeSerializable");
class NonEsmSourceTypeWarning extends WebpackError {
constructor(message) {
super(message);
this.name = "NonEsmSourceTypeWarning";
}
}
makeSerializable(
NonEsmSourceTypeWarning,
"webpack/lib/NonEsmSourceTypeWarning"
);
module.exports = NonEsmSourceTypeWarning;

View File

@ -1,74 +0,0 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Florent Cailhol @ooflorent
*/
"use strict";
const WebpackError = require("./WebpackError");
class WarnAmbiguousSourceTypePlugin {
apply(compiler) {
compiler.hooks.compilation.tap(
"WarnAmbiguousSourceTypePlugin",
(compilation, { normalModuleFactory }) => {
normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("WarnAmbiguousSourceTypePlugin", parser => {
function flagAsDynamic() {
const module = parser.state.module;
if (!module.buildInfo) module.buildInfo = {};
module.buildInfo.dynamic = true;
}
parser.hooks.call
.for("define")
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
parser.hooks.call
.for("require")
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
parser.hooks.call
.for("require.resolve")
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
parser.hooks.expression
.for("module")
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
parser.hooks.expression
.for("exports")
.tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic);
});
compilation.hooks.finishModules.tap(
"WarnAmbiguousSourceTypePlugin",
modules => {
for (const module of modules) {
if (module.type === "javascript/auto") {
if (
module.buildMeta &&
module.buildMeta.exportsType &&
module.buildInfo &&
module.buildInfo.dynamic
) {
compilation.warnings.push(
new AmbiguousSourceTypeWarning(module)
);
}
}
}
}
);
}
);
}
}
class AmbiguousSourceTypeWarning extends WebpackError {
constructor(module) {
super();
this.name = "AmbiguousSourceTypeWarning";
this.message = "Source type is ambiguous";
this.origin = this.module = module;
Error.captureStackTrace(this, this.constructor);
}
}
module.exports = WarnAmbiguousSourceTypePlugin;

View File

@ -0,0 +1,74 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Florent Cailhol @ooflorent
*/
"use strict";
const NonEsmSourceTypeWarning = require("./NonEsmSourceTypeWarning");
const createSchemaValidation = require("./util/create-schema-validation");
/** @typedef {import("../declarations/plugins/WarnNonEsmSourceTypePlugin").WarnNonEsmSourceTypePlugin} WarnNonEsmSourceTypePluginOptions */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./NormalModule")} NormalModule */
const validate = createSchemaValidation(
require("../schemas/plugins/WarnNonEsmSourceTypePlugin.check.js"),
() => require("../schemas/plugins/WarnNonEsmSourceTypePlugin.json"),
{
name: "Warn non esm source type plugin",
baseDataPath: "filter"
}
);
class WarnNonEsmSourceTypePlugin {
/**
* @param {WarnNonEsmSourceTypePluginOptions=} filter filter
*/
constructor(filter) {
this.filterFunction = undefined;
if (filter) {
validate(filter);
if (typeof filter === "string") {
this.filterFunction = resource => resource.startsWith(filter);
} else {
this.filterFunction = resource => filter.test(resource);
}
}
}
/**
* @param {Compiler} compiler compiler
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap(
"WarnNonEsmSourceTypePlugin",
compilation => {
compilation.hooks.finishModules.tap(
"WarnNonEsmSourceTypePlugin",
modules => {
for (const module of modules) {
const type = module.type;
if (type !== "javascript/esm" && type.startsWith("javascript/")) {
if (
this.filterFunction &&
!this.filterFunction(
/** @type {NormalModule} */ (module).resource
)
)
continue;
// TODO add type reason to NormalModule ?
module.addWarning(new NonEsmSourceTypeWarning(type));
}
}
}
);
}
);
}
}
module.exports = WarnNonEsmSourceTypePlugin;

View File

@ -293,6 +293,9 @@ module.exports = mergeExports(fn, {
get WatchIgnorePlugin() {
return require("./WatchIgnorePlugin");
},
get WarnNonEsmSourceTypePlugin() {
return require("./WarnNonEsmSourceTypePlugin");
},
get WebpackError() {
return require("./WebpackError");
},

View File

@ -188,6 +188,7 @@ module.exports = {
require("../sharing/ProvideForSharedDependency"),
UnsupportedFeatureWarning: () => require("../UnsupportedFeatureWarning"),
"util/LazySet": () => require("../util/LazySet"),
NonEsmSourceTypeWarning: () => require("../NonEsmSourceTypeWarning"),
UnhandledSchemeError: () => require("../UnhandledSchemeError"),
NodeStuffInWebError: () => require("../NodeStuffInWebError"),
WebpackError: () => require("../WebpackError"),

View File

@ -0,0 +1,7 @@
/*
* This file was automatically generated.
* DO NOT MODIFY BY HAND.
* Run `yarn special-lint-fix` to update
*/
declare const check: (options: any) => boolean;
export = check;

View File

@ -0,0 +1,6 @@
/*
* This file was automatically generated.
* DO NOT MODIFY BY HAND.
* Run `yarn special-lint-fix` to update
*/
"use strict";function t(n,{instancePath:r="",parentData:s,parentDataProperty:e,rootData:a=n}={}){let l=null,o=0;const u=o;let p=!1;const c=o;if(!(n instanceof RegExp)){const t={params:{}};null===l?l=[t]:l.push(t),o++}var i=c===o;if(p=p||i,!p){const t=o;if("string"!=typeof n){const t={params:{type:"string"}};null===l?l=[t]:l.push(t),o++}i=t===o,p=p||i}if(!p){const n={params:{}};return null===l?l=[n]:l.push(n),o++,t.errors=l,!1}return o=u,null!==l&&(u?l.length=u:l=null),t.errors=l,0===o}module.exports=t,module.exports.default=t;

View File

@ -0,0 +1,11 @@
{
"anyOf": [
{
"instanceof": "RegExp",
"tsType": "RegExp"
},
{
"type": "string"
}
]
}

6
types.d.ts vendored
View File

@ -11474,6 +11474,11 @@ declare interface VariableInfoInterface {
freeName: string | true;
tagInfo?: TagInfo;
}
declare class WarnNonEsmSourceTypePlugin {
constructor(filter?: string | RegExp);
filter?: string | RegExp;
apply(compiler: Compiler): void;
}
type WarningFilterItemTypes =
| string
| RegExp
@ -12362,6 +12367,7 @@ declare namespace exports {
Stats,
Template,
WatchIgnorePlugin,
WarnNonEsmSourceTypePlugin,
WebpackError,
WebpackOptionsApply,
WebpackOptionsDefaulter,