From 524a7b5d5548ab9ea69b6e1487bb1305afe41772 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Wed, 8 Jan 2025 07:43:04 +0900 Subject: [PATCH] chore(rollup): migrate commonjs to esmodule (#2946) * chore(rollup): migrate commonjs to esmodule * chore(package.json): change commonjs to esmodule style in 'patch-d-ts' script --- package.json | 2 +- rollup.config.js => rollup.config.mjs | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) rename rollup.config.js => rollup.config.mjs (85%) diff --git a/package.json b/package.json index 58cdc193a5..ec7d265eaa 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 --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)})\"" diff --git a/rollup.config.js b/rollup.config.mjs similarity index 85% rename from rollup.config.js rename to rollup.config.mjs index a5a8f8f7f8..8499d80d2b 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 = [ +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' }, @@ -90,7 +90,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, '/') @@ -103,5 +103,3 @@ module.exports = function (args) { createESMConfig(`src/${c}.ts`, `dist/esm/${c}.mjs`), ] } - -module.exports.entries = entries