Skip to content

Commit

Permalink
Merge pull request #1097 from stealjs/guard-importspecifiers
Browse files Browse the repository at this point in the history
Account for modules without importNames
  • Loading branch information
matthewp committed Nov 28, 2018
2 parents 492651f + 9442dc3 commit 14634e5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/graph/treeshake.js
Expand Up @@ -97,14 +97,15 @@ function loadFromGraph(getNode, data) {
for(let depName of dependants) {
let localNode = getNode(depName);
let specifiers = localNode.load.metadata.importSpecifiers || {};
let importNames = localNode.load.metadata.importNames || {};

for(let [impSource,] of entries(specifiers)) {
let depName = moduleNameFromSpecifier(localNode, impSource);
if(depName !== id) {
continue;
}

let imported = localNode.load.metadata.importNames[impSource] || [];
let imported = importNames[impSource] || [];
imported.forEach(imported => {
needToExport.add(imported);
});
Expand Down
2 changes: 2 additions & 0 deletions lib/stream/treeshake.js
@@ -1,6 +1,7 @@
var through = require("through2");
var prune = require("../graph/prune");
var treeshake = require("../graph/treeshake");
var winston = require("winston");

module.exports = function() {
return through.obj(function(data, enc, next) {
Expand All @@ -17,6 +18,7 @@ module.exports = function() {
return;
}

winston.info("Tree shaking...");
treeshake(data, options)
.then(function(){
prune(data);
Expand Down
1 change: 1 addition & 0 deletions test/tree_shaking_test.js
Expand Up @@ -274,6 +274,7 @@ describe("Tree-shaking", function(){
});

describe("import { dep } from 'cjs'", function() {
var app;
before(function(done){
this.timeout(20000);
var base = path.join(__dirname, "treeshake", "cjs");
Expand Down
1 change: 1 addition & 0 deletions test/treeshake/cjs/file.txt
@@ -0,0 +1 @@
THIS IS A TEXT FILE HA HA HA.
1 change: 1 addition & 0 deletions test/treeshake/cjs/index.js
@@ -1,4 +1,5 @@
import { assign } from './dep';
import './other';
import './file.txt!~/text';

window.APP = { assign };
10 changes: 10 additions & 0 deletions test/treeshake/cjs/text.js
@@ -0,0 +1,10 @@

exports.translate = function(load) {
load.metadata.importSpecifiers = {
"./dep": {
start: { line: 1, column: 28 },
end: { line: 1, column: 30 },
}
};
return "require('./dep'); module.exports = function(){};";
};

0 comments on commit 14634e5

Please sign in to comment.