Skip to content

Commit c5786ff

Browse files
authored
feat(octicons_react): add support for ESM import (#1008)
* feat(octicons_react): add support for ESM import * chore: add changeset
1 parent 79b9395 commit c5786ff

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

.changeset/tasty-countries-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/octicons-react': minor
3+
---
4+
5+
Update ESM import to use mjs extension when in parent CommonJS module

lib/octicons_react/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
"homepage": "https://primer.style/octicons",
66
"author": "GitHub, Inc.",
77
"license": "MIT",
8+
"type": "commonjs",
89
"main": "dist/index.umd.js",
9-
"module": "dist/index.esm.js",
10+
"module": "dist/index.esm.mjs",
1011
"exports": {
11-
"types": "./dist/index.d.ts",
12-
"import": "./dist/index.esm.js",
12+
"types": {
13+
"import": "./dist/index.d.mts",
14+
"require": "./dist/index.d.ts"
15+
},
16+
"import": "./dist/index.esm.mjs",
1317
"require": "./dist/index.umd.js"
1418
},
1519
"sideEffects": false,

lib/octicons_react/rollup.config.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import babel from '@rollup/plugin-babel'
22
import commonjs from '@rollup/plugin-commonjs'
3+
import packageJson from './package.json'
34

4-
const formats = ['esm', 'umd'] // 'cjs' ?
5+
const dependencies = [
6+
...Object.keys(packageJson.peerDependencies ?? {}),
7+
...Object.keys(packageJson.dependencies ?? {}),
8+
...Object.keys(packageJson.devDependencies ?? {})
9+
]
510

6-
export default {
11+
function createPackageRegex(name) {
12+
return new RegExp(`^${name}(/.*)?`)
13+
}
14+
15+
const baseConfig = {
716
input: 'src/index.js',
17+
external: dependencies.map(createPackageRegex),
818
plugins: [
919
babel({
1020
babelrc: false,
@@ -20,10 +30,26 @@ export default {
2030
babelHelpers: 'bundled'
2131
}),
2232
commonjs()
23-
],
24-
output: formats.map(format => ({
25-
file: `dist/index.${format}.js`,
26-
format,
27-
name: 'reocticons'
28-
}))
33+
]
2934
}
35+
36+
export default [
37+
{
38+
...baseConfig,
39+
output: {
40+
file: `dist/index.esm.mjs`,
41+
format: 'esm'
42+
}
43+
},
44+
{
45+
...baseConfig,
46+
output: {
47+
file: `dist/index.umd.js`,
48+
format: 'umd',
49+
name: 'reocticons',
50+
globals: {
51+
react: 'React'
52+
}
53+
}
54+
}
55+
]

lib/octicons_react/script/types.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ const destDir = resolve(__dirname, '../dist')
1010
const iconsDest = join(destDir, 'icons.d.ts')
1111
const indexDest = join(destDir, 'index.d.ts')
1212

13-
fse
14-
.copy(iconsSrc, iconsDest)
15-
.then(() => {
16-
return fse
17-
.readFile(indexSrc, 'utf8')
18-
.then(content => content.replace(/.\/__generated__\//g, './'))
19-
.then(content => fse.writeFile(indexDest, content, 'utf8'))
20-
})
21-
.catch(die)
13+
async function main() {
14+
await fse.copy(iconsSrc, iconsDest)
2215

23-
function die(err) {
24-
console.error(err.stack)
25-
process.exitCode = 1
16+
let contents = await fse.readFile(indexSrc, 'utf8')
17+
contents = contents.replace(/.\/__generated__\//g, './')
18+
19+
await fse.writeFile(indexDest, contents, 'utf8')
20+
await fse.writeFile(join(destDir, 'index.d.mts'), contents, 'utf8')
2621
}
22+
23+
main().catch(error => {
24+
console.error(error)
25+
process.exitCode = 1
26+
})

0 commit comments

Comments
 (0)