Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-foxes-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/pkg': minor
---

feat: support inline helpers
1 change: 1 addition & 0 deletions packages/pkg/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions packages/pkg/src/core/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/pkg/src/engine/shared/swcConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 17 additions & 1 deletion packages/pkg/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export interface PkgUserConfig
BundleUserConfig,
'externals' | 'name' | 'compileDependencies' | 'polyfill' | 'engine' | 'minify' | 'codeSplitting'
>,
Pick<UserConfig, 'alias' | 'define' | 'jsxRuntime' | 'declaration' | 'entry' | 'sourceMaps'> {
Pick<UserConfig, 'alias' | 'define' | 'jsxRuntime' | 'declaration' | 'entry' | 'sourceMaps' | 'helpers'> {
/**
* Unique id to indicate a package
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
55 changes: 55 additions & 0 deletions packages/pkg/tests/core/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 139 additions & 0 deletions tests/integration/helpers/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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);n<t;n++)e[n]=r[n];return e}function t(t){return function(t){if(Array.isArray(t))return r(t)}(t)||function(r){if("u">typeof 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<string, unknown>, sources: Array<Record<string, unknown>>): Record<string, unknown>;
"
`;

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);n<t;n++)e[n]=r[n];return e}function t(t,n){var e;return(e=Object).assign.apply(e,[{},t].concat(function(t){if(Array.isArray(t))return r(t)}(n)||function(r){if("u">typeof 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<string, unknown>, sources: Array<Record<string, unknown>>): Record<string, unknown>;
"
`;

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<string, unknown>, sources: Array<Record<string, unknown>>): Record<string, unknown>;
"
`;

exports[`Run config pkg-inline-helpers > file content esm/index.js 1`] = `
"export function spread(target, sources) {
return Object.assign({}, target, ...sources);
}
"
`;
42 changes: 42 additions & 0 deletions tests/integration/helpers/index.test.ts
Original file line number Diff line number Diff line change
@@ -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',
},
]);
9 changes: 9 additions & 0 deletions tests/integration/helpers/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
6 changes: 6 additions & 0 deletions tests/integration/helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function spread(
target: Record<string, unknown>,
sources: Array<Record<string, unknown>>,
): Record<string, unknown> {
return Object.assign({}, target, ...sources);
}
4 changes: 4 additions & 0 deletions tests/integration/helpers/test-pkg/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare function spread(
target: Record<string, unknown>,
sources: Array<Record<string, unknown>>,
): Record<string, unknown>;
3 changes: 3 additions & 0 deletions tests/integration/helpers/test-pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function spread(target, sources) {
return Object.assign({}, target, ...sources);
}
Loading