diff --git a/README.md b/README.md index e25dfcdf..bab76e78 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Then just run `npm run build`, or `pnpm build` / `yarn build` if you're using th - Output (`-o `): Specify output filename. - Format (`-f `): Set output format (default: `'esm'`). - External (`--external `): Specifying extra external dependencies, by default it is the list of `dependencies` and `peerDependencies` from `package.json`. Values are separate by comma. -- Target (`--target `): Set ECMAScript target (default: `'es2016'`). +- Target (`--target `): Set ECMAScript target (default: `'es2015'`). - Runtime (`--runtime `): Set build runtime (default: `'browser'`). - Environment (`--env `): Define environment variables. (default: `NODE_ENV`, separate by comma) - Working Directory (`--cwd `): Set current working directory where containing `package.json`. @@ -308,7 +308,7 @@ await bundle(path.resolve('./src/index.ts'), { sourcemap: false, // Boolean external: [], // string[] format: 'esm', // 'esm' | 'cjs' - target: 'es2016', // ES syntax target + target: 'es2015', // ES syntax target runtime: 'nodejs', // 'browser' | 'nodejs' cwd: process.cwd(), // string }) diff --git a/package.json b/package.json index a5ddebe9..70aa52eb 100644 --- a/package.json +++ b/package.json @@ -48,12 +48,13 @@ "license": "MIT", "dependencies": { "@rollup/plugin-commonjs": "^25.0.4", + "@rollup/plugin-esm-shim": "^0.1.5", "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.1", "@rollup/plugin-replace": "^5.0.2", "@rollup/plugin-wasm": "^6.1.3", "@rollup/pluginutils": "^5.0.4", - "@swc/core": "^1.3.93", + "@swc/core": "^1.3.99", "@swc/helpers": "^0.5.0", "arg": "^5.0.2", "pretty-bytes": "^5.6.0", @@ -61,7 +62,7 @@ "rollup": "^3.28.1", "rollup-plugin-dts": "^6.0.1", "rollup-plugin-swc3": "^0.10.3", - "rollup-swc-preserve-directives": "^0.5.0", + "rollup-preserve-directives": "^1.0.0", "tslib": "^2.5.0" }, "peerDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c786c0e5..d94415cb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ dependencies: '@rollup/plugin-commonjs': specifier: ^25.0.4 version: 25.0.4(rollup@3.28.1) + '@rollup/plugin-esm-shim': + specifier: ^0.1.5 + version: 0.1.5(rollup@3.28.1) '@rollup/plugin-json': specifier: ^6.0.0 version: 6.0.0(rollup@3.28.1) @@ -24,8 +27,8 @@ dependencies: specifier: ^5.0.4 version: 5.0.4(rollup@3.28.1) '@swc/core': - specifier: ^1.3.93 - version: 1.3.93(@swc/helpers@0.5.0) + specifier: ^1.3.99 + version: 1.3.99(@swc/helpers@0.5.0) '@swc/helpers': specifier: ^0.5.0 version: 0.5.0 @@ -46,10 +49,10 @@ dependencies: version: 6.0.1(rollup@3.28.1)(typescript@4.9.5) rollup-plugin-swc3: specifier: ^0.10.3 - version: 0.10.3(@swc/core@1.3.93)(rollup@3.28.1) - rollup-swc-preserve-directives: - specifier: ^0.5.0 - version: 0.5.0(@swc/core@1.3.93)(rollup@3.28.1) + version: 0.10.3(@swc/core@1.3.99)(rollup@3.28.1) + rollup-preserve-directives: + specifier: ^1.0.0 + version: 1.0.0(rollup@3.28.1) tslib: specifier: ^2.5.0 version: 2.5.0 @@ -60,7 +63,7 @@ devDependencies: version: 1.0.0 '@swc/jest': specifier: ^0.2.29 - version: 0.2.29(@swc/core@1.3.93) + version: 0.2.29(@swc/core@1.3.99) '@swc/types': specifier: ^0.1.5 version: 0.1.5 @@ -1144,6 +1147,19 @@ packages: rollup: 3.28.1 dev: false + /@rollup/plugin-esm-shim@0.1.5(rollup@3.28.1): + resolution: {integrity: sha512-xnIjDm/0EbqAw0/rR1UE7eAo9db0ftGPqT8RUCFtkFxtCuspbbmj+wutoyxm32jBytyO3SgkxSG17OR893fV7A==} + engines: {node: '>=14.0.0'} + peerDependencies: + rollup: ^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + dependencies: + magic-string: 0.30.3 + rollup: 3.28.1 + dev: false + /@rollup/plugin-json@6.0.0(rollup@3.28.1): resolution: {integrity: sha512-i/4C5Jrdr1XUarRhVu27EEwjt4GObltD7c+MkCIpO2QIbojw8MUs+CCTqOphQi3Qtg1FLmYt+l+6YeoIf51J7w==} engines: {node: '>=14.0.0'} @@ -1247,88 +1263,80 @@ packages: '@sinonjs/commons': 1.8.3 dev: true - /@swc/core-darwin-arm64@1.3.93: - resolution: {integrity: sha512-gEKgk7FVIgltnIfDO6GntyuQBBlAYg5imHpRgLxB1zSI27ijVVkksc6QwISzFZAhKYaBWIsFSVeL9AYSziAF7A==} + /@swc/core-darwin-arm64@1.3.99: + resolution: {integrity: sha512-Qj7Jct68q3ZKeuJrjPx7k8SxzWN6PqLh+VFxzA+KwLDpQDPzOlKRZwkIMzuFjLhITO4RHgSnXoDk/Syz0ZeN+Q==} engines: {node: '>=10'} cpu: [arm64] os: [darwin] requiresBuild: true optional: true - /@swc/core-darwin-x64@1.3.93: - resolution: {integrity: sha512-ZQPxm/fXdDQtn3yrYSL/gFfA8OfZ5jTi33yFQq6vcg/Y8talpZ+MgdSlYM0FkLrZdMTYYTNFiuBQuuvkA+av+Q==} + /@swc/core-darwin-x64@1.3.99: + resolution: {integrity: sha512-wR7m9QVJjgiBu1PSOHy7s66uJPa45Kf9bZExXUL+JAa9OQxt5y+XVzr+n+F045VXQOwdGWplgPnWjgbUUHEVyw==} engines: {node: '>=10'} cpu: [x64] os: [darwin] requiresBuild: true optional: true - /@swc/core-linux-arm-gnueabihf@1.3.93: - resolution: {integrity: sha512-OYFMMI2yV+aNe3wMgYhODxHdqUB/jrK0SEMHHS44GZpk8MuBXEF+Mcz4qjkY5Q1EH7KVQqXb/gVWwdgTHpjM2A==} - engines: {node: '>=10'} - cpu: [arm] - os: [linux] - requiresBuild: true - optional: true - - /@swc/core-linux-arm64-gnu@1.3.93: - resolution: {integrity: sha512-BT4dT78odKnJMNiq5HdjBsv29CiIdcCcImAPxeFqAeFw1LL6gh9nzI8E96oWc+0lVT5lfhoesCk4Qm7J6bty8w==} + /@swc/core-linux-arm64-gnu@1.3.99: + resolution: {integrity: sha512-gcGv1l5t0DScEONmw5OhdVmEI/o49HCe9Ik38zzH0NtDkc+PDYaCcXU5rvfZP2qJFaAAr8cua8iJcOunOSLmnA==} engines: {node: '>=10'} cpu: [arm64] os: [linux] requiresBuild: true optional: true - /@swc/core-linux-arm64-musl@1.3.93: - resolution: {integrity: sha512-yH5fWEl1bktouC0mhh0Chuxp7HEO4uCtS/ly1Vmf18gs6wZ8DOOkgAEVv2dNKIryy+Na++ljx4Ym7C8tSJTrLw==} + /@swc/core-linux-arm64-musl@1.3.99: + resolution: {integrity: sha512-XL1/eUsTO8BiKsWq9i3iWh7H99iPO61+9HYiWVKhSavknfj4Plbn+XyajDpxsauln5o8t+BRGitymtnAWJM4UQ==} engines: {node: '>=10'} cpu: [arm64] os: [linux] requiresBuild: true optional: true - /@swc/core-linux-x64-gnu@1.3.93: - resolution: {integrity: sha512-OFUdx64qvrGJhXKEyxosHxgoUVgba2ztYh7BnMiU5hP8lbI8G13W40J0SN3CmFQwPP30+3oEbW7LWzhKEaYjlg==} + /@swc/core-linux-x64-gnu@1.3.99: + resolution: {integrity: sha512-fGrXYE6DbTfGNIGQmBefYxSk3rp/1lgbD0nVg4rl4mfFRQPi7CgGhrrqSuqZ/ezXInUIgoCyvYGWFSwjLXt/Qg==} engines: {node: '>=10'} cpu: [x64] os: [linux] requiresBuild: true optional: true - /@swc/core-linux-x64-musl@1.3.93: - resolution: {integrity: sha512-4B8lSRwEq1XYm6xhxHhvHmKAS7pUp1Q7E33NQ2TlmFhfKvCOh86qvThcjAOo57x8DRwmpvEVrqvpXtYagMN6Ig==} + /@swc/core-linux-x64-musl@1.3.99: + resolution: {integrity: sha512-kvgZp/mqf3IJ806gUOL6gN6VU15+DfzM1Zv4Udn8GqgXiUAvbQehrtruid4Snn5pZTLj4PEpSCBbxgxK1jbssA==} engines: {node: '>=10'} cpu: [x64] os: [linux] requiresBuild: true optional: true - /@swc/core-win32-arm64-msvc@1.3.93: - resolution: {integrity: sha512-BHShlxtkven8ZjjvZ5QR6sC5fZCJ9bMujEkiha6W4cBUTY7ce7qGFyHmQd+iPC85d9kD/0cCiX/Xez8u0BhO7w==} + /@swc/core-win32-arm64-msvc@1.3.99: + resolution: {integrity: sha512-yt8RtZ4W/QgFF+JUemOUQAkVW58cCST7mbfKFZ1v16w3pl3NcWd9OrtppFIXpbjU1rrUX2zp2R7HZZzZ2Zk/aQ==} engines: {node: '>=10'} cpu: [arm64] os: [win32] requiresBuild: true optional: true - /@swc/core-win32-ia32-msvc@1.3.93: - resolution: {integrity: sha512-nEwNWnz4JzYAK6asVvb92yeylfxMYih7eMQOnT7ZVlZN5ba9WF29xJ6kcQKs9HRH6MvWhz9+wRgv3FcjlU6HYA==} + /@swc/core-win32-ia32-msvc@1.3.99: + resolution: {integrity: sha512-62p5fWnOJR/rlbmbUIpQEVRconICy5KDScWVuJg1v3GPLBrmacjphyHiJC1mp6dYvvoEWCk/77c/jcQwlXrDXw==} engines: {node: '>=10'} cpu: [ia32] os: [win32] requiresBuild: true optional: true - /@swc/core-win32-x64-msvc@1.3.93: - resolution: {integrity: sha512-jibQ0zUr4kwJaQVwgmH+svS04bYTPnPw/ZkNInzxS+wFAtzINBYcU8s2PMWbDb2NGYiRSEeoSGyAvS9H+24JFA==} + /@swc/core-win32-x64-msvc@1.3.99: + resolution: {integrity: sha512-PdppWhkoS45VGdMBxvClVgF1hVjqamtvYd82Gab1i4IV45OSym2KinoDCKE1b6j3LwBLOn2J9fvChGSgGfDCHQ==} engines: {node: '>=10'} cpu: [x64] os: [win32] requiresBuild: true optional: true - /@swc/core@1.3.93(@swc/helpers@0.5.0): - resolution: {integrity: sha512-690GRr1wUGmGYZHk7fUduX/JUwViMF2o74mnZYIWEcJaCcd9MQfkhsxPBtjeg6tF+h266/Cf3RPYhsFBzzxXcA==} + /@swc/core@1.3.99(@swc/helpers@0.5.0): + resolution: {integrity: sha512-8O996RfuPC4ieb4zbYMfbyCU9k4gSOpyCNnr7qBQ+o7IEmh8JCV6B8wwu+fT/Om/6Lp34KJe1IpJ/24axKS6TQ==} engines: {node: '>=10'} requiresBuild: true peerDependencies: @@ -1341,16 +1349,15 @@ packages: '@swc/helpers': 0.5.0 '@swc/types': 0.1.5 optionalDependencies: - '@swc/core-darwin-arm64': 1.3.93 - '@swc/core-darwin-x64': 1.3.93 - '@swc/core-linux-arm-gnueabihf': 1.3.93 - '@swc/core-linux-arm64-gnu': 1.3.93 - '@swc/core-linux-arm64-musl': 1.3.93 - '@swc/core-linux-x64-gnu': 1.3.93 - '@swc/core-linux-x64-musl': 1.3.93 - '@swc/core-win32-arm64-msvc': 1.3.93 - '@swc/core-win32-ia32-msvc': 1.3.93 - '@swc/core-win32-x64-msvc': 1.3.93 + '@swc/core-darwin-arm64': 1.3.99 + '@swc/core-darwin-x64': 1.3.99 + '@swc/core-linux-arm64-gnu': 1.3.99 + '@swc/core-linux-arm64-musl': 1.3.99 + '@swc/core-linux-x64-gnu': 1.3.99 + '@swc/core-linux-x64-musl': 1.3.99 + '@swc/core-win32-arm64-msvc': 1.3.99 + '@swc/core-win32-ia32-msvc': 1.3.99 + '@swc/core-win32-x64-msvc': 1.3.99 /@swc/counter@0.1.2: resolution: {integrity: sha512-9F4ys4C74eSTEUNndnER3VJ15oru2NumfQxS8geE+f3eB5xvfxpWyqE5XlVnxb/R14uoXi6SLbBwwiDSkv+XEw==} @@ -1360,14 +1367,14 @@ packages: dependencies: tslib: 2.5.0 - /@swc/jest@0.2.29(@swc/core@1.3.93): + /@swc/jest@0.2.29(@swc/core@1.3.99): resolution: {integrity: sha512-8reh5RvHBsSikDC3WGCd5ZTd2BXKkyOdK7QwynrCH58jk2cQFhhHhFBg/jvnWZehUQe/EoOImLENc9/DwbBFow==} engines: {npm: '>= 7.0.0'} peerDependencies: '@swc/core': '*' dependencies: '@jest/create-cache-key-function': 27.5.1 - '@swc/core': 1.3.93(@swc/helpers@0.5.0) + '@swc/core': 1.3.99(@swc/helpers@0.5.0) jsonc-parser: 3.2.0 dev: true @@ -3123,7 +3130,7 @@ packages: '@babel/code-frame': 7.22.13 dev: false - /rollup-plugin-swc3@0.10.3(@swc/core@1.3.93)(rollup@3.28.1): + /rollup-plugin-swc3@0.10.3(@swc/core@1.3.99)(rollup@3.28.1): resolution: {integrity: sha512-GWoMkm3ATumN8EPHBKLrpCufcRNn7SfLyvMKWUfCVLidPuPjlQZfNBeQXP6OEiHBguZzriCssX43EnV3+Y54bA==} engines: {node: '>=12'} peerDependencies: @@ -3132,20 +3139,29 @@ packages: dependencies: '@fastify/deepmerge': 1.3.0 '@rollup/pluginutils': 5.0.5(rollup@3.28.1) - '@swc/core': 1.3.93(@swc/helpers@0.5.0) + '@swc/core': 1.3.99(@swc/helpers@0.5.0) get-tsconfig: 4.7.2 rollup: 3.28.1 - rollup-swc-preserve-directives: 0.5.0(@swc/core@1.3.93)(rollup@3.28.1) + rollup-swc-preserve-directives: 0.5.0(@swc/core@1.3.99)(rollup@3.28.1) + dev: false + + /rollup-preserve-directives@1.0.0(rollup@3.28.1): + resolution: {integrity: sha512-9HVk/jWHQ8gQEvRGemMr6iSzKnflRWnbkYILpQ4Qbs0Xlcd09k4/GNnMlinPmkuBY6Hz3NtVtEf7sC4bNah2cQ==} + peerDependencies: + rollup: ^2.0.0 || ^3.0.0 || ^4.0.0 + dependencies: + '@napi-rs/magic-string': 0.3.4 + rollup: 3.28.1 dev: false - /rollup-swc-preserve-directives@0.5.0(@swc/core@1.3.93)(rollup@3.28.1): + /rollup-swc-preserve-directives@0.5.0(@swc/core@1.3.99)(rollup@3.28.1): resolution: {integrity: sha512-6lnPZn2laSsdYcdCSE28z4Dwg2mCN5loF+/wBjybh25GJmONjHTf3orWa5j1zjEWY3RcGRjJ8K/52ePqtfy6dw==} peerDependencies: '@swc/core': '>=1.3.79' rollup: ^2.0.0 || ^3.0.0 dependencies: '@napi-rs/magic-string': 0.3.4 - '@swc/core': 1.3.93(@swc/helpers@0.5.0) + '@swc/core': 1.3.99(@swc/helpers@0.5.0) rollup: 3.28.1 dev: false diff --git a/src/build-config.ts b/src/build-config.ts index 6d033719..9223501c 100644 --- a/src/build-config.ts +++ b/src/build-config.ts @@ -17,9 +17,10 @@ import commonjs from '@rollup/plugin-commonjs' import json from '@rollup/plugin-json' import { nodeResolve } from '@rollup/plugin-node-resolve' import replace from '@rollup/plugin-replace' +import esmShim from '@rollup/plugin-esm-shim' import { sizeCollector } from './plugins/size-plugin' import { inlineCss } from './plugins/inline-css' -import swcPreserveDirectivePlugin from 'rollup-swc-preserve-directives' +import preserveDirectives from 'rollup-preserve-directives' import { getTypings, getExportPaths, @@ -170,7 +171,7 @@ async function buildInputConfig( : [ ...commonPlugins, inlineCss({ exclude: /node_modules/ }), - swcPreserveDirectivePlugin(), + preserveDirectives(), replace({ values: getBuildEnv(options.env || []), preventAssignment: true, @@ -190,6 +191,7 @@ async function buildInputConfig( tsconfig: tsConfigPath, ...swcOptions, }), + esmShim() ] ).filter(isNotNull) diff --git a/src/cli.ts b/src/cli.ts index e776d0a2..c6495576 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -20,7 +20,7 @@ Options: -h, --help output usage information --external specify an external dependency, separate by comma --no-external do not bundle external dependencies - --target js features target: swc target es versions. default: es2016 + --target js features target: swc target es versions. default: es2015 --runtime build runtime (nodejs, browser). default: browser --env inlined process env variables, separate by comma. default: NODE_ENV --cwd specify current working directory diff --git a/test/integration.test.ts b/test/integration.test.ts index 73ddec33..938fc42b 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -377,6 +377,29 @@ const testCases: { expect(await fs.readFile(distFiles[0], 'utf-8')).toContain( '#!/usr/bin/env node', ) + } + }, + { + name: 'esm-shims', + args: [], + async expected(dir) { + const shimsCode = [ + 'const __filename = cjsUrl.fileURLToPath(import.meta.url)', + 'const __dirname = cjsPath.dirname(__filename)', + 'const require = cjsModule.createRequire(import.meta.url)', + ] + const esmOutput = await fs.readFile(join(dir, './dist/index.mjs'), 'utf-8') + const cjsOutput = await fs.readFile(join(dir, './dist/index.cjs'), 'utf-8') + expect( + shimsCode.every((code) => esmOutput.includes(code)), + ).toBe(true) + expect( + shimsCode.map((code) => cjsOutput.includes(code)), + ).toEqual([false, false, false]) + // for import.meta.url, should use pathToFileURL + URL polyfill + expect(cjsOutput).toContain('pathToFileURL') + expect(cjsOutput).toContain('new URL') + expect(cjsOutput).not.toContain('import.meta.url') }, }, ] diff --git a/test/integration/esm-shims/package.json b/test/integration/esm-shims/package.json new file mode 100644 index 00000000..621be307 --- /dev/null +++ b/test/integration/esm-shims/package.json @@ -0,0 +1,8 @@ +{ + "exports": { + ".": { + "import": "./dist/index.mjs", + "require": "./dist/index.cjs" + } + } +} diff --git a/test/integration/esm-shims/src/index.js b/test/integration/esm-shims/src/index.js new file mode 100644 index 00000000..c67f885b --- /dev/null +++ b/test/integration/esm-shims/src/index.js @@ -0,0 +1,3 @@ +console.log(__dirname) +console.log(__filename) +console.log(import.meta.url) diff --git a/test/typing.test.ts b/test/typing.test.ts index 1552a369..110f3e4e 100644 --- a/test/typing.test.ts +++ b/test/typing.test.ts @@ -10,7 +10,7 @@ describe('types', () => { sourcemap: false, external: [], format: 'esm', - target: 'es2016', + target: 'es2015', runtime: 'nodejs', } diff --git a/test/unit/es-advance/__snapshot__/es-advance.min.js.snapshot b/test/unit/es-advance/__snapshot__/es-advance.min.js.snapshot index a1c87972..324d5d09 100644 --- a/test/unit/es-advance/__snapshot__/es-advance.min.js.snapshot +++ b/test/unit/es-advance/__snapshot__/es-advance.min.js.snapshot @@ -1 +1 @@ -var l;const o={bar:"hello"};console.log("bar",null==o?void 0:o.bar);const a=null!=(l=o.bar)?l:"default";console.log("name",a); +var l;const o={bar:"hello"};console.log("bar",null==o?void 0:o.bar),console.log("name",null!=(l=o.bar)?l:"default"); diff --git a/test/unit/module/__snapshot__/module.min.js.snapshot b/test/unit/module/__snapshot__/module.min.js.snapshot index 1293a48b..42f3153b 100644 --- a/test/unit/module/__snapshot__/module.min.js.snapshot +++ b/test/unit/module/__snapshot__/module.min.js.snapshot @@ -1 +1 @@ -class s{f(){return 1}}const e=new class extends s{get x(){return super.f()}constructor(){super()}};console.log("main",e.x); +class e{f(){return 1}}console.log("main",new class extends e{get x(){return super.f()}constructor(){super()}}().x);