UNPKG

@zohodesk/client_build_tool

Version:

A CLI tool to build web applications and client libraries

112 lines (94 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EFCTemplatePlugin = void 0; var _fs = require("fs"); var _logger = require("../../../../logger"); var _LocaleChunkAssetsStore = require("./I18nSplitPlugin/LocaleChunkAssetsStore"); var _constants = require("../../../constants"); var _getInitialAssetsFuncTemplate = require("./getInitialAssetsFuncTemplate"); const INITIAL_ASSETS_TEMPLATE = '// <--getInitialAssets-->'; const PUBLIC_PATH_TEMPLATE = '"<--publicPath-->"'; const pluginName = 'EFCTemplatePlugin'; class EFCTemplatePlugin { constructor(options = {}) { const { i18nFileNameTemplate, entryPointName, outputFile, version, publicPath, templateFilePath } = options; this.options = options; this.templateFilePath = (0, _constants.joinWithAppPath)(templateFilePath); this.i18nFileNameTemplate = i18nFileNameTemplate; this.publicPath = publicPath; // NOTE: this logic may be needed for i18n splitted file name with contenthash cases // this.i18nManifestFileName = options.i18nManifestFileName; // IMPORTANT: here we merging options from package.json and options via constructor // So when debugging consider this as well // this.options = Object.assign({}, efcOptions, options); this.entryPointName = entryPointName || 'efc'; this.outputFile = outputFile.replace('[version]', version); } // NOTE: this logic may be needed for i18n splitted file name with contenthash cases // getI18nManifest(compilation) { // const i18nManifestFile = // compilation.assets[this.options.i18nManifestFileName]; // if (i18nManifestFile) { // const manifest = JSON.parse(i18nManifestFile.source()); // return manifest; // } // return {}; // } // eslint-disable-next-line class-methods-use-this templateReplacer(entryPoint, compilation) { const { enableRTLSplit } = this.options; // const i18nManifest = this.getI18nManifest(compilation); const initialAssetsFuncTemplate = (0, _getInitialAssetsFuncTemplate.getInitialAssetsFuncTemplate)({ entryPoint, compilation, enableRTLSplit, i18nStore: this.store, chunkSplitEnable: this.options.chunkSplitEnable, i18nFileNameTemplate: this.i18nFileNameTemplate, functionName: 'getInitialAssets' }); const templateStr = (0, _fs.readFileSync)(this.templateFilePath).toString(); return templateStr.replace(INITIAL_ASSETS_TEMPLATE, initialAssetsFuncTemplate).replace(PUBLIC_PATH_TEMPLATE, this.publicPath); } apply(compiler) { const { RawSource } = compiler.webpack.sources; compiler.hooks.thisCompilation.tap(pluginName, compilation => { // Get store for cache this.store = (0, _LocaleChunkAssetsStore.getLocaleChunkAssetsStore)(compilation); }); compiler.hooks.thisCompilation.tap(pluginName, compilation => { compilation.hooks.processAssets.tap({ name: pluginName, stage: compilation.PROCESS_ASSETS_STAGE_DERIVED }, () => { const { entryPointName, outputFile } = this; const entryPoint = compilation.entrypoints.get(entryPointName); if (!entryPoint) { return; } if (!(0, _fs.existsSync)(this.templateFilePath)) { (0, _logger.errorLogger)(`EFC Template file not exists ${this.templateFilePath}`); return; } const source = new RawSource(this.templateReplacer(entryPoint, compilation)); compilation.emitAsset(outputFile, source); (0, _logger.messageLogger)('The EFC embedded code was created successfully..!!!'); (0, _logger.messageLogger)(`The EFC SDK File name ${outputFile}`); }); }); } } exports.EFCTemplatePlugin = EFCTemplatePlugin;