@mochabug/adaptkit 1.0.0-beta.12 → 1.0.0-beta.2

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.
@@ -1,7 +1,5 @@
1
1
  import { build } from 'esbuild';
2
- import { sync as glob } from 'glob';
3
2
  import fs from 'node:fs';
4
- import path from 'node:path';
5
3
 
6
4
  if (!process.env.PLUGINS_CDN) {
7
5
  console.error('PLUGINS_CDN is required');
@@ -15,93 +13,34 @@ if (!manifest.organization || !manifest.name || !manifest.version) {
15
13
  }
16
14
  const CDN_PATH = `${process.env.PLUGINS_CDN}/${manifest.organization}/${manifest.name}/${manifest.version}`;
17
15
 
18
- fs.rmSync('./cdn', { recursive: true });
16
+ if (fs.existsSync('cdn')) {
17
+ fs.rmSync('cdn', { recursive: true, force: true })
18
+ }
19
19
 
20
- // Build the client and place all the assets onto the corresponding CDN for the plugin
21
- const loader = {
22
- '.css': 'css',
23
- '.woff': 'file',
24
- '.woff2': 'file',
25
- '.png': 'file',
26
- '.jpg': 'file',
27
- '.jpeg': 'file',
28
- '.gif': 'file',
29
- '.svg': 'file',
30
- '.ttf': 'file',
31
- '.otf': 'file',
32
- };
33
- const result = await build({
34
- entryPoints: glob('./src/vertices/**/ui/*.main.tsx'),
20
+ build({
21
+ entryPoints: [___ENTRYPOINTS___],
35
22
  outdir: 'cdn',
36
23
  bundle: true,
37
24
  minify: true,
25
+ sourcemap: true,
38
26
  platform: 'browser',
39
27
  splitting: true,
40
- tsconfig: 'tsconfig.json',
41
28
  format: 'esm',
42
- metafile: true,
43
- publicPath: CDN_PATH,
44
- loader,
45
- });
46
-
47
- const entrypoints = {
48
- configurators: {},
49
- executors: {},
50
- vertices: [],
51
- };
52
- for (const [outputPath, outputMeta] of Object.entries(result.metafile.outputs)) {
53
- if (!outputMeta.entryPoint) {
54
- continue;
55
- }
56
- let vertex = path.dirname(path.dirname(path.relative('./src/vertices', outputMeta.entryPoint)));
57
- if (entrypoints.vertices.findIndex((v) => v === vertex) === -1 && manifest.vertices.find((v) => v.name === vertex)) {
58
- entrypoints.vertices.push(vertex);
59
- }
60
- if (outputMeta.entryPoint.endsWith('executor.main.tsx')) {
61
- entrypoints.executors[vertex] = { js: `${CDN_PATH}/${outputPath.substring(4)}` };
62
- if (outputMeta.cssBundle) {
63
- entrypoints.executors[vertex].css = `${CDN_PATH}/${outputMeta.cssBundle.substring(4)}`;
64
- }
65
- } else if (outputMeta.entryPoint.endsWith('configurator.main.tsx')) {
66
- entrypoints.configurators[vertex] = { js: `${CDN_PATH}/${outputPath.substring(4)}` };
67
- if (outputMeta.cssBundle) {
68
- entrypoints.configurators[vertex].css = `${CDN_PATH}/${outputMeta.cssBundle.substring(4)}`;
69
- }
70
- }
71
- }
72
-
73
- // Build the static site, make sure that we have the CDN as the public path for the assets
74
- await build({
75
- entryPoints: glob('./src/vertices/**/*.ssg.tsx'),
76
- outdir: 'dist',
77
- tsconfig: 'tsconfig.json',
78
- bundle: true,
79
- platform: 'node',
80
- format: 'cjs',
81
- target: 'esnext',
82
29
  publicPath: CDN_PATH,
83
- loader,
84
- outExtension: {
85
- '.js': '.cjs',
30
+ metafile: true,
31
+ loader: {
32
+ '.css': 'css',
33
+ '.woff': 'file',
34
+ '.woff2': 'file',
35
+ '.png': 'file',
36
+ '.jpg': 'file',
37
+ '.jpeg': 'file',
38
+ '.gif': 'file',
39
+ '.svg': 'file',
40
+ '.ttf': 'file',
41
+ '.otf': 'file',
86
42
  },
87
- });
88
-
89
- const configIndices = {};
90
- for (const file of glob('./dist/**/configurator.ssg.cjs')) {
91
- const { default: render } = await import(`file://${path.resolve(file)}`);
92
- const vertex = entrypoints.vertices.length === 1 ? entrypoints.vertices[0] : path.dirname(path.dirname(path.relative('./dist', file)));
93
- const entrypoint = entrypoints.configurators[vertex];
94
- if (entrypoint) {
95
- configIndices[vertex] = render.default(entrypoint.js, entrypoint.css);
96
- }
97
- }
98
- fs.writeFileSync('./dist/configurator-index-html.json', JSON.stringify(configIndices));
99
- const execIndices = {};
100
- for (const file of glob('./dist/**/executor.ssg.cjs')) {
101
- const { default: render } = await import(`file://${path.resolve(file)}`);
102
- const vertex = entrypoints.vertices.length === 1 ? entrypoints.vertices[0] : path.dirname(path.dirname(path.relative('./dist', file)));
103
-
104
- const entrypoint = entrypoints.executors[vertex];
105
- execIndices[vertex] = render.default(entrypoint.js, entrypoint.css);
106
- }
107
- fs.writeFileSync('./dist/executor-index-html.json', JSON.stringify(execIndices));
43
+ }).catch((err) => {
44
+ console.error(err);
45
+ process.exit(1);
46
+ });
package/assets/build.js CHANGED
@@ -17,31 +17,20 @@ const CDN_PATH = `${process.env.PLUGINS_CDN}/${manifest.organization}/${manifest
17
17
 
18
18
  const { env } = defineEnv({ nodeCompat: true, presets: [cloudflare] });
19
19
 
20
+ if (fs.existsSync('dist')) {
21
+ fs.rmSync('dist', { recursive: true, force: true })
22
+ }
20
23
  const entryPoints = ['./src/executors']
21
24
  if (fs.existsSync('./src/configurators.ts')) {
22
25
  entryPoints.push('./src/configurators')
23
26
  }
24
27
 
25
- // Set up the defines, this will automatically load ssg index.html files if they exists
26
- const define = {
27
- PLUGINS_CDN: JSON.stringify(CDN_PATH)
28
- };
29
- if (fs.existsSync('./dist/configurator-index-html.json')) {
30
- const CONFIGURATOR_INDEX_HTML = fs.readFileSync('./dist/configurator-index-html.json', 'utf8');
31
- define.CONFIGURATOR_INDEX_HTML = CONFIGURATOR_INDEX_HTML;
32
- }
33
- if (fs.existsSync('./dist/executor-index-html.json')) {
34
- const EXECUTOR_INDEX_HTML = fs.readFileSync('./dist/executor-index-html.json', 'utf8');
35
- define.EXECUTOR_INDEX_HTML = EXECUTOR_INDEX_HTML;
36
- }
37
-
38
- await build({
28
+ build({
39
29
  entryPoints,
40
30
  outdir: 'dist',
41
31
  bundle: true,
42
32
  minify: true,
43
33
  treeShaking: true,
44
- sourcemap: false,
45
34
  splitting: false,
46
35
  format: 'esm',
47
36
  platform: 'node',
@@ -52,6 +41,10 @@ await build({
52
41
  conditions: ["workerd"],
53
42
  external: env.external,
54
43
  alias: env.alias,
55
- publicPath: CDN_PATH,
56
- define,
44
+ define: {
45
+ PLUGINS_CDN: JSON.stringify(CDN_PATH),
46
+ }
47
+ }).catch((error) => {
48
+ console.error(error)
49
+ process.exit(1)
57
50
  });
@@ -6,6 +6,9 @@ import {
6
6
  ExternalConfiguratorRouter,
7
7
  InternalConfiguratorRouter
8
8
  } from '@mochabug/adapt-plugin-toolkit/router';
9
+ import React from 'react';
10
+ import { renderToString } from 'react-dom/server';
11
+ import App from './../../frontend/___APP_IMPORT___';
9
12
 
10
13
  export default {
11
14
  external: new ExternalConfiguratorRouter()
@@ -30,9 +33,25 @@ export default {
30
33
  });
31
34
  })
32
35
  .add('GET', '{*any}', async () => {
33
- return new Response(CONFIGURATOR_INDEX_HTML['___VERTEX_NAME___'], {
34
- headers: { 'Content-Type': 'text/html' }
36
+ return new Response(readIndexHtml(), {
37
+ headers: {
38
+ 'Content-Type': 'text/html; charset=utf-8'
39
+ }
35
40
  });
36
41
  }),
37
42
  internal: new InternalConfiguratorRouter()
38
43
  };
44
+ function readIndexHtml() {
45
+ return `
46
+ <!DOCTYPE html>
47
+ <html>
48
+ <head>
49
+ <title>My SSR App</title>
50
+ </head>
51
+ <body>
52
+ <div id="root">${renderToString(<App />)}</div>
53
+ <script type="module" src="${PLUGINS_CDN}/___CLIENT__NAME___.js"></script>
54
+ </body>
55
+ </html>
56
+ `;
57
+ }
@@ -6,6 +6,9 @@ import {
6
6
  ExternalExecutorRouter,
7
7
  InternalExecutorRouter
8
8
  } from '@mochabug/adapt-plugin-toolkit/router';
9
+ import React from 'react';
10
+ import { renderToString } from 'react-dom/server';
11
+ import App from './../../frontend/___APP_IMPORT___';
9
12
 
10
13
  export default {
11
14
  external: new ExternalExecutorRouter()
@@ -27,8 +30,10 @@ export default {
27
30
  return new Response();
28
31
  })
29
32
  .add('GET', '{*any}', async () => {
30
- return new Response(EXECUTOR_INDEX_HTML['___VERTEX_NAME___'], {
31
- headers: { 'Content-Type': 'text/html' }
33
+ return new Response(readIndexHtml(), {
34
+ headers: {
35
+ 'Content-Type': 'text/html; charset=utf-8'
36
+ }
32
37
  });
33
38
  }),
34
39
  internal: new InternalExecutorRouter()
@@ -49,3 +54,17 @@ export default {
49
54
  console.log(res);
50
55
  })
51
56
  };
57
+ function readIndexHtml() {
58
+ return `
59
+ <!DOCTYPE html>
60
+ <html>
61
+ <head>
62
+ <title>My SSR App</title>
63
+ </head>
64
+ <body>
65
+ <div id="root">${renderToString(<App />)}</div>
66
+ <script type="module" src="${PLUGINS_CDN}/___CLIENT__NAME___.js"></script>
67
+ </body>
68
+ </html>
69
+ `;
70
+ }
@@ -1,7 +1,5 @@
1
1
  declare global {
2
2
  const PLUGINS_CDN: string;
3
- const CONFIGURATOR_INDEX_HTML: Record<string, string>;
4
- const EXECUTOR_INDEX_HTML: Record<string, string>;
5
3
  }
6
4
 
7
5
  export {};
package/assets/inject.js CHANGED
@@ -9,6 +9,13 @@ import consoleModule from '@cloudflare/unenv-preset/runtime/node/console/index'
9
9
  if (typeof globalThis.console === 'undefined') {
10
10
  globalThis.console = consoleModule
11
11
  }
12
+ import * as timers from '@cloudflare/unenv-preset/runtime/node/timers/index'
13
+ if (typeof globalThis.setImmediate === 'undefined') {
14
+ globalThis.setImmediate = timers.setImmediate
15
+ }
16
+ if (typeof globalThis.clearImmediate === 'undefined') {
17
+ globalThis.clearImmediate = timers.clearImmediate
18
+ }
12
19
  import perfModule from 'unenv/runtime/polyfill/performance'
13
20
  if (typeof globalThis.performance === 'undefined') {
14
21
  globalThis.performance = perfModule
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ import { hydrateRoot } from 'react-dom/client';
3
+ import App from '___APP_IMPORT___';
4
+
5
+ hydrateRoot(document.getElementById('root')!, <App />);