Skip to content
Permalink
Browse files

fix: limit import() rewrites (#547)

- only rewrite import() when necessary
0 If the chunk doesn't have any assets there's no point trying to rewrite it and exploding.
  • Loading branch information...
tivac committed Jan 21, 2019
1 parent 1b5f77a commit 32e58e6a4436cc2d1a43368b0a86fc9aae08d4a8
@@ -8,7 +8,7 @@ const formats = {
es : require("./formats/es.js"),
amd : require("./formats/amd.js"),
system : require("./formats/system.js"),

// Just an alias...
esm : require("./formats/es.js"),
};
@@ -45,49 +45,51 @@ module.exports = (opts) => {
code = "",
dynamicImports = [],
} = chunk;

// Guard against https://github.com/rollup/rollup/issues/2659
const deps = dynamicImports.filter(Boolean);

if(isAsset || !deps.length || !assets.length) {
return;
}

const { regex, loader, load } = formats[format];

const search = regex(deps.map(escape).join("|"));

const str = new MagicString(code);

if(options.loader) {
loader(options, str);
}

// Yay stateful regexes
search.lastIndex = 0;

let result = search.exec(code);

while(result) {
// Pull useful values out of the regex result
const [ statement, file ] = result;
const { index } = result;

const imports = chunks[file].assets.map((dep) =>
`${options.loadfn}("./${dep}")`
);

str.overwrite(
index,
index + statement.length,
dedent(load(options, imports.join(",\n"), statement))
);


if(chunks[file].assets) {
const imports = chunks[file].assets.map((dep) =>
`${options.loadfn}("./${dep}")`
);

str.overwrite(
index,
index + statement.length,
dedent(load(options, imports.join(",\n"), statement))
);
}

result = search.exec(code);
}

log("Updating", entry);

chunk.code = str.toString();
});
},
@@ -12,6 +12,275 @@ Array [
]
`;

exports[`rollup-rewriter should only rewrite when necessary: amd 1`] = `
Object {
"a.js": "
define(['require'], function (require) { 'use strict';

var css = {
\\"a\\": \\"a\\"
};

console.log(css);

new Promise(function (resolve, reject) { Promise.all([
lazyload(\\"./assets/chunk.css\\"),
new Promise(function (resolve, reject) { require(['./chunk.js'], resolve, reject) })
])
.then((results) => resolve(results[results.length - 1]))
.catch(reject) }).then(console.log);

});
",
"assets/a.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/a.css */
.a { color: aqua; }
",
"assets/b.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/b.css */
.b { color: blue; }
",
"assets/chunk.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/c.css */
.c { color: crimson; }
",
"b.js": "
define(['require'], function (require) { 'use strict';

var css = {
\\"b\\": \\"b\\"
};

console.log(css);

new Promise(function (resolve, reject) { require(['./chunk2.js'], resolve, reject) }).then(console.log);

});
",
"chunk.js": "
define(['exports'], function (exports) { 'use strict';

var css = {
\\"c\\": \\"c\\"
};

console.log(css);

var c$1 = \\"c\\";

exports.default = c$1;

});
",
"chunk2.js": "
define(['exports'], function (exports) { 'use strict';

var d = \\"d\\";

exports.default = d;

});
",
}
`;

exports[`rollup-rewriter should only rewrite when necessary: es 1`] = `
Object {
"a.js": "
var css = {
\\"a\\": \\"a\\"
};

console.log(css);

Promise.all([
lazyload(\\"./assets/chunk.css\\"),
import('./chunk.js')
])
.then((results) => results[results.length - 1]).then(console.log);
",
"assets/a.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/a.css */
.a { color: aqua; }
",
"assets/b.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/b.css */
.b { color: blue; }
",
"assets/chunk.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/c.css */
.c { color: crimson; }
",
"b.js": "
var css = {
\\"b\\": \\"b\\"
};

console.log(css);

import('./chunk2.js').then(console.log);
",
"chunk.js": "
var css = {
\\"c\\": \\"c\\"
};

console.log(css);

var c$1 = \\"c\\";

export default c$1;
",
"chunk2.js": "
var d = \\"d\\";

export default d;
",
}
`;

exports[`rollup-rewriter should only rewrite when necessary: esm 1`] = `
Object {
"a.js": "
var css = {
\\"a\\": \\"a\\"
};

console.log(css);

Promise.all([
lazyload(\\"./assets/chunk.css\\"),
import('./chunk.js')
])
.then((results) => results[results.length - 1]).then(console.log);
",
"assets/a.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/a.css */
.a { color: aqua; }
",
"assets/b.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/b.css */
.b { color: blue; }
",
"assets/chunk.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/c.css */
.c { color: crimson; }
",
"b.js": "
var css = {
\\"b\\": \\"b\\"
};

console.log(css);

import('./chunk2.js').then(console.log);
",
"chunk.js": "
var css = {
\\"c\\": \\"c\\"
};

console.log(css);

var c$1 = \\"c\\";

export default c$1;
",
"chunk2.js": "
var d = \\"d\\";

export default d;
",
}
`;

exports[`rollup-rewriter should only rewrite when necessary: system 1`] = `
Object {
"a.js": "
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

var css = {
\\"a\\": \\"a\\"
};

console.log(css);

Promise.all([
lazyload(\\"./assets/chunk.css\\"),
module.import('./chunk.js')
])
.then((results) => results[results.length - 1]).then(console.log);

}
};
});
",
"assets/a.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/a.css */
.a { color: aqua; }
",
"assets/b.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/b.css */
.b { color: blue; }
",
"assets/chunk.css": "
/* packages/rollup-rewriter/test/specimens/no-asset-imports/c.css */
.c { color: crimson; }
",
"b.js": "
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

var css = {
\\"b\\": \\"b\\"
};

console.log(css);

module.import('./chunk2.js').then(console.log);

}
};
});
",
"chunk.js": "
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

var css = {
\\"c\\": \\"c\\"
};

console.log(css);

var c$1 = exports('default', \\"c\\");

}
};
});
",
"chunk2.js": "
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

var d = exports('default', \\"d\\");

}
};
});
",
}
`;

exports[`rollup-rewriter should support loader & loadfn: amd 1`] = `
Object {
"a.js": "
Oops, something went wrong.

0 comments on commit 32e58e6

Please sign in to comment.
You can’t perform that action at this time.