diff --git a/packages/core/src/core/transferOptions.js b/packages/core/src/core/transferOptions.js index f4ebbae1d6..a39c94cebd 100644 --- a/packages/core/src/core/transferOptions.js +++ b/packages/core/src/core/transferOptions.js @@ -5,8 +5,10 @@ import { warn, findItem } from '@mpxjs/utils' export default function transferOptions (options, type, needConvert = true) { let currentInject - if (global.currentInject && global.currentInject.moduleId === global.currentModuleId) { - currentInject = global.currentInject + // currentModuleId -> _id + // currentInject -> _j + if (global._j && global._j.moduleId === global._id) { + currentInject = global._j } // 文件编译路径 options.mpxFileResource = global.currentResource @@ -28,7 +30,8 @@ export default function transferOptions (options, type, needConvert = true) { options.mixins.push(currentInject.pageEvents) } // 转换mode - options.mpxConvertMode = options.mpxConvertMode || getConvertMode(global.currentSrcMode) + // currentSrcMode -> _sm + options.mpxConvertMode = options.mpxConvertMode || getConvertMode(global._sm) const rawOptions = mergeOptions(options, type, needConvert) if (currentInject && currentInject.propKeys) { diff --git a/packages/core/src/platform/createApp.js b/packages/core/src/platform/createApp.js index 22a635abb9..978b3ab4d2 100644 --- a/packages/core/src/platform/createApp.js +++ b/packages/core/src/platform/createApp.js @@ -82,7 +82,8 @@ export default function createApp (option, config = {}) { if (__mpx_mode__ === 'web') { global.__mpxOptionsMap = global.__mpxOptionsMap || {} - global.__mpxOptionsMap[global.currentModuleId] = defaultOptions + // currentModuleId -> _id + global.__mpxOptionsMap[global._id] = defaultOptions global.getApp = function () { if (!isBrowser) { console.error('[Mpx runtime error]: Dangerous API! global.getApp method is running in non browser environments') @@ -90,7 +91,7 @@ export default function createApp (option, config = {}) { return appData } } else { - const ctor = config.customCtor || global.currentCtor || App + const ctor = config.customCtor || global._ctor || App ctor(defaultOptions) } } diff --git a/packages/core/src/platform/patch/index.js b/packages/core/src/platform/patch/index.js index 80719e299c..a350949c36 100644 --- a/packages/core/src/platform/patch/index.js +++ b/packages/core/src/platform/patch/index.js @@ -19,13 +19,16 @@ export default function createFactory (type) { options.__pageCtor__ = true } } else { - if (global.currentCtor) { - ctor = global.currentCtor - if (global.currentCtorType === 'page') { + // currentCtor -> _ctor + if (global._ctor) { + ctor = global._ctor + // currentCtorType -> _ctorT + if (global._ctorT === 'page') { options.__pageCtor__ = true } - if (global.currentResourceType && global.currentResourceType !== type) { - error(`The ${global.currentResourceType} [${global.currentResource}] is not supported to be created by ${type} constructor.`) + // currentResourceType -> _crt + if (global._crt && global._crt !== type) { + error(`The ${global._crt} [${global.currentResource}] is not supported to be created by ${type} constructor.`) } } else { if (type === 'page') { @@ -59,7 +62,8 @@ export default function createFactory (type) { const defaultOptions = getDefaultOptions(type, { rawOptions, currentInject }) if (__mpx_mode__ === 'web') { global.__mpxOptionsMap = global.__mpxOptionsMap || {} - global.__mpxOptionsMap[global.currentModuleId] = defaultOptions + // currentModuleId -> _id + global.__mpxOptionsMap[global._id] = defaultOptions } else if (ctor) { return ctor(defaultOptions) } diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 8747a9de17..8b520d4786 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -63,12 +63,9 @@ const stringifyLoadersAndResource = require('./utils/stringify-loaders-resource' const emitFile = require('./utils/emit-file') const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE } = require('./utils/const') const isEmptyObject = require('./utils/is-empty-object') +const { generateVariableNameBySource, isProductionLikeMode } = require('./utils/optimize-compress') require('./utils/check-core-version-match') -const isProductionLikeMode = options => { - return options.mode === 'production' || !options.mode -} - const isStaticModule = module => { if (!module.resource) return false const { queryObj } = parseRequest(module.resource) @@ -174,6 +171,11 @@ class MpxWebpackPlugin { options.optimizeRenderRules = options.optimizeRenderRules ? (Array.isArray(options.optimizeRenderRules) ? options.optimizeRenderRules : [options.optimizeRenderRules]) : [] options.retryRequireAsync = options.retryRequireAsync || false options.optimizeSize = options.optimizeSize || false + // 保留组件名(压缩后组件名不允许出现) + if (typeof options.reservedComponentName === 'object') { + options.reservedComponentName = options.reservedComponentName[options.mode] + } + options.reservedComponentName = options.reservedComponentName || [] this.options = options // Hack for buildDependencies const rawResolveBuildDependencies = FileSystemInfo.prototype.resolveBuildDependencies @@ -670,6 +672,8 @@ class MpxWebpackPlugin { }, asyncSubpackageRules: this.options.asyncSubpackageRules, optimizeRenderRules: this.options.optimizeRenderRules, + optimizeSize: this.options.optimizeSize, + reservedComponentName: this.options.reservedComponentName, pathHash: (resourcePath) => { if (this.options.pathHashMode === 'relative' && this.options.projectRoot) { return hash(path.relative(this.options.projectRoot, resourcePath)) @@ -701,7 +705,15 @@ class MpxWebpackPlugin { const customOutputPath = this.options.customOutputPath if (conflictPath) return conflictPath.replace(/(\.[^\\/]+)?$/, match => hash + match) if (typeof customOutputPath === 'function') return customOutputPath(type, name, hash, ext).replace(/^\//, '') - if (type === 'component' || type === 'page') return path.join(type + 's', name + hash, 'index' + ext) + if (type === 'component') { + if (this.options.optimizeSize && isProductionLikeMode(compiler.options)) { + const pathHash = generateVariableNameBySource(resourcePath, 'componentPath') + return path.join('c', pathHash, 'i' + ext) + } else { + return path.join('c', name + hash, 'i' + ext) + } + } + if (type === 'page') return path.join(type + 's', name + hash, 'index' + ext) return path.join(type, name + hash + ext) }, extractedFilesCache: new Map(), diff --git a/packages/webpack-plugin/lib/json-compiler/index.js b/packages/webpack-plugin/lib/json-compiler/index.js index 243e6bf47a..a8be61e18c 100644 --- a/packages/webpack-plugin/lib/json-compiler/index.js +++ b/packages/webpack-plugin/lib/json-compiler/index.js @@ -18,6 +18,7 @@ const resolveTabBarPath = require('../utils/resolve-tab-bar-path') const normalize = require('../utils/normalize') const mpxViewPath = normalize.lib('runtime/components/ali/mpx-view.mpx') const mpxTextPath = normalize.lib('runtime/components/ali/mpx-text.mpx') +const { isProductionLikeMode } = require('../utils/optimize-compress') module.exports = function (content) { const nativeCallback = this.async() @@ -43,6 +44,7 @@ module.exports = function (content) { const localSrcMode = queryObj.mode const srcMode = localSrcMode || globalSrcMode const projectRoot = mpx.projectRoot + const usingComponentsNameMap = JSON.parse(queryObj.usingComponentsNameMap) const isApp = !(pagesMap[resourcePath] || componentsMap[resourcePath]) const publicPath = this._compilation.outputOptions.publicPath || '' @@ -256,6 +258,35 @@ module.exports = function (content) { } } + const processOptimizeSize = (json, callback) => { + if (mpx.optimizeSize && isProductionLikeMode(this)) { + // placeholder 替换 + if (json.componentPlaceholder) { + const newComponentPlaceholder = {} + for (const [name, value] of Object.entries(json.componentPlaceholder)) { + const newName = usingComponentsNameMap[name] + newComponentPlaceholder[newName] = json.componentPlaceholder[name] + delete json.componentPlaceholder[name] + if (value in json.usingComponents) { + newComponentPlaceholder[newName] = usingComponentsNameMap[value] + } + } + Object.assign(json.componentPlaceholder, newComponentPlaceholder) + } + // usingComponents 替换 + if (json.usingComponents) { + const newUsingComponents = {} + for (const name of Object.keys(json.usingComponents)) { + const newName = usingComponentsNameMap[name] + newUsingComponents[newName] = json.usingComponents[name] + delete json.usingComponents[name] + } + Object.assign(json.usingComponents, newUsingComponents) + } + } + callback() + } + if (isApp) { // app.json const localPages = [] @@ -605,6 +636,9 @@ module.exports = function (content) { } async.parallel([ + (callback) => { + processOptimizeSize(json, callback) + }, (callback) => { // 添加首页标识 if (json.pages && json.pages[0]) { @@ -683,6 +717,9 @@ module.exports = function (content) { } } async.parallel([ + (callback) => { + processOptimizeSize(json, callback) + }, (callback) => { processComponents(json.usingComponents, this.context, callback) }, diff --git a/packages/webpack-plugin/lib/loader.js b/packages/webpack-plugin/lib/loader.js index 3ef11429e1..a0fafed5e0 100644 --- a/packages/webpack-plugin/lib/loader.js +++ b/packages/webpack-plugin/lib/loader.js @@ -21,6 +21,7 @@ const { MPX_APP_MODULE_ID } = require('./utils/const') const path = require('path') const processMainScript = require('./web/processMainScript') const getRulesRunner = require('./platform') +const { CompressName } = require('./utils/optimize-compress.js') module.exports = function (content) { this.cacheable() @@ -50,6 +51,7 @@ module.exports = function (content) { const localSrcMode = queryObj.mode const srcMode = localSrcMode || globalSrcMode const autoScope = matchCondition(resourcePath, mpx.autoScopeRules) + const compressName = new CompressName() const emitWarning = (msg) => { this.emitWarning( @@ -113,7 +115,10 @@ module.exports = function (content) { const hasComment = templateAttrs && templateAttrs.comments const isNative = false - let usingComponents = [].concat(Object.keys(mpx.usingComponents)) + const usingComponentsNameMap = {} + for (const name in mpx.usingComponents) { + usingComponentsNameMap[name] = name // compressName._generateName() + } let componentPlaceholder = [] let componentGenerics = {} @@ -134,7 +139,11 @@ module.exports = function (content) { const ret = JSON5.parse(parts.json.content) if (rulesRunner) rulesRunner(ret) if (ret.usingComponents) { - usingComponents = usingComponents.concat(Object.keys(ret.usingComponents)) + for (const name in ret.usingComponents) { + // Object.keys(mpx.usingComponents): 压缩组件名,禁止与全局组件同名 + // mpx.reservedComponentName 禁止与保留组件名重复(如 text/view/ad 等) + usingComponentsNameMap[name] = ctorType === 'app' ? name : compressName._occupiedGenerateName([...Object.keys(mpx.usingComponents), ...mpx.reservedComponentName]) + } } if (ret.componentPlaceholder) { componentPlaceholder = componentPlaceholder.concat(Object.values(ret.componentPlaceholder)) @@ -192,7 +201,7 @@ module.exports = function (content) { srcMode, moduleId, ctorType, - usingComponents, + usingComponentsNameMap, componentGenerics }, callback) }, @@ -249,7 +258,8 @@ module.exports = function (content) { } // 注入模块id及资源路径 - output += `global.currentModuleId = ${JSON.stringify(moduleId)}\n` + // currentModuleId -> _id + output += `global._id = ${JSON.stringify(moduleId)}\n` if (!isProduction) { output += `global.currentResource = ${JSON.stringify(filePath)}\n` } @@ -279,12 +289,14 @@ module.exports = function (content) { : ctorType === 'component' ? 'Component' : 'App' - - output += `global.currentCtor = ${ctor}\n` - output += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => { + // currentCtor -> _ctor + output += `global._ctor = ${ctor}\n` + // currentCtorType -> _ctorT + output += `global._ctorT = ${JSON.stringify(ctor.replace(/^./, (match) => { return match.toLowerCase() }))}\n` - output += `global.currentResourceType = ${JSON.stringify(ctorType)}\n` + // currentResourceType -> _crt + output += `global._crt = ${JSON.stringify(ctorType)}\n` // template output += '/* template */\n' @@ -299,7 +311,7 @@ module.exports = function (content) { hasComment, isNative, moduleId, - usingComponents, + usingComponentsNameMap: JSON.stringify(usingComponentsNameMap), componentPlaceholder // 添加babel处理渲染函数中可能包含的...展开运算符 // 由于...运算符应用范围极小以及babel成本极高,先关闭此特性后续看情况打开 @@ -336,7 +348,12 @@ module.exports = function (content) { output += '/* json */\n' // 给予json默认值, 确保生成json request以自动补全json const json = parts.json || {} - output += getRequire('json', json, json.src && { ...queryObj, resourcePath }) + '\n' + output += getRequire('json', json, { + ...(json.src + ? { ...queryObj, resourcePath } + : null), + usingComponentsNameMap: JSON.stringify(usingComponentsNameMap) + }) + '\n' // script output += '/* script */\n' @@ -345,7 +362,8 @@ module.exports = function (content) { const script = parts.script || {} if (script) { scriptSrcMode = script.mode || scriptSrcMode - if (scriptSrcMode) output += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n` + // currentSrcMode -> _sm + if (scriptSrcMode) output += `global._sm = ${JSON.stringify(scriptSrcMode)}\n` // 传递ctorType以补全js内容 const extraOptions = { ...script.src diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index 052329e7ff..a4510dd6a2 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -241,19 +241,23 @@ module.exports = function (content) { } // 注入模块id及资源路径 - let output = `global.currentModuleId = ${JSON.stringify(moduleId)}\n` + // currentModuleId -> _id + let output = `global._id = ${JSON.stringify(moduleId)}\n` if (!isProduction) { output += `global.currentResource = ${JSON.stringify(filePath)}\n` } - - output += `global.currentCtor = ${ctor}\n` - output += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => { + // currentCtor -> _ctor + output += `global._ctor = ${ctor}\n` + // currentCtorType -> _ctorT + output += `global._ctorT = ${JSON.stringify(ctor.replace(/^./, (match) => { return match.toLowerCase() }))}\n` - output += `global.currentResourceType = ${JSON.stringify(ctorType)}\n` + // currentResourceType -> _crt + output += `global._crt = ${JSON.stringify(ctorType)}\n` if (srcMode) { - output += `global.currentSrcMode = ${JSON.stringify(srcMode)}\n` + // currentSrcMode -> _sm + output += `global._sm = ${JSON.stringify(srcMode)}\n` } for (const type in typeResourceMap) { diff --git a/packages/webpack-plugin/lib/template-compiler/compiler.js b/packages/webpack-plugin/lib/template-compiler/compiler.js index 0ea67b5fa5..35e6c9dfe5 100644 --- a/packages/webpack-plugin/lib/template-compiler/compiler.js +++ b/packages/webpack-plugin/lib/template-compiler/compiler.js @@ -12,6 +12,7 @@ const transDynamicClassExpr = require('./trans-dynamic-class-expr') const dash2hump = require('../utils/hump-dash').dash2hump const makeMap = require('../utils/make-map') const { isNonPhrasingTag } = require('../utils/dom-tag-config') +const { isProductionLikeMode } = require('../utils/optimize-compress') const no = function () { return false @@ -623,6 +624,8 @@ function parse (template, options) { filePath = options.filePath i18n = options.i18n refId = 0 + options.usingComponentsNameMap = options.usingComponentsNameMap || {} + const usingComponents = Object.keys(options.usingComponentsNameMap) rulesRunner = getRulesRunner({ mode, @@ -630,7 +633,7 @@ function parse (template, options) { type: 'template', testKey: 'tag', data: { - usingComponents: options.usingComponents + usingComponents }, warn: _warn, error: _error @@ -780,7 +783,7 @@ function parse (template, options) { if (!tagNames.has('component') && options.checkUsingComponents) { const arr = [] - options.usingComponents.forEach((item) => { + usingComponents.forEach((item) => { if (!tagNames.has(item) && !options.globalComponents.includes(item) && !options.componentPlaceholder.includes(item)) { arr.push(item) } @@ -788,6 +791,10 @@ function parse (template, options) { arr.length && warn$1(`\n ${options.filePath} \n 组件 ${arr.join(' | ')} 注册了,但是未被对应的模板引用,建议删除!`) } + if (options.optimizeSize && isProductionLikeMode(this)) { + processOptimizeSize(root, options) + } + return { root, meta @@ -945,7 +952,7 @@ function processComponentIs (el, options) { } options = options || {} - el.components = options.usingComponents + el.components = Object.keys(options.usingComponentsNameMap) if (!el.components) { warn$1('Component in which tag is used must have a nonblank usingComponents field') } @@ -1661,7 +1668,7 @@ function isRealNode (el) { } function isComponentNode (el, options) { - return options.usingComponents.indexOf(el.tag) !== -1 || el.tag === 'component' + return options.usingComponentsNameMap[el.tag] || el.tag === 'component' } function processAliExternalClassesHack (el, options) { @@ -2213,6 +2220,41 @@ function postProcessComponentIs (el) { return el } +function processOptimizeSize (root, options) { + const { usingComponentsNameMap } = options + function walkNode (root, callback) { + if (!root) return + callback(root) + if (Array.isArray(root.children) && root.children.length) { + for (const node of root.children) { + if (!node) continue + walkNode(node, callback) + } + } + } + // 记录所有原生组件(判断条件:usingComponents中不存在的组件) + const nativeTags = new Set() + walkNode(root, (node) => { + if (node.tag && !usingComponentsNameMap[node.tag]) { + nativeTags.add(node.tag) + } + }) + // template中的使用到的组件不会经过压缩,如果使用自定义组件会出现问题,如果存在template,发出报错 + if (nativeTags.has('template')) error$1('使用template模板无法开启组件名压缩') + // 校验压缩后的组件名是否与原生组件冲突 + for (const name of Object.values(usingComponentsNameMap)) { + if (nativeTags.has(name)) { + error$1(`压缩后组件与原生组件 <${name}> 冲突,请在配置中添加白名单`) + } + } + walkNode(root, (node) => { + // 只有自定义组件才替换(判断条件:在usingComponents中的组件),并且新组件名不允许出现原生组件名 + if (node.tag && usingComponentsNameMap[node.tag]) { + node.tag = usingComponentsNameMap[node.tag] + } + }) +} + function stringifyAttr (val) { if (typeof val === 'string') { const hasSingle = val.indexOf('\'') > -1 diff --git a/packages/webpack-plugin/lib/template-compiler/index.js b/packages/webpack-plugin/lib/template-compiler/index.js index 9e2019a15a..63a34f3e02 100644 --- a/packages/webpack-plugin/lib/template-compiler/index.js +++ b/packages/webpack-plugin/lib/template-compiler/index.js @@ -13,6 +13,7 @@ module.exports = function (raw) { const env = mpx.env const defs = mpx.defs const i18n = mpx.i18n + const optimizeSize = mpx.optimizeSize const externalClasses = mpx.externalClasses const decodeHTMLText = mpx.decodeHTMLText const globalSrcMode = mpx.srcMode @@ -22,7 +23,7 @@ module.exports = function (raw) { const pagesMap = mpx.pagesMap const wxsContentMap = mpx.wxsContentMap const optimizeRenderRules = mpx.optimizeRenderRules - const usingComponents = queryObj.usingComponents || [] + const usingComponentsNameMap = queryObj.usingComponentsNameMap ? JSON.parse(queryObj.usingComponentsNameMap) : {} const componentPlaceholder = queryObj.componentPlaceholder || [] const hasComment = queryObj.hasComment const isNative = queryObj.isNative @@ -51,7 +52,7 @@ module.exports = function (raw) { const { root: ast, meta } = compiler.parse(raw, { warn, error, - usingComponents, + usingComponentsNameMap, componentPlaceholder, hasComment, isNative, @@ -68,6 +69,7 @@ module.exports = function (raw) { // 这里需传递resourcePath和wxsContentMap保持一致 filePath: resourcePath, i18n, + optimizeSize, checkUsingComponents: matchCondition(resourcePath, mpx.checkUsingComponentsRules), globalComponents: Object.keys(mpx.usingComponents), forceProxyEvent: matchCondition(resourcePath, mpx.forceProxyEventRules), @@ -92,9 +94,9 @@ module.exports = function (raw) { const src = loaderUtils.urlToRequest(meta.wxsModuleMap[module], root) resultSource += `var ${module} = require(${loaderUtils.stringifyRequest(this, src)});\n` } - + // currentInject -> _j resultSource += ` -global.currentInject = { +global._j = { moduleId: ${JSON.stringify(moduleId)} };\n` @@ -117,7 +119,7 @@ global.currentInject = { ignoreMap }) resultSource += ` -global.currentInject.render = function (_i, _c, _r, _sc) { +global._j.render = function (_i, _c, _r, _sc) { ${bindResult.code} _r(${optimizeRenderLevel === 2 ? 'true' : ''}); };\n` @@ -139,20 +141,20 @@ ${e.stack}`) if (meta.computed) { resultSource += bindThis.transform(` -global.currentInject.injectComputed = { +global._j.injectComputed = { ${meta.computed.join(',')} };`).code + '\n' } if (meta.refs) { resultSource += ` -global.currentInject.getRefsData = function () { +global._j.getRefsData = function () { return ${JSON.stringify(meta.refs)}; };\n` } if (meta.options) { - resultSource += `global.currentInject.injectOptions = ${JSON.stringify(meta.options)};` + '\n' + resultSource += `global._j.injectOptions = ${JSON.stringify(meta.options)};` + '\n' } this.emitFile(resourcePath, '', undefined, { diff --git a/packages/webpack-plugin/lib/utils/optimize-compress.js b/packages/webpack-plugin/lib/utils/optimize-compress.js new file mode 100644 index 0000000000..273bfa0fa0 --- /dev/null +++ b/packages/webpack-plugin/lib/utils/optimize-compress.js @@ -0,0 +1,55 @@ + +class CompressName { + constructor () { + this.index = 0 + this._compressMap = new Map() + } + + _generateName () { + let name = this.index.toString(36) + // 首字母不能是数字(转化为number不是NaN就是数字) + if (!isNaN(+name[0])) name = 'a' + name.substring(1) + this.index = parseInt(name, 36) + 1 + return name + } + + // 指定不允许使用的key + // occupiedKey: string[] | string + _occupiedGenerateName (occupiedKey) { + let key = this._generateName() + + if (!occupiedKey) return key + if (typeof occupiedKey === 'string') occupiedKey = [occupiedKey] + + while (occupiedKey.indexOf(key) !== -1) { + key = this._generateName() + } + return key + } + + compress (source, occupiedKey) { + if (!this._compressMap.has(source)) { + this._compressMap.set(source, this._occupiedGenerateName(occupiedKey)) + } + return this._compressMap.get(source) + } +} + +const namespaces = {} +function generateVariableNameBySource (source, namespace, occupiedKey) { + if (!namespaces[namespace]) { + // 限制只能字母开头 + namespaces[namespace] = new CompressName() + } + return namespaces[namespace].compress(source, occupiedKey) +} + +const isProductionLikeMode = options => { + return options.mode === 'production' || !options.mode +} + +module.exports = { + generateVariableNameBySource, + isProductionLikeMode, + CompressName +} diff --git a/packages/webpack-plugin/lib/web/processTemplate.js b/packages/webpack-plugin/lib/web/processTemplate.js index a3f69d76f8..2f89b3bd70 100644 --- a/packages/webpack-plugin/lib/web/processTemplate.js +++ b/packages/webpack-plugin/lib/web/processTemplate.js @@ -12,7 +12,7 @@ module.exports = function (template, { srcMode, moduleId, ctorType, - usingComponents, + usingComponentsNameMap, componentGenerics }, callback) { const mpx = loaderContext.getMpx() @@ -72,7 +72,7 @@ module.exports = function (template, { new Error('[template compiler][' + loaderContext.resource + ']: ' + msg) ) }, - usingComponents, + usingComponentsNameMap, hasComment, isNative, isComponent: ctorType === 'component', diff --git a/packages/webpack-plugin/lib/web/script-helper.js b/packages/webpack-plugin/lib/web/script-helper.js index 2eb891c153..679a4f9c6b 100644 --- a/packages/webpack-plugin/lib/web/script-helper.js +++ b/packages/webpack-plugin/lib/web/script-helper.js @@ -182,9 +182,12 @@ function buildGlobalParams ({ global.__mpxOptionsMap = global.__mpxOptionsMap || {} global.__mpxTransRpxFn = ${webConfig.transRpxFn} \n` } - content += ` global.currentModuleId = ${JSON.stringify(moduleId)}\n` - content += ` global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n` - content += ` global.currentInject = ${JSON.stringify({ moduleId })}\n` + // currentModuleId -> _id + content += ` global._id = ${JSON.stringify(moduleId)}\n` + // currentSrcMode -> _sm + content += ` global._sm = ${JSON.stringify(scriptSrcMode)}\n` + // currentInject -> _j + content += ` global._j = ${JSON.stringify({ moduleId })}\n` if (!isProduction) { content += ` global.currentResource = ${JSON.stringify(loaderContext.resourcePath)}\n` }