2014-06-03 14:45:26 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-02-21 18:35:09 +08:00
|
|
|
"use strict";
|
2014-06-03 14:45:26 +08:00
|
|
|
|
2018-03-22 19:05:58 +08:00
|
|
|
const { ConcatSource } = require("webpack-sources");
|
2017-08-02 21:39:43 +08:00
|
|
|
const Template = require("./Template");
|
2019-10-11 21:46:57 +08:00
|
|
|
const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
2014-06-03 14:45:26 +08:00
|
|
|
|
2019-10-02 14:54:21 +08:00
|
|
|
/** @typedef {import("./Compiler")} Compiler */
|
2018-08-07 01:39:43 +08:00
|
|
|
/** @typedef {import("./ModuleTemplate")} ModuleTemplate */
|
|
|
|
|
2019-01-05 21:23:59 +08:00
|
|
|
const joinIterableWithComma = iterable => {
|
|
|
|
// This is more performant than Array.from().join(", ")
|
|
|
|
// as it doesn't create an array
|
|
|
|
let str = "";
|
|
|
|
let first = true;
|
|
|
|
for (const item of iterable) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
str += ", ";
|
|
|
|
}
|
2019-05-21 15:44:06 +08:00
|
|
|
str += item;
|
2019-01-05 21:23:59 +08:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2019-03-14 19:06:59 +08:00
|
|
|
const printExportsInfoToSource = (source, indent, exportsInfo) => {
|
|
|
|
let hasExports = false;
|
|
|
|
for (const exportInfo of exportsInfo.orderedExports) {
|
|
|
|
source.add(
|
|
|
|
Template.toComment(
|
|
|
|
`${indent}export ${
|
|
|
|
exportInfo.name
|
|
|
|
} [${exportInfo.getProvidedInfo()}] [${exportInfo.getUsedInfo()}] [${exportInfo.getRenameInfo()}]`
|
|
|
|
) + "\n"
|
|
|
|
);
|
|
|
|
if (exportInfo.exportsInfo) {
|
|
|
|
printExportsInfoToSource(source, indent + " ", exportInfo.exportsInfo);
|
|
|
|
}
|
|
|
|
hasExports = true;
|
|
|
|
}
|
2019-10-31 19:43:43 +08:00
|
|
|
if (exportsInfo.redirectedToDefault) {
|
2019-03-14 19:06:59 +08:00
|
|
|
source.add(
|
|
|
|
Template.toComment(
|
2019-10-31 19:43:43 +08:00
|
|
|
`${indent}other exports redirect to default export children`
|
|
|
|
)
|
2019-03-14 19:06:59 +08:00
|
|
|
);
|
2019-10-31 19:43:43 +08:00
|
|
|
} else {
|
|
|
|
const otherExportsInfo = exportsInfo.otherExportsInfo;
|
|
|
|
if (
|
|
|
|
otherExportsInfo.provided !== false ||
|
|
|
|
otherExportsInfo.used !== false
|
|
|
|
) {
|
|
|
|
const title = hasExports ? "other exports" : "exports";
|
|
|
|
source.add(
|
|
|
|
Template.toComment(
|
|
|
|
`${indent}${title} [${otherExportsInfo.getProvidedInfo()}] [${otherExportsInfo.getUsedInfo()}]`
|
|
|
|
) + "\n"
|
|
|
|
);
|
|
|
|
}
|
2019-03-14 19:06:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-02 14:54:21 +08:00
|
|
|
class ModuleInfoHeaderPlugin {
|
2018-08-07 01:39:43 +08:00
|
|
|
/**
|
2019-10-02 14:54:21 +08:00
|
|
|
* @param {Compiler} compiler the compiler
|
2018-08-07 01:39:43 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-10-02 14:54:21 +08:00
|
|
|
apply(compiler) {
|
|
|
|
compiler.hooks.compilation.tap("ModuleInfoHeaderPlugin", compilation => {
|
|
|
|
const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
|
|
|
|
hooks.renderModulePackage.tap(
|
|
|
|
"ModuleInfoHeaderPlugin",
|
|
|
|
(
|
|
|
|
moduleSource,
|
|
|
|
module,
|
|
|
|
{ chunkGraph, moduleGraph, runtimeTemplate }
|
|
|
|
) => {
|
2018-02-25 09:00:20 +08:00
|
|
|
const source = new ConcatSource();
|
|
|
|
const req = module.readableIdentifier(
|
2019-10-02 14:54:21 +08:00
|
|
|
runtimeTemplate.requestShortener
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2019-01-05 21:23:59 +08:00
|
|
|
const reqStr = req.replace(/\*\//g, "*_/");
|
|
|
|
const reqStrStar = "*".repeat(reqStr.length);
|
|
|
|
source.add("/*!****" + reqStrStar + "****!*\\\n");
|
|
|
|
source.add(" !*** " + reqStr + " ***!\n");
|
|
|
|
source.add(" \\****" + reqStrStar + "****/\n");
|
2019-01-23 17:05:32 +08:00
|
|
|
const exportsInfo = moduleGraph.getExportsInfo(module);
|
2019-03-14 19:06:59 +08:00
|
|
|
printExportsInfoToSource(source, "", exportsInfo);
|
2018-11-17 01:18:44 +08:00
|
|
|
source.add(
|
|
|
|
Template.toComment(
|
2019-01-05 21:23:59 +08:00
|
|
|
`runtime requirements: ${joinIterableWithComma(
|
2018-11-17 01:18:44 +08:00
|
|
|
chunkGraph.getModuleRuntimeRequirements(module)
|
2019-01-05 21:23:59 +08:00
|
|
|
)}`
|
2018-11-17 01:18:44 +08:00
|
|
|
) + "\n"
|
|
|
|
);
|
2018-11-17 01:11:51 +08:00
|
|
|
const optimizationBailout = moduleGraph.getOptimizationBailout(
|
2018-08-16 19:55:41 +08:00
|
|
|
module
|
2018-08-06 16:42:41 +08:00
|
|
|
);
|
|
|
|
if (optimizationBailout) {
|
|
|
|
for (const text of optimizationBailout) {
|
2018-02-25 09:00:20 +08:00
|
|
|
let code;
|
|
|
|
if (typeof text === "function") {
|
2019-10-02 14:54:21 +08:00
|
|
|
code = text(runtimeTemplate.requestShortener);
|
2018-02-25 09:00:20 +08:00
|
|
|
} else {
|
|
|
|
code = text;
|
|
|
|
}
|
|
|
|
source.add(Template.toComment(`${code}`) + "\n");
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
source.add(moduleSource);
|
|
|
|
return source;
|
2017-05-28 21:25:07 +08:00
|
|
|
}
|
2019-10-02 14:54:21 +08:00
|
|
|
);
|
|
|
|
hooks.chunkHash.tap("ModuleInfoHeaderPlugin", (chunk, hash) => {
|
|
|
|
hash.update("ModuleInfoHeaderPlugin");
|
|
|
|
hash.update("1");
|
|
|
|
});
|
2017-02-21 18:35:09 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 14:54:21 +08:00
|
|
|
module.exports = ModuleInfoHeaderPlugin;
|