Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform-loader): await SourceMapConsumer and destroy #1018

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions packages/platform-loader/src/TraverseImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
});

Expand Down
20 changes: 14 additions & 6 deletions packages/platform-loader/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -59,21 +59,25 @@ 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();

const loaderOptions = loaderUtils.getOptions(this);
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);
Expand All @@ -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));
};