@@ -15,11 +15,68 @@ import Alias from '@rollup/plugin-alias';
1515import postcss from 'rollup-plugin-postcss' ;
1616import strip from '@rollup/plugin-strip' ;
1717import vue from '@vitejs/plugin-vue' ;
18+ import * as ts from 'typescript' ;
1819import * as path from 'path' ;
1920import { Config } from './config' ;
2021
2122const useTypescriptPlugin2 = process . env . USE_TYPESCRIPT2 === 'true' ;
22- const typescript = useTypescriptPlugin2 ? require ( 'rollup-plugin-typescript2' ) : require ( '@rollup/plugin-typescript' ) ;
23+ const typescript1 = require ( '@rollup/plugin-typescript' ) ;
24+ const typescript2 = require ( 'rollup-plugin-typescript2' ) ;
25+
26+ function transpileVueTsSfc ( ) : Plugin {
27+ return {
28+ name : 'transpile-vue-ts-sfc' ,
29+ transform ( code , id ) {
30+ if ( ! id . includes ( '?vue' ) ) {
31+ return null ;
32+ }
33+ if ( ! id . includes ( '&lang.ts' ) && ! id . includes ( '&lang.tsx' ) ) {
34+ return null ;
35+ }
36+ const result = ts . transpileModule ( code , {
37+ compilerOptions : {
38+ target : ts . ScriptTarget . ES2017 ,
39+ module : ts . ModuleKind . ESNext ,
40+ jsx : ts . JsxEmit . Preserve ,
41+ sourceMap : true
42+ } ,
43+ fileName : id
44+ } ) ;
45+ return {
46+ code : result . outputText ,
47+ map : result . sourceMapText ? JSON . parse ( result . sourceMapText ) : null
48+ } ;
49+ }
50+ } ;
51+ }
52+
53+ function transpileTsForRollup ( ) : Plugin {
54+ return {
55+ name : 'transpile-ts-for-rollup' ,
56+ transform ( code , id ) {
57+ if ( id . endsWith ( '.d.ts' ) ) {
58+ return null ;
59+ }
60+ const isTsLike = id . endsWith ( '.ts' ) || id . endsWith ( '.tsx' ) || id . includes ( '&lang.ts' ) || id . includes ( '&lang.tsx' ) ;
61+ if ( ! isTsLike ) {
62+ return null ;
63+ }
64+ const result = ts . transpileModule ( code , {
65+ compilerOptions : {
66+ target : ts . ScriptTarget . ES2017 ,
67+ module : ts . ModuleKind . ESNext ,
68+ jsx : ts . JsxEmit . Preserve ,
69+ sourceMap : true
70+ } ,
71+ fileName : id
72+ } ) ;
73+ return {
74+ code : result . outputText ,
75+ map : result . sourceMapText ? JSON . parse ( result . sourceMapText ) : null
76+ } ;
77+ }
78+ } ;
79+ }
2380
2481function getExternal (
2582 rawPackageJson : RawPackageJson ,
@@ -42,25 +99,26 @@ export function getRollupOptions(
4299 config : Config ,
43100 tsconfigOverride ?: Record < string , unknown >
44101) : RollupOptions {
45- const tsOption = {
46- tsconfig : path . resolve ( projectRoot , config . tsconfig )
47- } ;
48-
49- if ( useTypescriptPlugin2 && tsconfigOverride ) {
50- ( tsOption as any ) . tsconfigOverride = tsconfigOverride ;
51- }
102+ const tsconfigPath = path . resolve ( projectRoot , config . tsconfig ) ;
103+ const typescriptPlugin : Plugin = useTypescriptPlugin2
104+ ? typescript2 ( tsconfigOverride ? { tsconfig : tsconfigPath , tsconfigOverride } : { tsconfig : tsconfigPath } )
105+ : typescript1 ( { tsconfig : tsconfigPath } ) ;
52106 return {
53107 input : entry ,
54108 external : getExternal ( rawPackageJson , config . external ) ,
55109 ...config . rollupOptions ,
56110 plugins : [
57- resolve ( ) ,
111+ resolve ( {
112+ extensions : [ '.mjs' , '.js' , '.jsx' , '.json' , '.node' , '.ts' , '.tsx' , '.mts' , '.cts' , '.vue' ]
113+ } ) ,
58114 commonjs ( ) ,
59115 vue ( ) ,
60116 // typescript(),
61- babel ( { ...babelPlugins , babelHelpers : 'bundled' } ) ,
62117 replace ( { ...config . envs , preventAssignment : true } ) ,
63- typescript ( tsOption ) ,
118+ transpileVueTsSfc ( ) ,
119+ typescriptPlugin ,
120+ ...( useTypescriptPlugin2 ? [ transpileTsForRollup ( ) ] : [ ] ) ,
121+ babel ( { ...babelPlugins , babelHelpers : 'bundled' } ) ,
64122 postcss ( {
65123 extensions : [ '.css' ]
66124 } ) ,
0 commit comments