@wrdagency/react-islands 1.0.4 → 2.0.0
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/README.md +106 -3
- package/dist/index.d.ts +46 -6
- package/dist/index.js +96 -136
- package/package.json +37 -15
- package/src/bin/commands/build.ts +73 -0
- package/src/bin/index.ts +7 -0
- package/src/bin/rollup/console.ts +23 -0
- package/src/bin/rollup/generation.ts +57 -0
- package/src/bin/rollup/index.ts +178 -0
- package/src/bin/rollup/options.ts +74 -0
- package/src/bin/rollup/plugins/runScriptAfterBuildPlugin.ts +43 -0
- package/src/bin/rollup/plugins/virtualizeDependencyPlugin.ts +34 -0
- package/src/bin/util/command.ts +101 -0
- package/src/index.tsx +5 -0
- package/src/island.tsx +94 -0
- package/src/util.tsx +41 -0
- package/dist/index.d.mts +0 -9
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -116
- package/dist/index.mjs.map +0 -1
- package/src/index.ts +0 -113
- package/tsconfig.json +0 -13
- package/tsup.config.ts +0 -10
package/README.md
CHANGED
|
@@ -1,7 +1,110 @@
|
|
|
1
|
-
|
|
1
|
+
# React Islands
|
|
2
2
|
|
|
3
3
|
Created by Kyle Cooper at [WRD.agency](https://webresultsdirect.com)
|
|
4
4
|
|
|
5
|
-
React Islands is a way of introducing React into static pages. We developed React Islands as a way of introducing React-based interactivity sprinkled into places on WordPress sites (rendered via PHP).
|
|
5
|
+
React Islands is a way of introducing pockets of React into otherwise static pages, with an option for build-type rendering to help with layout shift. We developed React Islands as a way of introducing React-based interactivity sprinkled into places on WordPress sites (rendered via PHP).
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Example Setup
|
|
8
|
+
|
|
9
|
+
Create your island. Here we use an existing component and use our Islands directory to only setup the islands and not for worrying about functionality.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
// ./islands/my-component.tsx
|
|
13
|
+
import MyComponent from "./components";
|
|
14
|
+
import { createIsland } from "@wrdagency/react-islands";
|
|
15
|
+
|
|
16
|
+
export const myComponentIsland = createIsland(MyComponent, {
|
|
17
|
+
name: "my-component",
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
// ./islands/index.ts
|
|
23
|
+
export * from "./my-component";
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
On the client you can then render each of those islands. They'll automatically hook into the DOM where the selector is matched (similar to a portal) to create an island of reactivity.
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
// index.ts
|
|
30
|
+
import * as islands from "./islands";
|
|
31
|
+
import { hydrateIslands } from "@wrdagency/react-islands";
|
|
32
|
+
|
|
33
|
+
hydrateIslands( islands );
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Create a pre-render script. You can configure your build tool to use this as a seperate entrypoint.
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
// prerender.ts
|
|
41
|
+
|
|
42
|
+
import path from "node:path";
|
|
43
|
+
import { fileURLToPath } from "node:url";
|
|
44
|
+
import * as islands from "./islands";
|
|
45
|
+
import { prerenderIslands } from "@wrdagency/react-islands/server";
|
|
46
|
+
|
|
47
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
48
|
+
const __dirname = path.dirname(__filename);
|
|
49
|
+
const outDir = path.resolve(__dirname, "ssg");
|
|
50
|
+
|
|
51
|
+
prerenderIslands({ islands, outDir });
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
For our example we're using Vite. We'll build our prerendering script.
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
npx vite build --ssr ./src/prerender.tsx --outDir ./dist
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
And then we can run that compiled script. It'll create all of our statically rendered islands and put them into the `outDir` we specified.
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
node ./dist/prerender.js
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
For convenience we'd recommend setting up a script in your `package.json` for this like so:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
"scripts": {
|
|
70
|
+
"prerender": "npx vite build --ssr ./src/prerender.tsx --outDir ./dist && node ./dist/prerender.js",
|
|
71
|
+
},
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## API
|
|
75
|
+
|
|
76
|
+
### `createIsland`
|
|
77
|
+
|
|
78
|
+
`(component: React.FC, options: IslandOpts) => Island`
|
|
79
|
+
|
|
80
|
+
Creates an island.
|
|
81
|
+
|
|
82
|
+
#### `IslandOpts`
|
|
83
|
+
|
|
84
|
+
| Options | Type | Default | Description |
|
|
85
|
+
| ------------ | -------- | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
|
86
|
+
| name | string | Required | The name of the island. Used for the default selector and the filename of pre-rendering. |
|
|
87
|
+
| selector | string? | [data-hydrate="{{NAME}}"] | The query selector to match for the islands root. |
|
|
88
|
+
| multiple | boolean? | false | If enabled, the island will by instantiated for every element that matches the selector, not just the first. |
|
|
89
|
+
| keepChildren | boolean? | false | If enabled, the children props of the island component will be set to the raw HTML of the existing node's children. Experimental. |
|
|
90
|
+
|
|
91
|
+
### `withProps`
|
|
92
|
+
|
|
93
|
+
`<T>(component: React.FC<T>, props: Partial<T>) => React.FC<T>`
|
|
94
|
+
|
|
95
|
+
Creates a version of your React component with props already set. Useful for creating multiple islands with variants of the same component.
|
|
96
|
+
|
|
97
|
+
### `isServer`
|
|
98
|
+
|
|
99
|
+
`() => boolean`
|
|
100
|
+
|
|
101
|
+
Checks if the current environment is the server. Useful for disabled certain features not available during the prerender step.
|
|
102
|
+
|
|
103
|
+
### `prerenderIslands`
|
|
104
|
+
|
|
105
|
+
`(options: PrerenderIslandsOpts) => Promise<void>`
|
|
106
|
+
|
|
107
|
+
| Option | Type | Description |
|
|
108
|
+
| ------- | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
|
109
|
+
| islands | Record<string, Island> | The islands the pre-render. The key of the record is not used, it's just useful to accept a record if we're using `import * as Islands`. |
|
|
110
|
+
| ourDir | string | Path of the directory to output the static HTML to. This directory will be emptied before pre-rendering begins. |
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,49 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FC } from 'react';
|
|
2
|
+
import { JsxPreset } from 'rollup';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
interface IslandRenderOptions {
|
|
5
|
+
shouldHydrate: boolean;
|
|
6
|
+
multiple: boolean;
|
|
7
|
+
keepChildren: boolean;
|
|
6
8
|
}
|
|
7
|
-
|
|
9
|
+
interface IslandOpts extends Partial<IslandRenderOptions> {
|
|
10
|
+
}
|
|
11
|
+
declare class Island {
|
|
12
|
+
readonly component: React.FC;
|
|
13
|
+
readonly renderOptions: IslandRenderOptions;
|
|
14
|
+
constructor(component: React.FC, opts?: IslandOpts);
|
|
15
|
+
private getProps;
|
|
16
|
+
private getRoots;
|
|
17
|
+
render(name: string): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Create a higher-order component with certain fixed.
|
|
22
|
+
*
|
|
23
|
+
* Useful for quickly creating multiple variants of the same component to use as islands.
|
|
24
|
+
*
|
|
25
|
+
* @param component FC<T>
|
|
26
|
+
* @param setProps Partial<T>
|
|
27
|
+
* @returns FC<T>
|
|
28
|
+
*/
|
|
29
|
+
declare function withProps<T>(component: FC<T>, setProps: Partial<T>): FC<T>;
|
|
30
|
+
/**
|
|
31
|
+
* Checks if the current script is running in a pre-render.
|
|
32
|
+
*
|
|
33
|
+
* @returns boolean
|
|
34
|
+
*/
|
|
35
|
+
declare function isServer(): boolean;
|
|
36
|
+
|
|
37
|
+
type BuildOptions = {
|
|
38
|
+
name: string;
|
|
39
|
+
input: string;
|
|
40
|
+
output: string;
|
|
41
|
+
minify?: boolean;
|
|
42
|
+
ssg?: boolean;
|
|
43
|
+
jsx?: JsxPreset;
|
|
44
|
+
typescript?: boolean;
|
|
45
|
+
common?: string[] | Record<string, string>;
|
|
46
|
+
};
|
|
8
47
|
|
|
9
|
-
export {
|
|
48
|
+
export { Island, isServer, withProps };
|
|
49
|
+
export type { BuildOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,142 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var __async = (__this, __arguments, generator) => {
|
|
30
|
-
return new Promise((resolve, reject) => {
|
|
31
|
-
var fulfilled = (value) => {
|
|
32
|
-
try {
|
|
33
|
-
step(generator.next(value));
|
|
34
|
-
} catch (e) {
|
|
35
|
-
reject(e);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var rejected = (value) => {
|
|
39
|
-
try {
|
|
40
|
-
step(generator.throw(value));
|
|
41
|
-
} catch (e) {
|
|
42
|
-
reject(e);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
46
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
47
|
-
});
|
|
48
|
-
};
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { createRoot } from 'react-dom/client';
|
|
3
|
+
import { useRef, useEffect } from 'react';
|
|
49
4
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
filename: "[name].js"
|
|
76
|
-
},
|
|
77
|
-
module: options.module,
|
|
78
|
-
stats: false,
|
|
79
|
-
devtool: false,
|
|
80
|
-
optimization: {
|
|
81
|
-
minimize: false
|
|
82
|
-
},
|
|
83
|
-
plugins
|
|
5
|
+
/**
|
|
6
|
+
* Internal component for rendering raw HTML in a React component.
|
|
7
|
+
*/
|
|
8
|
+
function RawHTML({ html }) {
|
|
9
|
+
const ref = useRef(null);
|
|
10
|
+
// important to not have ANY deps
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (ref.current) {
|
|
13
|
+
ref.current.outerHTML = html;
|
|
14
|
+
}
|
|
15
|
+
}, []);
|
|
16
|
+
return jsx("script", { ref: ref });
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Create a higher-order component with certain fixed.
|
|
20
|
+
*
|
|
21
|
+
* Useful for quickly creating multiple variants of the same component to use as islands.
|
|
22
|
+
*
|
|
23
|
+
* @param component FC<T>
|
|
24
|
+
* @param setProps Partial<T>
|
|
25
|
+
* @returns FC<T>
|
|
26
|
+
*/
|
|
27
|
+
function withProps(component, setProps) {
|
|
28
|
+
return (props) => {
|
|
29
|
+
return component({ ...props, ...setProps });
|
|
84
30
|
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Checks if the current script is running in a pre-render.
|
|
34
|
+
*
|
|
35
|
+
* @returns boolean
|
|
36
|
+
*/
|
|
37
|
+
function isServer() {
|
|
38
|
+
return !(typeof window != "undefined" && window.document);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class Island {
|
|
42
|
+
component;
|
|
43
|
+
renderOptions;
|
|
44
|
+
constructor(component, opts = {}) {
|
|
45
|
+
this.component = component;
|
|
46
|
+
this.renderOptions = {
|
|
47
|
+
shouldHydrate: opts.shouldHydrate ?? true,
|
|
48
|
+
multiple: opts.multiple ?? false,
|
|
49
|
+
keepChildren: opts.keepChildren ?? false,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
getProps(element) {
|
|
53
|
+
const { keepChildren } = this.renderOptions;
|
|
54
|
+
let props = {};
|
|
55
|
+
try {
|
|
56
|
+
const json = element.dataset.props || "{}";
|
|
57
|
+
if (json) {
|
|
58
|
+
const parsed = JSON.parse(json);
|
|
59
|
+
if (typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
60
|
+
throw new Error(`Parsed JSON is not a valid dictionary object: '${json}'`);
|
|
61
|
+
}
|
|
62
|
+
if (parsed) {
|
|
63
|
+
// Ignore null.
|
|
64
|
+
props = parsed;
|
|
65
|
+
}
|
|
107
66
|
}
|
|
108
|
-
callback();
|
|
109
|
-
console.log("ReactIslandsWebpackPlugin: Compilation complete.");
|
|
110
|
-
});
|
|
111
67
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const { default: component } = require(file);
|
|
119
|
-
const markup = (0, import_server.renderToStaticMarkup)(component());
|
|
120
|
-
compilation.emitAsset(`${name}.html`, new RawSource(markup));
|
|
121
|
-
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
console.warn("Could not parse JSON props for React Island.");
|
|
70
|
+
console.error(e);
|
|
71
|
+
}
|
|
72
|
+
if (keepChildren) {
|
|
73
|
+
props.children = jsx(RawHTML, { html: element.innerHTML });
|
|
122
74
|
}
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
75
|
+
return props;
|
|
76
|
+
}
|
|
77
|
+
getRoots(name) {
|
|
78
|
+
const selector = `[data-island="${name}"]`;
|
|
79
|
+
const { multiple } = this.renderOptions;
|
|
80
|
+
const nodes = [...document.querySelectorAll(selector)];
|
|
81
|
+
if (!nodes) {
|
|
82
|
+
console.warn(`Could not render React Island because DOM node (${selector}) could not be found.`);
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
if (nodes.length > 1 && !multiple) {
|
|
86
|
+
console.warn(`Multiple elements matched React Island selector (${selector}) but multiple was not enabled. Choosing first element as root.`);
|
|
87
|
+
return [nodes[0]];
|
|
88
|
+
}
|
|
89
|
+
return nodes;
|
|
90
|
+
}
|
|
91
|
+
render(name) {
|
|
92
|
+
this.getRoots(name).forEach((element) => {
|
|
93
|
+
const props = this.getProps(element);
|
|
94
|
+
const Component = withProps(this.component, props);
|
|
95
|
+
const root = createRoot(element);
|
|
96
|
+
root.render(jsx(Component, {}));
|
|
97
|
+
return root;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
136
100
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
ReactIslandsWebpackPlugin,
|
|
140
|
-
createIsland
|
|
141
|
-
});
|
|
142
|
-
//# sourceMappingURL=index.js.map
|
|
101
|
+
|
|
102
|
+
export { Island, isServer, withProps };
|
package/package.json
CHANGED
|
@@ -1,34 +1,56 @@
|
|
|
1
1
|
{
|
|
2
|
+
"type": "module",
|
|
2
3
|
"name": "@wrdagency/react-islands",
|
|
3
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
4
5
|
"description": "",
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
|
-
"
|
|
7
|
-
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"src"
|
|
10
|
+
],
|
|
11
|
+
"bin": {
|
|
12
|
+
"react-islands": "./bin/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
8
20
|
"scripts": {
|
|
9
|
-
"build": "
|
|
21
|
+
"build": "npx rollup -c rollup.config.js"
|
|
10
22
|
},
|
|
11
23
|
"keywords": [
|
|
12
24
|
"react",
|
|
13
|
-
"
|
|
14
|
-
"static"
|
|
25
|
+
"islands"
|
|
15
26
|
],
|
|
16
27
|
"author": "Kyle Thomas Cooper @ WRD",
|
|
17
28
|
"license": "MIT",
|
|
18
29
|
"devDependencies": {
|
|
19
|
-
"@
|
|
20
|
-
"@
|
|
21
|
-
"@
|
|
22
|
-
"
|
|
23
|
-
"
|
|
30
|
+
"@rollup/plugin-commonjs": "^28.0.6",
|
|
31
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
32
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
33
|
+
"@rollup/plugin-replace": "^6.0.2",
|
|
34
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
35
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
36
|
+
"@types/command-line-args": "^5.2.3",
|
|
37
|
+
"@types/command-line-usage": "^5.0.4",
|
|
38
|
+
"@types/react": "^19.1.8",
|
|
39
|
+
"@types/react-dom": "^19.1.6",
|
|
40
|
+
"command-line-args": "^6.0.1",
|
|
41
|
+
"command-line-usage": "^7.0.3",
|
|
42
|
+
"rollup": "^4.45.1",
|
|
43
|
+
"rollup-plugin-dts": "^6.2.1",
|
|
44
|
+
"rollup-plugin-typescript-paths": "^1.5.0",
|
|
45
|
+
"tslib": "^2.8.1",
|
|
46
|
+
"typescript": "^5.4.3",
|
|
47
|
+
"yocto-spinner": "^1.0.0"
|
|
24
48
|
},
|
|
25
49
|
"repository": {
|
|
26
50
|
"type": "git",
|
|
27
51
|
"url": "https://github.com/kyletcooper/react-islands"
|
|
28
52
|
},
|
|
29
|
-
"
|
|
30
|
-
"react-dom": "^
|
|
31
|
-
"tmp": "^0.2.3",
|
|
32
|
-
"webpack": "^5.91.0"
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"react-dom": "^19.1.0"
|
|
33
55
|
}
|
|
34
56
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { readFileSync } from "fs";
|
|
2
|
+
import { OutputOptions } from "rollup";
|
|
3
|
+
import yoctoSpinner from "yocto-spinner";
|
|
4
|
+
import { createCommonConfig, createRollupConfigs, runRollup } from "../rollup";
|
|
5
|
+
import { Command } from "../util/command";
|
|
6
|
+
|
|
7
|
+
export default new Command({
|
|
8
|
+
description: "Build and statically render the islands.",
|
|
9
|
+
args: [
|
|
10
|
+
{
|
|
11
|
+
name: "config",
|
|
12
|
+
type: String,
|
|
13
|
+
alias: "c",
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
description: "The config file to use.",
|
|
16
|
+
defaultValue: "islands.config.json",
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
callback: async (args) => {
|
|
20
|
+
const { config } = args;
|
|
21
|
+
|
|
22
|
+
const configJson = JSON.parse(readFileSync(config, "utf8"));
|
|
23
|
+
|
|
24
|
+
const { islands, ...restConfig } = configJson;
|
|
25
|
+
|
|
26
|
+
if (restConfig.common !== false) {
|
|
27
|
+
const spinner = yoctoSpinner({
|
|
28
|
+
text: `Creating common dependencies file...`,
|
|
29
|
+
}).start();
|
|
30
|
+
|
|
31
|
+
const commonRollupConfig = createCommonConfig(restConfig);
|
|
32
|
+
const { output, ...input } = commonRollupConfig;
|
|
33
|
+
const success = await runRollup(input, output as OutputOptions);
|
|
34
|
+
|
|
35
|
+
if (success) {
|
|
36
|
+
spinner.success(`Succeeded: common dependencies file`);
|
|
37
|
+
} else {
|
|
38
|
+
spinner.warning(`Failed: common dependencies file`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const [name, input] of Object.entries(islands)) {
|
|
43
|
+
const spinner = yoctoSpinner({
|
|
44
|
+
text: `Creating island ${name}...`,
|
|
45
|
+
}).start();
|
|
46
|
+
|
|
47
|
+
const rollupConfigs = createRollupConfigs({
|
|
48
|
+
input,
|
|
49
|
+
name,
|
|
50
|
+
...restConfig,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
let hadFailure = false;
|
|
54
|
+
|
|
55
|
+
for (const rollupConfig of rollupConfigs) {
|
|
56
|
+
const { output, ...input } = rollupConfig;
|
|
57
|
+
const success = await runRollup(input, output as OutputOptions);
|
|
58
|
+
|
|
59
|
+
if (!success) {
|
|
60
|
+
hadFailure = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
rollupConfigs.map(async (options) => {});
|
|
65
|
+
|
|
66
|
+
if (hadFailure) {
|
|
67
|
+
spinner.warning(`Failed island: ${name}`);
|
|
68
|
+
} else {
|
|
69
|
+
spinner.success(`Succeeded island: ${name}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
});
|
package/src/bin/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { exec } from "child_process";
|
|
2
|
+
import { promisify } from "util";
|
|
3
|
+
|
|
4
|
+
const execPromise = promisify(exec);
|
|
5
|
+
|
|
6
|
+
export const consoleExecute = async (command: string) => {
|
|
7
|
+
try {
|
|
8
|
+
const { stdout, stderr } = await execPromise(command);
|
|
9
|
+
|
|
10
|
+
if (stdout) {
|
|
11
|
+
console.log(stdout);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (stderr) {
|
|
15
|
+
console.error(stderr);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return stdout;
|
|
19
|
+
} catch (e) {
|
|
20
|
+
console.error(e);
|
|
21
|
+
throw e;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export function createGlobalObject(namespace: string): string {
|
|
2
|
+
return namespace
|
|
3
|
+
.split(".")
|
|
4
|
+
.filter(Boolean)
|
|
5
|
+
.map((_, index, levels) => {
|
|
6
|
+
const path = "window." + levels.slice(0, index + 1).join(".");
|
|
7
|
+
return `${path} = ${path} || {}`;
|
|
8
|
+
})
|
|
9
|
+
.join(";\n");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function assignGlobalProperty(
|
|
13
|
+
namespace: string,
|
|
14
|
+
property: string,
|
|
15
|
+
value: string
|
|
16
|
+
): string {
|
|
17
|
+
const namespacing = createGlobalObject(namespace);
|
|
18
|
+
return `${namespacing};\nwindow.${namespace}['${property}'] = ${value};\n`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function importPackage(packageName: string): string {
|
|
22
|
+
return `import * as ${packageNameToProperty(
|
|
23
|
+
packageName
|
|
24
|
+
)} from "${packageName}";\n`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function importAndGlobalisePackage(
|
|
28
|
+
packageName: string,
|
|
29
|
+
globalName: string
|
|
30
|
+
): string {
|
|
31
|
+
let str = "";
|
|
32
|
+
|
|
33
|
+
str += importPackage(packageName);
|
|
34
|
+
str += `${globalName} = ${packageNameToProperty(packageName)};\n`;
|
|
35
|
+
|
|
36
|
+
return str;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function packageNameToProperty(packageName: string): string {
|
|
40
|
+
return packageName
|
|
41
|
+
.replace(/^@/, "")
|
|
42
|
+
.replace(/\//g, "_")
|
|
43
|
+
.replace(/[-_](.)/g, (_, c) => c.toUpperCase())
|
|
44
|
+
.replace(/^[a-z]/, (c) => c.toUpperCase());
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function renderComponentToFile(
|
|
48
|
+
filename: string,
|
|
49
|
+
component: string
|
|
50
|
+
): string {
|
|
51
|
+
return `var server = require('react-dom/server');
|
|
52
|
+
var fs = require('node:fs/promises');
|
|
53
|
+
var path = require('node:path');
|
|
54
|
+
const html = server.renderToString( module.exports.component( {} ) );
|
|
55
|
+
const file = path.resolve(__dirname, '${filename}');
|
|
56
|
+
fs.writeFile(file, html, { flag: "w+" });`;
|
|
57
|
+
}
|