Skip to content

feat: enable inline for CommonJS bundle #2442

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

Merged
merged 6 commits into from
Oct 18, 2022
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
5 changes: 5 additions & 0 deletions .changeset/spicy-suits-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Inline ESM-only dependencies in CommonJS bundle
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"codemods",
"dist",
"lib",
"lib/node_modules",
"lib-esm",
"index.d.ts",
"drafts/package.json",
Expand Down
38 changes: 11 additions & 27 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,22 @@ const input = new Set([
])

const extensions = ['.js', '.jsx', '.ts', '.tsx']
const external = [
const ESM_ONLY = new Set(['@github/combobox-nav', '@github/markdown-toolbar-element', '@github/paste-markdown'])
const dependencies = [
...Object.keys(packageJson.peerDependencies ?? {}),
...Object.keys(packageJson.dependencies ?? {}),
...Object.keys(packageJson.devDependencies ?? {})
]
function isExternal(external) {
return id => {
// Match on import paths that are the same as dependencies listed in
// `package.json`
const match = external.find(pkg => {
return id === pkg
})

if (match) {
return true
}

// In some cases, there may be a subpath import from a module which should
// also be treated as external. For example:
//
// External: @primer/behaviors
// Import: @primer/behaviors/utils
return external.some(pkg => {
// Include the / to not match imports like: @primer/behaviors-for-acme/utils
return id.startsWith(`${pkg}/`)
})
}
function createPackageRegex(name) {
return new RegExp(`^${name}(/.*)?`)
}

const baseConfig = {
input: Array.from(input),
plugins: [
// Note: it's important that the babel plugin is ordered first for plugins
// like babel-plugin-preval to work as-intended
// Note: it's important that the babel-plugin-preval is loaded first
// to work as-intended
babel({
extensions,
exclude: /node_modules/,
Expand Down Expand Up @@ -116,7 +98,9 @@ const baseConfig = {
]
]
}),
commonjs(),
commonjs({
extensions
}),
resolve({
extensions
})
Expand All @@ -127,7 +111,7 @@ export default [
// ESM
{
...baseConfig,
external: isExternal(external),
external: dependencies.map(createPackageRegex),
output: {
dir: 'lib-esm',
format: 'esm',
Expand All @@ -139,7 +123,7 @@ export default [
// CommonJS
{
...baseConfig,
external: isExternal(external),
external: dependencies.filter(name => !ESM_ONLY.has(name)).map(createPackageRegex),
output: {
dir: 'lib',
format: 'commonjs',
Expand Down