@quilted/rollup 0.2.13 → 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/build/esm/app.mjs +87 -66
- package/build/esm/index.mjs +3 -1
- package/build/esm/server.mjs +45 -12
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/typescript/app.d.ts +112 -28
- package/build/typescript/app.d.ts.map +1 -1
- package/build/typescript/index.d.ts +4 -2
- package/build/typescript/index.d.ts.map +1 -1
- package/build/typescript/server.d.ts +86 -25
- package/build/typescript/server.d.ts.map +1 -1
- package/build/typescript/shared/server.d.ts +35 -0
- package/build/typescript/shared/server.d.ts.map +1 -0
- package/package.json +2 -2
- package/source/app.ts +152 -91
- package/source/index.ts +13 -4
- package/source/server.ts +88 -32
- package/source/shared/server.ts +46 -0
- package/build/esm/features/request-router.mjs +0 -31
- package/source/features/request-router.ts +0 -34
package/source/app.ts
CHANGED
|
@@ -36,6 +36,9 @@ import {
|
|
|
36
36
|
type BrowserGroupTargetSelection,
|
|
37
37
|
} from './shared/browserslist.ts';
|
|
38
38
|
import {Project} from './shared/project.ts';
|
|
39
|
+
import type {ServerRuntime} from './shared/server.ts';
|
|
40
|
+
|
|
41
|
+
import type {NodeServerRuntimeOptions} from './server.ts';
|
|
39
42
|
|
|
40
43
|
export interface AppBaseOptions {
|
|
41
44
|
/**
|
|
@@ -91,6 +94,11 @@ export interface AppOptions extends AppBaseOptions {
|
|
|
91
94
|
*/
|
|
92
95
|
server?: Omit<AppServerOptions, keyof AppBaseOptions> &
|
|
93
96
|
Pick<AppServerOptions, 'env'>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Customizations to the application for the runtime it will execute in.
|
|
100
|
+
*/
|
|
101
|
+
runtime?: AppRuntime;
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
export interface AppBrowserOptions extends AppBaseOptions {
|
|
@@ -114,6 +122,11 @@ export interface AppBrowserOptions extends AppBaseOptions {
|
|
|
114
122
|
* Customizes the assets created for your application.
|
|
115
123
|
*/
|
|
116
124
|
assets?: AppBrowserAssetsOptions;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Customizations to the application for the runtime it will execute in.
|
|
128
|
+
*/
|
|
129
|
+
runtime?: Omit<AppRuntime, 'server'>;
|
|
117
130
|
}
|
|
118
131
|
|
|
119
132
|
export interface AppBrowserModuleOptions {
|
|
@@ -140,6 +153,7 @@ export interface AppBrowserAssetsOptions {
|
|
|
140
153
|
*/
|
|
141
154
|
minify?: boolean;
|
|
142
155
|
|
|
156
|
+
directory?: string;
|
|
143
157
|
baseURL?: string;
|
|
144
158
|
targets?: BrowserGroupTargetSelection;
|
|
145
159
|
priority?: number;
|
|
@@ -193,6 +207,11 @@ export interface AppServerOptions extends AppBaseOptions {
|
|
|
193
207
|
* Customizes the output files created for your server.
|
|
194
208
|
*/
|
|
195
209
|
output?: AppServerOutputOptions;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Customizations to the server for the runtime it will execute in.
|
|
213
|
+
*/
|
|
214
|
+
runtime?: AppServerRuntime;
|
|
196
215
|
}
|
|
197
216
|
|
|
198
217
|
export interface AppServerOutputOptions
|
|
@@ -214,13 +233,29 @@ export interface AppServerOutputOptions
|
|
|
214
233
|
* @default 'async-only'
|
|
215
234
|
*/
|
|
216
235
|
hash?: boolean | 'async-only';
|
|
236
|
+
}
|
|
217
237
|
|
|
238
|
+
export interface AppRuntime {
|
|
218
239
|
/**
|
|
219
|
-
*
|
|
220
|
-
|
|
221
|
-
|
|
240
|
+
* Overrides to the assets for this application.
|
|
241
|
+
*/
|
|
242
|
+
assets?: {
|
|
243
|
+
/**
|
|
244
|
+
* The directory to output the application’s assets into.
|
|
245
|
+
*/
|
|
246
|
+
directory?: string;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Customizations to the server for this runtime.
|
|
222
251
|
*/
|
|
223
|
-
|
|
252
|
+
server?: AppServerRuntime;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export interface AppServerRuntime extends Omit<ServerRuntime, 'requestRouter'> {
|
|
256
|
+
requestRouter?(options: {
|
|
257
|
+
assets: Required<Pick<AppBrowserAssetsOptions, 'baseURL'>>;
|
|
258
|
+
}): string;
|
|
224
259
|
}
|
|
225
260
|
|
|
226
261
|
export {
|
|
@@ -232,7 +267,7 @@ export {
|
|
|
232
267
|
|
|
233
268
|
const require = createRequire(import.meta.url);
|
|
234
269
|
|
|
235
|
-
export async function
|
|
270
|
+
export async function quiltApp({
|
|
236
271
|
root = process.cwd(),
|
|
237
272
|
app,
|
|
238
273
|
env,
|
|
@@ -240,6 +275,7 @@ export async function quiltAppOptions({
|
|
|
240
275
|
assets,
|
|
241
276
|
browser: browserOptions,
|
|
242
277
|
server: serverOptions,
|
|
278
|
+
runtime,
|
|
243
279
|
}: AppOptions = {}) {
|
|
244
280
|
const project = Project.load(root);
|
|
245
281
|
|
|
@@ -251,10 +287,11 @@ export async function quiltAppOptions({
|
|
|
251
287
|
|
|
252
288
|
browserGroupEntries.forEach(([name, browsers], index) => {
|
|
253
289
|
optionPromises.push(
|
|
254
|
-
|
|
290
|
+
quiltAppBrowser({
|
|
255
291
|
root: project.root,
|
|
256
292
|
app,
|
|
257
293
|
graphql,
|
|
294
|
+
runtime,
|
|
258
295
|
...browserOptions,
|
|
259
296
|
env: {
|
|
260
297
|
...resolveEnvOption(env),
|
|
@@ -274,10 +311,11 @@ export async function quiltAppOptions({
|
|
|
274
311
|
});
|
|
275
312
|
|
|
276
313
|
optionPromises.push(
|
|
277
|
-
|
|
314
|
+
quiltAppServer({
|
|
278
315
|
root: project.root,
|
|
279
316
|
app,
|
|
280
317
|
graphql,
|
|
318
|
+
runtime: runtime?.server,
|
|
281
319
|
...serverOptions,
|
|
282
320
|
env: {
|
|
283
321
|
...resolveEnvOption(env),
|
|
@@ -290,12 +328,12 @@ export async function quiltAppOptions({
|
|
|
290
328
|
return Promise.all(optionPromises);
|
|
291
329
|
}
|
|
292
330
|
|
|
293
|
-
export async function
|
|
294
|
-
const {root = process.cwd(), assets} = options;
|
|
331
|
+
export async function quiltAppBrowser(options: AppBrowserOptions = {}) {
|
|
332
|
+
const {root = process.cwd(), assets, runtime} = options;
|
|
295
333
|
const project = Project.load(root);
|
|
296
334
|
|
|
297
335
|
const [plugins, browserGroup] = await Promise.all([
|
|
298
|
-
|
|
336
|
+
quiltAppBrowserPlugins(options),
|
|
299
337
|
getBrowserGroupTargetDetails(assets?.targets, {
|
|
300
338
|
root: project.root,
|
|
301
339
|
}),
|
|
@@ -311,7 +349,9 @@ export async function quiltAppBrowserOptions(options: AppBrowserOptions = {}) {
|
|
|
311
349
|
plugins,
|
|
312
350
|
output: {
|
|
313
351
|
format: isESM ? 'esm' : 'systemjs',
|
|
314
|
-
dir: project.resolve(
|
|
352
|
+
dir: project.resolve(
|
|
353
|
+
assets?.directory ?? runtime?.assets?.directory ?? `build/assets`,
|
|
354
|
+
),
|
|
315
355
|
entryFileNames: `[name]${targetFilenamePart}.[hash].js`,
|
|
316
356
|
assetFileNames: `[name]${targetFilenamePart}.[hash].[ext]`,
|
|
317
357
|
chunkFileNames: `[name]${targetFilenamePart}.[hash].js`,
|
|
@@ -322,7 +362,7 @@ export async function quiltAppBrowserOptions(options: AppBrowserOptions = {}) {
|
|
|
322
362
|
} satisfies RollupOptions;
|
|
323
363
|
}
|
|
324
364
|
|
|
325
|
-
export async function
|
|
365
|
+
export async function quiltAppBrowserPlugins({
|
|
326
366
|
root = process.cwd(),
|
|
327
367
|
app,
|
|
328
368
|
entry,
|
|
@@ -510,20 +550,22 @@ export function quiltAppBrowserInput({
|
|
|
510
550
|
} satisfies Plugin;
|
|
511
551
|
}
|
|
512
552
|
|
|
513
|
-
export async function
|
|
514
|
-
const {
|
|
553
|
+
export async function quiltAppServer(options: AppServerOptions = {}) {
|
|
554
|
+
const {
|
|
555
|
+
output,
|
|
556
|
+
root = process.cwd(),
|
|
557
|
+
runtime = nodeAppServerRuntime(),
|
|
558
|
+
} = options;
|
|
515
559
|
|
|
516
560
|
const project = Project.load(root);
|
|
517
561
|
const hash = output?.hash ?? 'async-only';
|
|
518
|
-
const outputFormat = output?.format ?? 'esmodules';
|
|
519
562
|
|
|
520
|
-
const plugins = await
|
|
563
|
+
const plugins = await quiltAppServerPlugins({...options, root, runtime});
|
|
521
564
|
|
|
522
565
|
return {
|
|
523
566
|
plugins,
|
|
524
567
|
output: {
|
|
525
|
-
format:
|
|
526
|
-
outputFormat === 'commonjs' || outputFormat === 'cjs' ? 'cjs' : 'esm',
|
|
568
|
+
format: 'esm',
|
|
527
569
|
dir: project.resolve(`build/server`),
|
|
528
570
|
entryFileNames: `[name]${hash === true ? `.[hash]` : ''}.js`,
|
|
529
571
|
chunkFileNames: `[name]${
|
|
@@ -531,11 +573,12 @@ export async function quiltAppServerOptions(options: AppServerOptions = {}) {
|
|
|
531
573
|
}.js`,
|
|
532
574
|
assetFileNames: `[name]${hash === true ? `.[hash]` : ''}.[ext]`,
|
|
533
575
|
generatedCode: 'es2015',
|
|
576
|
+
...runtime.output?.options,
|
|
534
577
|
},
|
|
535
578
|
} satisfies RollupOptions;
|
|
536
579
|
}
|
|
537
580
|
|
|
538
|
-
export async function
|
|
581
|
+
export async function quiltAppServerPlugins({
|
|
539
582
|
root = process.cwd(),
|
|
540
583
|
app,
|
|
541
584
|
env,
|
|
@@ -544,6 +587,7 @@ export async function quiltAppServer({
|
|
|
544
587
|
graphql = true,
|
|
545
588
|
assets,
|
|
546
589
|
output,
|
|
590
|
+
runtime,
|
|
547
591
|
}: AppServerOptions = {}) {
|
|
548
592
|
const project = Project.load(root);
|
|
549
593
|
const mode = (typeof env === 'object' ? env?.mode : env) ?? 'production';
|
|
@@ -584,10 +628,20 @@ export async function quiltAppServer({
|
|
|
584
628
|
quiltAppServerInput({root: project.root, entry, format}),
|
|
585
629
|
...nodePlugins,
|
|
586
630
|
replaceProcessEnv({mode}),
|
|
587
|
-
magicModuleEnv({...resolveEnvOption(env), mode}),
|
|
631
|
+
magicModuleEnv({runtime: runtime?.env, ...resolveEnvOption(env), mode}),
|
|
588
632
|
magicModuleAppComponent({entry: app, root: project.root}),
|
|
589
|
-
|
|
590
|
-
|
|
633
|
+
createMagicModulePlugin({
|
|
634
|
+
name: '@quilted/request-router',
|
|
635
|
+
sideEffects: true,
|
|
636
|
+
module: MAGIC_MODULE_ENTRY,
|
|
637
|
+
source() {
|
|
638
|
+
const options = {assets: {baseURL}};
|
|
639
|
+
|
|
640
|
+
return (
|
|
641
|
+
runtime?.requestRouter?.(options) ??
|
|
642
|
+
nodeAppServerRuntime().requestRouter(options)
|
|
643
|
+
);
|
|
644
|
+
},
|
|
591
645
|
}),
|
|
592
646
|
magicModuleAppRequestRouter({entry, root: project.root}),
|
|
593
647
|
magicModuleAppAssetManifests(),
|
|
@@ -694,6 +748,82 @@ export function quiltAppServerInput({
|
|
|
694
748
|
} satisfies Plugin;
|
|
695
749
|
}
|
|
696
750
|
|
|
751
|
+
export interface NodeAppServerRuntimeOptions extends NodeServerRuntimeOptions {
|
|
752
|
+
/**
|
|
753
|
+
* Whether the server should serve assets from the asset output directory.
|
|
754
|
+
*
|
|
755
|
+
* @default true
|
|
756
|
+
*/
|
|
757
|
+
assets?: boolean;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
export function nodeAppServerRuntime({
|
|
761
|
+
host,
|
|
762
|
+
port,
|
|
763
|
+
format = 'module',
|
|
764
|
+
assets: serveAssets = true,
|
|
765
|
+
}: NodeAppServerRuntimeOptions = {}) {
|
|
766
|
+
const rollupFormat =
|
|
767
|
+
format === 'commonjs' || format === 'cjs' ? 'cjs' : 'esm';
|
|
768
|
+
|
|
769
|
+
return {
|
|
770
|
+
env: 'process.env',
|
|
771
|
+
output: {
|
|
772
|
+
options: {
|
|
773
|
+
format: rollupFormat,
|
|
774
|
+
},
|
|
775
|
+
},
|
|
776
|
+
requestRouter({assets}) {
|
|
777
|
+
const {baseURL} = assets;
|
|
778
|
+
|
|
779
|
+
return multiline`
|
|
780
|
+
${serveAssets ? `import * as path from 'path';` : ''}
|
|
781
|
+
${rollupFormat === 'cjs' ? '' : `import {fileURLToPath} from 'url';`}
|
|
782
|
+
import {createServer} from 'http';
|
|
783
|
+
|
|
784
|
+
import requestRouter from ${JSON.stringify(
|
|
785
|
+
MAGIC_MODULE_REQUEST_ROUTER,
|
|
786
|
+
)};
|
|
787
|
+
|
|
788
|
+
import {createHttpRequestListener${
|
|
789
|
+
serveAssets ? ', serveStatic' : ''
|
|
790
|
+
}} from '@quilted/quilt/request-router/node';
|
|
791
|
+
|
|
792
|
+
const port = ${port ?? 'Number.parseInt(process.env.PORT, 10)'};
|
|
793
|
+
const host = ${host ? JSON.stringify(host) : 'process.env.HOST'};
|
|
794
|
+
|
|
795
|
+
${
|
|
796
|
+
serveAssets
|
|
797
|
+
? multiline`
|
|
798
|
+
const dirname = ${
|
|
799
|
+
rollupFormat === 'cjs'
|
|
800
|
+
? `__dirname`
|
|
801
|
+
: `path.dirname(fileURLToPath(import.meta.url))`
|
|
802
|
+
};
|
|
803
|
+
const serve = serveStatic(path.resolve(dirname, '../assets'), {
|
|
804
|
+
baseUrl: ${JSON.stringify(baseURL)},
|
|
805
|
+
});
|
|
806
|
+
`
|
|
807
|
+
: ''
|
|
808
|
+
}
|
|
809
|
+
const listener = createHttpRequestListener(requestRouter);
|
|
810
|
+
|
|
811
|
+
createServer(async (request, response) => {
|
|
812
|
+
${
|
|
813
|
+
serveAssets
|
|
814
|
+
? `if (request.url.startsWith(${JSON.stringify(
|
|
815
|
+
baseURL,
|
|
816
|
+
)})) return serve(request, response);`
|
|
817
|
+
: ''
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
await listener(request, response);
|
|
821
|
+
}).listen(port, host);
|
|
822
|
+
`;
|
|
823
|
+
},
|
|
824
|
+
} satisfies AppServerRuntime;
|
|
825
|
+
}
|
|
826
|
+
|
|
697
827
|
export function magicModuleAppComponent({
|
|
698
828
|
entry,
|
|
699
829
|
root = process.cwd(),
|
|
@@ -818,75 +948,6 @@ export function magicModuleAppBrowserEntry({
|
|
|
818
948
|
});
|
|
819
949
|
}
|
|
820
950
|
|
|
821
|
-
export function magicModuleAppServerEntry({
|
|
822
|
-
host,
|
|
823
|
-
port,
|
|
824
|
-
assets,
|
|
825
|
-
format = 'module',
|
|
826
|
-
}: {
|
|
827
|
-
host?: string;
|
|
828
|
-
port?: number;
|
|
829
|
-
assets?: boolean | {baseURL: string};
|
|
830
|
-
format?: 'module' | 'commonjs';
|
|
831
|
-
} = {}) {
|
|
832
|
-
const baseURL = typeof assets === 'object' ? assets.baseURL : '/assets/';
|
|
833
|
-
|
|
834
|
-
return createMagicModulePlugin({
|
|
835
|
-
name: '@quilted/request-router/app-server',
|
|
836
|
-
module: MAGIC_MODULE_ENTRY,
|
|
837
|
-
sideEffects: true,
|
|
838
|
-
async source() {
|
|
839
|
-
const serveAssets = Boolean(assets);
|
|
840
|
-
|
|
841
|
-
return multiline`
|
|
842
|
-
${serveAssets ? `import * as path from 'path';` : ''}
|
|
843
|
-
${
|
|
844
|
-
serveAssets && format === 'module'
|
|
845
|
-
? `import {fileURLToPath} from 'url';`
|
|
846
|
-
: ''
|
|
847
|
-
}
|
|
848
|
-
import {createServer} from 'http';
|
|
849
|
-
|
|
850
|
-
import requestRouter from ${JSON.stringify(
|
|
851
|
-
MAGIC_MODULE_REQUEST_ROUTER,
|
|
852
|
-
)};
|
|
853
|
-
|
|
854
|
-
import {createHttpRequestListener${
|
|
855
|
-
serveAssets ? ', serveStatic' : ''
|
|
856
|
-
}} from '@quilted/quilt/request-router/node';
|
|
857
|
-
|
|
858
|
-
const port = ${port ?? 'Number.parseInt(process.env.PORT, 10)'};
|
|
859
|
-
const host = ${host ? JSON.stringify(host) : 'process.env.HOST'};
|
|
860
|
-
|
|
861
|
-
${
|
|
862
|
-
serveAssets
|
|
863
|
-
? `const dirname = ${
|
|
864
|
-
format === 'module'
|
|
865
|
-
? 'path.dirname(fileURLToPath(import.meta.url))'
|
|
866
|
-
: '__dirname'
|
|
867
|
-
};\nconst serve = serveStatic(path.resolve(dirname, '../assets'), {
|
|
868
|
-
baseUrl: ${JSON.stringify(baseURL)},
|
|
869
|
-
});`
|
|
870
|
-
: ''
|
|
871
|
-
}
|
|
872
|
-
const listener = createHttpRequestListener(requestRouter);
|
|
873
|
-
|
|
874
|
-
createServer(async (request, response) => {
|
|
875
|
-
${
|
|
876
|
-
serveAssets
|
|
877
|
-
? `if (request.url.startsWith(${JSON.stringify(
|
|
878
|
-
baseURL,
|
|
879
|
-
)})) return serve(request, response);`
|
|
880
|
-
: ''
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
await listener(request, response);
|
|
884
|
-
}).listen(port, host);
|
|
885
|
-
`;
|
|
886
|
-
},
|
|
887
|
-
});
|
|
888
|
-
}
|
|
889
|
-
|
|
890
951
|
export function magicModuleAppAssetManifests() {
|
|
891
952
|
return createMagicModulePlugin({
|
|
892
953
|
name: '@quilted/magic-module/asset-manifests',
|
package/source/index.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
export {
|
|
2
|
-
|
|
2
|
+
quiltApp,
|
|
3
3
|
quiltAppBrowser,
|
|
4
|
-
|
|
4
|
+
quiltAppBrowserPlugins,
|
|
5
5
|
quiltAppServer,
|
|
6
|
-
|
|
6
|
+
quiltAppServerPlugins,
|
|
7
7
|
type AppOptions,
|
|
8
8
|
type AppBaseOptions,
|
|
9
9
|
type AppBrowserOptions,
|
|
10
10
|
type AppServerOptions,
|
|
11
|
+
type AppRuntime,
|
|
12
|
+
type AppServerRuntime,
|
|
11
13
|
} from './app.ts';
|
|
12
14
|
export {quiltModule, type ModuleOptions} from './module.ts';
|
|
13
15
|
export {
|
|
@@ -16,4 +18,11 @@ export {
|
|
|
16
18
|
quiltPackageESNext,
|
|
17
19
|
type PackageOptions,
|
|
18
20
|
} from './package.ts';
|
|
19
|
-
export {
|
|
21
|
+
export {
|
|
22
|
+
quiltServer,
|
|
23
|
+
type ServerOptions,
|
|
24
|
+
type ServerOutputOptions,
|
|
25
|
+
type ServerRuntime,
|
|
26
|
+
} from './server.ts';
|
|
27
|
+
export {multiline} from './shared/strings.ts';
|
|
28
|
+
export * from './constants.ts';
|
package/source/server.ts
CHANGED
|
@@ -7,10 +7,11 @@ import {
|
|
|
7
7
|
removeBuildFiles,
|
|
8
8
|
} from './shared/rollup.ts';
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import {multiline} from './shared/strings.ts';
|
|
11
11
|
import {resolveEnvOption, type MagicModuleEnvOptions} from './features/env.ts';
|
|
12
12
|
import {MAGIC_MODULE_ENTRY, MAGIC_MODULE_REQUEST_ROUTER} from './constants.ts';
|
|
13
13
|
import {createMagicModulePlugin} from './shared/magic-module.ts';
|
|
14
|
+
import type {ServerRuntime} from './shared/server.ts';
|
|
14
15
|
|
|
15
16
|
export interface ServerOptions {
|
|
16
17
|
/**
|
|
@@ -60,25 +61,20 @@ export interface ServerOptions {
|
|
|
60
61
|
output?: ServerOutputOptions;
|
|
61
62
|
|
|
62
63
|
/**
|
|
63
|
-
*
|
|
64
|
-
* when you use the `request-router` format — if you use the `custom` format,
|
|
65
|
-
* you are responsible for starting the server yourself.
|
|
66
|
-
*
|
|
67
|
-
* If you do not provide a value here, the server will listen for requests on
|
|
68
|
-
* the port specified by `process.env.NODE_ENV`.
|
|
69
|
-
*/
|
|
70
|
-
port?: number | string;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The host that the server will listen on when it runs. This only applies
|
|
74
|
-
* when you use the `request-router` format — if you use the `custom` format,
|
|
75
|
-
* you are responsible for starting the server yourself.
|
|
64
|
+
* Customizations to the server for the runtime it will execute in.
|
|
76
65
|
*/
|
|
77
|
-
|
|
66
|
+
runtime?: ServerRuntime;
|
|
78
67
|
}
|
|
79
68
|
|
|
80
69
|
export interface ServerOutputOptions
|
|
81
70
|
extends Pick<RollupNodePluginOptions, 'bundle'> {
|
|
71
|
+
/**
|
|
72
|
+
* The directory to output the server into.
|
|
73
|
+
*
|
|
74
|
+
* @default 'build/server'
|
|
75
|
+
*/
|
|
76
|
+
directory?: string;
|
|
77
|
+
|
|
82
78
|
/**
|
|
83
79
|
* Whether to minify assets created for this server.
|
|
84
80
|
*
|
|
@@ -96,15 +92,9 @@ export interface ServerOutputOptions
|
|
|
96
92
|
* @default 'async-only'
|
|
97
93
|
*/
|
|
98
94
|
hash?: boolean | 'async-only';
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* What module format to use for the server output.
|
|
102
|
-
*
|
|
103
|
-
* @default 'esmodules'
|
|
104
|
-
*/
|
|
105
|
-
format?: 'esmodules' | 'esm' | 'es' | 'commonjs' | 'cjs';
|
|
106
95
|
}
|
|
107
96
|
|
|
97
|
+
export type {ServerRuntime};
|
|
108
98
|
export {MAGIC_MODULE_ENTRY, MAGIC_MODULE_REQUEST_ROUTER};
|
|
109
99
|
|
|
110
100
|
export async function quiltServer({
|
|
@@ -114,18 +104,18 @@ export async function quiltServer({
|
|
|
114
104
|
env,
|
|
115
105
|
graphql = true,
|
|
116
106
|
output,
|
|
117
|
-
|
|
118
|
-
host,
|
|
107
|
+
runtime = nodeServerRuntime(),
|
|
119
108
|
}: ServerOptions = {}) {
|
|
120
109
|
const project = Project.load(rootPath);
|
|
121
110
|
const mode =
|
|
122
111
|
(typeof env === 'object' ? env?.mode : undefined) ?? 'production';
|
|
123
|
-
const outputDirectory = project.resolve(
|
|
112
|
+
const outputDirectory = project.resolve(
|
|
113
|
+
output?.directory ?? runtime.output?.directory ?? 'build/server',
|
|
114
|
+
);
|
|
124
115
|
|
|
125
116
|
const minify = output?.minify ?? false;
|
|
126
117
|
const bundle = output?.bundle;
|
|
127
118
|
const hash = output?.hash ?? 'async-only';
|
|
128
|
-
const outputFormat = output?.format ?? 'esmodules';
|
|
129
119
|
|
|
130
120
|
const [
|
|
131
121
|
{visualizer},
|
|
@@ -159,7 +149,7 @@ export async function quiltServer({
|
|
|
159
149
|
const plugins: InputPluginOption[] = [
|
|
160
150
|
...nodePlugins,
|
|
161
151
|
replaceProcessEnv({mode}),
|
|
162
|
-
magicModuleEnv({...resolveEnvOption(env), mode}),
|
|
152
|
+
magicModuleEnv({runtime: runtime.env, ...resolveEnvOption(env), mode}),
|
|
163
153
|
sourceCode({mode, targets: ['current node']}),
|
|
164
154
|
tsconfigAliases({root: project.root}),
|
|
165
155
|
monorepoPackageAliases({root: project.root}),
|
|
@@ -175,9 +165,15 @@ export async function quiltServer({
|
|
|
175
165
|
module: MAGIC_MODULE_REQUEST_ROUTER,
|
|
176
166
|
alias: serverEntry,
|
|
177
167
|
}),
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
168
|
+
createMagicModulePlugin({
|
|
169
|
+
name: '@quilted/request-router',
|
|
170
|
+
sideEffects: true,
|
|
171
|
+
module: MAGIC_MODULE_ENTRY,
|
|
172
|
+
source() {
|
|
173
|
+
return (
|
|
174
|
+
runtime.requestRouter?.() ?? nodeServerRuntime().requestRouter()
|
|
175
|
+
);
|
|
176
|
+
},
|
|
181
177
|
}),
|
|
182
178
|
);
|
|
183
179
|
}
|
|
@@ -206,8 +202,7 @@ export async function quiltServer({
|
|
|
206
202
|
finalEntry === MAGIC_MODULE_ENTRY ? {server: finalEntry} : finalEntry,
|
|
207
203
|
plugins,
|
|
208
204
|
output: {
|
|
209
|
-
format:
|
|
210
|
-
outputFormat === 'commonjs' || outputFormat === 'cjs' ? 'cjs' : 'esm',
|
|
205
|
+
format: 'esm',
|
|
211
206
|
dir: outputDirectory,
|
|
212
207
|
entryFileNames: `[name]${hash === true ? `.[hash]` : ''}.js`,
|
|
213
208
|
chunkFileNames: `[name]${
|
|
@@ -215,10 +210,71 @@ export async function quiltServer({
|
|
|
215
210
|
}.js`,
|
|
216
211
|
assetFileNames: `[name]${hash === true ? `.[hash]` : ''}.[ext]`,
|
|
217
212
|
generatedCode: 'es2015',
|
|
213
|
+
...runtime.output?.options,
|
|
218
214
|
},
|
|
219
215
|
} satisfies RollupOptions;
|
|
220
216
|
}
|
|
221
217
|
|
|
218
|
+
export interface NodeServerRuntimeOptions {
|
|
219
|
+
/**
|
|
220
|
+
* The port that the server will listen on when it runs. This only applies
|
|
221
|
+
* when you use the `request-router` format — if you use the `custom` format,
|
|
222
|
+
* you are responsible for starting the server yourself.
|
|
223
|
+
*
|
|
224
|
+
* If you do not provide a value here, the server will listen for requests on
|
|
225
|
+
* the port specified by `process.env.NODE_ENV`.
|
|
226
|
+
*/
|
|
227
|
+
port?: number | string;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The host that the server will listen on when it runs.
|
|
231
|
+
*/
|
|
232
|
+
host?: string;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* What module format to use for the server output.
|
|
236
|
+
*
|
|
237
|
+
* @default 'module'
|
|
238
|
+
*/
|
|
239
|
+
format?:
|
|
240
|
+
| 'module'
|
|
241
|
+
| 'modules'
|
|
242
|
+
| 'esmodules'
|
|
243
|
+
| 'esm'
|
|
244
|
+
| 'es'
|
|
245
|
+
| 'commonjs'
|
|
246
|
+
| 'cjs';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function nodeServerRuntime({
|
|
250
|
+
host,
|
|
251
|
+
port,
|
|
252
|
+
format = 'module',
|
|
253
|
+
}: NodeServerRuntimeOptions = {}) {
|
|
254
|
+
return {
|
|
255
|
+
env: 'process.env',
|
|
256
|
+
output: {
|
|
257
|
+
options: {
|
|
258
|
+
format: format === 'commonjs' || format === 'cjs' ? 'cjs' : 'esm',
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
requestRouter() {
|
|
262
|
+
return multiline`
|
|
263
|
+
import requestRouter from ${JSON.stringify(
|
|
264
|
+
MAGIC_MODULE_REQUEST_ROUTER,
|
|
265
|
+
)};
|
|
266
|
+
|
|
267
|
+
import {createHttpServer} from '@quilted/quilt/request-router/node';
|
|
268
|
+
|
|
269
|
+
const port = ${port ?? 'Number.parseInt(process.env.PORT, 10)'};
|
|
270
|
+
const host = ${host ? JSON.stringify(host) : 'process.env.HOST'};
|
|
271
|
+
|
|
272
|
+
createHttpServer(requestRouter).listen(port, host);
|
|
273
|
+
`;
|
|
274
|
+
},
|
|
275
|
+
} satisfies ServerRuntime;
|
|
276
|
+
}
|
|
277
|
+
|
|
222
278
|
async function sourceForServer(project: Project) {
|
|
223
279
|
const {main, exports} = project.packageJSON.raw;
|
|
224
280
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type {OutputOptions} from 'rollup';
|
|
2
|
+
|
|
3
|
+
export interface ServerRuntime {
|
|
4
|
+
/**
|
|
5
|
+
* A string that will be inlined directly as code to reference a runtime constant
|
|
6
|
+
* that contains environment variables.
|
|
7
|
+
*/
|
|
8
|
+
env?: string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Overrides to the output options for this server.
|
|
12
|
+
*/
|
|
13
|
+
output?: {
|
|
14
|
+
/**
|
|
15
|
+
* What module format to use for the server output.
|
|
16
|
+
*
|
|
17
|
+
* @default 'module'
|
|
18
|
+
*/
|
|
19
|
+
format?:
|
|
20
|
+
| 'module'
|
|
21
|
+
| 'modules'
|
|
22
|
+
| 'esmodules'
|
|
23
|
+
| 'esm'
|
|
24
|
+
| 'es'
|
|
25
|
+
| 'commonjs'
|
|
26
|
+
| 'cjs';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The directory to output the server to.
|
|
30
|
+
*/
|
|
31
|
+
directory?: string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Overrides to the Rollup output options.
|
|
35
|
+
*/
|
|
36
|
+
options?: OutputOptions;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The content to use as the entry point when the server uses the `request-router`
|
|
41
|
+
* format for their server. This file should import the request router instance for
|
|
42
|
+
* this app from 'quilt:module/request-router', and create a server that is appropriate
|
|
43
|
+
* for this runtime.
|
|
44
|
+
*/
|
|
45
|
+
requestRouter?(): string;
|
|
46
|
+
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { MAGIC_MODULE_ENTRY, MAGIC_MODULE_REQUEST_ROUTER } from '../constants.mjs';
|
|
2
|
-
import { createMagicModulePlugin } from '../shared/magic-module.mjs';
|
|
3
|
-
import { multiline } from '../shared/strings.mjs';
|
|
4
|
-
|
|
5
|
-
function magicModuleRequestRouterEntry({
|
|
6
|
-
host,
|
|
7
|
-
port
|
|
8
|
-
} = {}) {
|
|
9
|
-
return createMagicModulePlugin({
|
|
10
|
-
name: "@quilted/request-router",
|
|
11
|
-
sideEffects: true,
|
|
12
|
-
module: MAGIC_MODULE_ENTRY,
|
|
13
|
-
async source() {
|
|
14
|
-
const initialContent = multiline`
|
|
15
|
-
import requestRouter from ${JSON.stringify(
|
|
16
|
-
MAGIC_MODULE_REQUEST_ROUTER
|
|
17
|
-
)};
|
|
18
|
-
|
|
19
|
-
import {createHttpServer} from '@quilted/quilt/request-router/node';
|
|
20
|
-
|
|
21
|
-
const port = ${port ?? "Number.parseInt(process.env.PORT, 10)"};
|
|
22
|
-
const host = ${host ? JSON.stringify(host) : "process.env.HOST"};
|
|
23
|
-
|
|
24
|
-
createHttpServer(requestRouter).listen(port, host);
|
|
25
|
-
`;
|
|
26
|
-
return initialContent;
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export { magicModuleRequestRouterEntry };
|