Skip to content

Instantly share code, notes, and snippets.

@surma
Last active July 28, 2020 13:13
Show Gist options
  • Save surma/519428064297de8a32639d1ec1acc720 to your computer and use it in GitHub Desktop.
Save surma/519428064297de8a32639d1ec1acc720 to your computer and use it in GitHub Desktop.
Rollup prunes dynamic import hook
build
node_modules
package-lock.json
console.log("lol");
import("./dynamic.js");
{
"name": "rollup-purge",
"scripts": {
"build": "rollup -c"
}
}
const MARKER = "myimport:";
export default {
input: "input.js",
output: {
dir: "build",
format: "esm"
},
preserveEntrySignatures: false,
plugins: [
{
resolveId(id) {
if (id === MARKER) {
return MARKER;
}
},
load(id) {
if (id === MARKER) {
return `export function myimport(id){}`;
}
},
transform(code, id) {
if (id === MARKER) {
return;
}
return `
import {myimport} from "${MARKER}"; ${code}
// It works with the following line
// self[Symbol()] = myimport;
`;
},
renderDynamicImport(moduleId) {
if (moduleId === MARKER) {
return;
}
return {
left: "myimport(",
right: ", import.meta.url)"
};
}
}
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment