@ -14,136 +14,113 @@ const is_publish = !!process.env.PUBLISH;
const ts _plugin = is _publish
const ts _plugin = is _publish
? typescript ( {
? typescript ( {
typescript : require ( 'typescript' )
typescript : require ( 'typescript' ) ,
} )
} )
: sucrase ( {
: sucrase ( {
transforms : [ 'typescript' ]
transforms : [ 'typescript' ] ,
} ) ;
} ) ;
// The following external and path logic is necessary so that the bundled runtime pieces and the index file
fs . writeFileSync (
// reference each other correctly instead of bundling their references to each other
` ./compiler.d.ts ` ,
` export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index.js'; `
) ;
/ * *
const runtime _entrypoints = Object . fromEntries (
* Ensures that relative imports inside ` src/runtime ` like ` ./internal ` and ` ../store ` are externalized correctly
fs
* /
. readdirSync ( 'src/runtime' , { withFileTypes : true } )
const external = ( id , parent _id ) => {
. filter ( ( dirent ) => dirent . isDirectory ( ) )
const parent _segments = parent _id . replace ( /\\/g , '/' ) . split ( '/' ) ;
. map ( ( dirent ) => [ dirent . name , ` src/runtime/ ${ dirent . name } /index.ts ` ] )
// TODO needs to be adjusted when we move to JS modules
) ;
if ( parent _segments [ parent _segments . length - 3 ] === 'runtime' ) {
return /\.\.\/\w+$/ . test ( id ) ;
} else {
return id === './internal' && parent _segments [ parent _segments . length - 2 ] === 'runtime' ;
}
}
/ * *
* Transforms externalized import paths like ` ../store ` into correct relative imports with correct index file extension import
* /
const replace _relative _svelte _imports = ( id , ending ) => {
id = id . replace ( /\\/g , '/' ) ;
// TODO needs to be adjusted when we move to JS modules
return /src\/runtime\/\w+$/ . test ( id ) && ` ../ ${ id . split ( '/' ) . pop ( ) } / ${ ending } ` ;
}
/ * *
/ * *
* Transforms externalized ` ./internal ` import path into correct relative import with correct index file extension import
* @ type { import ( "rollup" ) . RollupOptions [ ] }
* /
* /
const replace _relative _internal _import = ( id , ending ) => {
id = id . replace ( /\\/g , '/' ) ;
// TODO needs to be adjusted when we move to JS modules
return id . endsWith ( 'src/runtime/internal' ) && ` ./internal/ ${ ending } ` ;
}
fs . writeFileSync ( ` ./compiler.d.ts ` , ` export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index'; ` ) ;
export default [
export default [
/* runtime */
{
{
input : ` src/runtime/index.ts ` ,
input : {
output : [
... runtime _entrypoints ,
{
index : 'src/runtime/index.ts' ,
file : ` index.mjs ` ,
ssr : 'src/runtime/ssr.ts'
format : 'esm' ,
} ,
paths : id => replace _relative _internal _import ( id , 'index.mjs' )
output : [ 'es' , 'cjs' ] . map (
} ,
/** @returns {import('rollup').OutputOptions} */
{
( format ) => {
file : ` index.js ` ,
const ext = format === 'es' ? 'mjs' : 'js' ;
format : 'cjs' ,
return {
paths : id => replace _relative _internal _import ( id , 'index.js' )
entryFileNames : ( entry ) => {
}
if ( entry . isEntry ) {
] ,
if ( entry . name === 'index' ) return ` index. ${ ext } ` ;
external ,
else if ( entry . name === 'ssr' ) return ` ssr. ${ ext } ` ;
plugins : [ ts _plugin ]
} ,
{
return ` ${ entry . name } /index. ${ ext } ` ;
input : ` src/runtime/ssr.ts ` ,
}
output : [
} ,
{
chunkFileNames : ` internal/[name]-[hash]. ${ ext } ` ,
file : ` ssr.mjs ` ,
format ,
format : 'esm' ,
minifyInternalExports : false ,
paths : id => replace _relative _internal _import ( id , 'index.mjs' )
dir : '.' ,
} ,
} ;
{
file : ` ssr.js ` ,
format : 'cjs' ,
paths : id => replace _relative _internal _import ( id , 'index.js' )
}
}
] ,
) ,
external ,
plugins : [
plugins : [ ts _plugin ]
replace ( {
} ,
preventAssignment : true ,
values : {
... fs . readdirSync ( 'src/runtime' )
_ _VERSION _ _ : pkg . version ,
. filter ( dir => fs . statSync ( ` src/runtime/ ${ dir } ` ) . isDirectory ( ) )
. map ( dir => ( {
input : ` src/runtime/ ${ dir } /index.ts ` ,
output : [
{
file : ` ${ dir } /index.mjs ` ,
format : 'esm' ,
paths : id => replace _relative _svelte _imports ( id , 'index.mjs' )
} ,
} ,
{
} ) ,
file : ` ${ dir } /index.js ` ,
ts _plugin ,
format : 'cjs' ,
{
paths : id => replace _relative _svelte _imports ( id , 'index.js' )
writeBundle ( options , bundle ) {
}
if ( options . format !== 'es' ) return ;
] ,
external ,
for ( const entry of Object . values ( bundle ) ) {
plugins : [
const dir = entry . name ;
replace ( {
if ( ! entry . isEntry || ! runtime _entrypoints [ dir ] ) continue ;
_ _VERSION _ _ : pkg . version
} ) ,
ts _plugin ,
{
writeBundle ( _options , bundle ) {
if ( dir === 'internal' ) {
if ( dir === 'internal' ) {
const mod = bundle [ 'index.mjs' ] ;
const mod = bundle [ ` internal/index.mjs ` ] ;
if ( mod ) {
if ( mod ) {
fs . writeFileSync ( 'src/compiler/compile/internal_exports.ts' , ` // This file is automatically generated \n export default new Set( ${ JSON . stringify ( mod . exports ) } ); ` ) ;
fs . writeFileSync (
'src/compiler/compile/internal_exports.ts' ,
` // This file is automatically generated \n ` +
` export default new Set( ${ JSON . stringify ( mod . exports ) } ); `
) ;
}
}
}
}
fs . writeFileSync ( ` ${ dir } /package.json ` , JSON . stringify ( {
fs . writeFileSync (
main : './index' ,
` ${ dir } /package.json ` ,
module : './index.mjs' ,
JSON . stringify (
types : './index.d.ts'
{
} , null , ' ' ) ) ;
main : './index.js' ,
module : './index.mjs' ,
types : './index.d.ts' ,
} ,
null ,
' '
)
) ;
fs . writeFileSync ( ` ${ dir } /index.d.ts ` , ` export * from '../types/runtime/ ${ dir } /index'; ` ) ;
fs . writeFileSync (
` ${ dir } /index.d.ts ` ,
` export * from '../types/runtime/ ${ dir } /index.js'; `
) ;
}
}
}
}
]
}
} ) ) ,
]
} ,
/* compiler.js */
/* compiler.js */
{
{
input : 'src/compiler/index.ts' ,
input : 'src/compiler/index.ts' ,
plugins : [
plugins : [
replace ( {
replace ( {
_ _VERSION _ _ : pkg . version ,
preventAssignment : true ,
'process.env.NODE_DEBUG' : false // appears inside the util package
values : {
_ _VERSION _ _ : pkg . version ,
'process.env.NODE_DEBUG' : false // appears inside the util package
} ,
} ) ,
} ) ,
{
{
resolveId ( id ) {
resolveId ( id ) {
@ -152,7 +129,7 @@ export default [
if ( id === 'util' ) {
if ( id === 'util' ) {
return require . resolve ( './node_modules/util' ) ; // just 'utils' would resolve this to the built-in module
return require . resolve ( './node_modules/util' ) ; // just 'utils' would resolve this to the built-in module
}
}
}
} ,
} ,
} ,
resolve ( ) ,
resolve ( ) ,
commonjs ( {
commonjs ( {
@ -177,6 +154,7 @@ export default [
] ,
] ,
external : is _publish
external : is _publish
? [ ]
? [ ]
: id => id === 'acorn' || id === 'magic-string' || id . startsWith ( 'css-tree' )
: ( id ) =>
id === 'acorn' || id === 'magic-string' || id . startsWith ( 'css-tree' )
}
}
] ;
] ;