From a45f4bf9bea4129958e1464173af86af5c888352 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Mon, 17 Oct 2022 12:50:42 -0500 Subject: [PATCH 1/3] feat: enable inline for CommonJS bundle --- package.json | 1 + rollup.config.js | 38 +++++++++++--------------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 24030770bb7..dde245c6535 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "codemods", "dist", "lib", + "lib/node_modules", "lib-esm", "index.d.ts", "drafts/package.json", diff --git a/rollup.config.js b/rollup.config.js index 73a2dcb8d51..50f38494226 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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/, @@ -116,7 +98,9 @@ const baseConfig = { ] ] }), - commonjs(), + commonjs({ + extensions + }), resolve({ extensions }) @@ -127,7 +111,7 @@ export default [ // ESM { ...baseConfig, - external: isExternal(external), + external: dependencies.map(createPackageRegex), output: { dir: 'lib-esm', format: 'esm', @@ -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', From d6809060a78ffe83cecb913e83f2a8ef83fb44a6 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Mon, 17 Oct 2022 12:57:06 -0500 Subject: [PATCH 2/3] chore: add changeset --- .changeset/spicy-suits-help.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spicy-suits-help.md diff --git a/.changeset/spicy-suits-help.md b/.changeset/spicy-suits-help.md new file mode 100644 index 00000000000..bf4af39f9e6 --- /dev/null +++ b/.changeset/spicy-suits-help.md @@ -0,0 +1,5 @@ +--- +'@primer/react': patch +--- + +Inline ESM-only dependencies in CommonJS bundle From c7e8ee2e65d645cc38a222b1fd5c2c0ada6e7bf5 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Mon, 17 Oct 2022 13:07:27 -0500 Subject: [PATCH 3/3] chore: address eslint violations --- rollup.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rollup.config.js b/rollup.config.js index 50f38494226..e65259a88ae 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -58,7 +58,7 @@ const dependencies = [ ] function createPackageRegex(name) { - return new RegExp(`^${name}(\/.*)?`) + return new RegExp(`^${name}(/.*)?`) } const baseConfig = {