@quilted/rollup 0.0.0-preview-20231014022518
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 +13 -0
- package/LICENSE.md +21 -0
- package/README.md +1 -0
- package/build/cjs/app.cjs +158 -0
- package/build/cjs/constants.cjs +13 -0
- package/build/cjs/env.cjs +135 -0
- package/build/cjs/index.cjs +16 -0
- package/build/cjs/request-router.cjs +31 -0
- package/build/cjs/shared/magic-module.cjs +32 -0
- package/build/cjs/shared/rollup.cjs +15 -0
- package/build/cjs/shared/strings.cjs +16 -0
- package/build/esm/app.mjs +151 -0
- package/build/esm/constants.mjs +7 -0
- package/build/esm/env.mjs +112 -0
- package/build/esm/index.mjs +3 -0
- package/build/esm/request-router.mjs +29 -0
- package/build/esm/shared/magic-module.mjs +30 -0
- package/build/esm/shared/rollup.mjs +13 -0
- package/build/esm/shared/strings.mjs +14 -0
- package/build/esnext/app.esnext +151 -0
- package/build/esnext/constants.esnext +7 -0
- package/build/esnext/env.esnext +112 -0
- package/build/esnext/index.esnext +3 -0
- package/build/esnext/request-router.esnext +29 -0
- package/build/esnext/shared/magic-module.esnext +30 -0
- package/build/esnext/shared/rollup.esnext +13 -0
- package/build/esnext/shared/strings.esnext +14 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/build/typescript/app.d.ts +163 -0
- package/build/typescript/app.d.ts.map +1 -0
- package/build/typescript/constants.d.ts +6 -0
- package/build/typescript/constants.d.ts.map +1 -0
- package/build/typescript/env.d.ts +55 -0
- package/build/typescript/env.d.ts.map +1 -0
- package/build/typescript/index.d.ts +4 -0
- package/build/typescript/index.d.ts.map +1 -0
- package/build/typescript/request-router.d.ts +15 -0
- package/build/typescript/request-router.d.ts.map +1 -0
- package/build/typescript/shared/magic-module.d.ts +19 -0
- package/build/typescript/shared/magic-module.d.ts.map +1 -0
- package/build/typescript/shared/rollup.d.ts +5 -0
- package/build/typescript/shared/rollup.d.ts.map +1 -0
- package/build/typescript/shared/strings.d.ts +2 -0
- package/build/typescript/shared/strings.d.ts.map +1 -0
- package/package.json +53 -0
- package/quilt.project.ts +5 -0
- package/source/app.ts +225 -0
- package/source/constants.ts +5 -0
- package/source/env.ts +176 -0
- package/source/index.ts +13 -0
- package/source/request-router.ts +34 -0
- package/source/shared/magic-module.ts +42 -0
- package/source/shared/rollup.ts +38 -0
- package/source/shared/strings.ts +17 -0
- package/tsconfig.json +10 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @quilted/rollup
|
|
2
|
+
|
|
3
|
+
## 0.0.0-preview-20231014022518
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Fix initial rollup plugins
|
|
8
|
+
|
|
9
|
+
## 0.1.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [`513f349a`](https://github.com/lemonmade/quilt/commit/513f349a4415af3fd4f6848532ea32a59385512c) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix initial rollup plugins
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019-present, Chris Sauvé and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# `@quilted/rollup`
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var constants = require('./constants.cjs');
|
|
4
|
+
var strings = require('./shared/strings.cjs');
|
|
5
|
+
var magicModule = require('./shared/magic-module.cjs');
|
|
6
|
+
|
|
7
|
+
function quiltApp({
|
|
8
|
+
env,
|
|
9
|
+
entry
|
|
10
|
+
} = {}) {
|
|
11
|
+
return {
|
|
12
|
+
name: '@quilted/app',
|
|
13
|
+
async options(originalOptions) {
|
|
14
|
+
const newPlugins = [...(Array.isArray(originalOptions.plugins) ? originalOptions.plugins : originalOptions.plugins ? [originalOptions.plugins] : [])];
|
|
15
|
+
const newOptions = {
|
|
16
|
+
...originalOptions,
|
|
17
|
+
plugins: newPlugins
|
|
18
|
+
};
|
|
19
|
+
if (env) {
|
|
20
|
+
const {
|
|
21
|
+
magicModuleEnv,
|
|
22
|
+
replaceProcessEnv
|
|
23
|
+
} = await Promise.resolve().then(function () { return require('./env.cjs'); });
|
|
24
|
+
if (typeof env === 'boolean') {
|
|
25
|
+
newPlugins.push(replaceProcessEnv({
|
|
26
|
+
mode: 'production'
|
|
27
|
+
}));
|
|
28
|
+
newPlugins.push(magicModuleEnv({
|
|
29
|
+
mode: 'production'
|
|
30
|
+
}));
|
|
31
|
+
} else {
|
|
32
|
+
newPlugins.push(replaceProcessEnv({
|
|
33
|
+
mode: env.mode ?? 'production'
|
|
34
|
+
}));
|
|
35
|
+
newPlugins.push(magicModuleEnv({
|
|
36
|
+
mode: 'production',
|
|
37
|
+
...env
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (entry) {
|
|
42
|
+
newPlugins.push(magicModuleAppComponent({
|
|
43
|
+
entry
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
return newOptions;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function quiltAppBrowser(options = {}) {
|
|
51
|
+
return {
|
|
52
|
+
name: '@quilted/app/browser',
|
|
53
|
+
options(originalOptions) {
|
|
54
|
+
const newPlugins = [...(Array.isArray(originalOptions.plugins) ? originalOptions.plugins : originalOptions.plugins ? [originalOptions.plugins] : [])];
|
|
55
|
+
const newOptions = {
|
|
56
|
+
...originalOptions,
|
|
57
|
+
plugins: newPlugins
|
|
58
|
+
};
|
|
59
|
+
newPlugins.push(magicModuleAppBrowserEntry(options));
|
|
60
|
+
return newOptions;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function quiltAppServer(options = {}) {
|
|
65
|
+
return {
|
|
66
|
+
name: '@quilted/app/server',
|
|
67
|
+
async options(originalOptions) {
|
|
68
|
+
const newPlugins = [...(Array.isArray(originalOptions.plugins) ? originalOptions.plugins : originalOptions.plugins ? [originalOptions.plugins] : [])];
|
|
69
|
+
const [{
|
|
70
|
+
magicModuleRequestRouterEntry
|
|
71
|
+
}] = await Promise.all([Promise.resolve().then(function () { return require('./request-router.cjs'); })]);
|
|
72
|
+
const newOptions = {
|
|
73
|
+
...originalOptions,
|
|
74
|
+
plugins: newPlugins
|
|
75
|
+
};
|
|
76
|
+
newPlugins.push(magicModuleRequestRouterEntry());
|
|
77
|
+
newPlugins.push(magicModuleAppRequestRouter(options));
|
|
78
|
+
return newOptions;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function magicModuleAppComponent({
|
|
83
|
+
entry
|
|
84
|
+
}) {
|
|
85
|
+
return magicModule.createMagicModulePlugin({
|
|
86
|
+
name: '@quilted/magic-module/app',
|
|
87
|
+
module: constants.MAGIC_MODULE_APP_COMPONENT,
|
|
88
|
+
alias: entry
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function magicModuleAppRequestRouter({
|
|
92
|
+
entry
|
|
93
|
+
} = {}) {
|
|
94
|
+
return magicModule.createMagicModulePlugin({
|
|
95
|
+
name: '@quilted/magic-module/app-request-router',
|
|
96
|
+
module: constants.MAGIC_MODULE_REQUEST_ROUTER,
|
|
97
|
+
alias: entry,
|
|
98
|
+
source: entry ? undefined : async function source() {
|
|
99
|
+
return strings.multiline`
|
|
100
|
+
import '@quilted/quilt/globals';
|
|
101
|
+
|
|
102
|
+
import {jsx} from 'react/jsx-runtime';
|
|
103
|
+
import {RequestRouter} from '@quilted/quilt/request-router';
|
|
104
|
+
import {renderToResponse} from '@quilted/quilt/server';
|
|
105
|
+
|
|
106
|
+
import App from ${JSON.stringify(constants.MAGIC_MODULE_APP_COMPONENT)};
|
|
107
|
+
import {BrowserAssets} from ${JSON.stringify(constants.MAGIC_MODULE_BROWSER_ASSETS)};
|
|
108
|
+
|
|
109
|
+
const router = new RequestRouter();
|
|
110
|
+
const assets = new BrowserAssets();
|
|
111
|
+
|
|
112
|
+
// For all GET requests, render our React application.
|
|
113
|
+
router.get(async (request) => {
|
|
114
|
+
const response = await renderToResponse(jsx(App), {
|
|
115
|
+
request,
|
|
116
|
+
assets,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return response;
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
export default router;
|
|
123
|
+
`;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function magicModuleAppBrowserEntry({
|
|
128
|
+
hydrate = true,
|
|
129
|
+
selector = '#app'
|
|
130
|
+
} = {}) {
|
|
131
|
+
return magicModule.createMagicModulePlugin({
|
|
132
|
+
name: '@quilted/magic-module/app-browser-entry',
|
|
133
|
+
module: constants.MAGIC_MODULE_ENTRY,
|
|
134
|
+
sideEffects: true,
|
|
135
|
+
async source() {
|
|
136
|
+
const reactRootFunction = hydrate ? 'hydrateRoot' : 'createRoot';
|
|
137
|
+
return strings.multiline`
|
|
138
|
+
import '@quilted/quilt/globals';
|
|
139
|
+
|
|
140
|
+
import {jsx} from 'react/jsx-runtime';
|
|
141
|
+
import {${reactRootFunction}} from 'react-dom/client';
|
|
142
|
+
|
|
143
|
+
import App from ${JSON.stringify(constants.MAGIC_MODULE_APP_COMPONENT)};
|
|
144
|
+
|
|
145
|
+
const element = document.querySelector(${JSON.stringify(selector)});
|
|
146
|
+
|
|
147
|
+
${hydrate ? `${reactRootFunction}(element, jsx(App));` : `${reactRootFunction}(element).render(jsx(App));`}
|
|
148
|
+
`;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
exports.magicModuleAppBrowserEntry = magicModuleAppBrowserEntry;
|
|
154
|
+
exports.magicModuleAppComponent = magicModuleAppComponent;
|
|
155
|
+
exports.magicModuleAppRequestRouter = magicModuleAppRequestRouter;
|
|
156
|
+
exports.quiltApp = quiltApp;
|
|
157
|
+
exports.quiltAppBrowser = quiltAppBrowser;
|
|
158
|
+
exports.quiltAppServer = quiltAppServer;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const MAGIC_MODULE_ENV = 'quilt:module/env';
|
|
4
|
+
const MAGIC_MODULE_ENTRY = 'quilt:module/entry';
|
|
5
|
+
const MAGIC_MODULE_APP_COMPONENT = 'quilt:module/app';
|
|
6
|
+
const MAGIC_MODULE_BROWSER_ASSETS = 'quilt:module/assets';
|
|
7
|
+
const MAGIC_MODULE_REQUEST_ROUTER = 'quilt:module/request-router';
|
|
8
|
+
|
|
9
|
+
exports.MAGIC_MODULE_APP_COMPONENT = MAGIC_MODULE_APP_COMPONENT;
|
|
10
|
+
exports.MAGIC_MODULE_BROWSER_ASSETS = MAGIC_MODULE_BROWSER_ASSETS;
|
|
11
|
+
exports.MAGIC_MODULE_ENTRY = MAGIC_MODULE_ENTRY;
|
|
12
|
+
exports.MAGIC_MODULE_ENV = MAGIC_MODULE_ENV;
|
|
13
|
+
exports.MAGIC_MODULE_REQUEST_ROUTER = MAGIC_MODULE_REQUEST_ROUTER;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var path = require('node:path');
|
|
4
|
+
var fs = require('node:fs');
|
|
5
|
+
var constants = require('./constants.cjs');
|
|
6
|
+
var strings = require('./shared/strings.cjs');
|
|
7
|
+
var rollup = require('./shared/rollup.cjs');
|
|
8
|
+
var magicModule = require('./shared/magic-module.cjs');
|
|
9
|
+
|
|
10
|
+
function _interopNamespaceDefault(e) {
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n["default"] = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
28
|
+
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
29
|
+
|
|
30
|
+
const EMPTY_PROCESS_ENV_OBJECT = {
|
|
31
|
+
'globalThis.process.env.': `({}).`,
|
|
32
|
+
'global.process.env.': `({}).`,
|
|
33
|
+
'process.env.': `({}).`
|
|
34
|
+
};
|
|
35
|
+
function replaceProcessEnv({
|
|
36
|
+
mode,
|
|
37
|
+
preserve = true
|
|
38
|
+
}) {
|
|
39
|
+
return rollup.smartReplace({
|
|
40
|
+
// @see https://github.com/vitejs/vite/blob/2b1ffe86328f9d06ef9528ee117b61889893ddcc/packages/vite/src/node/plugins/define.ts#L112
|
|
41
|
+
'globalThis.process.env.NODE_ENV': JSON.stringify(mode),
|
|
42
|
+
'global.process.env.NODE_ENV': JSON.stringify(mode),
|
|
43
|
+
'process.env.NODE_ENV': JSON.stringify(mode),
|
|
44
|
+
...(preserve ? {} : EMPTY_PROCESS_ENV_OBJECT)
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function magicModuleEnv({
|
|
48
|
+
mode,
|
|
49
|
+
dotenv = {
|
|
50
|
+
roots: ['.', 'configuration']
|
|
51
|
+
},
|
|
52
|
+
inline = [],
|
|
53
|
+
runtime = '{}'
|
|
54
|
+
} = {}) {
|
|
55
|
+
return magicModule.createMagicModulePlugin({
|
|
56
|
+
name: '@quilted/magic-module/env',
|
|
57
|
+
module: constants.MAGIC_MODULE_ENV,
|
|
58
|
+
async source() {
|
|
59
|
+
const inlineEnv = {};
|
|
60
|
+
if (mode) {
|
|
61
|
+
inlineEnv.MODE = mode;
|
|
62
|
+
}
|
|
63
|
+
const loadedEnv = await loadEnv.call(this, {
|
|
64
|
+
mode,
|
|
65
|
+
dotenv
|
|
66
|
+
});
|
|
67
|
+
for (const inlineVariable of inline.sort()) {
|
|
68
|
+
if (inlineVariable in inlineEnv) continue;
|
|
69
|
+
const value = process.env[inlineVariable] ?? loadedEnv[inlineVariable];
|
|
70
|
+
if (value == null) continue;
|
|
71
|
+
inlineEnv[inlineVariable] = typeof value === 'string' && value[0] === '"' && value[value.length - 1] === '"' ? JSON.parse(value) : value;
|
|
72
|
+
}
|
|
73
|
+
return strings.multiline`
|
|
74
|
+
const runtime = (${runtime});
|
|
75
|
+
const inline = JSON.parse(${JSON.stringify(JSON.stringify(inlineEnv))});
|
|
76
|
+
|
|
77
|
+
const Env = new Proxy(
|
|
78
|
+
{},
|
|
79
|
+
{
|
|
80
|
+
get(_, property) {
|
|
81
|
+
return runtime[property] ?? inline[property];
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
export default Env;
|
|
87
|
+
`;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Inspired by https://github.com/vitejs/vite/blob/e0a4d810598d1834933ed437ac5a2168cbbbf2f8/packages/vite/source/node/config.ts#L1050-L1113
|
|
93
|
+
async function loadEnv({
|
|
94
|
+
mode,
|
|
95
|
+
dotenv
|
|
96
|
+
}) {
|
|
97
|
+
const env = {
|
|
98
|
+
...process.env
|
|
99
|
+
};
|
|
100
|
+
if (dotenv !== false) {
|
|
101
|
+
const {
|
|
102
|
+
parse
|
|
103
|
+
} = await import('dotenv');
|
|
104
|
+
let files = dotenv.files;
|
|
105
|
+
if (files == null) {
|
|
106
|
+
const testFiles = [
|
|
107
|
+
// default file
|
|
108
|
+
`.env`,
|
|
109
|
+
// local file
|
|
110
|
+
`.env.local`];
|
|
111
|
+
if (mode) {
|
|
112
|
+
testFiles.push(
|
|
113
|
+
// mode file
|
|
114
|
+
`.env.${mode}`,
|
|
115
|
+
// mode local file
|
|
116
|
+
`.env.${mode}.local`);
|
|
117
|
+
}
|
|
118
|
+
files = testFiles.flatMap(file => (dotenv.roots ?? ['.', 'configuration']).map(root => path__namespace.resolve(root, file)));
|
|
119
|
+
}
|
|
120
|
+
const envFileResults = await Promise.all(files.map(async file => {
|
|
121
|
+
if (fs__namespace.existsSync(file)) {
|
|
122
|
+
this.addWatchFile(file);
|
|
123
|
+
return parse(await fs__namespace.promises.readFile(file, 'utf-8'));
|
|
124
|
+
}
|
|
125
|
+
}));
|
|
126
|
+
for (const envFileResult of envFileResults) {
|
|
127
|
+
if (envFileResult == null) continue;
|
|
128
|
+
Object.assign(env, envFileResult);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return env;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
exports.magicModuleEnv = magicModuleEnv;
|
|
135
|
+
exports.replaceProcessEnv = replaceProcessEnv;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var env = require('./env.cjs');
|
|
4
|
+
var app = require('./app.cjs');
|
|
5
|
+
var requestRouter = require('./request-router.cjs');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
exports.magicModuleEnv = env.magicModuleEnv;
|
|
10
|
+
exports.magicModuleAppBrowserEntry = app.magicModuleAppBrowserEntry;
|
|
11
|
+
exports.magicModuleAppComponent = app.magicModuleAppComponent;
|
|
12
|
+
exports.magicModuleAppRequestRouter = app.magicModuleAppRequestRouter;
|
|
13
|
+
exports.quiltApp = app.quiltApp;
|
|
14
|
+
exports.quiltAppBrowser = app.quiltAppBrowser;
|
|
15
|
+
exports.quiltAppServer = app.quiltAppServer;
|
|
16
|
+
exports.magicModuleRequestRouterEntry = requestRouter.magicModuleRequestRouterEntry;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var constants = require('./constants.cjs');
|
|
4
|
+
var magicModule = require('./shared/magic-module.cjs');
|
|
5
|
+
var strings = require('./shared/strings.cjs');
|
|
6
|
+
|
|
7
|
+
function magicModuleRequestRouterEntry({
|
|
8
|
+
host,
|
|
9
|
+
port
|
|
10
|
+
} = {}) {
|
|
11
|
+
return magicModule.createMagicModulePlugin({
|
|
12
|
+
name: '@quilted/request-router',
|
|
13
|
+
sideEffects: true,
|
|
14
|
+
module: constants.MAGIC_MODULE_ENTRY,
|
|
15
|
+
async source() {
|
|
16
|
+
const initialContent = strings.multiline`
|
|
17
|
+
import requestRouter from ${JSON.stringify(constants.MAGIC_MODULE_REQUEST_ROUTER)};
|
|
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
|
+
exports.magicModuleRequestRouterEntry = magicModuleRequestRouterEntry;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const VIRTUAL_MODULE_PREFIX = '\0';
|
|
4
|
+
const VIRTUAL_MODULE_POSTFIX = '/module.js';
|
|
5
|
+
function createMagicModulePlugin({
|
|
6
|
+
name,
|
|
7
|
+
module,
|
|
8
|
+
alias = `${VIRTUAL_MODULE_PREFIX}${module}${VIRTUAL_MODULE_POSTFIX}`,
|
|
9
|
+
source: getSource,
|
|
10
|
+
sideEffects = false
|
|
11
|
+
}) {
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
resolveId(id) {
|
|
15
|
+
if (id !== module) return null;
|
|
16
|
+
return {
|
|
17
|
+
id: alias,
|
|
18
|
+
moduleSideEffects: sideEffects ? 'no-treeshake' : undefined
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
load: getSource ? async function load(source) {
|
|
22
|
+
if (source !== alias) return null;
|
|
23
|
+
const code = await getSource.call(this);
|
|
24
|
+
return {
|
|
25
|
+
code,
|
|
26
|
+
moduleSideEffects: sideEffects ? 'no-treeshake' : undefined
|
|
27
|
+
};
|
|
28
|
+
} : undefined
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports.createMagicModulePlugin = createMagicModulePlugin;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var replace = require('@rollup/plugin-replace');
|
|
4
|
+
|
|
5
|
+
function smartReplace(values, options) {
|
|
6
|
+
return replace({
|
|
7
|
+
// @see https://github.com/vitejs/vite/blob/2b1ffe86328f9d06ef9528ee117b61889893ddcc/packages/vite/src/node/plugins/define.ts#L108-L119
|
|
8
|
+
delimiters: ['(?<![\\p{L}\\p{N}_$]|(?<!\\.\\.)\\.)(', ')(?:(?<=\\.)|(?![\\p{L}\\p{N}_$]|\\s*?=[^=]))'],
|
|
9
|
+
preventAssignment: true,
|
|
10
|
+
...options,
|
|
11
|
+
values
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
exports.smartReplace = smartReplace;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function multiline(strings, ...values) {
|
|
4
|
+
let result = strings.reduce((combined, string, index) => `${combined}${string}${values[index] ?? ''}`, '');
|
|
5
|
+
|
|
6
|
+
// Inspired by https://github.com/zspecza/common-tags/blob/master/src/stripIndentTransformer/stripIndentTransformer.js#L8
|
|
7
|
+
const match = result.match(/^[^\S\n]*(?=\S)/gm);
|
|
8
|
+
const indent = match && Math.min(...match.map(indent => indent.length));
|
|
9
|
+
if (indent) {
|
|
10
|
+
const regexp = new RegExp(`^.{${indent}}`, 'gm');
|
|
11
|
+
result = result.replace(regexp, '');
|
|
12
|
+
}
|
|
13
|
+
return result.trim();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.multiline = multiline;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { MAGIC_MODULE_APP_COMPONENT, MAGIC_MODULE_REQUEST_ROUTER, MAGIC_MODULE_BROWSER_ASSETS, MAGIC_MODULE_ENTRY } from './constants.mjs';
|
|
2
|
+
import { multiline } from './shared/strings.mjs';
|
|
3
|
+
import { createMagicModulePlugin } from './shared/magic-module.mjs';
|
|
4
|
+
|
|
5
|
+
function quiltApp({
|
|
6
|
+
env,
|
|
7
|
+
entry
|
|
8
|
+
} = {}) {
|
|
9
|
+
return {
|
|
10
|
+
name: '@quilted/app',
|
|
11
|
+
async options(originalOptions) {
|
|
12
|
+
const newPlugins = [...(Array.isArray(originalOptions.plugins) ? originalOptions.plugins : originalOptions.plugins ? [originalOptions.plugins] : [])];
|
|
13
|
+
const newOptions = {
|
|
14
|
+
...originalOptions,
|
|
15
|
+
plugins: newPlugins
|
|
16
|
+
};
|
|
17
|
+
if (env) {
|
|
18
|
+
const {
|
|
19
|
+
magicModuleEnv,
|
|
20
|
+
replaceProcessEnv
|
|
21
|
+
} = await import('./env.mjs');
|
|
22
|
+
if (typeof env === 'boolean') {
|
|
23
|
+
newPlugins.push(replaceProcessEnv({
|
|
24
|
+
mode: 'production'
|
|
25
|
+
}));
|
|
26
|
+
newPlugins.push(magicModuleEnv({
|
|
27
|
+
mode: 'production'
|
|
28
|
+
}));
|
|
29
|
+
} else {
|
|
30
|
+
newPlugins.push(replaceProcessEnv({
|
|
31
|
+
mode: env.mode ?? 'production'
|
|
32
|
+
}));
|
|
33
|
+
newPlugins.push(magicModuleEnv({
|
|
34
|
+
mode: 'production',
|
|
35
|
+
...env
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (entry) {
|
|
40
|
+
newPlugins.push(magicModuleAppComponent({
|
|
41
|
+
entry
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
return newOptions;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function quiltAppBrowser(options = {}) {
|
|
49
|
+
return {
|
|
50
|
+
name: '@quilted/app/browser',
|
|
51
|
+
options(originalOptions) {
|
|
52
|
+
const newPlugins = [...(Array.isArray(originalOptions.plugins) ? originalOptions.plugins : originalOptions.plugins ? [originalOptions.plugins] : [])];
|
|
53
|
+
const newOptions = {
|
|
54
|
+
...originalOptions,
|
|
55
|
+
plugins: newPlugins
|
|
56
|
+
};
|
|
57
|
+
newPlugins.push(magicModuleAppBrowserEntry(options));
|
|
58
|
+
return newOptions;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function quiltAppServer(options = {}) {
|
|
63
|
+
return {
|
|
64
|
+
name: '@quilted/app/server',
|
|
65
|
+
async options(originalOptions) {
|
|
66
|
+
const newPlugins = [...(Array.isArray(originalOptions.plugins) ? originalOptions.plugins : originalOptions.plugins ? [originalOptions.plugins] : [])];
|
|
67
|
+
const [{
|
|
68
|
+
magicModuleRequestRouterEntry
|
|
69
|
+
}] = await Promise.all([import('./request-router.mjs')]);
|
|
70
|
+
const newOptions = {
|
|
71
|
+
...originalOptions,
|
|
72
|
+
plugins: newPlugins
|
|
73
|
+
};
|
|
74
|
+
newPlugins.push(magicModuleRequestRouterEntry());
|
|
75
|
+
newPlugins.push(magicModuleAppRequestRouter(options));
|
|
76
|
+
return newOptions;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function magicModuleAppComponent({
|
|
81
|
+
entry
|
|
82
|
+
}) {
|
|
83
|
+
return createMagicModulePlugin({
|
|
84
|
+
name: '@quilted/magic-module/app',
|
|
85
|
+
module: MAGIC_MODULE_APP_COMPONENT,
|
|
86
|
+
alias: entry
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function magicModuleAppRequestRouter({
|
|
90
|
+
entry
|
|
91
|
+
} = {}) {
|
|
92
|
+
return createMagicModulePlugin({
|
|
93
|
+
name: '@quilted/magic-module/app-request-router',
|
|
94
|
+
module: MAGIC_MODULE_REQUEST_ROUTER,
|
|
95
|
+
alias: entry,
|
|
96
|
+
source: entry ? undefined : async function source() {
|
|
97
|
+
return multiline`
|
|
98
|
+
import '@quilted/quilt/globals';
|
|
99
|
+
|
|
100
|
+
import {jsx} from 'react/jsx-runtime';
|
|
101
|
+
import {RequestRouter} from '@quilted/quilt/request-router';
|
|
102
|
+
import {renderToResponse} from '@quilted/quilt/server';
|
|
103
|
+
|
|
104
|
+
import App from ${JSON.stringify(MAGIC_MODULE_APP_COMPONENT)};
|
|
105
|
+
import {BrowserAssets} from ${JSON.stringify(MAGIC_MODULE_BROWSER_ASSETS)};
|
|
106
|
+
|
|
107
|
+
const router = new RequestRouter();
|
|
108
|
+
const assets = new BrowserAssets();
|
|
109
|
+
|
|
110
|
+
// For all GET requests, render our React application.
|
|
111
|
+
router.get(async (request) => {
|
|
112
|
+
const response = await renderToResponse(jsx(App), {
|
|
113
|
+
request,
|
|
114
|
+
assets,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
return response;
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
export default router;
|
|
121
|
+
`;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function magicModuleAppBrowserEntry({
|
|
126
|
+
hydrate = true,
|
|
127
|
+
selector = '#app'
|
|
128
|
+
} = {}) {
|
|
129
|
+
return createMagicModulePlugin({
|
|
130
|
+
name: '@quilted/magic-module/app-browser-entry',
|
|
131
|
+
module: MAGIC_MODULE_ENTRY,
|
|
132
|
+
sideEffects: true,
|
|
133
|
+
async source() {
|
|
134
|
+
const reactRootFunction = hydrate ? 'hydrateRoot' : 'createRoot';
|
|
135
|
+
return multiline`
|
|
136
|
+
import '@quilted/quilt/globals';
|
|
137
|
+
|
|
138
|
+
import {jsx} from 'react/jsx-runtime';
|
|
139
|
+
import {${reactRootFunction}} from 'react-dom/client';
|
|
140
|
+
|
|
141
|
+
import App from ${JSON.stringify(MAGIC_MODULE_APP_COMPONENT)};
|
|
142
|
+
|
|
143
|
+
const element = document.querySelector(${JSON.stringify(selector)});
|
|
144
|
+
|
|
145
|
+
${hydrate ? `${reactRootFunction}(element, jsx(App));` : `${reactRootFunction}(element).render(jsx(App));`}
|
|
146
|
+
`;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export { magicModuleAppBrowserEntry, magicModuleAppComponent, magicModuleAppRequestRouter, quiltApp, quiltAppBrowser, quiltAppServer };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const MAGIC_MODULE_ENV = 'quilt:module/env';
|
|
2
|
+
const MAGIC_MODULE_ENTRY = 'quilt:module/entry';
|
|
3
|
+
const MAGIC_MODULE_APP_COMPONENT = 'quilt:module/app';
|
|
4
|
+
const MAGIC_MODULE_BROWSER_ASSETS = 'quilt:module/assets';
|
|
5
|
+
const MAGIC_MODULE_REQUEST_ROUTER = 'quilt:module/request-router';
|
|
6
|
+
|
|
7
|
+
export { MAGIC_MODULE_APP_COMPONENT, MAGIC_MODULE_BROWSER_ASSETS, MAGIC_MODULE_ENTRY, MAGIC_MODULE_ENV, MAGIC_MODULE_REQUEST_ROUTER };
|