From 68407b039e2b9aded656a0a6fc2293ed0e9ca422 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Mon, 6 Jan 2025 19:50:18 +0900 Subject: [PATCH 1/6] chore(rollup): migrate commonjs to esmodule --- rollup.config.js => rollup.config.mjs | 28 +++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) rename rollup.config.js => rollup.config.mjs (81%) diff --git a/rollup.config.js b/rollup.config.mjs similarity index 81% rename from rollup.config.js rename to rollup.config.mjs index acbb0946bf..8f297f9175 100644 --- a/rollup.config.js +++ b/rollup.config.mjs @@ -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' }, { find: /.*\/react\/shallow\.ts$/, replacement: 'zustand/react/shallow' }, { find: /.*\/vanilla\.ts$/, replacement: 'zustand/vanilla' }, @@ -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') @@ -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((e) => !e.find.test(input)), + }), resolve({ extensions }), replace({ 'import.meta.env?.MODE': 'process.env.NODE_ENV', @@ -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, '/') @@ -104,4 +108,4 @@ module.exports = function (args) { ] } -module.exports.entries = [] +export const entries = [] From e7476d6d783357afd5af6a175b826e3a0008cf9f Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Mon, 6 Jan 2025 19:51:40 +0900 Subject: [PATCH 2/6] chore(package.json): change 'rollup.config.js' to 'rollup.config.mjs' in 'patch-d-ts' script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d5bec31087..ab79ce1aa2 100644 --- a/package.json +++ b/package.json @@ -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 -e \"var {entries}=require('./rollup.config.mjs');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)})\"", "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)})\"" From 29d959344f272fb4989b646419861a425fb63fce Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Mon, 6 Jan 2025 20:58:03 +0900 Subject: [PATCH 3/6] chore(package.json): change commonjs to esmodule style in 'patch-d-ts' script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ab79ce1aa2..bd8453e542 100644 --- a/package.json +++ b/package.json @@ -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.mjs');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)})\"" From 8ecb78ad7dce1e83ad10aa7ffbeba353f56cb028 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Mon, 6 Jan 2025 21:07:06 +0900 Subject: [PATCH 4/6] chore(rollup): clarify the callback argument --- rollup.config.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rollup.config.mjs b/rollup.config.mjs index 8f297f9175..fe1a741f4e 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -81,7 +81,7 @@ function createCommonJSConfig(input, output) { external, plugins: [ alias({ - entries: _entries.filter((e) => !e.find.test(input)), + entries: _entries.filter((entry) => !entry.find.test(input)), }), resolve({ extensions }), replace({ From a80bdd9fe4fad1f92ba1ef387f93b575c8667437 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Mon, 6 Jan 2025 21:47:56 +0900 Subject: [PATCH 5/6] chore(rollup): change constant '_entries' to 'aliasEntries' --- rollup.config.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rollup.config.mjs b/rollup.config.mjs index fe1a741f4e..819a45919a 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -8,7 +8,7 @@ import esbuild from 'rollup-plugin-esbuild' const extensions = ['.js', '.ts', '.tsx'] const { root } = path.parse(process.cwd()) -const _entries = [ +const aliasEntries = [ { find: /.*\/vanilla\/shallow\.ts$/, replacement: 'zustand/vanilla/shallow' }, { find: /.*\/react\/shallow\.ts$/, replacement: 'zustand/react/shallow' }, { find: /.*\/vanilla\.ts$/, replacement: 'zustand/vanilla' }, @@ -51,7 +51,7 @@ function createESMConfig(input, output) { external, plugins: [ alias({ - entries: _entries.filter((entry) => !entry.find.test(input)), + entries: aliasEntries.filter((entry) => !entry.find.test(input)), }), resolve({ extensions }), replace({ @@ -81,7 +81,7 @@ function createCommonJSConfig(input, output) { external, plugins: [ alias({ - entries: _entries.filter((entry) => !entry.find.test(input)), + entries: aliasEntries.filter((entry) => !entry.find.test(input)), }), resolve({ extensions }), replace({ From 89173fb27759e036a4dba88b062582ebf04e18ce Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Tue, 7 Jan 2025 13:21:05 +0900 Subject: [PATCH 6/6] chore(rollup): change constant 'aliasEntries' to 'entries' --- rollup.config.mjs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/rollup.config.mjs b/rollup.config.mjs index 819a45919a..6b7296eec8 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -8,7 +8,7 @@ import esbuild from 'rollup-plugin-esbuild' const extensions = ['.js', '.ts', '.tsx'] const { root } = path.parse(process.cwd()) -const aliasEntries = [ +export const entries = [ { find: /.*\/vanilla\/shallow\.ts$/, replacement: 'zustand/vanilla/shallow' }, { find: /.*\/react\/shallow\.ts$/, replacement: 'zustand/react/shallow' }, { find: /.*\/vanilla\.ts$/, replacement: 'zustand/vanilla' }, @@ -51,7 +51,7 @@ function createESMConfig(input, output) { external, plugins: [ alias({ - entries: aliasEntries.filter((entry) => !entry.find.test(input)), + entries: entries.filter((entry) => !entry.find.test(input)), }), resolve({ extensions }), replace({ @@ -81,7 +81,7 @@ function createCommonJSConfig(input, output) { external, plugins: [ alias({ - entries: aliasEntries.filter((entry) => !entry.find.test(input)), + entries: entries.filter((entry) => !entry.find.test(input)), }), resolve({ extensions }), replace({ @@ -107,5 +107,3 @@ export default function (args) { createESMConfig(`src/${c}.ts`, `dist/esm/${c}.mjs`), ] } - -export const entries = []