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

feat(rollup.config): improve tree-shaking using preserveModules: true #1133

Merged
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
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/inquirer": "^8.2.5",
Expand All @@ -106,4 +105,4 @@
"src"
],
"private": true
}
}
39 changes: 22 additions & 17 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
const path = require("path");
const typescript = require("@rollup/plugin-typescript");
const terser = require("@rollup/plugin-terser");
const nodeResolve = require("@rollup/plugin-node-resolve");
const commomnjs = require("@rollup/plugin-commonjs");

const outDir = "lib";

module.exports = {
input: "./src/index.ts",
output: {
dir: outDir,
dir: "./lib",
format: "esm",
entryFileNames: "[name].mjs",
sourcemap: true,
entryFileNames: (chunkInfo) => {
const ext = `mjs`
const externalDir = `_external`;
const nodeModulesDir = `node_modules`;
if (chunkInfo.name.includes(nodeModulesDir)) {
/** replace / to _ and the last part of the path is the file name */
const nameSplit = chunkInfo.name.split('/')
const chunkName = path.join(externalDir, nameSplit.slice(0, -1).join('_'), nameSplit.at(-1))
console.table({
before: chunkInfo.name,
after: chunkName,
})
return `${chunkName}.${ext}`;
}
return `[name].${ext}`;
},
preserveModules: true,
preserveModulesRoot: "src",
},
plugins: [
nodeResolve(),
commomnjs(),
typescript({
tsconfig: "tsconfig.json",
module: "ES2020",
target: "ES2020",
}),
terser({
format: {
comments: "some",
beautify: true,
ecma: "2020",
},
compress: false,
mangle: false,
module: true,
module: "ESNext",
target: "ESNext",
}),
],
};