diff --git a/.changeset/eight-foxes-grab.md b/.changeset/eight-foxes-grab.md new file mode 100644 index 00000000..802a197e --- /dev/null +++ b/.changeset/eight-foxes-grab.md @@ -0,0 +1,5 @@ +--- +'@ice/pkg': minor +--- + +feat: support inline helpers diff --git a/packages/pkg/src/config/schema.ts b/packages/pkg/src/config/schema.ts index 371c4946..3b252e5e 100644 --- a/packages/pkg/src/config/schema.ts +++ b/packages/pkg/src/config/schema.ts @@ -61,6 +61,7 @@ export const userConfigSchema = z.object({ generateTypeForJs: z.boolean().optional(), jsxRuntime: z.enum(['classic', 'automatic']).optional(), plugins: z.any().array().optional(), + helpers: z.enum(['external', 'inline']).optional(), transform: transformSchema.optional(), bundle: bundleSchema.optional(), diff --git a/packages/pkg/src/core/init.ts b/packages/pkg/src/core/init.ts index 00fff7e3..4e3886da 100644 --- a/packages/pkg/src/core/init.ts +++ b/packages/pkg/src/core/init.ts @@ -66,6 +66,7 @@ function initSharedTask(buildTask: BuildTask, options: InitTaskOptions) { config.sourcemap ??= pkg?.sourceMaps ?? userConfig.sourceMaps ?? command === 'start'; config.jsxRuntime ??= pkg?.jsxRuntime ?? userConfig.jsxRuntime; + config.helpers ??= pkg?.helpers ?? userConfig.helpers; } /** diff --git a/packages/pkg/src/engine/shared/swcConfig.ts b/packages/pkg/src/engine/shared/swcConfig.ts index b9335a59..4de3b175 100644 --- a/packages/pkg/src/engine/shared/swcConfig.ts +++ b/packages/pkg/src/engine/shared/swcConfig.ts @@ -30,7 +30,7 @@ export const getDefaultBundleSwcConfig = (bundleTaskConfig: BundleTaskConfig): C const browserTargets = BROWSER_TARGETS_MAP[formatTarget] ?? BROWSER_TARGETS_MAP.es2017; return { jsc: { - externalHelpers: true, + externalHelpers: bundleTaskConfig.helpers !== 'inline', }, minify: false, // Always generate map in bundle mode, @@ -68,7 +68,7 @@ export const getDefaultTransformSwcConfig = (transformTaskConfig: TransformTaskC }, // Helpers function will not be inlined into the output files for sake of optimizing. // Get more info https://github.com/ice-lab/ice-next/issues/95 - externalHelpers: true, + externalHelpers: transformTaskConfig.helpers !== 'inline', }, minify: false, module, diff --git a/packages/pkg/src/types.ts b/packages/pkg/src/types.ts index beafac6f..b95de94d 100644 --- a/packages/pkg/src/types.ts +++ b/packages/pkg/src/types.ts @@ -176,7 +176,7 @@ export interface PkgUserConfig BundleUserConfig, 'externals' | 'name' | 'compileDependencies' | 'polyfill' | 'engine' | 'minify' | 'codeSplitting' >, - Pick { + Pick { /** * Unique id to indicate a package */ @@ -303,6 +303,14 @@ export interface UserConfig { * @default false */ server?: boolean | ServerUserConfig; + + /** + * Configure how SWC helper functions are handled globally. + * - 'external': Import from @swc/helpers package (default, smaller output size) + * - 'inline': Inline helpers into each file (no external dependency needed) + * @default 'external' + */ + helpers?: 'external' | 'inline'; } export type PluginUserConfig = string | [string, Json?] | Plugin; @@ -384,6 +392,14 @@ interface _TaskConfig { */ order?: TaskOrder; + /** + * Configure how SWC helper functions are handled for this task. + * Resolved from userConfig.helpers or pkg.helpers. + * - 'external': Import from @swc/helpers package (default) + * - 'inline': Inline helpers into each file + */ + helpers?: 'external' | 'inline'; + pkg?: PkgResolvedConfig; } diff --git a/packages/pkg/tests/core/init.test.ts b/packages/pkg/tests/core/init.test.ts index d23db51e..562eff79 100644 --- a/packages/pkg/tests/core/init.test.ts +++ b/packages/pkg/tests/core/init.test.ts @@ -233,6 +233,61 @@ describe('initTask', () => { }); }); + describe('helpers', () => { + it('without helpers defaults to external', () => { + const task = initTask(tt('esm', 'esm:es5', {}), c({})); + expect(task.config.helpers).toBeUndefined(); + }); + + it('userConfig sets global helpers', () => { + const task = initTask( + tt('esm', 'esm:es5', {}), + c({ + helpers: 'inline', + }), + ); + expect(task.config.helpers).toEqual('inline'); + }); + + it('pkg helpers overrides userConfig', () => { + const task = initTask( + tt('esm', 'esm:es5', { + pkg: { + id: 'test-pkg', + module: 'esm', + target: 'es5', + pluginInfos: [], + helpers: 'inline', + }, + }), + c({ + helpers: 'external', + }), + ); + expect(task.config.helpers).toEqual('inline'); + }); + + it('taskConfig helpers has highest priority over pkg and userConfig', () => { + const task = initTask( + tt('esm', 'esm:es5', { + helpers: 'inline', + pkg: { + id: 'test-pkg', + module: 'esm', + target: 'es5', + pluginInfos: [], + helpers: 'external', + }, + }), + c({ + helpers: 'external', + }), + ); + // Task config has highest priority + expect(task.config.helpers).toEqual('inline'); + }); + }); + describe('transform', () => { describe('modes', () => { it('default is based on command', () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 115deefb..a96719b5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -652,6 +652,15 @@ importers: specifier: workspace:* version: link:../../../packages/pkg + tests/integration/helpers: + dependencies: + '@ice/pkg': + specifier: workspace:* + version: link:../../../packages/pkg + '@swc/helpers': + specifier: ^0.5.15 + version: 0.5.17 + tests/integration/mix-cjs-mjs: dependencies: '@ice/pkg': diff --git a/tests/integration/helpers/__snapshots__/index.test.ts.snap b/tests/integration/helpers/__snapshots__/index.test.ts.snap new file mode 100644 index 00000000..cea2ba06 --- /dev/null +++ b/tests/integration/helpers/__snapshots__/index.test.ts.snap @@ -0,0 +1,139 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`Run config external-helpers > cjs structure 1`] = `null`; + +exports[`Run config external-helpers > dist structure 1`] = ` +[ + "dist/", + "├── index.esm.es5.production.js", + "└── vendor.esm.es5.production.js", +] +`; + +exports[`Run config external-helpers > es2017 structure 1`] = `null`; + +exports[`Run config external-helpers > esm structure 1`] = ` +[ + "esm/", + "├── index.d.ts", + "└── index.js", +] +`; + +exports[`Run config external-helpers > file content dist/index.esm.es5.production.js 1`] = ` +"import{_ as r}from"./vendor.esm.es5.production.js";function o(o,e){var n;return(n=Object).assign.apply(n,[{},o].concat(r(e)))}export{o as spread}; +" +`; + +exports[`Run config external-helpers > file content dist/vendor.esm.es5.production.js 1`] = ` +"function r(r,t){(null==t||t>r.length)&&(t=r.length);for(var n=0,e=Array(t);ntypeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}(t)||function(t,n){if(t){if("string"==typeof t)return r(t,void 0);var e=Object.prototype.toString.call(t).slice(8,-1);if("Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e)return Array.from(e);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return r(t,void 0)}}(t)||function(){throw TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}export{t as _}; +" +`; + +exports[`Run config external-helpers > file content esm/index.d.ts 1`] = ` +"export declare function spread(target: Record, sources: Array>): Record; +" +`; + +exports[`Run config external-helpers > file content esm/index.js 1`] = ` +"import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array"; +export function spread(target, sources) { + var _Object; + return (_Object = Object).assign.apply(_Object, [ + {}, + target + ].concat(_to_consumable_array(sources))); +} +" +`; + +exports[`Run config inline-helpers > cjs structure 1`] = `null`; + +exports[`Run config inline-helpers > dist structure 1`] = ` +[ + "dist/", + "└── index.esm.es5.production.js", +] +`; + +exports[`Run config inline-helpers > es2017 structure 1`] = `null`; + +exports[`Run config inline-helpers > esm structure 1`] = ` +[ + "esm/", + "├── index.d.ts", + "└── index.js", +] +`; + +exports[`Run config inline-helpers > file content dist/index.esm.es5.production.js 1`] = ` +"function r(r,t){(null==t||t>r.length)&&(t=r.length);for(var n=0,e=Array(t);ntypeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}(n)||function(t,n){if(t){if("string"==typeof t)return r(t,void 0);var e=Object.prototype.toString.call(t).slice(8,-1);if("Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e)return Array.from(e);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return r(t,void 0)}}(n)||function(){throw TypeError("Invalid attempt to spread non-iterable instance.\\\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()))}export{t as spread}; +" +`; + +exports[`Run config inline-helpers > file content esm/index.d.ts 1`] = ` +"export declare function spread(target: Record, sources: Array>): Record; +" +`; + +exports[`Run config inline-helpers > file content esm/index.js 1`] = ` +"function _array_like_to_array(arr, len) { + if (len == null || len > arr.length) len = arr.length; + for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; + return arr2; +} +function _array_without_holes(arr) { + if (Array.isArray(arr)) return _array_like_to_array(arr); +} +function _iterable_to_array(iter) { + if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); +} +function _non_iterable_spread() { + throw new TypeError("Invalid attempt to spread non-iterable instance.\\\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); +} +function _to_consumable_array(arr) { + return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); +} +function _unsupported_iterable_to_array(o, minLen) { + if (!o) return; + if (typeof o === "string") return _array_like_to_array(o, minLen); + var n = Object.prototype.toString.call(o).slice(8, -1); + if (n === "Object" && o.constructor) n = o.constructor.name; + if (n === "Map" || n === "Set") return Array.from(n); + if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); +} +export function spread(target, sources) { + var _Object; + return (_Object = Object).assign.apply(_Object, [ + {}, + target + ].concat(_to_consumable_array(sources))); +} +" +`; + +exports[`Run config pkg-inline-helpers > cjs structure 1`] = `null`; + +exports[`Run config pkg-inline-helpers > dist structure 1`] = `null`; + +exports[`Run config pkg-inline-helpers > es2017 structure 1`] = `null`; + +exports[`Run config pkg-inline-helpers > esm structure 1`] = ` +[ + "esm/", + "├── index.d.ts", + "└── index.js", +] +`; + +exports[`Run config pkg-inline-helpers > file content esm/index.d.ts 1`] = ` +"export declare function spread(target: Record, sources: Array>): Record; +" +`; + +exports[`Run config pkg-inline-helpers > file content esm/index.js 1`] = ` +"export function spread(target, sources) { + return Object.assign({}, target, ...sources); +} +" +`; diff --git a/tests/integration/helpers/index.test.ts b/tests/integration/helpers/index.test.ts new file mode 100644 index 00000000..43bc199d --- /dev/null +++ b/tests/integration/helpers/index.test.ts @@ -0,0 +1,42 @@ +import { runProjectTest } from '../../helpers/run'; + +runProjectTest(import.meta.url, [ + { + name: 'inline-helpers', + config: { + helpers: 'inline', + transform: { + formats: ['esm'], + }, + bundle: { + formats: ['esm'], + }, + }, + snapshot: 'full', + }, + { + name: 'external-helpers', + config: { + helpers: 'external', + transform: { + formats: ['esm'], + }, + bundle: { + formats: ['esm'], + }, + }, + snapshot: 'full', + }, + { + name: 'pkg-inline-helpers', + config: { + pkgs: [ + { + module: 'esm', + helpers: 'inline', + }, + ], + }, + snapshot: 'full', + }, +]); diff --git a/tests/integration/helpers/package.json b/tests/integration/helpers/package.json new file mode 100644 index 00000000..fcac873f --- /dev/null +++ b/tests/integration/helpers/package.json @@ -0,0 +1,9 @@ +{ + "name": "@ice/pkg-tests-helpers-config", + "version": "0.0.0", + "private": true, + "dependencies": { + "@ice/pkg": "workspace:*", + "@swc/helpers": "^0.5.15" + } +} diff --git a/tests/integration/helpers/src/index.ts b/tests/integration/helpers/src/index.ts new file mode 100644 index 00000000..4d3fe02f --- /dev/null +++ b/tests/integration/helpers/src/index.ts @@ -0,0 +1,6 @@ +export function spread( + target: Record, + sources: Array>, +): Record { + return Object.assign({}, target, ...sources); +} diff --git a/tests/integration/helpers/test-pkg/index.d.ts b/tests/integration/helpers/test-pkg/index.d.ts new file mode 100644 index 00000000..b76c8f42 --- /dev/null +++ b/tests/integration/helpers/test-pkg/index.d.ts @@ -0,0 +1,4 @@ +export declare function spread( + target: Record, + sources: Array>, +): Record; diff --git a/tests/integration/helpers/test-pkg/index.js b/tests/integration/helpers/test-pkg/index.js new file mode 100644 index 00000000..7b8f0ed7 --- /dev/null +++ b/tests/integration/helpers/test-pkg/index.js @@ -0,0 +1,3 @@ +export function spread(target, sources) { + return Object.assign({}, target, ...sources); +}