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/calm-bikes-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/pkg': patch
---

fix: support @swc/helpers with correct import between cjs and mjs
8 changes: 7 additions & 1 deletion packages/pkg/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
65 changes: 33 additions & 32 deletions packages/pkg/src/engine/rollup/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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: [
Expand All @@ -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,
}),
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.

145 changes: 145 additions & 0 deletions tests/integration/mix-cjs-mjs/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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
}));
"
`;
12 changes: 12 additions & 0 deletions tests/integration/mix-cjs-mjs/build.config.swc-helpers.mts
Original file line number Diff line number Diff line change
@@ -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/]
},
})
9 changes: 9 additions & 0 deletions tests/integration/mix-cjs-mjs/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { runProjectTest } from '../../helpers/run';

runProjectTest(import.meta.url, [
{
name: 'swc-helpers',
config: 'build.config.swc-helpers.mts',
snapshot: 'full',
},
]);
9 changes: 9 additions & 0 deletions tests/integration/mix-cjs-mjs/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions tests/integration/mix-cjs-mjs/src/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports.add = function (a, b) {
return a + b;
};

module.exports.extend = function (a, b) {
return { ...a, ...b };
};
7 changes: 7 additions & 0 deletions tests/integration/mix-cjs-mjs/src/entry-mts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { join, extend } from './cjs';

export { join, extend };

export const foo = {
...extend({ a: 1 }, { b: 2 }),
};
Loading