Skip to content

Commit b6bee69

Browse files
committed
fix: swc auto transform .mjs to .mts suffix in output file content (#541)
* fix: transfrom mjs to mts * fix: source name * chore: upgrade example @swc/helpers * fix: alias parser
1 parent 55159fa commit b6bee69

File tree

9 files changed

+71
-70
lines changed

9 files changed

+71
-70
lines changed

examples/rax-component/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"component"
4848
],
4949
"dependencies": {
50-
"@swc/helpers": "^0.4.14",
50+
"@swc/helpers": "^0.5.1",
5151
"babel-runtime-jsx-plus": "^0.1.5",
5252
"rax-view": "^2.3.0"
5353
},

examples/react-component/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"dependencies": {
3939
"@ice/jsx-runtime": "^0.2.0",
40-
"@swc/helpers": "^0.4.14",
40+
"@swc/helpers": "^0.5.1",
4141
"babel-runtime-jsx-plus": "^0.1.5"
4242
},
4343
"devDependencies": {

examples/react-multi-components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"dependencies": {
4646
"@ice/jsx-runtime": "^0.2.0",
47-
"@swc/helpers": "^0.4.14"
47+
"@swc/helpers": "^0.5.1"
4848
},
4949
"devDependencies": {
5050
"@ice/pkg": "workspace:*",

packages/pkg/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@rollup/plugin-node-resolve": "^13.1.3",
4545
"@rollup/plugin-replace": "^5.0.1",
4646
"@rollup/pluginutils": "^4.1.2",
47-
"@swc/core": "1.3.55",
47+
"@swc/core": "1.3.57",
4848
"acorn": "^8.7.0",
4949
"autoprefixer": "^10.4.2",
5050
"build-scripts": "^2.0.0",

packages/pkg/src/helpers/defaultSwcConfig.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export const getDefaultBundleSwcConfig = (
3131
const browserTargets = taskName === TaskName.BUNDLE_ES2017 ? MODERN_BROWSER_TARGETS : LEGACY_BROWSER_TARGETS;
3232
return {
3333
jsc: {
34-
baseUrl: ctx.rootDir,
3534
paths: formatAliasToTSPathsConfig(bundleTaskConfig.alias),
3635
externalHelpers: true,
3736
},
@@ -63,7 +62,6 @@ export const getDefaultTransformSwcConfig = (
6362
return {
6463
jsc: {
6564
target,
66-
baseUrl: ctx.rootDir,
6765
paths: formatAliasToTSPathsConfig(transformTaskConfig.alias),
6866
transform: {
6967
optimizer: {

packages/pkg/src/helpers/getRollupOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function getRollupOptions(
5858
rollupOptions.plugins.push(
5959
swcPlugin(
6060
taskConfig.jsxRuntime,
61+
rootDir,
6162
taskConfig.swcCompileOptions,
6263
),
6364
);

packages/pkg/src/rollupPlugins/swc.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extname } from 'path';
1+
import { extname, basename, relative, sep } from 'path';
22
import * as swc from '@swc/core';
33
import deepmerge from 'deepmerge';
44
import { isTypescriptOnly } from '../helpers/suffix.js';
@@ -44,7 +44,10 @@ const normalizeSwcConfig = (
4444
externalHelpers: false,
4545
loose: false, // Not recommend
4646
},
47+
// Disable minimize on every file transform when bundling
4748
minify: false,
49+
swcrc: false,
50+
configFile: false,
4851
};
4952

5053
return deepmerge.all([
@@ -56,11 +59,15 @@ const normalizeSwcConfig = (
5659
/**
5760
* plugin-swc works as substitute of plugin-typescript, babel, babel-preset-env and plugin-minify.
5861
*/
59-
const swcPlugin = (jsxRuntime: TaskConfig['jsxRuntime'], extraSwcOptions?: Config): Plugin => {
62+
const swcPlugin = (
63+
jsxRuntime: TaskConfig['jsxRuntime'],
64+
rootDir: string,
65+
extraSwcOptions?: Config,
66+
): Plugin => {
6067
return {
6168
name: 'ice-pkg:swc',
6269

63-
transform(_, id) {
70+
async transform(source, id) {
6471
if (!scriptsFilter(id)) {
6572
return null;
6673
}
@@ -70,25 +77,25 @@ const swcPlugin = (jsxRuntime: TaskConfig['jsxRuntime'], extraSwcOptions?: Confi
7077
absolutePath: id,
7178
ext: extname(id),
7279
};
73-
74-
const { code, map } = swc.transformSync(
75-
_,
80+
// If file's name comes with .mjs、.mts、.cjs、.cts suffix
81+
const destExtname = ['m', 'c'].includes(file.ext[1]) ? `.${file.ext[1]}js` : '.js';
82+
const destFilename = basename(id).replace(RegExp(`${extname(id)}$`), destExtname);
83+
const { code, map } = await swc.transform(
84+
source,
7685
normalizeSwcConfig(file, jsxRuntime, {
7786
...extraSwcOptions,
78-
// Disable minimize on every file transform when bundling
79-
minify: false,
80-
// If filename is omitted, will lose filename info in sourcemap
87+
// If filename is omitted, will lose filename info in sourcemap.
88+
// e.g: ./src/index.mts
89+
sourceFileName: `.${sep}${relative(rootDir, id)}`,
8190
filename: id,
8291
}),
8392
);
8493

8594
return {
8695
code,
8796
map,
88-
// Additional option to re-define extname
8997
meta: {
90-
// If file's name comes with .mjs、.mts、.cjs、.cts suffix
91-
ext: ['m', 'c'].includes(file.ext[1]) ? `.${file.ext[1]}js` : '.js',
98+
filename: destFilename,
9299
},
93100
};
94101
},

packages/pkg/src/tasks/transform.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { performance } from 'perf_hooks';
2-
import { isAbsolute, resolve, extname, dirname, relative } from 'path';
2+
import { isAbsolute, resolve, extname, dirname, relative, basename } from 'path';
33
import fs from 'fs-extra';
44
import semver from 'semver';
55
import consola from 'consola';
@@ -173,11 +173,10 @@ async function runTransform(
173173
files[i].code = code = transformResult.code;
174174
files[i].map = map = transformResult.map;
175175

176-
const finalExtname = transformResult?.meta?.ext;
176+
const destFilename = transformResult?.meta?.filename;
177177

178-
// If extname changed
179-
if (finalExtname) {
180-
files[i].dest = (files[i].dest).replace(files[i].ext, finalExtname);
178+
if (destFilename) {
179+
files[i].dest = (files[i].dest).replace(basename(files[i].dest), destFilename);
181180
}
182181
}
183182

@@ -186,7 +185,7 @@ async function runTransform(
186185

187186
fs.writeFileSync(
188187
files[i].dest,
189-
`${code}\n //# sourceMappingURL=${files[i].dest}.map`,
188+
`${code}\n //# sourceMappingURL=${transformResult?.meta?.filename}.map`,
190189
'utf-8',
191190
);
192191
fs.writeFileSync(`${files[i].dest}.map`, standardizedMap, 'utf-8');

pnpm-lock.yaml

Lines changed: 42 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)