define-variable-webpack-plugin
Version:
Enhancement of Webpack DefinePlugin to store defined things in actual variables.
46 lines (45 loc) • 1.88 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const Stats_1 = require("./Stats");
class DefineVariablePlugin {
constructor(options) {
this.pluginName = this.constructor.name;
this.options = {};
this.options = Object.assign({}, this.options, options);
}
apply(compiler) {
const ifs = compiler.inputFileSystem;
const statStorage = ifs._statStorage;
const readFileStorage = ifs._readFileStorage;
let CONTENT = 'const dynamicImporter = {};\n';
let target = 'dynamicImporter';
let value = 'null';
for (const [varName, opt] of Object.entries(this.options)) {
if (typeof opt === 'string') {
value = opt;
}
else {
target = opt.type || 'dynamicImporter';
target = target === 'const' ? 'dynamicImporter' : target;
value = opt.value;
}
CONTENT += `${target}["${varName}"] = ${value};\n`;
}
CONTENT += 'export { dynamicImporter };\n';
const PATH = path_1.default.resolve(compiler['context'], 'node_modules/define-variable-webpack-plugin/dynamicImporter');
compiler.hooks.normalModuleFactory.tap(this.pluginName, nmf => {
nmf.hooks.beforeResolve.tap(this.pluginName, () => {
if (readFileStorage.data.has(PATH)) {
return;
}
statStorage.data.set(PATH, [null, Stats_1.Stats.genericStats(CONTENT)]);
readFileStorage.data.set(PATH, [null, CONTENT]);
});
});
}
}
exports.DefineVariablePlugin = DefineVariablePlugin;
;