@zohodesk/client_build_tool
Version:
A CLI tool to build web applications and client libraries
119 lines (101 loc) • 4.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _htmlWebpackPlugin = _interopRequireDefault(require("html-webpack-plugin"));
var _webpack = require("webpack");
var _nameTemplates = require("../../common/nameTemplates");
var _modeUtils = require("../../common/modeUtils");
var _webpackCustomJsUrlLoader = _interopRequireDefault(require("./webpackCustomJsUrlLoader"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// import { RuntimeGlobals, RuntimeModule } from 'webpack';
// eslint-disable-next-line import/extensions
// import { Template } from 'webpack';
// eslint-disable-next-line import/extensions
const pluginName = 'CdnChangePlugin'; // const MODULE_TYPE = 'css/mini-extract';
// class CdnChangeRuntimePlugin extends RuntimeModule {
// constructor(compiler, { variableName }) {
// super('cdn loading', 10);
// this.variableName = variableName;
// }
// generate() {
// return `${RuntimeGlobals.getChunkCssFilename} = function(chunkId) {
// const cssCdnUrl = window[${this.variableName}] || chunkId;
// return cssCdnUrl;
// }`;
// }
// }
class CdnChangePlugin {
constructor(options) {
this.createSeparateSMap = options.createSeparateSMap;
this.i18nTemplate = options.i18nTemplate || '{{__I18N_CDN__}}';
this.variableName = options.variableName || 'window.__SMAP_PATH__';
this.cssTemplate = options.cssTemplate || '{{__CSS_CDN__}}';
this.jsTemplate = options.jsTemplate || '{{__JS_CDN__}}';
this.mode = options.mode || 'prod';
}
apply(compiler) {
// compiler.hooks.thisCompilation.tap(pluginName, compilation => {
// const enabledChunks = new WeakSet();
// const handler = (entryRuntimeChunk, set) => {
// if (enabledChunks.has(entryRuntimeChunk)) {
// return;
// }
// enabledChunks.add(entryRuntimeChunk);
// set.add(RuntimeGlobals.getFullHash);
// set.add(RuntimeGlobals.getChunkCssFilename);
// compilation.addRuntimeModule(
// entryRuntimeChunk,
// new CdnChangeRuntimePlugin(set, {
// compiler,
// variableName: this.variableName
// })
// );
// };
// compilation.hooks.runtimeRequirementInTree
// .for(RuntimeGlobals.ensureChunkHandlers)
// .tap(pluginName, handler);
// });
compiler.hooks.compilation.tap(pluginName, compilation => {
_htmlWebpackPlugin.default.getHooks(compilation).beforeAssetTagGeneration.tap(pluginName, (data, cb) => {
const {
createSeparateSMap,
mode
} = this; // eslint-disable-next-line no-param-reassign
data.assets = { ...data.assets,
css: data.assets.css.map(css => `${this.cssTemplate}${css}`),
js: data.assets.js.map(js => {
if ((0, _modeUtils.isProductionMode)(mode) && createSeparateSMap && !(0, _nameTemplates.isI18nFile)(js)) {
if (js.includes('smap')) {
js = js.replace('smap/js/', `{{--js-smap}}js/`); //eslint-disable-line
} else {
js = js.replace('js/', `{{--js-smap}}js/`); //eslint-disable-line
}
}
return `${(0, _nameTemplates.isI18nFile)(js) ? this.i18nTemplate : this.jsTemplate}${js}`;
})
};
cb && cb(null, data);
});
compilation.hooks.runtimeRequirementInTree.for(_webpack.RuntimeGlobals.getChunkScriptFilename).tap(pluginName, (chunk, set) => {
if (typeof compilation.outputOptions.chunkFilename === 'string' && /\[(full)?hash(:\d+)?\]/.test(compilation.outputOptions.chunkFilename)) {
set.add(_webpack.RuntimeGlobals.getFullHash);
}
compilation.addRuntimeModule(chunk, // eslint-disable-next-line new-cap
new _webpackCustomJsUrlLoader.default('javascript', 'javascript', _webpack.RuntimeGlobals.getChunkScriptFilename, chunk => chunk.filenameTemplate || (chunk.canBeInitial() ? compilation.outputOptions.filename : compilation.outputOptions.chunkFilename), false, this.variableName));
return true;
});
});
}
}
var _default = CdnChangePlugin; // Cdn Change Plugin for runtime chunks???
/**
* the filename of the script part of the chunk
*/
// exports.getChunkScriptFilename = "__webpack_require__.u";
// /**
// * the filename of the css part of the chunk
// */
// exports.getChunkCssFilename = "__webpack_require__.k";
exports.default = _default;