1- import typescript from '@rollup/plugin-typescript ' ;
2- import resolve from '@rollup/plugin- node-resolve ' ;
1+ import fs from 'node:fs ' ;
2+ import path from 'node:path ' ;
33import commonjs from '@rollup/plugin-commonjs' ;
4- import dts from 'rollup- plugin-dts ' ;
4+ import resolve from '@ rollup/ plugin-node-resolve ' ;
55import terser from '@rollup/plugin-terser' ;
6+ import typescript from '@rollup/plugin-typescript' ;
7+ import dts from 'rollup-plugin-dts' ;
8+
9+ const packageJson = JSON . parse (
10+ fs . readFileSync ( new URL ( './package.json' , import . meta. url ) , 'utf8' ) ,
11+ ) ;
12+
13+ const externalPackages = [
14+ ...Object . keys ( packageJson . dependencies ?? { } ) ,
15+ ...Object . keys ( packageJson . peerDependencies ?? { } ) ,
16+ ...Object . keys ( packageJson . optionalDependencies ?? { } ) ,
17+ ] ;
18+
19+ const isExternal = ( id ) =>
20+ ! id . startsWith ( '.' ) &&
21+ ! path . isAbsolute ( id ) &&
22+ externalPackages . some ( ( pkg ) => id === pkg || id . startsWith ( `${ pkg } /` ) ) ;
623
24+ const createPlugins = ( ) => [
25+ resolve ( {
26+ browser : true ,
27+ extensions : [ '.mjs' , '.js' , '.json' , '.ts' ] ,
28+ } ) ,
29+ commonjs ( ) ,
30+ typescript ( {
31+ tsconfig : './tsconfig.rollup.json' ,
32+ declaration : false ,
33+ } ) ,
34+ ] ;
735
8- // 主库配置
9- const mainConfig = {
36+ const umdConfig = {
1037 input : 'src/index.ts' ,
1138 output : [
12- // UMD 格式
1339 {
1440 file : 'dist/index.js' ,
1541 format : 'umd' ,
@@ -23,61 +49,60 @@ const mainConfig = {
2349 sourcemap : true ,
2450 plugins : [ terser ( ) ] ,
2551 } ,
26- // ESM 格式
52+ ] ,
53+ plugins : createPlugins ( ) ,
54+ } ;
55+
56+ const esmConfig = {
57+ input : 'src/index.ts' ,
58+ output : [
2759 {
2860 dir : 'lib' ,
2961 format : 'esm' ,
3062 sourcemap : true ,
3163 preserveModules : true ,
3264 preserveModulesRoot : 'src' ,
33- }
34- ] ,
35- plugins : [
36- resolve ( ) ,
37- commonjs ( ) ,
38- typescript ( {
39- tsconfig : './tsconfig.rollup.json' ,
40- declaration : false ,
41- } ) ,
65+ } ,
66+ {
67+ dir : 'esm' ,
68+ format : 'esm' ,
69+ sourcemap : true ,
70+ preserveModules : true ,
71+ preserveModulesRoot : 'src' ,
72+ } ,
4273 ] ,
74+ external : isExternal ,
75+ plugins : createPlugins ( ) ,
4376} ;
4477
45- // Worker ESM
4678const workerESMConfig = {
4779 input : 'src/worker.ts' ,
48- output : {
49- file : 'lib/worker.js' ,
50- format : 'esm' ,
51- sourcemap : true ,
52- } ,
53- plugins : [
54- resolve ( ) ,
55- commonjs ( ) ,
56- typescript ( {
57- tsconfig : './tsconfig.rollup.json' ,
58- declaration : false ,
59- } ) ,
80+ output : [
81+ {
82+ file : 'lib/worker.js' ,
83+ format : 'esm' ,
84+ sourcemap : true ,
85+ } ,
86+ {
87+ file : 'esm/worker.js' ,
88+ format : 'esm' ,
89+ sourcemap : true ,
90+ } ,
6091 ] ,
92+ external : isExternal ,
93+ plugins : createPlugins ( ) ,
6194} ;
6295
63- // Worker IIFE
6496const workerIIFEConfig = {
6597 input : 'src/worker.ts' ,
6698 output : {
6799 file : 'dist/worker.js' ,
68100 format : 'iife' ,
69101 sourcemap : true ,
70102 name : 'LayoutWorker' ,
71- plugins : [ terser ( ) ] , // 压缩 Worker
103+ plugins : [ terser ( ) ] ,
72104 } ,
73- plugins : [
74- resolve ( ) ,
75- commonjs ( ) ,
76- typescript ( {
77- tsconfig : './tsconfig.rollup.json' ,
78- declaration : false ,
79- } ) ,
80- ] ,
105+ plugins : createPlugins ( ) ,
81106} ;
82107
83108const dtsConfig = {
@@ -88,12 +113,8 @@ const dtsConfig = {
88113 preserveModules : true ,
89114 preserveModulesRoot : 'src' ,
90115 } ,
116+ external : isExternal ,
91117 plugins : [ dts ( { tsconfig : './tsconfig.rollup.json' } ) ] ,
92118} ;
93119
94- export default [
95- mainConfig ,
96- workerESMConfig ,
97- workerIIFEConfig ,
98- dtsConfig ,
99- ] ;
120+ export default [ umdConfig , esmConfig , workerESMConfig , workerIIFEConfig , dtsConfig ] ;
0 commit comments