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: limit import() rewrites #547

Merged
merged 2 commits into from
Jan 21, 2019
Merged
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
44 changes: 23 additions & 21 deletions packages/rollup-rewriter/rewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};
Expand Down Expand Up @@ -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();
});
},
Expand Down
269 changes: 269 additions & 0 deletions packages/rollup-rewriter/test/__snapshots__/rewriter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": "
Expand Down
Loading