Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rollup): migrate commonjs to esmodule #2939

Closed
wants to merge 9 commits into from
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"test:types": "tsc --noEmit",
"test:lint": "eslint .",
"test:spec": "vitest run",
"patch-d-ts": "node -e \"var {entries}=require('./rollup.config.js');require('shelljs').find('dist/**/*.d.ts').forEach(f=>{entries.forEach(({find,replacement})=>require('shelljs').sed('-i',new RegExp(' from \\''+find.source.slice(0,-1)+'\\';$'),' from \\''+replacement+'\\';',f));require('shelljs').sed('-i',/ from '(\\.[^']+)\\.ts';$/,' from \\'\\$1\\';',f)})\"",
"patch-d-ts": "node --input-type=module -e \"import { entries } from './rollup.config.mjs'; import shelljs from 'shelljs'; const { find, sed } = shelljs; find('dist/**/*.d.ts').forEach(f => { entries.forEach(({ find, replacement }) => sed('-i', new RegExp(' from \\'' + find.source.slice(0, -1) + '\\';$'), ' from \\'' + replacement + '\\';', f)); sed('-i', / from '(\\.[^']+)\\.ts';$/, ' from \\'\\$1\\';', f); });\"",
"copy": "shx cp -r dist/src/* dist/esm && shx cp -r dist/src/* dist && shx rm -rf dist/src && shx rm -rf dist/{src,tests} && shx cp package.json readme.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined;\"",
"patch-old-ts": "shx touch dist/ts_version_4.5_and_above_is_required.d.ts",
"patch-esm-ts": "node -e \"require('shelljs').find('dist/esm/**/*.d.ts').forEach(f=>{var f2=f.replace(/\\.ts$/,'.mts');require('fs').renameSync(f,f2);require('shelljs').sed('-i',/ from '(\\.[^']+)';$/,' from \\'\\$1.mjs\\';',f2);require('shelljs').sed('-i',/^declare module '(\\.[^']+)'/,'declare module \\'\\$1.mjs\\'',f2)})\""
Expand Down
28 changes: 16 additions & 12 deletions rollup.config.js → rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* eslint-disable no-undef */
const path = require('path')
const alias = require('@rollup/plugin-alias')
const resolve = require('@rollup/plugin-node-resolve')
const replace = require('@rollup/plugin-replace')
const typescript = require('@rollup/plugin-typescript')
const { default: esbuild } = require('rollup-plugin-esbuild')
/*global process*/
import path from 'path'
import alias from '@rollup/plugin-alias'
import resolve from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import typescript from '@rollup/plugin-typescript'
import esbuild from 'rollup-plugin-esbuild'

const extensions = ['.js', '.ts', '.tsx']
const { root } = path.parse(process.cwd())
const entries = [
const _entries = [
{ find: /.*\/vanilla\/shallow\.ts$/, replacement: 'zustand/vanilla/shallow' },
sukvvon marked this conversation as resolved.
Show resolved Hide resolved
{ find: /.*\/react\/shallow\.ts$/, replacement: 'zustand/react/shallow' },
{ find: /.*\/vanilla\.ts$/, replacement: 'zustand/vanilla' },
Expand Down Expand Up @@ -50,7 +50,9 @@ function createESMConfig(input, output) {
output: { file: output, format: 'esm' },
external,
plugins: [
alias({ entries: entries.filter((e) => !e.find.test(input)) }),
alias({
entries: _entries.filter((entry) => !entry.find.test(input)),
}),
resolve({ extensions }),
replace({
...(output.endsWith('.js')
Expand Down Expand Up @@ -78,7 +80,9 @@ function createCommonJSConfig(input, output) {
output: { file: output, format: 'cjs' },
external,
plugins: [
alias({ entries: entries.filter((e) => !e.find.test(input)) }),
alias({
entries: _entries.filter((entry) => !entry.find.test(input)),
}),
resolve({ extensions }),
replace({
'import.meta.env?.MODE': 'process.env.NODE_ENV',
Expand All @@ -90,7 +94,7 @@ function createCommonJSConfig(input, output) {
}
}

module.exports = function (args) {
export default function (args) {
let c = Object.keys(args).find((key) => key.startsWith('config-'))
if (c) {
c = c.slice('config-'.length).replace(/_/g, '/')
Expand All @@ -104,4 +108,4 @@ module.exports = function (args) {
]
}

module.exports.entries = []
export const entries = []
Loading