Skip to content

Commit

Permalink
feat(rollup-rewriter): let loader option be a function (#667)
Browse files Browse the repository at this point in the history
Allows for picking a file out of the bundle to be used, even if the output name is hashed by rollup.
  • Loading branch information
tivac committed Sep 11, 2019
1 parent 5845c60 commit a57cddf
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/rollup-rewriter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ export default {

## Options

### `loader` (string)
### `loader` (string|function)

The `loader` option can be set if you want the plugin to inject a reference to a CSS loader that returns a promise (I like [`lazyload-css`](https://npmjs.com/lazyload-css)). This loader must be available statically, so this option is most useful in `es`/`esm` mode where it can be loaded via `import`.
The `loader` option can be set if you want the plugin to inject a reference to a CSS loader that returns a promise (I like [`lazyload-css`](https://npmjs.com/lazyload-css)). This loader must be available statically, so this option is most useful in `es`/`esm` mode where it can be loaded via `import`. If given a function instead of a string that function will be called once per chunk being modified, it gets passed a single argument of the form `{ chunks }` where `chunks` is the raw rollup output chunks.

### `loadfn` (string)

Expand Down
4 changes: 2 additions & 2 deletions packages/rollup-rewriter/formats/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports.regex = (deps) => {
);
};

exports.loader = (options, str) => {
exports.loader = ({ content, str }) => {
const s = str.toString();
const i = s.indexOf(search);

Expand All @@ -28,7 +28,7 @@ exports.loader = (options, str) => {
// + 1 is for the newline...
str.appendRight(
i + search.length + 1,
`${options.loader}\n`
`${content}\n`
);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/rollup-rewriter/formats/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.regex = (deps) => {
);
};

exports.loader = (options, str) => str.prepend(`${options.loader}\n`);
exports.loader = ({ content, str }) => str.prepend(`${content}\n`);

exports.load = (options, imports, statement) => `
Promise.all([
Expand Down
4 changes: 2 additions & 2 deletions packages/rollup-rewriter/formats/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.regex = (deps) => {
);
};

exports.loader = (options, str) => {
exports.loader = ({ content, str }) => {
const s = str.toString();
const i = s.indexOf(search);

Expand All @@ -24,7 +24,7 @@ exports.loader = (options, str) => {
// + 1 is for the newline...
str.appendRight(
i + search.length + 1,
`${options.loader}\n`
`${content}\n`
);
};

Expand Down
6 changes: 5 additions & 1 deletion packages/rollup-rewriter/rewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ module.exports = (opts) => {
const str = new MagicString(code);

if(options.loader) {
loader(options, str);
const content = typeof options.loader === "function" ?
options.loader({ chunks, options }) :
options.loader;

loader({ content, str });
}

// Yay stateful regexes
Expand Down
288 changes: 288 additions & 0 deletions packages/rollup-rewriter/test/__snapshots__/rewriter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,294 @@ System.register([], function (exports) {
}
`;

exports[`rollup-rewriter should support loader being a function ("amd") 1`] = `
Object {
"a": "
define(['require'], function (require) { 'use strict';
import chunkCountIs8 from \\"./css.js\\";
var css = {
\\"a\\": \\"a\\"
};
function a() {
console.log(css);
new Promise(function (resolve, reject) { Promise.all([
lazyload(\\"./assets/c.css\\"),
new Promise(function (resolve, reject) { require(['./c'], resolve, reject) })
])
.then((results) => resolve(results[results.length - 1]))
.catch(reject) }).then(console.log);
}
return a;
});
",
"b": "
define(['require'], function (require) { 'use strict';
import chunkCountIs8 from \\"./css.js\\";
var css = {
\\"b\\": \\"b\\"
};
function b() {
console.log(css);
new Promise(function (resolve, reject) { Promise.all([
lazyload(\\"./assets/d.css\\"),
new Promise(function (resolve, reject) { require(['./d'], resolve, reject) })
])
.then((results) => resolve(results[results.length - 1]))
.catch(reject) }).then(console.log);
}
return b;
});
",
"c": "
define(['exports'], function (exports) { 'use strict';
var css = {
\\"c\\": \\"c\\"
};
console.log(css);
exports.default = css;
});
",
"d": "
define(['exports'], function (exports) { 'use strict';
var css = {
\\"d\\": \\"d\\"
};
console.log(css);
exports.default = css;
});
",
}
`;

exports[`rollup-rewriter should support loader being a function ("es") 1`] = `
Object {
"a": "
import chunkCountIs8 from \\"./css.js\\";
var css = {
\\"a\\": \\"a\\"
};
function a() {
console.log(css);
Promise.all([
lazyload(\\"./assets/c.css\\"),
import('./c.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}
export default a;
",
"b": "
import chunkCountIs8 from \\"./css.js\\";
var css = {
\\"b\\": \\"b\\"
};
function b() {
console.log(css);
Promise.all([
lazyload(\\"./assets/d.css\\"),
import('./d.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}
export default b;
",
"c": "
var css = {
\\"c\\": \\"c\\"
};
console.log(css);
export default css;
",
"d": "
var css = {
\\"d\\": \\"d\\"
};
console.log(css);
export default css;
",
}
`;

exports[`rollup-rewriter should support loader being a function ("esm") 1`] = `
Object {
"a": "
import chunkCountIs8 from \\"./css.js\\";
var css = {
\\"a\\": \\"a\\"
};
function a() {
console.log(css);
Promise.all([
lazyload(\\"./assets/c.css\\"),
import('./c.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}
export default a;
",
"b": "
import chunkCountIs8 from \\"./css.js\\";
var css = {
\\"b\\": \\"b\\"
};
function b() {
console.log(css);
Promise.all([
lazyload(\\"./assets/d.css\\"),
import('./d.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}
export default b;
",
"c": "
var css = {
\\"c\\": \\"c\\"
};
console.log(css);
export default css;
",
"d": "
var css = {
\\"d\\": \\"d\\"
};
console.log(css);
export default css;
",
}
`;

exports[`rollup-rewriter should support loader being a function ("system") 1`] = `
Object {
"a": "
System.register([], function (exports, module) {
'use strict';
import chunkCountIs8 from \\"./css.js\\";
return {
execute: function () {
exports('default', a);
var css = {
\\"a\\": \\"a\\"
};
function a() {
console.log(css);
Promise.all([
lazyload(\\"./assets/c.css\\"),
module.import('./c.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}
}
};
});
",
"b": "
System.register([], function (exports, module) {
'use strict';
import chunkCountIs8 from \\"./css.js\\";
return {
execute: function () {
exports('default', b);
var css = {
\\"b\\": \\"b\\"
};
function b() {
console.log(css);
Promise.all([
lazyload(\\"./assets/d.css\\"),
module.import('./d.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}
}
};
});
",
"c": "
System.register([], function (exports) {
'use strict';
return {
execute: function () {
var css = {
\\"c\\": \\"c\\"
};
console.log(css);
exports('default', css);
}
};
});
",
"d": "
System.register([], function (exports) {
'use strict';
return {
execute: function () {
var css = {
\\"d\\": \\"d\\"
};
console.log(css);
exports('default', css);
}
};
});
",
}
`;

exports[`rollup-rewriter shouldn't require a loader ("amd") 1`] = `
Object {
"a.js": "
Expand Down
Loading

0 comments on commit a57cddf

Please sign in to comment.