@@ -14,136 +14,113 @@ const is_publish = !!process.env.PUBLISH;
1414
1515const ts_plugin = is_publish
1616 ? typescript ( {
17- typescript : require ( 'typescript' )
18- } )
17+ typescript : require ( 'typescript' ) ,
18+ } )
1919 : sucrase ( {
20- transforms : [ 'typescript' ]
21- } ) ;
20+ transforms : [ 'typescript' ] ,
21+ } ) ;
2222
23- // The following external and path logic is necessary so that the bundled runtime pieces and the index file
24- // reference each other correctly instead of bundling their references to each other
23+ fs . writeFileSync (
24+ `./compiler.d.ts` ,
25+ `export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index.js';`
26+ ) ;
2527
26- /**
27- * Ensures that relative imports inside `src/runtime` like `./internal` and `../store` are externalized correctly
28- */
29- const external = ( id , parent_id ) => {
30- const parent_segments = parent_id . replace ( / \\ / g, '/' ) . split ( '/' ) ;
31- // TODO needs to be adjusted when we move to JS modules
32- if ( parent_segments [ parent_segments . length - 3 ] === 'runtime' ) {
33- return / \. \. \/ \w + $ / . test ( id ) ;
34- } else {
35- return id === './internal' && parent_segments [ parent_segments . length - 2 ] === 'runtime' ;
36- }
37- }
38-
39- /**
40- * Transforms externalized import paths like `../store` into correct relative imports with correct index file extension import
41- */
42- const replace_relative_svelte_imports = ( id , ending ) => {
43- id = id . replace ( / \\ / g, '/' ) ;
44- // TODO needs to be adjusted when we move to JS modules
45- return / s r c \/ r u n t i m e \/ \w + $ / . test ( id ) && `../${ id . split ( '/' ) . pop ( ) } /${ ending } ` ;
46- }
28+ const runtime_entrypoints = Object . fromEntries (
29+ fs
30+ . readdirSync ( 'src/runtime' , { withFileTypes : true } )
31+ . filter ( ( dirent ) => dirent . isDirectory ( ) )
32+ . map ( ( dirent ) => [ dirent . name , `src/runtime/${ dirent . name } /index.ts` ] )
33+ ) ;
4734
4835/**
49- * Transforms externalized `./internal` import path into correct relative import with correct index file extension import
36+ * @type { import("rollup").RollupOptions[] }
5037 */
51- const replace_relative_internal_import = ( id , ending ) => {
52- id = id . replace ( / \\ / g, '/' ) ;
53- // TODO needs to be adjusted when we move to JS modules
54- return id . endsWith ( 'src/runtime/internal' ) && `./internal/${ ending } ` ;
55- }
56-
57- fs . writeFileSync ( `./compiler.d.ts` , `export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index';` ) ;
58-
5938export default [
60- /* runtime */
6139 {
62- input : `src/runtime/index.ts` ,
63- output : [
64- {
65- file : `index.mjs` ,
66- format : 'esm' ,
67- paths : id => replace_relative_internal_import ( id , 'index.mjs' )
68- } ,
69- {
70- file : `index.js` ,
71- format : 'cjs' ,
72- paths : id => replace_relative_internal_import ( id , 'index.js' )
73- }
74- ] ,
75- external,
76- plugins : [ ts_plugin ]
77- } ,
40+ input : {
41+ ...runtime_entrypoints ,
42+ index : 'src/runtime/index.ts' ,
43+ ssr : 'src/runtime/ssr.ts'
44+ } ,
45+ output : [ 'es' , 'cjs' ] . map (
46+ /** @returns {import('rollup').OutputOptions } */
47+ ( format ) => {
48+ const ext = format === 'es' ? 'mjs' : 'js' ;
49+ return {
50+ entryFileNames : ( entry ) => {
51+ if ( entry . isEntry ) {
52+ if ( entry . name === 'index' ) return `index.${ ext } ` ;
53+ else if ( entry . name === 'ssr' ) return `ssr.${ ext } ` ;
7854
79- {
80- input : `src/runtime/ssr.ts` ,
81- output : [
82- {
83- file : `ssr.mjs` ,
84- format : 'esm' ,
85- paths : id => replace_relative_internal_import ( id , 'index.mjs' )
86- } ,
87- {
88- file : `ssr.js` ,
89- format : 'cjs' ,
90- paths : id => replace_relative_internal_import ( id , 'index.js' )
55+ return `${ entry . name } /index.${ ext } ` ;
56+ }
57+ } ,
58+ chunkFileNames : `internal/[name]-[hash].${ ext } ` ,
59+ format,
60+ minifyInternalExports : false ,
61+ dir : '.' ,
62+ } ;
9163 }
92- ] ,
93- external,
94- plugins : [ ts_plugin ]
95- } ,
96-
97- ...fs . readdirSync ( 'src/runtime' )
98- . filter ( dir => fs . statSync ( `src/runtime/${ dir } ` ) . isDirectory ( ) )
99- . map ( dir => ( {
100- input : `src/runtime/${ dir } /index.ts` ,
101- output : [
102- {
103- file : `${ dir } /index.mjs` ,
104- format : 'esm' ,
105- paths : id => replace_relative_svelte_imports ( id , 'index.mjs' )
64+ ) ,
65+ plugins : [
66+ replace ( {
67+ preventAssignment : true ,
68+ values : {
69+ __VERSION__ : pkg . version ,
10670 } ,
107- {
108- file : `${ dir } /index.js` ,
109- format : 'cjs' ,
110- paths : id => replace_relative_svelte_imports ( id , 'index.js' )
111- }
112- ] ,
113- external,
114- plugins : [
115- replace ( {
116- __VERSION__ : pkg . version
117- } ) ,
118- ts_plugin ,
119- {
120- writeBundle ( _options , bundle ) {
71+ } ) ,
72+ ts_plugin ,
73+ {
74+ writeBundle ( options , bundle ) {
75+ if ( options . format !== 'es' ) return ;
76+
77+ for ( const entry of Object . values ( bundle ) ) {
78+ const dir = entry . name ;
79+ if ( ! entry . isEntry || ! runtime_entrypoints [ dir ] ) continue ;
80+
12181 if ( dir === 'internal' ) {
122- const mod = bundle [ ' index.mjs' ] ;
82+ const mod = bundle [ `internal/ index.mjs` ] ;
12383 if ( mod ) {
124- fs . writeFileSync ( 'src/compiler/compile/internal_exports.ts' , `// This file is automatically generated\nexport default new Set(${ JSON . stringify ( mod . exports ) } );` ) ;
84+ fs . writeFileSync (
85+ 'src/compiler/compile/internal_exports.ts' ,
86+ `// This file is automatically generated\n` +
87+ `export default new Set(${ JSON . stringify ( mod . exports ) } );`
88+ ) ;
12589 }
12690 }
12791
128- fs . writeFileSync ( `${ dir } /package.json` , JSON . stringify ( {
129- main : './index' ,
130- module : './index.mjs' ,
131- types : './index.d.ts'
132- } , null , ' ' ) ) ;
92+ fs . writeFileSync (
93+ `${ dir } /package.json` ,
94+ JSON . stringify (
95+ {
96+ main : './index.js' ,
97+ module : './index.mjs' ,
98+ types : './index.d.ts' ,
99+ } ,
100+ null ,
101+ ' '
102+ )
103+ ) ;
133104
134- fs . writeFileSync ( `${ dir } /index.d.ts` , `export * from '../types/runtime/${ dir } /index';` ) ;
105+ fs . writeFileSync (
106+ `${ dir } /index.d.ts` ,
107+ `export * from '../types/runtime/${ dir } /index.js';`
108+ ) ;
135109 }
136110 }
137- ]
138- } ) ) ,
139-
111+ }
112+ ]
113+ } ,
140114 /* compiler.js */
141115 {
142116 input : 'src/compiler/index.ts' ,
143117 plugins : [
144118 replace ( {
145- __VERSION__ : pkg . version ,
146- 'process.env.NODE_DEBUG' : false // appears inside the util package
119+ preventAssignment : true ,
120+ values : {
121+ __VERSION__ : pkg . version ,
122+ 'process.env.NODE_DEBUG' : false // appears inside the util package
123+ } ,
147124 } ) ,
148125 {
149126 resolveId ( id ) {
@@ -152,7 +129,7 @@ export default [
152129 if ( id === 'util' ) {
153130 return require . resolve ( './node_modules/util' ) ; // just 'utils' would resolve this to the built-in module
154131 }
155- }
132+ } ,
156133 } ,
157134 resolve ( ) ,
158135 commonjs ( {
@@ -177,6 +154,7 @@ export default [
177154 ] ,
178155 external : is_publish
179156 ? [ ]
180- : id => id === 'acorn' || id === 'magic-string' || id . startsWith ( 'css-tree' )
157+ : ( id ) =>
158+ id === 'acorn' || id === 'magic-string' || id . startsWith ( 'css-tree' )
181159 }
182160] ;
0 commit comments