Skip to content

Commit 6b559a9

Browse files
authored
feat: enable inline for CommonJS bundle (#2442)
* feat: enable inline for CommonJS bundle * chore: add changeset * chore: address eslint violations
1 parent e00d03c commit 6b559a9

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

.changeset/spicy-suits-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Inline ESM-only dependencies in CommonJS bundle

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"codemods",
6464
"dist",
6565
"lib",
66+
"lib/node_modules",
6667
"lib-esm",
6768
"index.d.ts",
6869
"drafts/package.json",

rollup.config.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,22 @@ const input = new Set([
5050
])
5151

5252
const extensions = ['.js', '.jsx', '.ts', '.tsx']
53-
const external = [
53+
const ESM_ONLY = new Set(['@github/combobox-nav', '@github/markdown-toolbar-element', '@github/paste-markdown'])
54+
const dependencies = [
5455
...Object.keys(packageJson.peerDependencies ?? {}),
5556
...Object.keys(packageJson.dependencies ?? {}),
5657
...Object.keys(packageJson.devDependencies ?? {})
5758
]
58-
function isExternal(external) {
59-
return id => {
60-
// Match on import paths that are the same as dependencies listed in
61-
// `package.json`
62-
const match = external.find(pkg => {
63-
return id === pkg
64-
})
65-
66-
if (match) {
67-
return true
68-
}
6959

70-
// In some cases, there may be a subpath import from a module which should
71-
// also be treated as external. For example:
72-
//
73-
// External: @primer/behaviors
74-
// Import: @primer/behaviors/utils
75-
return external.some(pkg => {
76-
// Include the / to not match imports like: @primer/behaviors-for-acme/utils
77-
return id.startsWith(`${pkg}/`)
78-
})
79-
}
60+
function createPackageRegex(name) {
61+
return new RegExp(`^${name}(/.*)?`)
8062
}
8163

8264
const baseConfig = {
8365
input: Array.from(input),
8466
plugins: [
85-
// Note: it's important that the babel plugin is ordered first for plugins
86-
// like babel-plugin-preval to work as-intended
67+
// Note: it's important that the babel-plugin-preval is loaded first
68+
// to work as-intended
8769
babel({
8870
extensions,
8971
exclude: /node_modules/,
@@ -116,7 +98,9 @@ const baseConfig = {
11698
]
11799
]
118100
}),
119-
commonjs(),
101+
commonjs({
102+
extensions
103+
}),
120104
resolve({
121105
extensions
122106
})
@@ -127,7 +111,7 @@ export default [
127111
// ESM
128112
{
129113
...baseConfig,
130-
external: isExternal(external),
114+
external: dependencies.map(createPackageRegex),
131115
output: {
132116
dir: 'lib-esm',
133117
format: 'esm',
@@ -139,7 +123,7 @@ export default [
139123
// CommonJS
140124
{
141125
...baseConfig,
142-
external: isExternal(external),
126+
external: dependencies.filter(name => !ESM_ONLY.has(name)).map(createPackageRegex),
143127
output: {
144128
dir: 'lib',
145129
format: 'commonjs',

0 commit comments

Comments
 (0)