diff --git a/.changeset/calm-bikes-punch.md b/.changeset/calm-bikes-punch.md new file mode 100644 index 00000000..1392aac8 --- /dev/null +++ b/.changeset/calm-bikes-punch.md @@ -0,0 +1,5 @@ +--- +'@ice/pkg': patch +--- + +fix: support @swc/helpers with correct import between cjs and mjs diff --git a/packages/pkg/src/config/schema.ts b/packages/pkg/src/config/schema.ts index d28bc17b..e3b0f655 100644 --- a/packages/pkg/src/config/schema.ts +++ b/packages/pkg/src/config/schema.ts @@ -10,7 +10,13 @@ export const bundleSchema = z.object({ outputDir: z.string().optional(), modes: z.enum(['production', 'development']).array().optional(), formats: z.string().array().optional(), - externals: z.union([z.boolean(), z.record(z.string())]).optional(), + externals: z + .union([ + z.boolean(), + z.record(z.string()), + z.array(z.union([z.string(), z.instanceof(RegExp), z.record(z.string())])), + ]) + .optional(), minify: z .union([ z.boolean(), diff --git a/packages/pkg/src/engine/rollup/options.ts b/packages/pkg/src/engine/rollup/options.ts index 86b2c299..36cb2444 100644 --- a/packages/pkg/src/engine/rollup/options.ts +++ b/packages/pkg/src/engine/rollup/options.ts @@ -47,16 +47,16 @@ export function getRollupOptions(context: Context, taskRunnerContext: TaskRunner ), ); } - plugins.push( - swcPlugin( - taskConfig.jsxRuntime, - rootDir, - taskConfig.swcCompileOptions, - taskConfig.type === 'bundle' && taskConfig.compileDependencies, - ), + + const swcPluginInstance = swcPlugin( + taskConfig.jsxRuntime, + rootDir, + taskConfig.swcCompileOptions, + taskConfig.type === 'bundle' && taskConfig.compileDependencies, ); if (taskConfig.type === 'transform') { + plugins.push(swcPluginInstance); plugins.push(transformAliasPlugin(rootDir, taskConfig.alias)); } else if (taskConfig.type === 'bundle') { const [external, globals] = getExternalsAndGlobals(taskConfig, pkg as PkgJson); @@ -86,22 +86,17 @@ export function getRollupOptions(context: Context, taskRunnerContext: TaskRunner : taskConfig.alias[key]; }); plugins.push( - replace({ - values: { - ...getDefaultDefineValues(taskRunnerContext.mode), - // User define can override above. - ...taskConfig.define, - }, - preventAssignment: true, + commonjs({ + // To convert commonjs to import, make it compatible with rollup to bundle + extensions: [ + '.js', // plugin-commonjs default extensions + '.jsx', + '.ts', + '.tsx', + ...(taskConfig.extensions || []), + ], + transformMixedEsModules: true, }), - styles( - (taskConfig.modifyStylesOptions ?? [(options) => options]).reduce( - (prevStylesOptions, modifyStylesOptions) => modifyStylesOptions(prevStylesOptions), - defaultStylesOptions, - ), - ), - image(), - json(), nodeResolve({ // To locates modules using the node resolution algorithm. extensions: [ @@ -119,17 +114,23 @@ export function getRollupOptions(context: Context, taskRunnerContext: TaskRunner ], browser: taskConfig.browser, }), - commonjs({ - // To convert commonjs to import, make it compatible with rollup to bundle - extensions: [ - '.js', // plugin-commonjs default extensions - '.jsx', - '.ts', - '.tsx', - ...(taskConfig.extensions || []), - ], - transformMixedEsModules: true, + replace({ + values: { + ...getDefaultDefineValues(taskRunnerContext.mode), + // User define can override above. + ...taskConfig.define, + }, + preventAssignment: true, }), + styles( + (taskConfig.modifyStylesOptions ?? [(options) => options]).reduce( + (prevStylesOptions, modifyStylesOptions) => modifyStylesOptions(prevStylesOptions), + defaultStylesOptions, + ), + ), + image(), + json(), + swcPluginInstance, bundleAliasPlugin({ entries: alias, }), diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5cd144f8..46425403 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -496,6 +496,15 @@ importers: specifier: workspace:* version: link:../../../packages/pkg + tests/integration/mix-cjs-mjs: + dependencies: + '@ice/pkg': + specifier: workspace:* + version: link:../../../packages/pkg + '@swc/helpers': + specifier: ^0.5.15 + version: 0.5.17 + tests/integration/mock-entry-package: {} tests/integration/plugins: diff --git a/tests/integration/mix-cjs-mjs/__snapshots__/index.test.ts.snap b/tests/integration/mix-cjs-mjs/__snapshots__/index.test.ts.snap new file mode 100644 index 00000000..c9dd0489 --- /dev/null +++ b/tests/integration/mix-cjs-mjs/__snapshots__/index.test.ts.snap @@ -0,0 +1,145 @@ +// Vitest Snapshot v1 + +exports[`Run config swc-helpers > cjs structure 1`] = ` +{ + "files": [ + { + "name": "cjs.js", + }, + { + "name": "entry-mts.d.ts", + }, + { + "name": "entry-mts.js", + }, + ], + "name": "cjs", +} +`; + +exports[`Run config swc-helpers > dist structure 1`] = ` +{ + "files": [ + { + "name": "entry-mts.cjs.es5.production.js", + }, + { + "name": "entry-mts.esm.es5.production.js", + }, + ], + "name": "dist", +} +`; + +exports[`Run config swc-helpers > es2017 structure 1`] = `null`; + +exports[`Run config swc-helpers > esm structure 1`] = ` +{ + "files": [ + { + "name": "cjs.js", + }, + { + "name": "entry-mts.d.ts", + }, + { + "name": "entry-mts.js", + }, + ], + "name": "esm", +} +`; + +exports[`Run config swc-helpers > file content cjs/cjs.js 1`] = ` +"\\"use strict\\"; +Object.defineProperty(exports, \\"__esModule\\", { + value: true +}); +var _object_spread = require(\\"@swc/helpers/_/_object_spread\\"); +module.exports.add = function(a, b) { + return a + b; +}; +module.exports.extend = function(a, b) { + return _object_spread._({}, a, b); +}; +" +`; + +exports[`Run config swc-helpers > file content cjs/entry-mts.d.ts 1`] = ` +"import { join, extend } from './cjs'; +export { join, extend }; +export declare const foo: any; +" +`; + +exports[`Run config swc-helpers > file content cjs/entry-mts.js 1`] = ` +"\\"use strict\\"; +Object.defineProperty(exports, \\"__esModule\\", { + value: true +}); +function _export(target, all) { + for(var name in all)Object.defineProperty(target, name, { + enumerable: true, + get: all[name] + }); +} +_export(exports, { + extend: function() { + return _cjs.extend; + }, + foo: function() { + return foo; + }, + join: function() { + return _cjs.join; + } +}); +var _object_spread = require(\\"@swc/helpers/_/_object_spread\\"); +var _cjs = require(\\"./cjs\\"); +var foo = _object_spread._({}, (0, _cjs.extend)({ + a: 1 +}, { + b: 2 +})); +" +`; + +exports[`Run config swc-helpers > file content dist/entry-mts.cjs.es5.production.js 1`] = ` +"\\"use strict\\";var e,t=require(\\"@swc/helpers/_/_object_spread\\"),r={},n=(e||(e=1,r.add=function(e,t){return e+t},r.extend=function(e,r){return t._({},e,r)}),r),o=t._({},n.extend({a:1},{b:2}));exports.extend=n.extend,exports.foo=o,exports.join=n.join; +" +`; + +exports[`Run config swc-helpers > file content dist/entry-mts.esm.es5.production.js 1`] = ` +"import{_ as e}from\\"@swc/helpers/_/_object_spread\\";var n,t={},o=(n||(n=1,t.add=function(e,n){return e+n},t.extend=function(n,t){return e({},n,t)}),t),r=e({},o.extend({a:1},{b:2})),d=o.extend,i=o.join;export{d as extend,r as foo,i as join}; +" +`; + +exports[`Run config swc-helpers > file content esm/cjs.js 1`] = ` +"import { _ as _object_spread } from \\"@swc/helpers/_/_object_spread\\"; +module.exports.add = function(a, b) { + return a + b; +}; +module.exports.extend = function(a, b) { + return _object_spread({}, a, b); +}; +" +`; + +exports[`Run config swc-helpers > file content esm/entry-mts.d.ts 1`] = ` +"import { join, extend } from './cjs'; +export { join, extend }; +export declare const foo: any; +" +`; + +exports[`Run config swc-helpers > file content esm/entry-mts.js 1`] = ` +"import { _ as _object_spread } from \\"@swc/helpers/_/_object_spread\\"; +import { join, extend } from './cjs'; +export { join, extend }; +export var foo = _object_spread({}, extend({ + a: 1 +}, { + b: 2 +})); +" +`; diff --git a/tests/integration/mix-cjs-mjs/build.config.swc-helpers.mts b/tests/integration/mix-cjs-mjs/build.config.swc-helpers.mts new file mode 100644 index 00000000..b85d1427 --- /dev/null +++ b/tests/integration/mix-cjs-mjs/build.config.swc-helpers.mts @@ -0,0 +1,12 @@ +import { defineConfig } from '@ice/pkg' + +export default defineConfig({ + entry: './src/entry-mts.ts', + transform: { + formats: ['cjs', 'esm'] + }, + bundle: { + formats: ['cjs', 'esm'], + externals: [/@swc\/helpers/] + }, +}) diff --git a/tests/integration/mix-cjs-mjs/index.test.ts b/tests/integration/mix-cjs-mjs/index.test.ts new file mode 100644 index 00000000..c1e32718 --- /dev/null +++ b/tests/integration/mix-cjs-mjs/index.test.ts @@ -0,0 +1,9 @@ +import { runProjectTest } from '../../helpers/run'; + +runProjectTest(import.meta.url, [ + { + name: 'swc-helpers', + config: 'build.config.swc-helpers.mts', + snapshot: 'full', + }, +]); diff --git a/tests/integration/mix-cjs-mjs/package.json b/tests/integration/mix-cjs-mjs/package.json new file mode 100644 index 00000000..aec01631 --- /dev/null +++ b/tests/integration/mix-cjs-mjs/package.json @@ -0,0 +1,9 @@ +{ + "name": "@ice/pkg-tests-mix-cjs-mjs", + "version": "0.0.0", + "private": true, + "dependencies": { + "@ice/pkg": "workspace:*", + "@swc/helpers": "^0.5.15" + } +} diff --git a/tests/integration/mix-cjs-mjs/src/cjs.js b/tests/integration/mix-cjs-mjs/src/cjs.js new file mode 100644 index 00000000..cb859f8f --- /dev/null +++ b/tests/integration/mix-cjs-mjs/src/cjs.js @@ -0,0 +1,7 @@ +module.exports.add = function (a, b) { + return a + b; +}; + +module.exports.extend = function (a, b) { + return { ...a, ...b }; +}; diff --git a/tests/integration/mix-cjs-mjs/src/entry-mts.ts b/tests/integration/mix-cjs-mjs/src/entry-mts.ts new file mode 100644 index 00000000..181c7197 --- /dev/null +++ b/tests/integration/mix-cjs-mjs/src/entry-mts.ts @@ -0,0 +1,7 @@ +import { join, extend } from './cjs'; + +export { join, extend }; + +export const foo = { + ...extend({ a: 1 }, { b: 2 }), +};