slightning-coco-widget--webpack
Version:
SLIGHTNING 的 CoCo 控件框架 —— webpack 相关工具。
71 lines (70 loc) • 3.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WrapperPlugin = exports.WrapperArgumentArray = exports.WrapperParamArray = void 0;
const bypass_1 = require("../bypass");
exports.WrapperParamArray = [
"require",
"exports",
"__slightning_coco_widget_require__",
"__slightning_coco_widget_exports__",
"InvisibleWidget",
"VisibleWidget",
"widgetClass",
"React"
];
exports.WrapperArgumentArray = [
"require",
"exports",
"require",
"exports",
"typeof InvisibleWidget==\"undefined\"?void 0:InvisibleWidget",
"typeof VisibleWidget==\"undefined\"?void 0:VisibleWidget",
"typeof widgetClass==\"undefined\"?void 0:widgetClass",
"React"
];
class WrapperPlugin {
constructor(options) {
if (options == null) {
options = {};
}
this.options = Object.assign({
test: /(?<!\.hot-update)\.js$/,
stringify: false,
catchError: true
}, options);
}
apply(compiler) {
const { webpack } = compiler;
compiler.hooks.compilation.tap(WrapperPlugin.name, (compilation) => {
compilation.hooks.processAssets.tap({
name: WrapperPlugin.name,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
}, (assets) => {
for (const filename of Object.keys(assets)) {
if (!this.options.test.test(filename)) {
continue;
}
const originalSource = compilation.assets[filename];
let sourceContent = String(originalSource.source());
if (this.options.catchError) {
sourceContent = `try{\n${sourceContent}}catch(e){\nif(e instanceof Error)alert(e.message);\nconsole.error(e);};\n`;
}
if (this.options.stringify) {
sourceContent = `(new Function(${exports.WrapperParamArray.map((name) => JSON.stringify(name)).join(",")}, // SLIGHTNING CoCo Widget Wrapper\n${(0, bypass_1.bypassKeyWordsCheck)(JSON.stringify(sourceContent)).replace(/;$/, "")}))(${exports.WrapperArgumentArray.join(",")});\n`;
}
else {
sourceContent = `(new Function(${exports.WrapperParamArray.map((name) => JSON.stringify(name)).join(",")},\"(\"+function () { // SLIGHTNING CoCo Widget Wrapper\n${(0, bypass_1.bypassKeyWordsCheck)(sourceContent)}}.toString()+\")();\"))(${exports.WrapperArgumentArray.join(",")});\n`;
}
let sourceMap = originalSource.map();
if (sourceMap != null) {
assets[filename] = new webpack.sources.SourceMapSource(sourceContent, filename, sourceMap);
}
else {
assets[filename] = new webpack.sources.OriginalSource(sourceContent, filename);
}
}
});
});
}
}
exports.WrapperPlugin = WrapperPlugin;