@@ -2,7 +2,7 @@ import type { Plugin } from '@rolldown/browser';
2
2
import type { MinifyOptions } from 'terser' ;
3
3
import { minify } from 'terser' ;
4
4
import type { PkgUrls , ReplInputOptions } from '../types' ;
5
- import { QWIK_PKG_NAME } from '../repl-constants' ;
5
+ import { QWIK_PKG_NAME_V1 } from '../repl-constants' ;
6
6
7
7
export const definesPlugin = ( defines : Record < string , string > ) : Plugin => {
8
8
return {
@@ -35,29 +35,54 @@ export const replResolver = (
35
35
return srcInputs . find ( ( i ) => i . path === id ) ?. path ;
36
36
} ;
37
37
38
- return {
38
+ const getQwik = ( id : string , external ?: true ) => {
39
+ const path = deps [ QWIK_PKG_NAME_V1 ] [ id ] ;
40
+ if ( ! path ) {
41
+ throw new Error ( `Unknown Qwik path: ${ id } ` ) ;
42
+ }
43
+ return {
44
+ id : `\0@qwik${ id } ` ,
45
+ sideEffects : false ,
46
+ // It would be nice to load qwik as external, but
47
+ // we import core and core/build so we need processing
48
+ } ;
49
+ } ;
50
+ const plugin : Plugin = {
39
51
name : 'repl-resolver' ,
40
52
41
53
resolveId ( id , importer ) {
42
- // Re-resolve
43
- if ( id . startsWith ( '@qwik/' ) ) {
44
- return id ;
45
- }
46
- if (
47
- id === '@builder.io/qwik' ||
48
- id === '@builder.io/qwik/jsx-runtime' ||
49
- id === '@builder.io/qwik/jsx-dev-runtime'
50
- ) {
51
- return '@qwik/dist/core.mjs' ;
54
+ // Assets and vite dev mode
55
+ if ( id . startsWith ( '/assets/' ) || id . startsWith ( '/raw-fs/' ) ) {
56
+ return { id : new URL ( id , location . href ) . href , external : true } ;
52
57
}
53
- if ( id === '@builder.io/qwik/server' ) {
54
- return '@qwik/dist/server.mjs' ;
58
+ if ( id . startsWith ( 'http' ) ) {
59
+ return { id , external : true } ;
55
60
}
56
- if ( id . includes ( '@builder.io/ qwik/preloader ') ) {
57
- return '@qwik/dist/preloader.mjs' ;
61
+ if ( id . startsWith ( '\0@ qwik/') ) {
62
+ return id ;
58
63
}
59
- if ( id . includes ( '@builder.io/qwik/qwikloader' ) ) {
60
- return '@qwik/dist/qwikloader.js' ;
64
+ const match = id . match ( / ( @ b u i l d e r \. i o \/ q w i k | @ q w i k \. d e v \/ c o r e ) ( .* ) / ) ;
65
+ if ( match ) {
66
+ const pkgName = match [ 2 ] ;
67
+
68
+ if ( pkgName === '/build' ) {
69
+ return `\0@qwik/build` ;
70
+ }
71
+ if ( ! pkgName || pkgName === '/jsx-runtime' || pkgName === '/jsx-dev-runtime' ) {
72
+ return getQwik ( '/dist/core.mjs' ) ;
73
+ }
74
+ if ( pkgName === '/server' ) {
75
+ return getQwik ( '/dist/server.mjs' ) ;
76
+ }
77
+ if ( pkgName . includes ( '/preloader' ) ) {
78
+ return getQwik ( '/dist/preloader.mjs' ) ;
79
+ }
80
+ if ( pkgName . includes ( '/qwikloader' ) ) {
81
+ return getQwik ( '/dist/qwikloader.js' ) ;
82
+ }
83
+ if ( pkgName . includes ( '/handlers' ) ) {
84
+ return getQwik ( '/handlers.mjs' ) ;
85
+ }
61
86
}
62
87
// Simple relative file resolution
63
88
if ( / ^ [ . / ] / . test ( id ) ) {
@@ -82,23 +107,34 @@ export const replResolver = (
82
107
if ( input && typeof input . code === 'string' ) {
83
108
return input . code ;
84
109
}
85
- if ( id . startsWith ( '@qwik/' ) ) {
86
- const path = id . slice ( 5 ) ;
87
- const url = deps [ QWIK_PKG_NAME ] [ path ] ;
110
+ if ( id . startsWith ( '\0@qwik/' ) ) {
111
+ const path = id . slice ( '\0@qwik' . length ) ;
112
+ if ( path === '/build' ) {
113
+ // Virtual module for Qwik build
114
+ const isDev = options . buildMode === 'development' ;
115
+ const isServer = target === 'ssr' ;
116
+ return `
117
+ export const isDev = ${ isDev } ;
118
+ export const isServer = ${ isServer } ;
119
+ export const isClient = ${ ! isServer } ;
120
+ ` ;
121
+ }
122
+ const url = deps [ QWIK_PKG_NAME_V1 ] [ path ] ;
88
123
if ( url ) {
89
124
const rsp = await fetch ( url ) ;
90
125
if ( rsp . ok ) {
91
126
return rsp . text ( ) ;
92
127
}
93
128
}
94
- throw new Error ( `Unable to load Qwik module: ${ id } ` ) ;
129
+ throw new Error ( `Unable to load Qwik module: ${ path } ` ) ;
95
130
}
96
131
// We're the fallback, we know all the files
97
132
if ( / \. [ j t ] s x ? $ / . test ( id ) ) {
98
133
throw new Error ( `load: unknown module ${ id } ` ) ;
99
134
}
100
135
} ,
101
136
} ;
137
+ return plugin ;
102
138
} ;
103
139
104
140
export const replCss = ( options : Pick < ReplInputOptions , 'srcInputs' > ) : Plugin => {
0 commit comments