webpack/lib/ModuleInfoHeaderPlugin.js

127 lines
3.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const { ConcatSource } = require("webpack-sources");
const Template = require("./Template");
2019-10-11 21:46:57 +08:00
const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./ModuleTemplate")} ModuleTemplate */
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;
}
return str;
};
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;
}
if (exportsInfo.redirectedToDefault) {
source.add(
Template.toComment(
`${indent}other exports redirect to default export children`
)
);
} 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"
);
}
}
};
class ModuleInfoHeaderPlugin {
/**
* @param {Compiler} compiler the compiler
* @returns {void}
*/
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(
runtimeTemplate.requestShortener
2018-02-25 09:00:20 +08:00
);
const reqStr = req.replace(/\*\//g, "*_/");
const reqStrStar = "*".repeat(reqStr.length);
source.add("/*!****" + reqStrStar + "****!*\\\n");
source.add(" !*** " + reqStr + " ***!\n");
source.add(" \\****" + reqStrStar + "****/\n");
const exportsInfo = moduleGraph.getExportsInfo(module);
printExportsInfoToSource(source, "", exportsInfo);
2018-11-17 01:18:44 +08:00
source.add(
Template.toComment(
`runtime requirements: ${joinIterableWithComma(
2018-11-17 01:18:44 +08:00
chunkGraph.getModuleRuntimeRequirements(module)
)}`
2018-11-17 01:18:44 +08:00
) + "\n"
);
const optimizationBailout = moduleGraph.getOptimizationBailout(
module
);
if (optimizationBailout) {
for (const text of optimizationBailout) {
2018-02-25 09:00:20 +08:00
let code;
if (typeof text === "function") {
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;
}
);
hooks.chunkHash.tap("ModuleInfoHeaderPlugin", (chunk, hash) => {
hash.update("ModuleInfoHeaderPlugin");
hash.update("1");
});
});
}
}
module.exports = ModuleInfoHeaderPlugin;