Skip to content

Commit

Permalink
feat: enable inline for CommonJS bundle (#2442)
Browse files Browse the repository at this point in the history
* feat: enable inline for CommonJS bundle

* chore: add changeset

* chore: address eslint violations
  • Loading branch information
joshblack committed Oct 18, 2022
1 parent e00d03c commit 6b559a9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
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

0 comments on commit 6b559a9

Please sign in to comment.