@wrdagency/react-islands 1.0.5 → 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 +36 -14
- 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
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import commonjsPlugin from "@rollup/plugin-commonjs";
|
|
2
|
+
import resolvePlugin from "@rollup/plugin-node-resolve";
|
|
3
|
+
import replacePlugin from "@rollup/plugin-replace";
|
|
4
|
+
import terserPlugin from "@rollup/plugin-terser";
|
|
5
|
+
import typescriptPlugin from "@rollup/plugin-typescript";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import {
|
|
8
|
+
InputOptions,
|
|
9
|
+
ModuleFormat,
|
|
10
|
+
OutputOptions,
|
|
11
|
+
Plugin,
|
|
12
|
+
rollup,
|
|
13
|
+
RollupBuild,
|
|
14
|
+
RollupOptions,
|
|
15
|
+
} from "rollup";
|
|
16
|
+
import { typescriptPaths as typescriptPathsPlugin } from "rollup-plugin-typescript-paths";
|
|
17
|
+
import * as codeGen from "./generation";
|
|
18
|
+
import { BuildOptions, normalizeOptions } from "./options";
|
|
19
|
+
import { runScriptAfterBuildPlugin } from "./plugins/runScriptAfterBuildPlugin";
|
|
20
|
+
import { virtualizeDependencyPlugin } from "./plugins/virtualizeDependencyPlugin";
|
|
21
|
+
|
|
22
|
+
export function createRollupConfigs(options: BuildOptions): RollupOptions[] {
|
|
23
|
+
const normalized = normalizeOptions(options);
|
|
24
|
+
const { name, input, output, minify, ssg, jsx, typescript, common } =
|
|
25
|
+
normalized;
|
|
26
|
+
|
|
27
|
+
const configs: (RollupOptions | null)[] = [];
|
|
28
|
+
|
|
29
|
+
const createRollupConfig = (opts: {
|
|
30
|
+
active?: boolean;
|
|
31
|
+
name?: string;
|
|
32
|
+
subName: string;
|
|
33
|
+
format: ModuleFormat;
|
|
34
|
+
globals?: Record<string, string>;
|
|
35
|
+
prefix?: (name: string) => string;
|
|
36
|
+
suffix?: (name: string) => string;
|
|
37
|
+
plugins?: Plugin[];
|
|
38
|
+
}): RollupOptions | null => {
|
|
39
|
+
const {
|
|
40
|
+
active = true,
|
|
41
|
+
name: mainName = `Islands.${name}`,
|
|
42
|
+
subName,
|
|
43
|
+
format,
|
|
44
|
+
globals = {},
|
|
45
|
+
prefix,
|
|
46
|
+
suffix,
|
|
47
|
+
plugins = [],
|
|
48
|
+
} = opts;
|
|
49
|
+
|
|
50
|
+
if (!active) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
input,
|
|
56
|
+
external: Object.keys(globals),
|
|
57
|
+
jsx,
|
|
58
|
+
output: {
|
|
59
|
+
name: mainName,
|
|
60
|
+
globals,
|
|
61
|
+
format,
|
|
62
|
+
entryFileNames: `[name]/${subName}`,
|
|
63
|
+
dir: output,
|
|
64
|
+
banner: prefix && ((chunk) => prefix(chunk.name)),
|
|
65
|
+
footer: suffix && ((chunk) => suffix(chunk.name)),
|
|
66
|
+
},
|
|
67
|
+
plugins: [
|
|
68
|
+
resolvePlugin({
|
|
69
|
+
extensions: [
|
|
70
|
+
".cjs",
|
|
71
|
+
".mjs",
|
|
72
|
+
".js",
|
|
73
|
+
".json",
|
|
74
|
+
".node",
|
|
75
|
+
".jsx",
|
|
76
|
+
".ts",
|
|
77
|
+
".tsx",
|
|
78
|
+
],
|
|
79
|
+
}),
|
|
80
|
+
commonjsPlugin(),
|
|
81
|
+
replacePlugin({
|
|
82
|
+
preventAssignment: true,
|
|
83
|
+
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
84
|
+
}),
|
|
85
|
+
minify && terserPlugin(),
|
|
86
|
+
typescript &&
|
|
87
|
+
typescriptPlugin({
|
|
88
|
+
jsx,
|
|
89
|
+
}),
|
|
90
|
+
typescript && typescriptPathsPlugin(),
|
|
91
|
+
...plugins,
|
|
92
|
+
],
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
configs.push(
|
|
97
|
+
createRollupConfig({
|
|
98
|
+
active: true,
|
|
99
|
+
subName: "client.js",
|
|
100
|
+
format: "iife",
|
|
101
|
+
globals: common,
|
|
102
|
+
suffix: (name) => `\nwindow.Islands['${name}']?.render('${name}')`,
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
configs.push(
|
|
107
|
+
createRollupConfig({
|
|
108
|
+
active: ssg,
|
|
109
|
+
subName: "server.cjs",
|
|
110
|
+
format: "cjs",
|
|
111
|
+
suffix: () =>
|
|
112
|
+
codeGen.renderComponentToFile("ssg.html", "module.exports.component"),
|
|
113
|
+
plugins: [
|
|
114
|
+
runScriptAfterBuildPlugin({
|
|
115
|
+
deleteAfterRunning: true,
|
|
116
|
+
}),
|
|
117
|
+
],
|
|
118
|
+
})
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return configs.filter(isRollupOptions);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function createCommonConfig(options: BuildOptions): RollupOptions {
|
|
125
|
+
const normalized = normalizeOptions(options);
|
|
126
|
+
const { output, minify, jsx, common } = normalized;
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
input: "virtual-entry",
|
|
130
|
+
jsx,
|
|
131
|
+
output: {
|
|
132
|
+
name: "Islands._Common",
|
|
133
|
+
file: path.resolve(output, "common.js"),
|
|
134
|
+
format: "iife",
|
|
135
|
+
},
|
|
136
|
+
plugins: [
|
|
137
|
+
replacePlugin({
|
|
138
|
+
preventAssignment: true,
|
|
139
|
+
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
140
|
+
}),
|
|
141
|
+
virtualizeDependencyPlugin({
|
|
142
|
+
dependencies: common,
|
|
143
|
+
namespace: "Islands._Common",
|
|
144
|
+
}),
|
|
145
|
+
resolvePlugin(),
|
|
146
|
+
commonjsPlugin(),
|
|
147
|
+
minify && terserPlugin(),
|
|
148
|
+
],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function isRollupOptions(
|
|
153
|
+
config: RollupOptions | null
|
|
154
|
+
): config is RollupOptions {
|
|
155
|
+
return config !== null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function runRollup(
|
|
159
|
+
inputOptions: InputOptions,
|
|
160
|
+
outputOptions: OutputOptions
|
|
161
|
+
) {
|
|
162
|
+
let bundle: RollupBuild | undefined;
|
|
163
|
+
let buildFailed = false;
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
bundle = await rollup(inputOptions);
|
|
167
|
+
await bundle.write(outputOptions);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
buildFailed = true;
|
|
170
|
+
console.error(error);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (bundle) {
|
|
174
|
+
await bundle.close();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return !buildFailed;
|
|
178
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { JsxPreset } from "rollup";
|
|
2
|
+
import { packageNameToProperty } from "./generation";
|
|
3
|
+
|
|
4
|
+
export type BuildOptions = {
|
|
5
|
+
name: string;
|
|
6
|
+
input: string;
|
|
7
|
+
output: string;
|
|
8
|
+
minify?: boolean;
|
|
9
|
+
ssg?: boolean;
|
|
10
|
+
jsx?: JsxPreset;
|
|
11
|
+
typescript?: boolean;
|
|
12
|
+
common?: string[] | Record<string, string>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type NormalizedBuildOptions = {
|
|
16
|
+
name: string;
|
|
17
|
+
input: string;
|
|
18
|
+
output: string;
|
|
19
|
+
minify: boolean;
|
|
20
|
+
ssg: boolean;
|
|
21
|
+
jsx: JsxPreset;
|
|
22
|
+
typescript: boolean;
|
|
23
|
+
common: Record<string, string>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const normalizeOptions = (
|
|
27
|
+
options: BuildOptions
|
|
28
|
+
): NormalizedBuildOptions => {
|
|
29
|
+
return {
|
|
30
|
+
name: options.name,
|
|
31
|
+
input: options.input,
|
|
32
|
+
output: options.output || "./dist/",
|
|
33
|
+
minify: normalizeBoolean(options.minify, true),
|
|
34
|
+
ssg: normalizeBoolean(options.ssg, true),
|
|
35
|
+
jsx: options.jsx || "react-jsx",
|
|
36
|
+
typescript: normalizeBoolean(options.ssg, false),
|
|
37
|
+
common: normalizeCommonOption(
|
|
38
|
+
options.common || [
|
|
39
|
+
"react",
|
|
40
|
+
"react-dom/client",
|
|
41
|
+
"@wrdagency/react-islands",
|
|
42
|
+
]
|
|
43
|
+
),
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const normalizeBoolean = (
|
|
48
|
+
value: boolean | undefined | null | void,
|
|
49
|
+
fallback: boolean
|
|
50
|
+
): boolean => {
|
|
51
|
+
if (value === false) {
|
|
52
|
+
return false;
|
|
53
|
+
} else if (value === true) {
|
|
54
|
+
return true;
|
|
55
|
+
} else {
|
|
56
|
+
return fallback;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const normalizeCommonOption = (
|
|
61
|
+
share: BuildOptions["common"] = [""]
|
|
62
|
+
): NormalizedBuildOptions["common"] => {
|
|
63
|
+
if (Array.isArray(share)) {
|
|
64
|
+
return share.reduce(
|
|
65
|
+
(record, packageName) => ({
|
|
66
|
+
...record,
|
|
67
|
+
[packageName]: "Islands._Common." + packageNameToProperty(packageName),
|
|
68
|
+
}),
|
|
69
|
+
{}
|
|
70
|
+
);
|
|
71
|
+
} else {
|
|
72
|
+
return share;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { Plugin } from "rollup";
|
|
4
|
+
import { consoleExecute } from "../console";
|
|
5
|
+
|
|
6
|
+
type Options = {
|
|
7
|
+
deleteAfterRunning?: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function runScriptAfterBuildPlugin(opts: Options): Plugin {
|
|
11
|
+
const { deleteAfterRunning = false } = opts;
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
name: "rollup-plugin-run-script-after-builder",
|
|
15
|
+
writeBundle(outputOptions, bundle) {
|
|
16
|
+
const outputDir = outputOptions.dir
|
|
17
|
+
? outputOptions.dir
|
|
18
|
+
: path.dirname(outputOptions.file || "");
|
|
19
|
+
|
|
20
|
+
for (const [fileName, chunkInfo] of Object.entries(bundle)) {
|
|
21
|
+
const filePath = path.resolve(outputDir, fileName);
|
|
22
|
+
|
|
23
|
+
if (chunkInfo.type !== "chunk" || !chunkInfo.isEntry) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!filePath && !filePath.endsWith("js")) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!fs.existsSync(filePath)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
consoleExecute(`node ${filePath}`).then(() => {
|
|
36
|
+
if (deleteAfterRunning) {
|
|
37
|
+
fs.unlinkSync(filePath);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Plugin } from "rollup";
|
|
2
|
+
import * as codeGen from "../generation";
|
|
3
|
+
|
|
4
|
+
type Options = {
|
|
5
|
+
dependencies: Record<string, string>;
|
|
6
|
+
namespace?: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function virtualizeDependencyPlugin(opts: Options): Plugin {
|
|
10
|
+
const { dependencies, namespace = "" } = opts;
|
|
11
|
+
|
|
12
|
+
const VIRTUAL_MODULE_ID = "\0virtual-entry";
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
name: "rollup-plugin-virtualize-dependency",
|
|
16
|
+
resolveId(source) {
|
|
17
|
+
if (source === "virtual-entry") return VIRTUAL_MODULE_ID;
|
|
18
|
+
return null;
|
|
19
|
+
},
|
|
20
|
+
load(id) {
|
|
21
|
+
if (id !== VIRTUAL_MODULE_ID) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let str = codeGen.createGlobalObject(namespace) + ";\n";
|
|
26
|
+
|
|
27
|
+
for (const [dependency, name] of Object.entries(dependencies)) {
|
|
28
|
+
str += codeGen.importAndGlobalisePackage(dependency, name);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return str;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import commandLineArgs from "command-line-args";
|
|
2
|
+
import commandLineUsage from "command-line-usage";
|
|
3
|
+
|
|
4
|
+
export class Command {
|
|
5
|
+
public readonly args: commandLineArgs.OptionDefinition[];
|
|
6
|
+
public readonly callback: (args: commandLineArgs.CommandLineOptions) => void;
|
|
7
|
+
public readonly description: string;
|
|
8
|
+
|
|
9
|
+
constructor(options: {
|
|
10
|
+
args: commandLineArgs.OptionDefinition[];
|
|
11
|
+
callback: (args: commandLineArgs.CommandLineOptions) => void;
|
|
12
|
+
description: string;
|
|
13
|
+
}) {
|
|
14
|
+
let { args, callback, description } = options;
|
|
15
|
+
|
|
16
|
+
args.push({
|
|
17
|
+
name: "help",
|
|
18
|
+
alias: "h",
|
|
19
|
+
type: Boolean,
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
description: "Display this usage guide.",
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
this.args = args;
|
|
25
|
+
this.callback = callback;
|
|
26
|
+
this.description = description;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
run(argv: string[]) {
|
|
30
|
+
const { help, ...args } = commandLineArgs(this.args, { argv });
|
|
31
|
+
|
|
32
|
+
if (help) {
|
|
33
|
+
console.log(
|
|
34
|
+
commandLineUsage([
|
|
35
|
+
{
|
|
36
|
+
header: "Options",
|
|
37
|
+
optionList: this.args,
|
|
38
|
+
},
|
|
39
|
+
])
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return this.callback(args);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function commandset(commands: Record<string, Command>) {
|
|
50
|
+
const {
|
|
51
|
+
command,
|
|
52
|
+
help = false,
|
|
53
|
+
_unknown: argv = [],
|
|
54
|
+
} = commandLineArgs(
|
|
55
|
+
[
|
|
56
|
+
{
|
|
57
|
+
name: "command",
|
|
58
|
+
type: String,
|
|
59
|
+
defaultOption: true,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: "help",
|
|
63
|
+
alias: "h",
|
|
64
|
+
type: Boolean,
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
description: "Display this usage guide.",
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
{
|
|
70
|
+
stopAtFirstUnknown: true,
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
if (!command || !(command in commands)) {
|
|
75
|
+
if (help) {
|
|
76
|
+
const commandsSummary = Object.keys(commands).map((key) => ({
|
|
77
|
+
name: key,
|
|
78
|
+
summary: commands[key].description,
|
|
79
|
+
}));
|
|
80
|
+
|
|
81
|
+
const usage = commandLineUsage([
|
|
82
|
+
{
|
|
83
|
+
header: "Synopsis",
|
|
84
|
+
content: "npx react-islands <command> <options>",
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
header: "Command List",
|
|
88
|
+
content: commandsSummary,
|
|
89
|
+
},
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
console.log(usage);
|
|
93
|
+
} else {
|
|
94
|
+
console.error(`Command '${command}' not recognized by React Islands.`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return commands[command].run([...argv, help && "--help"].filter(Boolean));
|
|
101
|
+
}
|
package/src/index.tsx
ADDED
package/src/island.tsx
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { createRoot } from "react-dom/client";
|
|
2
|
+
import { RawHTML, withProps } from "./util";
|
|
3
|
+
|
|
4
|
+
export interface IslandRenderOptions {
|
|
5
|
+
shouldHydrate: boolean;
|
|
6
|
+
multiple: boolean;
|
|
7
|
+
keepChildren: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface IslandOpts extends Partial<IslandRenderOptions> {}
|
|
11
|
+
|
|
12
|
+
export class Island {
|
|
13
|
+
public readonly component: React.FC;
|
|
14
|
+
public readonly renderOptions: IslandRenderOptions;
|
|
15
|
+
|
|
16
|
+
public constructor(component: React.FC, opts: IslandOpts = {}) {
|
|
17
|
+
this.component = component;
|
|
18
|
+
|
|
19
|
+
this.renderOptions = {
|
|
20
|
+
shouldHydrate: opts.shouldHydrate ?? true,
|
|
21
|
+
multiple: opts.multiple ?? false,
|
|
22
|
+
keepChildren: opts.keepChildren ?? false,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private getProps(element: HTMLElement): React.PropsWithChildren {
|
|
27
|
+
const { keepChildren } = this.renderOptions;
|
|
28
|
+
let props: React.PropsWithChildren = {};
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
const json = element.dataset.props || "{}";
|
|
32
|
+
|
|
33
|
+
if (json) {
|
|
34
|
+
const parsed = JSON.parse(json);
|
|
35
|
+
|
|
36
|
+
if (typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
`Parsed JSON is not a valid dictionary object: '${json}'`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (parsed) {
|
|
43
|
+
// Ignore null.
|
|
44
|
+
props = parsed;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
} catch (e) {
|
|
48
|
+
console.warn("Could not parse JSON props for React Island.");
|
|
49
|
+
console.error(e);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (keepChildren) {
|
|
53
|
+
props.children = <RawHTML html={element.innerHTML} />;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return props;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private getRoots(name: string): HTMLElement[] {
|
|
60
|
+
const selector = `[data-island="${name}"]`;
|
|
61
|
+
const { multiple } = this.renderOptions;
|
|
62
|
+
const nodes = [...document.querySelectorAll(selector)];
|
|
63
|
+
|
|
64
|
+
if (!nodes) {
|
|
65
|
+
console.warn(
|
|
66
|
+
`Could not render React Island because DOM node (${selector}) could not be found.`
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (nodes.length > 1 && !multiple) {
|
|
73
|
+
console.warn(
|
|
74
|
+
`Multiple elements matched React Island selector (${selector}) but multiple was not enabled. Choosing first element as root.`
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return [nodes[0] as HTMLElement];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return nodes as HTMLElement[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public render(name: string): void {
|
|
84
|
+
this.getRoots(name).forEach((element) => {
|
|
85
|
+
const props = this.getProps(element);
|
|
86
|
+
const Component = withProps(this.component, props);
|
|
87
|
+
|
|
88
|
+
const root = createRoot(element);
|
|
89
|
+
root.render(<Component />);
|
|
90
|
+
|
|
91
|
+
return root;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
package/src/util.tsx
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { FC, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Internal component for rendering raw HTML in a React component.
|
|
5
|
+
*/
|
|
6
|
+
export function RawHTML({ html }: { html: string }) {
|
|
7
|
+
const ref = useRef<HTMLScriptElement>(null);
|
|
8
|
+
|
|
9
|
+
// important to not have ANY deps
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (ref.current) {
|
|
12
|
+
ref.current.outerHTML = html;
|
|
13
|
+
}
|
|
14
|
+
}, []);
|
|
15
|
+
|
|
16
|
+
return <script ref={ref} />;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a higher-order component with certain fixed.
|
|
21
|
+
*
|
|
22
|
+
* Useful for quickly creating multiple variants of the same component to use as islands.
|
|
23
|
+
*
|
|
24
|
+
* @param component FC<T>
|
|
25
|
+
* @param setProps Partial<T>
|
|
26
|
+
* @returns FC<T>
|
|
27
|
+
*/
|
|
28
|
+
export function withProps<T>(component: FC<T>, setProps: Partial<T>): FC<T> {
|
|
29
|
+
return (props: T) => {
|
|
30
|
+
return component({ ...props, ...setProps });
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Checks if the current script is running in a pre-render.
|
|
36
|
+
*
|
|
37
|
+
* @returns boolean
|
|
38
|
+
*/
|
|
39
|
+
export function isServer(): boolean {
|
|
40
|
+
return !(typeof window != "undefined" && window.document);
|
|
41
|
+
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { WebpackOptionsNormalized, Compiler } from 'webpack';
|
|
2
|
-
|
|
3
|
-
declare class ReactIslandsWebpackPlugin {
|
|
4
|
-
createSubCompiler(dir: string, entry: Record<string, any>, options: WebpackOptionsNormalized): Compiler;
|
|
5
|
-
apply(compiler: Compiler): Promise<void>;
|
|
6
|
-
}
|
|
7
|
-
declare function createIsland(name: string, Component: React.FC): void;
|
|
8
|
-
|
|
9
|
-
export { ReactIslandsWebpackPlugin, createIsland };
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n\tCompilation,\n\tCompiler,\n\tConfiguration,\n\tWebpackOptionsNormalized,\n\twebpack,\n} from \"webpack\";\nimport tmp from \"tmp\";\nimport { renderToStaticMarkup } from \"react-dom/server\";\nimport { hydrateRoot } from \"react-dom/client\";\n\ntmp.setGracefulCleanup();\n\nexport class ReactIslandsWebpackPlugin {\n\tcreateSubCompiler(\n\t\tdir: string,\n\t\tentry: Record<string, any>,\n\t\toptions: WebpackOptionsNormalized\n\t): Compiler {\n\t\tconst plugins = options.plugins.filter(\n\t\t\t(plugin) => !(plugin instanceof ReactIslandsWebpackPlugin)\n\t\t);\n\n\t\tconst compilerOptions: Configuration = {\n\t\t\tmode: \"development\",\n\t\t\ttarget: \"node\",\n\t\t\tcontext: process.cwd(),\n\t\t\tentry,\n\t\t\toutput: {\n\t\t\t\tlibraryTarget: \"commonjs\",\n\t\t\t\tpath: dir,\n\t\t\t\tfilename: \"[name].js\",\n\t\t\t},\n\t\t\tmodule: options.module,\n\t\t\tstats: false,\n\t\t\tdevtool: false,\n\t\t\toptimization: {\n\t\t\t\tminimize: false,\n\t\t\t},\n\t\t\tplugins,\n\t\t};\n\n\t\treturn webpack(compilerOptions);\n\t}\n\n\tasync apply(compiler: Compiler) {\n\t\tlet components: string[] = [];\n\n\t\tconst options = compiler.options;\n\n\t\tconst entry =\n\t\t\ttypeof options.entry === \"function\"\n\t\t\t\t? await options.entry()\n\t\t\t\t: options.entry;\n\n\t\tconst { name: dir } = tmp.dirSync();\n\n\t\tconst { RawSource } = compiler.webpack.sources;\n\n\t\tcompiler.hooks.run.tapAsync(\n\t\t\t\"ReactIslandsWebpackPlugin\",\n\t\t\t(compiler: Compiler, callback) => {\n\t\t\t\tcomponents = Object.keys(entry);\n\n\t\t\t\tconst subCompiler = this.createSubCompiler(dir, entry, options);\n\n\t\t\t\tsubCompiler.run((err, stats) => {\n\t\t\t\t\tif (err) {\n\t\t\t\t\t\tconsole.error(err);\n\t\t\t\t\t\tthrow new Error(\"ReactIslandsWebpackPlugin: Error during compile.\");\n\t\t\t\t\t}\n\n\t\t\t\t\tif (stats?.hasErrors()) {\n\t\t\t\t\t\tconsole.log(stats?.toString());\n\t\t\t\t\t\tthrow new Error(\"ReactIslandsWebpackPlugin: Failed to compile.\");\n\t\t\t\t\t}\n\n\t\t\t\t\tcallback();\n\t\t\t\t\tconsole.log(\"ReactIslandsWebpackPlugin: Compilation complete.\");\n\t\t\t\t});\n\t\t\t}\n\t\t);\n\n\t\tcompiler.hooks.thisCompilation.tap(\n\t\t\t\"ReactIslandsWebpackPlugin\",\n\t\t\t(compilation: Compilation) => {\n\t\t\t\tfor (const name of components) {\n\t\t\t\t\tconst file = `${dir}/${name}.js`;\n\t\t\t\t\tconst { default: component } = require(file);\n\n\t\t\t\t\tconst markup = renderToStaticMarkup(component());\n\n\t\t\t\t\tcompilation.emitAsset(`${name}.html`, new RawSource(markup));\n\t\t\t\t}\n\t\t\t}\n\t\t);\n\t}\n}\n\nexport function createIsland(name: string, Component: React.FC) {\n\tconst isBrowser =\n\t\ttypeof window !== \"undefined\" && typeof window.document !== \"undefined\";\n\n\tif (isBrowser) {\n\t\tdocument\n\t\t\t.querySelectorAll(`[data-react-component=\"${name}\"]`)\n\t\t\t.forEach((element) => {\n\t\t\t\tconst propsJSON = element.getAttribute(\"data-react-props\") || \"{}\";\n\t\t\t\tconst props = JSON.parse(propsJSON);\n\t\t\t\thydrateRoot(element, Component(props));\n\t\t\t});\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAMO;AACP,iBAAgB;AAChB,oBAAqC;AACrC,oBAA4B;AAE5B,WAAAA,QAAI,mBAAmB;AAEhB,IAAM,4BAAN,MAAM,2BAA0B;AAAA,EACtC,kBACC,KACA,OACA,SACW;AACX,UAAM,UAAU,QAAQ,QAAQ;AAAA,MAC/B,CAAC,WAAW,EAAE,kBAAkB;AAAA,IACjC;AAEA,UAAM,kBAAiC;AAAA,MACtC,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,SAAS,QAAQ,IAAI;AAAA,MACrB;AAAA,MACA,QAAQ;AAAA,QACP,eAAe;AAAA,QACf,MAAM;AAAA,QACN,UAAU;AAAA,MACX;AAAA,MACA,QAAQ,QAAQ;AAAA,MAChB,OAAO;AAAA,MACP,SAAS;AAAA,MACT,cAAc;AAAA,QACb,UAAU;AAAA,MACX;AAAA,MACA;AAAA,IACD;AAEA,eAAO,wBAAQ,eAAe;AAAA,EAC/B;AAAA,EAEM,MAAM,UAAoB;AAAA;AAC/B,UAAI,aAAuB,CAAC;AAE5B,YAAM,UAAU,SAAS;AAEzB,YAAM,QACL,OAAO,QAAQ,UAAU,aACtB,MAAM,QAAQ,MAAM,IACpB,QAAQ;AAEZ,YAAM,EAAE,MAAM,IAAI,IAAI,WAAAA,QAAI,QAAQ;AAElC,YAAM,EAAE,UAAU,IAAI,SAAS,QAAQ;AAEvC,eAAS,MAAM,IAAI;AAAA,QAClB;AAAA,QACA,CAACC,WAAoB,aAAa;AACjC,uBAAa,OAAO,KAAK,KAAK;AAE9B,gBAAM,cAAc,KAAK,kBAAkB,KAAK,OAAO,OAAO;AAE9D,sBAAY,IAAI,CAAC,KAAK,UAAU;AAC/B,gBAAI,KAAK;AACR,sBAAQ,MAAM,GAAG;AACjB,oBAAM,IAAI,MAAM,kDAAkD;AAAA,YACnE;AAEA,gBAAI,+BAAO,aAAa;AACvB,sBAAQ,IAAI,+BAAO,UAAU;AAC7B,oBAAM,IAAI,MAAM,+CAA+C;AAAA,YAChE;AAEA,qBAAS;AACT,oBAAQ,IAAI,kDAAkD;AAAA,UAC/D,CAAC;AAAA,QACF;AAAA,MACD;AAEA,eAAS,MAAM,gBAAgB;AAAA,QAC9B;AAAA,QACA,CAAC,gBAA6B;AAC7B,qBAAW,QAAQ,YAAY;AAC9B,kBAAM,OAAO,GAAG,GAAG,IAAI,IAAI;AAC3B,kBAAM,EAAE,SAAS,UAAU,IAAI,QAAQ,IAAI;AAE3C,kBAAM,aAAS,oCAAqB,UAAU,CAAC;AAE/C,wBAAY,UAAU,GAAG,IAAI,SAAS,IAAI,UAAU,MAAM,CAAC;AAAA,UAC5D;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA;AACD;AAEO,SAAS,aAAa,MAAc,WAAqB;AAC/D,QAAM,YACL,OAAO,WAAW,eAAe,OAAO,OAAO,aAAa;AAE7D,MAAI,WAAW;AACd,aACE,iBAAiB,0BAA0B,IAAI,IAAI,EACnD,QAAQ,CAAC,YAAY;AACrB,YAAM,YAAY,QAAQ,aAAa,kBAAkB,KAAK;AAC9D,YAAM,QAAQ,KAAK,MAAM,SAAS;AAClC,qCAAY,SAAS,UAAU,KAAK,CAAC;AAAA,IACtC,CAAC;AAAA,EACH;AACD;","names":["tmp","compiler"]}
|