1
- import fs from 'fs' ;
1
+ import fs from 'node:fs' ;
2
+ import { createRequire } from 'node:module' ;
2
3
import replace from '@rollup/plugin-replace' ;
3
4
import resolve from '@rollup/plugin-node-resolve' ;
4
5
import commonjs from '@rollup/plugin-commonjs' ;
5
6
import json from '@rollup/plugin-json' ;
6
7
import sucrase from '@rollup/plugin-sucrase' ;
7
8
import 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' ) ) ;
9
12
10
13
const is_publish = ! ! process . env . PUBLISH ;
11
14
12
15
const ts_plugin = is_publish
13
16
? typescript ( {
14
- include : 'src/**' ,
15
17
typescript : require ( 'typescript' )
16
18
} )
17
19
: sucrase ( {
18
20
transforms : [ 'typescript' ]
19
21
} ) ;
20
22
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
+ }
22
56
23
57
fs . writeFileSync ( `./compiler.d.ts` , `export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index';` ) ;
24
58
@@ -30,12 +64,12 @@ export default [
30
64
{
31
65
file : `index.mjs` ,
32
66
format : 'esm' ,
33
- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '.' ) } / index.mjs`
67
+ paths : id => replace_relative_internal_import ( id , 'index.mjs' )
34
68
} ,
35
69
{
36
70
file : `index.js` ,
37
71
format : 'cjs' ,
38
- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '.' ) } / index.js`
72
+ paths : id => replace_relative_internal_import ( id , 'index.js' )
39
73
}
40
74
] ,
41
75
external,
@@ -48,12 +82,12 @@ export default [
48
82
{
49
83
file : `ssr.mjs` ,
50
84
format : 'esm' ,
51
- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '.' ) } / index.mjs`
85
+ paths : id => replace_relative_internal_import ( id , 'index.mjs' )
52
86
} ,
53
87
{
54
88
file : `ssr.js` ,
55
89
format : 'cjs' ,
56
- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '.' ) } / index.js`
90
+ paths : id => replace_relative_internal_import ( id , 'index.js' )
57
91
}
58
92
] ,
59
93
external,
@@ -68,12 +102,12 @@ export default [
68
102
{
69
103
file : `${ dir } /index.mjs` ,
70
104
format : 'esm' ,
71
- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '..' ) } / index.mjs`
105
+ paths : id => replace_relative_svelte_imports ( id , 'index.mjs' )
72
106
} ,
73
107
{
74
108
file : `${ dir } /index.js` ,
75
109
format : 'cjs' ,
76
- paths : id => id . startsWith ( 'svelte/' ) && ` ${ id . replace ( 'svelte' , '..' ) } / index.js`
110
+ paths : id => replace_relative_svelte_imports ( id , 'index.js' )
77
111
}
78
112
] ,
79
113
external,
@@ -83,7 +117,7 @@ export default [
83
117
} ) ,
84
118
ts_plugin ,
85
119
{
86
- writeBundle ( bundle ) {
120
+ writeBundle ( _options , bundle ) {
87
121
if ( dir === 'internal' ) {
88
122
const mod = bundle [ 'index.mjs' ] ;
89
123
if ( mod ) {
0 commit comments