3
3
/* !! This project can't be ESM yet, so hack it to get sveltekit to work. !! */
4
4
const esbuild = require ( "esbuild" )
5
5
const path = require ( "path" )
6
+ const babelCore = require ( "@babel/core" )
7
+ const pirates = require ( "pirates" )
6
8
9
+ /* Build config */
7
10
esbuild . buildSync ( {
8
11
entryPoints : [ require . resolve ( "./svelte.config.esm.mjs" ) ] ,
9
12
outfile : path . join ( __dirname , "./svelte.config-dist.js" ) ,
@@ -16,12 +19,70 @@ esbuild.buildSync({
16
19
"./docs-svelte-kit/build-system/build.js" ,
17
20
] ,
18
21
} )
22
+
23
+ /* transpile */
19
24
const { register } = require ( "esbuild-register/dist/node" )
20
25
register ( {
21
26
format : "cjs" ,
22
27
extensions : [ ".js" , ".mjs" , ".cjs" , ".ts" ] ,
23
28
} )
24
29
30
+ /** transform '@sveltejs/kit/ssr' path */
31
+ pirates . addHook ( transformImportSsr , {
32
+ exts : [ ".js" , ".mjs" ] ,
33
+ } )
34
+
35
+ function transformImportSsr ( code , _filename ) {
36
+ if ( code . includes ( "@sveltejs/kit/ssr" ) ) {
37
+ const resolvedPath = require . resolve (
38
+ "./node_modules/@sveltejs/kit/dist/ssr" ,
39
+ )
40
+ let transformed = false
41
+ const newCode = babelCore . transformSync ( code , {
42
+ babelrc : false ,
43
+ plugins : [
44
+ {
45
+ visitor : {
46
+ CallExpression ( path ) {
47
+ const callee = path . get ( "callee" )
48
+ if (
49
+ callee . type !== "Identifier" ||
50
+ callee . node . name !== "require"
51
+ ) {
52
+ return
53
+ }
54
+ const args = path . get ( "arguments" )
55
+ if ( args . length !== 1 ) {
56
+ return
57
+ }
58
+ const arg = args [ 0 ]
59
+ if (
60
+ arg . type === "StringLiteral" &&
61
+ arg . node . value === "@sveltejs/kit/ssr"
62
+ ) {
63
+ arg . node . value = resolvedPath
64
+ transformed = true
65
+ }
66
+ } ,
67
+ ImportDeclaration ( path ) {
68
+ if ( path . node . source . value === "@sveltejs/kit/ssr" ) {
69
+ path . node . source . value = resolvedPath
70
+ transformed = true
71
+ }
72
+ } ,
73
+ } ,
74
+ } ,
75
+ ] ,
76
+ } )
77
+ if ( ! transformed ) {
78
+ return code
79
+ }
80
+ return `${ newCode . code } `
81
+ }
82
+
83
+ return code
84
+ }
85
+
25
86
/* eslint node/no-missing-require: 0 -- ignore */
26
87
const config = require ( "./svelte.config-dist.js" ) . default
27
88
0 commit comments