UNPKG

@zohodesk/client_build_tool

Version:

A CLI tool to build web applications and client libraries

76 lines (62 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _utils = require("./utils"); var _LocaleChunkAssetsStore = require("./LocaleChunkAssetsStore"); /* eslint-disable max-classes-per-file */ const pluginName = 'I18nKeysIdentifier'; /** * this plugin search the i18 keys and make it as I18nDependency. * how it works? * this plugin get jsResourceI18nKeys in constructor. * we walk thought the js files AST * if any string literal in that AST is in jsResourceI18nKeys then we consider that string as I18n key * after the walk of AST we get some I18n keys * then we make it as I18nDependency. */ class I18nKeysIdentifer { constructor({ jsResourceI18nKeys }) { this.jsResourceI18nKeys = jsResourceI18nKeys; this.store = new _LocaleChunkAssetsStore.LocaleChunkAssetsStore(); } apply(compiler) { // const { splitChunks } = compiler.options.optimization; // if (splitChunks) { // if (splitChunks.defaultSizeTypes.includes('...')) { // splitChunks.defaultSizeTypes.push(MODULE_TYPE); // } // } compiler.hooks.thisCompilation.tap(pluginName, compilation => { this.store.updateCompilation(compilation); (0, _LocaleChunkAssetsStore.setLocaleChunkAssetsStore)(compilation, this.store); }); compiler.hooks.normalModuleFactory.tap(pluginName, factory => { // handler for parser const handler = parser => { parser.hooks.program.tap(pluginName, (ast, comments) => { const { module } = parser.state; // if (!(module && /\.jsx?$/.test(module.resource))) { return; } // i18n grep proper contract regex let i18nKeys = (0, _utils.collectI18nKeysfromAST)(ast, this.jsResourceI18nKeys); i18nKeys = i18nKeys.concat( // i18n grep proper contract regex (0, _utils.collectI18nKeysfromComments)(comments, this.jsResourceI18nKeys)); if (i18nKeys.length) { // console.log('i18nKeys.length', module.resource, i18nKeys); this.store.updateModuleI18nKeys(module.resource, i18nKeys); } }); }; // this below hook was tapped for collect I18n Key from files and add as Dependency // const factory = params.normalModuleFactory; factory.hooks.parser.for('javascript/auto').tap(pluginName, handler); factory.hooks.parser.for('javascript/dynamic').tap(pluginName, handler); }); } } exports.default = I18nKeysIdentifer;