webpack/lib/ConcatenationScope.js

86 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-09-11 15:06:24 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const MODULE_REFERENCE_REGEXP = /^__WEBPACK_MODULE_REFERENCE__(\d+)_([\da-f]+|ns)(_call)?(_directImport)?(_strict)?(?:_asiSafe(\d))?__$/;
class ConcatenationScope {
constructor(modulesWithInfo, currentModule, isRoot) {
2020-09-11 15:06:24 +08:00
this._currentModule = currentModule;
this.isRoot = isRoot;
2020-09-11 15:06:24 +08:00
this._modulesWithInfo = modulesWithInfo;
this._modulesMap = new Map();
for (const info of modulesWithInfo) {
this._modulesMap.set(info.module, info);
}
}
isModuleInScope(module) {
return this._modulesMap.has(module);
}
registerExport(exportName, symbol) {
if (!this._currentModule.exportMap.has(exportName)) {
this._currentModule.exportMap.set(exportName, symbol);
}
}
registerNamespaceExport() {
this._currentModule.hasNamespaceObject = true;
return "__WEBPACK_NAMESPACE_OBJECT__";
}
2020-09-11 15:06:24 +08:00
createModuleReference(
module,
{
ids = undefined,
call = false,
directImport = false,
strict = false,
asiSafe = false
}
) {
const info = this._modulesMap.get(module);
const callFlag = call ? "_call" : "";
const directImportFlag = directImport ? "_directImport" : "";
const strictFlag = strict ? "_strict" : "";
const asiSafeFlag = asiSafe
? "_asiSafe1"
: asiSafe === false
? "_asiSafe0"
: "";
const exportData = ids
? Buffer.from(JSON.stringify(ids), "utf-8").toString("hex")
: "ns";
return `__WEBPACK_MODULE_REFERENCE__${info.index}_${exportData}${callFlag}${directImportFlag}${strictFlag}${asiSafeFlag}__`;
}
static isModuleReference(name) {
return MODULE_REFERENCE_REGEXP.test(name);
}
static matchModuleReference(name, modulesWithInfo) {
const match = MODULE_REFERENCE_REGEXP.exec(name);
if (!match) return null;
const index = +match[1];
const asiSafe = match[6];
return {
index,
info: modulesWithInfo[index],
ids:
match[2] === "ns"
? []
: JSON.parse(Buffer.from(match[2], "hex").toString("utf-8")),
call: !!match[3],
directImport: !!match[4],
strict: !!match[5],
asiSafe: asiSafe ? asiSafe === "1" : undefined
};
}
}
module.exports = ConcatenationScope;