From e9395ebc03118ce20483b8a44ff5c3b8a59157cd Mon Sep 17 00:00:00 2001 From: shiftj18 Date: Tue, 30 Aug 2022 22:57:58 +0800 Subject: [PATCH 1/3] fix(platform-loader): await SourceMapConsumer and destroy --- packages/platform-loader/src/index.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/platform-loader/src/index.js b/packages/platform-loader/src/index.js index 1b0cb625d..4aa1716a0 100644 --- a/packages/platform-loader/src/index.js +++ b/packages/platform-loader/src/index.js @@ -23,10 +23,10 @@ const traverseImport = require('./TraverseImport'); * ``` */ -function mergeSourceMap(map, inputMap) { +async function mergeSourceMap(map, inputMap) { if (inputMap) { - const inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap); - const outputMapConsumer = new sourceMap.SourceMapConsumer(map); + const inputMapConsumer = await new sourceMap.SourceMapConsumer(inputMap); + const outputMapConsumer = await new sourceMap.SourceMapConsumer(map); const mergedGenerator = new sourceMap.SourceMapGenerator({ file: inputMapConsumer.file, @@ -59,13 +59,17 @@ function mergeSourceMap(map, inputMap) { const mergedMap = mergedGenerator.toJSON(); inputMap.mappings = mergedMap.mappings; + + inputMapConsumer.destroy(); + outputMapConsumer.destroy(); + return inputMap; } else { return map; } } -module.exports = function (inputSource, inputSourceMap) { +module.exports = async function (inputSource, inputSourceMap) { this.cacheable(); const callback = this.async(); @@ -90,5 +94,5 @@ module.exports = function (inputSource, inputSourceMap) { sourceFileName: resourcePath, }); - callback(null, code, mergeSourceMap(map, inputSourceMap)); + callback(null, code, await mergeSourceMap(map, inputSourceMap)); }; From a0276931f89b6027fbdbe8c4dc349477e2644fb0 Mon Sep 17 00:00:00 2001 From: shiftj18 Date: Fri, 9 Sep 2022 17:52:01 +0800 Subject: [PATCH 2/3] fix(platform-loader): can't insert isWeex when import {isWeex as iw} from * --- .../platform-loader/src/TraverseImport.js | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/packages/platform-loader/src/TraverseImport.js b/packages/platform-loader/src/TraverseImport.js index ca8c3158f..f1def8618 100644 --- a/packages/platform-loader/src/TraverseImport.js +++ b/packages/platform-loader/src/TraverseImport.js @@ -188,25 +188,18 @@ module.exports = function traverseImport(options, inputSource, sourceMapOption) ], )); } else { + // Support custom alias import: + // import { isWeex as iw } from 'universal-env'; + // Correct the logic of next line. Variable "isWeex" can be declared again after alias to "iw”. So, can't insert "const isWeex = true". + // const isWeex = true; + // const iw = true; const newNodeInit = platformMap[options.platform].indexOf(specObj.imported) >= 0; - let newNode = variableDeclarationMethod( - specObj.imported, + const hasAlias = specObj.imported !== specObj.local; + const newNode = variableDeclarationMethod( + hasAlias ? specObj.local : specObj.imported, newNodeInit, ); - path.insertAfter(newNode); - - // Support custom alise import: - // import { isWeex as iw } from 'universal-env'; - // const isWeex = true; - // const iw = true; - if (specObj.imported !== specObj.local) { - newNode = variableDeclarationMethod( - specObj.local, - newNodeInit, - ); - path.insertAfter(newNode); - } } }); From 50e66a0917ff3c951bad764bba390d2f206e76cc Mon Sep 17 00:00:00 2001 From: shiftj18 Date: Tue, 13 Sep 2022 11:17:56 +0800 Subject: [PATCH 3/3] feat(platform-loader): add options.memberExpObjName --- packages/platform-loader/src/TraverseImport.js | 2 +- packages/platform-loader/src/index.js | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/platform-loader/src/TraverseImport.js b/packages/platform-loader/src/TraverseImport.js index f1def8618..51a8659e0 100644 --- a/packages/platform-loader/src/TraverseImport.js +++ b/packages/platform-loader/src/TraverseImport.js @@ -150,7 +150,7 @@ module.exports = function traverseImport(options, inputSource, sourceMapOption) // don't remove like: var _universalEnv = {isWeex: false}; if(_universalEnv.isWeex){ xxx } // change _universalEnv.isWeex to false const { node } = path; - if (hasPlatformSpecified && node.object.name === '_universalEnv') { + if (hasPlatformSpecified && options.memberExpObjName.indexOf(node.object.name) !== -1) { if (platformMap[options.platform].indexOf(node.property.name) >= 0) { path.replaceWith(types.Identifier('true')); } else { diff --git a/packages/platform-loader/src/index.js b/packages/platform-loader/src/index.js index 4aa1716a0..94eb2a186 100644 --- a/packages/platform-loader/src/index.js +++ b/packages/platform-loader/src/index.js @@ -77,7 +77,7 @@ module.exports = async function (inputSource, inputSourceMap) { const { resourcePath } = this; const sourceMapTarget = path.basename(resourcePath); - const options = Object.assign({ name: 'universal-env' }, loaderOptions); + const options = Object.assign({ name: 'universal-env', memberExpObjName: '_universalEnv' }, loaderOptions); if (!options.platform) { callback(null, inputSource); @@ -88,6 +88,10 @@ module.exports = async function (inputSource, inputSourceMap) { options.name = [options.name]; } + if (!Array.isArray(options.memberExpObjName)) { + options.memberExpObjName = [options.memberExpObjName]; + } + const { code, map } = traverseImport(options, inputSource, { sourceMaps: true, sourceMapTarget,