From af57e404196a2814208fb8fd1fb97317a4462b53 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Mon, 4 Mar 2024 14:00:42 -0600 Subject: [PATCH 1/2] feat(octicons_react): add support for ESM import --- lib/octicons_react/package.json | 10 ++++--- lib/octicons_react/rollup.config.js | 42 +++++++++++++++++++++++------ lib/octicons_react/script/types.js | 24 ++++++++--------- package.json | 3 ++- 4 files changed, 55 insertions(+), 24 deletions(-) diff --git a/lib/octicons_react/package.json b/lib/octicons_react/package.json index a4a4218e0..9e42ce4ce 100644 --- a/lib/octicons_react/package.json +++ b/lib/octicons_react/package.json @@ -5,11 +5,15 @@ "homepage": "https://primer.style/octicons", "author": "GitHub, Inc.", "license": "MIT", + "type": "commonjs", "main": "dist/index.umd.js", - "module": "dist/index.esm.js", + "module": "dist/index.esm.mjs", "exports": { - "types": "./dist/index.d.ts", - "import": "./dist/index.esm.js", + "types": { + "import": "./dist/index.d.mts", + "require": "./dist/index.d.ts" + }, + "import": "./dist/index.esm.mjs", "require": "./dist/index.umd.js" }, "sideEffects": false, diff --git a/lib/octicons_react/rollup.config.js b/lib/octicons_react/rollup.config.js index 1b04c208a..23cfa253a 100644 --- a/lib/octicons_react/rollup.config.js +++ b/lib/octicons_react/rollup.config.js @@ -1,10 +1,20 @@ import babel from '@rollup/plugin-babel' import commonjs from '@rollup/plugin-commonjs' +import packageJson from './package.json' -const formats = ['esm', 'umd'] // 'cjs' ? +const dependencies = [ + ...Object.keys(packageJson.peerDependencies ?? {}), + ...Object.keys(packageJson.dependencies ?? {}), + ...Object.keys(packageJson.devDependencies ?? {}) +] -export default { +function createPackageRegex(name) { + return new RegExp(`^${name}(/.*)?`) +} + +const baseConfig = { input: 'src/index.js', + external: dependencies.map(createPackageRegex), plugins: [ babel({ babelrc: false, @@ -20,10 +30,26 @@ export default { babelHelpers: 'bundled' }), commonjs() - ], - output: formats.map(format => ({ - file: `dist/index.${format}.js`, - format, - name: 'reocticons' - })) + ] } + +export default [ + { + ...baseConfig, + output: { + file: `dist/index.esm.mjs`, + format: 'esm' + } + }, + { + ...baseConfig, + output: { + file: `dist/index.umd.js`, + format: 'umd', + name: 'reocticons', + globals: { + react: 'React' + } + } + } +] diff --git a/lib/octicons_react/script/types.js b/lib/octicons_react/script/types.js index 99860a8ad..43cecec2a 100755 --- a/lib/octicons_react/script/types.js +++ b/lib/octicons_react/script/types.js @@ -10,17 +10,17 @@ const destDir = resolve(__dirname, '../dist') const iconsDest = join(destDir, 'icons.d.ts') const indexDest = join(destDir, 'index.d.ts') -fse - .copy(iconsSrc, iconsDest) - .then(() => { - return fse - .readFile(indexSrc, 'utf8') - .then(content => content.replace(/.\/__generated__\//g, './')) - .then(content => fse.writeFile(indexDest, content, 'utf8')) - }) - .catch(die) +async function main() { + await fse.copy(iconsSrc, iconsDest) -function die(err) { - console.error(err.stack) - process.exitCode = 1 + let contents = await fse.readFile(indexSrc, 'utf8') + contents = contents.replace(/.\/__generated__\//g, './') + + await fse.writeFile(indexDest, contents, 'utf8') + await fse.writeFile(join(destDir, 'index.d.mts'), contents, 'utf8') } + +main().catch(error => { + console.error(error) + process.exitCode = 1 +}) diff --git a/package.json b/package.json index 823f5655a..751d625e0 100644 --- a/package.json +++ b/package.json @@ -49,5 +49,6 @@ "github/no-then": 0, "eslint-comments/no-use": 0 } - } + }, + "packageManager": "yarn@1.22.1" } From 47c942060d8c0ea08c0fd6987b84813608322906 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Mon, 4 Mar 2024 14:01:26 -0600 Subject: [PATCH 2/2] chore: add changeset --- .changeset/tasty-countries-grow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tasty-countries-grow.md diff --git a/.changeset/tasty-countries-grow.md b/.changeset/tasty-countries-grow.md new file mode 100644 index 000000000..c426c4358 --- /dev/null +++ b/.changeset/tasty-countries-grow.md @@ -0,0 +1,5 @@ +--- +'@primer/octicons-react': minor +--- + +Update ESM import to use mjs extension when in parent CommonJS module