@@ -2,18 +2,17 @@ import { build, BuildOptions, Plugin } from 'esbuild';
22import { join } from 'path' ;
33import {
44 BuildConfig ,
5- banner ,
5+ getBanner ,
66 importPath ,
77 injectGlobalThisPoly ,
8- nodeBuiltIns ,
8+ injectGlobalPoly ,
99 nodeTarget ,
10- PackageJSON ,
1110 target ,
1211 watcher ,
13- injectGlobalPoly ,
1412} from './util' ;
1513import { inlineQwikScriptsEsBuild } from './submodule-qwikloader' ;
16- import { readPackageJson , writePackageJson } from './package-json' ;
14+ import { readPackageJson } from './package-json' ;
15+ import { readFileSync } from 'fs' ;
1716
1817/**
1918 * Builds @builder.io/server
@@ -25,69 +24,71 @@ export async function submoduleServer(config: BuildConfig) {
2524 const submodule = 'server' ;
2625
2726 const qwikDomPlugin = await bundleQwikDom ( config ) ;
27+ const qwikDomVersion = await getQwikDomVersion ( ) ;
2828
2929 const opts : BuildOptions = {
3030 entryPoints : [ join ( config . srcDir , submodule , 'index.ts' ) ] ,
31- outdir : join ( config . distPkgDir , submodule ) ,
31+ entryNames : 'server' ,
32+ outdir : config . distPkgDir ,
3233 sourcemap : config . dev ,
3334 bundle : true ,
3435 target,
35- banner,
36- external : [ ...nodeBuiltIns , '@builder.io/qwik-dom' ] ,
37- define : {
38- ...( await inlineQwikScriptsEsBuild ( config ) ) ,
39- 'globalThis.QWIK_VERSION' : JSON . stringify ( config . distVersion ) ,
40- 'globalThis.DOMINO_VERSION' : JSON . stringify ( await getDominoVersion ( ) ) ,
41- } ,
36+ external : [ /* no nodejs built-in externals allowed! */ '@builder.io/qwik-dom' ] ,
4237 } ;
4338
4439 const esm = build ( {
4540 ...opts ,
4641 format : 'esm' ,
42+ banner : { js : getBanner ( '@builder.io/qwik/server' ) } ,
4743 outExtension : { '.js' : '.mjs' } ,
48- plugins : [
49- importPath ( / ^ @ b u i l d e r \. i o \/ q w i k $ / , '../core.mjs' ) ,
50- importPath ( / ^ @ b u i l d e r \. i o \/ q w i k \/ o p t i m i z e r $ / , '../optimizer.mjs' ) ,
51- qwikDomPlugin ,
52- ] ,
44+ plugins : [ importPath ( / ^ @ b u i l d e r \. i o \/ q w i k $ / , './core.mjs' ) , qwikDomPlugin ] ,
5345 watch : watcher ( config , submodule ) ,
5446 inject : [ injectGlobalPoly ( config ) ] ,
47+ define : {
48+ ...( await inlineQwikScriptsEsBuild ( config ) ) ,
49+ 'globalThis.IS_CJS' : 'false' ,
50+ 'globalThis.IS_ESM' : 'true' ,
51+ 'globalThis.QWIK_VERSION' : JSON . stringify ( config . distVersion ) ,
52+ 'globalThis.QWIK_DOM_VERSION' : JSON . stringify ( qwikDomVersion ) ,
53+ } ,
5554 } ) ;
5655
56+ const cjsBanner = [
57+ getBanner ( '@builder.io/qwik/server' ) ,
58+ readFileSync ( injectGlobalThisPoly ( config ) , 'utf-8' ) ,
59+ readFileSync ( injectGlobalPoly ( config ) , 'utf-8' ) ,
60+ `globalThis.qwikServer = (function (module) {` ,
61+ browserCjsRequireShim ,
62+ ] . join ( '\n' ) ;
63+
5764 const cjs = build ( {
5865 ...opts ,
5966 format : 'cjs' ,
67+ banner : {
68+ js : cjsBanner ,
69+ } ,
70+ footer : {
71+ js : `return module.exports; })(typeof module === 'object' && module.exports ? module : { exports: {} });` ,
72+ } ,
6073 outExtension : { '.js' : '.cjs' } ,
61- plugins : [
62- importPath ( / ^ @ b u i l d e r \. i o \/ q w i k $ / , '../core.cjs' ) ,
63- importPath ( / ^ @ b u i l d e r \. i o \/ q w i k \/ o p t i m i z e r $ / , '../optimizer.cjs' ) ,
64- qwikDomPlugin ,
65- ] ,
74+ plugins : [ importPath ( / ^ @ b u i l d e r \. i o \/ q w i k $ / , './core.cjs' ) , qwikDomPlugin ] ,
6675 watch : watcher ( config ) ,
6776 platform : 'node' ,
6877 target : nodeTarget ,
69- inject : [ injectGlobalThisPoly ( config ) , injectGlobalPoly ( config ) ] ,
78+ define : {
79+ ...( await inlineQwikScriptsEsBuild ( config ) ) ,
80+ 'globalThis.IS_CJS' : 'true' ,
81+ 'globalThis.IS_ESM' : 'false' ,
82+ 'globalThis.QWIK_VERSION' : JSON . stringify ( config . distVersion ) ,
83+ 'globalThis.QWIK_DOM_VERSION' : JSON . stringify ( qwikDomVersion ) ,
84+ } ,
7085 } ) ;
7186
7287 await Promise . all ( [ esm , cjs ] ) ;
73- await generateServerPackageJson ( config ) ;
7488
7589 console . log ( '🐰' , submodule ) ;
7690}
7791
78- async function generateServerPackageJson ( config : BuildConfig ) {
79- const pkg : PackageJSON = {
80- name : '@builder.io/qwik/server' ,
81- version : config . distVersion ,
82- main : 'index.cjs' ,
83- module : 'index.mjs' ,
84- types : 'index.d.ts' ,
85- private : true ,
86- } ;
87- const serverDistDir = join ( config . distPkgDir , 'server' ) ;
88- await writePackageJson ( serverDistDir , pkg ) ;
89- }
90-
9192async function bundleQwikDom ( config : BuildConfig ) {
9293 const outfile = join ( config . distDir , 'qwikdom.mjs' ) ;
9394
@@ -104,7 +105,7 @@ async function bundleQwikDom(config: BuildConfig) {
104105 await build ( opts ) ;
105106
106107 const qwikDomPlugin : Plugin = {
107- name : 'domiqwikDomPluginnoPlugin ' ,
108+ name : 'qwikDomPlugin ' ,
108109 setup ( build ) {
109110 build . onResolve ( { filter : / @ b u i l d e r .i o \/ q w i k - d o m / } , ( ) => {
110111 return {
@@ -117,9 +118,23 @@ async function bundleQwikDom(config: BuildConfig) {
117118 return qwikDomPlugin ;
118119}
119120
120- async function getDominoVersion ( ) {
121+ async function getQwikDomVersion ( ) {
121122 const indexPath = require . resolve ( '@builder.io/qwik-dom' ) ;
122123 const pkgJsonPath = join ( indexPath , '..' , '..' ) ;
123124 const pkgJson = await readPackageJson ( pkgJsonPath ) ;
124125 return pkgJson . version ;
125126}
127+
128+ const browserCjsRequireShim = `
129+ if (typeof require !== 'function' && typeof location !== 'undefined' && typeof navigator !== 'undefined') {
130+ // shim cjs require() for core.cjs within a browser
131+ globalThis.require = function(path) {
132+ if (path === './core.cjs') {
133+ if (!self.qwikCore) {
134+ throw new Error('Qwik Core global, "globalThis.qwikCore", must already be loaded for the Qwik Server to be used within a browser.');
135+ }
136+ return self.qwikCore;
137+ }
138+ throw new Error('Unable to require() path "' + path + '" from a browser environment.');
139+ };
140+ }` ;
0 commit comments