diff --git a/packages/platform-loader/src/TraverseImport.js b/packages/platform-loader/src/TraverseImport.js index ca8c3158f..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 { @@ -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); - } } }); diff --git a/packages/platform-loader/src/index.js b/packages/platform-loader/src/index.js index 1b0cb625d..94eb2a186 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(); @@ -73,7 +77,7 @@ module.exports = 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); @@ -84,11 +88,15 @@ module.exports = 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, sourceFileName: resourcePath, }); - callback(null, code, mergeSourceMap(map, inputSourceMap)); + callback(null, code, await mergeSourceMap(map, inputSourceMap)); };