@wrdagency/react-islands 1.0.5 → 2.0.1
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/bin/index.js +411 -0
- package/dist/index.d.ts +46 -6
- package/dist/index.js +96 -136
- package/package.json +37 -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,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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,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"]}
|
package/dist/index.mjs
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined")
|
|
5
|
-
return require.apply(this, arguments);
|
|
6
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
-
});
|
|
8
|
-
var __async = (__this, __arguments, generator) => {
|
|
9
|
-
return new Promise((resolve, reject) => {
|
|
10
|
-
var fulfilled = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.next(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var rejected = (value) => {
|
|
18
|
-
try {
|
|
19
|
-
step(generator.throw(value));
|
|
20
|
-
} catch (e) {
|
|
21
|
-
reject(e);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
25
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// src/index.ts
|
|
30
|
-
import {
|
|
31
|
-
webpack
|
|
32
|
-
} from "webpack";
|
|
33
|
-
import tmp from "tmp";
|
|
34
|
-
import { renderToStaticMarkup } from "react-dom/server";
|
|
35
|
-
import { hydrateRoot } from "react-dom/client";
|
|
36
|
-
tmp.setGracefulCleanup();
|
|
37
|
-
var ReactIslandsWebpackPlugin = class _ReactIslandsWebpackPlugin {
|
|
38
|
-
createSubCompiler(dir, entry, options) {
|
|
39
|
-
const plugins = options.plugins.filter(
|
|
40
|
-
(plugin) => !(plugin instanceof _ReactIslandsWebpackPlugin)
|
|
41
|
-
);
|
|
42
|
-
const compilerOptions = {
|
|
43
|
-
mode: "development",
|
|
44
|
-
target: "node",
|
|
45
|
-
context: process.cwd(),
|
|
46
|
-
entry,
|
|
47
|
-
output: {
|
|
48
|
-
libraryTarget: "commonjs",
|
|
49
|
-
path: dir,
|
|
50
|
-
filename: "[name].js"
|
|
51
|
-
},
|
|
52
|
-
module: options.module,
|
|
53
|
-
stats: false,
|
|
54
|
-
devtool: false,
|
|
55
|
-
optimization: {
|
|
56
|
-
minimize: false
|
|
57
|
-
},
|
|
58
|
-
plugins
|
|
59
|
-
};
|
|
60
|
-
return webpack(compilerOptions);
|
|
61
|
-
}
|
|
62
|
-
apply(compiler) {
|
|
63
|
-
return __async(this, null, function* () {
|
|
64
|
-
let components = [];
|
|
65
|
-
const options = compiler.options;
|
|
66
|
-
const entry = typeof options.entry === "function" ? yield options.entry() : options.entry;
|
|
67
|
-
const { name: dir } = tmp.dirSync();
|
|
68
|
-
const { RawSource } = compiler.webpack.sources;
|
|
69
|
-
compiler.hooks.run.tapAsync(
|
|
70
|
-
"ReactIslandsWebpackPlugin",
|
|
71
|
-
(compiler2, callback) => {
|
|
72
|
-
components = Object.keys(entry);
|
|
73
|
-
const subCompiler = this.createSubCompiler(dir, entry, options);
|
|
74
|
-
subCompiler.run((err, stats) => {
|
|
75
|
-
if (err) {
|
|
76
|
-
console.error(err);
|
|
77
|
-
throw new Error("ReactIslandsWebpackPlugin: Error during compile.");
|
|
78
|
-
}
|
|
79
|
-
if (stats == null ? void 0 : stats.hasErrors()) {
|
|
80
|
-
console.log(stats == null ? void 0 : stats.toString());
|
|
81
|
-
throw new Error("ReactIslandsWebpackPlugin: Failed to compile.");
|
|
82
|
-
}
|
|
83
|
-
callback();
|
|
84
|
-
console.log("ReactIslandsWebpackPlugin: Compilation complete.");
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
);
|
|
88
|
-
compiler.hooks.thisCompilation.tap(
|
|
89
|
-
"ReactIslandsWebpackPlugin",
|
|
90
|
-
(compilation) => {
|
|
91
|
-
for (const name of components) {
|
|
92
|
-
const file = `${dir}/${name}.js`;
|
|
93
|
-
const { default: component } = __require(file);
|
|
94
|
-
const markup = renderToStaticMarkup(component());
|
|
95
|
-
compilation.emitAsset(`${name}.html`, new RawSource(markup));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
);
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
function createIsland(name, Component) {
|
|
103
|
-
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
104
|
-
if (isBrowser) {
|
|
105
|
-
document.querySelectorAll(`[data-react-component="${name}"]`).forEach((element) => {
|
|
106
|
-
const propsJSON = element.getAttribute("data-react-props") || "{}";
|
|
107
|
-
const props = JSON.parse(propsJSON);
|
|
108
|
-
hydrateRoot(element, Component(props));
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
export {
|
|
113
|
-
ReactIslandsWebpackPlugin,
|
|
114
|
-
createIsland
|
|
115
|
-
};
|
|
116
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.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,EAKC;AAAA,OACM;AACP,OAAO,SAAS;AAChB,SAAS,4BAA4B;AACrC,SAAS,mBAAmB;AAE5B,IAAI,mBAAmB;AAEhB,IAAM,4BAAN,MAAM,2BAA0B;AAAA,EACtC,kBACC,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7B,qBAAW,QAAQ,YAAY;AAC9B,kBAAM,OAAO,GAAG,GAAG,IAAI,IAAI;AAC3B,kBAAM,EAAE,SAAS,UAAU,IAAI,UAAQ,IAAI;AAE3C,kBAAM,SAAS,qBAAqB,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,kBAAY,SAAS,UAAU,KAAK,CAAC;AAAA,IACtC,CAAC;AAAA,EACH;AACD;","names":["compiler"]}
|
package/src/index.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Compilation,
|
|
3
|
-
Compiler,
|
|
4
|
-
Configuration,
|
|
5
|
-
WebpackOptionsNormalized,
|
|
6
|
-
webpack,
|
|
7
|
-
} from "webpack";
|
|
8
|
-
import tmp from "tmp";
|
|
9
|
-
import { renderToStaticMarkup } from "react-dom/server";
|
|
10
|
-
import { hydrateRoot } from "react-dom/client";
|
|
11
|
-
|
|
12
|
-
tmp.setGracefulCleanup();
|
|
13
|
-
|
|
14
|
-
export class ReactIslandsWebpackPlugin {
|
|
15
|
-
createSubCompiler(
|
|
16
|
-
dir: string,
|
|
17
|
-
entry: Record<string, any>,
|
|
18
|
-
options: WebpackOptionsNormalized
|
|
19
|
-
): Compiler {
|
|
20
|
-
const plugins = options.plugins.filter(
|
|
21
|
-
(plugin) => !(plugin instanceof ReactIslandsWebpackPlugin)
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const compilerOptions: Configuration = {
|
|
25
|
-
mode: "development",
|
|
26
|
-
target: "node",
|
|
27
|
-
context: process.cwd(),
|
|
28
|
-
entry,
|
|
29
|
-
output: {
|
|
30
|
-
libraryTarget: "commonjs",
|
|
31
|
-
path: dir,
|
|
32
|
-
filename: "[name].js",
|
|
33
|
-
},
|
|
34
|
-
module: options.module,
|
|
35
|
-
stats: false,
|
|
36
|
-
devtool: false,
|
|
37
|
-
optimization: {
|
|
38
|
-
minimize: false,
|
|
39
|
-
},
|
|
40
|
-
plugins,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return webpack(compilerOptions);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async apply(compiler: Compiler) {
|
|
47
|
-
let components: string[] = [];
|
|
48
|
-
|
|
49
|
-
const options = compiler.options;
|
|
50
|
-
|
|
51
|
-
const entry =
|
|
52
|
-
typeof options.entry === "function"
|
|
53
|
-
? await options.entry()
|
|
54
|
-
: options.entry;
|
|
55
|
-
|
|
56
|
-
const { name: dir } = tmp.dirSync();
|
|
57
|
-
|
|
58
|
-
const { RawSource } = compiler.webpack.sources;
|
|
59
|
-
|
|
60
|
-
compiler.hooks.run.tapAsync(
|
|
61
|
-
"ReactIslandsWebpackPlugin",
|
|
62
|
-
(compiler: Compiler, callback) => {
|
|
63
|
-
components = Object.keys(entry);
|
|
64
|
-
|
|
65
|
-
const subCompiler = this.createSubCompiler(dir, entry, options);
|
|
66
|
-
|
|
67
|
-
subCompiler.run((err, stats) => {
|
|
68
|
-
if (err) {
|
|
69
|
-
console.error(err);
|
|
70
|
-
throw new Error("ReactIslandsWebpackPlugin: Error during compile.");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (stats?.hasErrors()) {
|
|
74
|
-
console.log(stats?.toString());
|
|
75
|
-
throw new Error("ReactIslandsWebpackPlugin: Failed to compile.");
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
callback();
|
|
79
|
-
console.log("ReactIslandsWebpackPlugin: Compilation complete.");
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
compiler.hooks.thisCompilation.tap(
|
|
85
|
-
"ReactIslandsWebpackPlugin",
|
|
86
|
-
(compilation: Compilation) => {
|
|
87
|
-
for (const name of components) {
|
|
88
|
-
const file = `${dir}/${name}.js`;
|
|
89
|
-
const { default: component } = require(file);
|
|
90
|
-
|
|
91
|
-
const markup = renderToStaticMarkup(component());
|
|
92
|
-
|
|
93
|
-
compilation.emitAsset(`${name}.html`, new RawSource(markup));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function createIsland(name: string, Component: React.FC) {
|
|
101
|
-
const isBrowser =
|
|
102
|
-
typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
103
|
-
|
|
104
|
-
if (isBrowser) {
|
|
105
|
-
document
|
|
106
|
-
.querySelectorAll(`[data-react-component="${name}"]`)
|
|
107
|
-
.forEach((element) => {
|
|
108
|
-
const propsJSON = element.getAttribute("data-react-props") || "{}";
|
|
109
|
-
const props = JSON.parse(propsJSON);
|
|
110
|
-
hydrateRoot(element, Component(props));
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"outDir": "./dist/",
|
|
4
|
-
"target": "es2016",
|
|
5
|
-
"module": "Node16",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"allowSyntheticDefaultImports": true,
|
|
8
|
-
"forceConsistentCasingInFileNames": true,
|
|
9
|
-
"strict": true,
|
|
10
|
-
"skipLibCheck": true
|
|
11
|
-
},
|
|
12
|
-
"exclude": ["dist", "node_modules"]
|
|
13
|
-
}
|