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/few-pets-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/pkg': minor
---

feat: improve cjs import and performance
2 changes: 2 additions & 0 deletions packages/pkg/src/engine/rollup/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export function getRollupOptions(context: Context, taskRunnerContext: TaskRunner
'.jsx',
'.ts',
'.tsx',
'.cts',
'.cjs',
...(taskConfig.extensions || []),
],
transformMixedEsModules: true,
Expand Down
39 changes: 26 additions & 13 deletions packages/pkg/src/engine/shared/swcConfig.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { BundleTaskConfig, TransformTaskConfig, NodeEnvMode } from '../../types.js';
import { BundleTaskConfig, TransformTaskConfig, NodeEnvMode, JsTarget } from '../../types.js';
import type { Config, ModuleConfig } from '@swc/core';
import getDefaultDefineValues from './define.js';
import { ALL_FORMAT_TARGET } from '../../constants.js';

// https://github.com/ice-lab/ice-next/issues/54#issuecomment-1083263523
const LEGACY_BROWSER_TARGETS = {
chrome: 49,
ie: 11,
};
const MODERN_BROWSER_TARGETS = {
chrome: 61,
safari: 11,
firefox: 60,
edge: 16,
ios: 11,
const BROWSER_TARGETS_MAP: Record<JsTarget, any> = {
es5: {
chrome: 49,
ie: 11,
},
es2017: {
chrome: 61,
safari: 11,
firefox: 60,
edge: 16,
ios: 11,
},
es2022: {
chrome: 85,
safari: 15,
firefox: 79,
edge: 85,
ios: 15,
},
};

export const getDefaultBundleSwcConfig = (bundleTaskConfig: BundleTaskConfig): Config => {
const browserTargets = bundleTaskConfig.formats[0].target !== 'es5' ? MODERN_BROWSER_TARGETS : LEGACY_BROWSER_TARGETS;
const formatTarget = bundleTaskConfig.formats[0].target;
const browserTargets = BROWSER_TARGETS_MAP[formatTarget] ?? BROWSER_TARGETS_MAP.es2017;
return {
jsc: {
externalHelpers: true,
Expand All @@ -38,7 +49,9 @@ export const getDefaultTransformSwcConfig = (transformTaskConfig: TransformTaskC
const module: ModuleConfig | undefined =
transformTaskConfig.format.module === 'cjs' ? { type: 'commonjs' } : undefined;

const target = transformTaskConfig.format.target === 'es2017' ? 'es2017' : 'es5';
const target = ALL_FORMAT_TARGET.includes(transformTaskConfig.format.target)
? transformTaskConfig.format.target
: 'es5';

return {
jsc: {
Expand Down
68 changes: 3 additions & 65 deletions packages/pkg/src/rollupPlugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
formatCnpmDepFilepath,
getIncludeNodeModuleScripts,
} from '../utils.js';
import { init, parse } from 'es-module-lexer';
import MagicString from 'magic-string';
import type { Options as SwcCompileOptions, Config, TsParserConfig, EsParserConfig } from '@swc/core';
import type { TaskConfig, OutputFile, BundleTaskConfig } from '../types.js';
import type { Plugin } from 'rollup';
Expand Down Expand Up @@ -74,65 +72,6 @@ const normalizeSwcConfig = (
});
};

// Transform @swc/helpers to cjs path if in commonjs module.
async function transformImport(source: string, sourceFilename: string) {
await init;
const [imports, exports] = await parse(source);
let s: MagicString | undefined;
const str = () => {
if (!s) {
s = new MagicString(source);
}
return s;
};
const isESM =
exports.length > 0 ||
imports.some((targetImport) => {
const importString = targetImport.n;
// `targetImport.n` get undefined when code has `import.meta.*`.
return importString && !importString.includes('core-js') && !importString.includes('@swc/helpers');
});

imports.forEach((targetImport) => {
if (!targetImport.n) {
// If visiting `import.meta.*`, `targetImport.n` will be undefined, that should be ignored.
return;
}
if (targetImport.n.startsWith('@swc/helpers')) {
if (!isESM) {
// Replace @swc/helpers with cjs
const importStr = source.substring(targetImport.ss, targetImport.se);
// Import rule: import { _ as _type_of } from "@swc/helpers/_/_type_of";
const matchImport = importStr.match(/import\s+{\s+([\w*\s{},]*)\s+}\s+from\s+['"](.*)['"]/);
if (matchImport) {
const [, identifier] = matchImport;
const replaceModule = `var ${identifier.split(' as ')[1].trim()} = require('${targetImport.n}')._`;
str().overwrite(targetImport.ss, targetImport.se, replaceModule);
}
}
} else if (targetImport.n.startsWith('core-js')) {
// Replace core-js with cjs
if (!isESM) {
const replaceRequire = `require('${targetImport.n}')`;
str().overwrite(targetImport.ss, targetImport.se, replaceRequire);
}
}
});

return s
? {
code: s.toString(),
map: s.generateMap({
source: sourceFilename,
file: `${sourceFilename}.map`,
includeContent: true,
}),
}
: {
code: source,
};
}

/**
* plugin-swc works as substitute of plugin-typescript, babel, babel-preset-env and plugin-minify.
*/
Expand Down Expand Up @@ -169,14 +108,13 @@ const swcPlugin = (
// e.g: ./src/index.mts
sourceFileName,
filename: id,
isModule: 'unknown',
}),
);

const transformedCode = await transformImport(code, sourceFileName);

return {
code: transformedCode.code,
map: transformedCode.map ?? map,
code: code,
map: map,
meta: {
filename: destFilename,
},
Expand Down
Loading
Loading