Skip to content

Commit

Permalink
feat(octicons_react): add support for ESM import
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblack committed Mar 4, 2024
1 parent 6bc78de commit af57e40
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
10 changes: 7 additions & 3 deletions lib/octicons_react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 34 additions & 8 deletions lib/octicons_react/rollup.config.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'
}
}
}
]
24 changes: 12 additions & 12 deletions lib/octicons_react/script/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
"github/no-then": 0,
"eslint-comments/no-use": 0
}
}
},
"packageManager": "[email protected]"
}

0 comments on commit af57e40

Please sign in to comment.