1- import { extname } from 'path' ;
1+ import { extname , basename , relative , sep } from 'path' ;
22import * as swc from '@swc/core' ;
33import deepmerge from 'deepmerge' ;
44import { 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 } ,
0 commit comments