1- import fs from 'fs' ;
1+ import fs from 'node:fs' ;
2+ import { createRequire } from 'node:module' ;
23import replace from '@rollup/plugin-replace' ;
34import resolve from '@rollup/plugin-node-resolve' ;
45import commonjs from '@rollup/plugin-commonjs' ;
56import json from '@rollup/plugin-json' ;
67import sucrase from '@rollup/plugin-sucrase' ;
78import typescript from '@rollup/plugin-typescript' ;
8- import pkg from './package.json' ;
9+
10+ const require = createRequire ( import . meta. url ) ;
11+ const pkg = JSON . parse ( fs . readFileSync ( 'package.json' , 'utf-8' ) ) ;
912
1013const is_publish = ! ! process . env . PUBLISH ;
1114
1215const ts_plugin = is_publish
1316 ? typescript ( {
14- include : 'src/**' ,
1517 typescript : require ( 'typescript' )
1618 } )
1719 : sucrase ( {
1820 transforms : [ 'typescript' ]
1921 } ) ;
2022
21- const external = id => id . startsWith ( 'svelte/' ) ;
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
25+
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+ }
47+
48+ /**
49+ * Transforms externalized `./internal` import path into correct relative import with correct index file extension import
50+ */
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+ }
2256
2357fs . writeFileSync ( `./compiler.d.ts` , `export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index';` ) ;
2458
@@ -30,12 +64,12 @@ export default [
3064 {
3165 file : `index.mjs` ,
3266 format : 'esm' ,
33- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '.' ) } / index.mjs`
67+ paths : id => replace_relative_internal_import ( id , 'index.mjs' )
3468 } ,
3569 {
3670 file : `index.js` ,
3771 format : 'cjs' ,
38- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '.' ) } / index.js`
72+ paths : id => replace_relative_internal_import ( id , 'index.js' )
3973 }
4074 ] ,
4175 external,
@@ -48,12 +82,12 @@ export default [
4882 {
4983 file : `ssr.mjs` ,
5084 format : 'esm' ,
51- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '.' ) } / index.mjs`
85+ paths : id => replace_relative_internal_import ( id , 'index.mjs' )
5286 } ,
5387 {
5488 file : `ssr.js` ,
5589 format : 'cjs' ,
56- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '.' ) } / index.js`
90+ paths : id => replace_relative_internal_import ( id , 'index.js' )
5791 }
5892 ] ,
5993 external,
@@ -68,12 +102,12 @@ export default [
68102 {
69103 file : `${ dir } /index.mjs` ,
70104 format : 'esm' ,
71- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '..' ) } / index.mjs`
105+ paths : id => replace_relative_svelte_imports ( id , 'index.mjs' )
72106 } ,
73107 {
74108 file : `${ dir } /index.js` ,
75109 format : 'cjs' ,
76- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '..' ) } / index.js`
110+ paths : id => replace_relative_svelte_imports ( id , 'index.js' )
77111 }
78112 ] ,
79113 external,
@@ -83,7 +117,7 @@ export default [
83117 } ) ,
84118 ts_plugin ,
85119 {
86- writeBundle ( bundle ) {
120+ writeBundle ( _options , bundle ) {
87121 if ( dir === 'internal' ) {
88122 const mod = bundle [ 'index.mjs' ] ;
89123 if ( mod ) {
0 commit comments