@tsrx/turbopack-plugin-react 0.1.110 → 0.2.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 +14 -2
- package/package.json +4 -4
- package/src/client-boundary-loader.js +156 -0
- package/src/index.js +45 -2
- package/src/loader.js +5 -2
- package/types/index.d.ts +11 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# @tsrx/turbopack-plugin-react
|
|
2
2
|
|
|
3
|
-
Turbopack integration for compiling `@tsrx/react` `.tsrx` files in Next.js.
|
|
3
|
+
Turbopack integration for compiling `@tsrx/react` `.tsrx` files in Next.js 16.2 or
|
|
4
|
+
later.
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
@@ -21,7 +22,18 @@ The helper installs Turbopack rules that compile `.tsrx` modules with
|
|
|
21
22
|
`@tsrx/react` and return the result as `*.tsx` so Next's React pipeline can finish
|
|
22
23
|
the JSX transform. Sibling-scoped `<style>` blocks (each styles its siblings and
|
|
23
24
|
everything below them) are emitted through a sibling `?tsrx-css&lang.css` import
|
|
24
|
-
and handed back to Turbopack as a
|
|
25
|
+
and handed back to Turbopack as a `*.css` module, so Next links the stylesheet
|
|
26
|
+
into App Router pages. In the Pages Router, Next rejects this import with "Global
|
|
27
|
+
CSS cannot be imported from files other than your Custom <App>".
|
|
28
|
+
|
|
29
|
+
A module that starts with `'use client'` is imported through a small module that
|
|
30
|
+
keeps the directive and re-exports the compiled component, so Next can create the
|
|
31
|
+
client reference for it.
|
|
32
|
+
|
|
33
|
+
A `'use server'` `.tsrx` module imported by a client component fails when the
|
|
34
|
+
action runs, with "Could not parse module '…/actions.tsrx.tsx', file not found".
|
|
35
|
+
Import the actions in a server component and pass them down, define them inline in
|
|
36
|
+
a server component, or write the actions file as `.ts`.
|
|
25
37
|
|
|
26
38
|
It also adds `.tsrx` to `turbopack.resolveExtensions`.
|
|
27
39
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Turbopack integration for @tsrx/react",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Dominic Gannaway",
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.2.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@tsrx/core": "0.
|
|
24
|
-
"@tsrx/react": "0.
|
|
23
|
+
"@tsrx/core": "0.6.0",
|
|
24
|
+
"@tsrx/react": "0.5.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"next": ">=
|
|
27
|
+
"next": ">=16.2.0"
|
|
28
28
|
},
|
|
29
29
|
"peerDependenciesMeta": {
|
|
30
30
|
"next": {
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { parse } from '@tsrx/react';
|
|
3
|
+
|
|
4
|
+
const CLIENT_QUERY = '?tsrx-client&lang.tsx';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {{
|
|
8
|
+
* resourcePath: string,
|
|
9
|
+
* async: () => (err: unknown, output?: string | null) => void,
|
|
10
|
+
* }} LoaderContext
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** @typedef {ReturnType<typeof parse>} Program */
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {{ type: string, name?: string, value?: unknown }} node
|
|
17
|
+
* @returns {unknown}
|
|
18
|
+
*/
|
|
19
|
+
function export_name(node) {
|
|
20
|
+
return node.type === 'Identifier' ? node.name : node.value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {any} pattern
|
|
25
|
+
* @param {Set<string>} names
|
|
26
|
+
*/
|
|
27
|
+
function add_pattern_names(pattern, names) {
|
|
28
|
+
switch (pattern?.type) {
|
|
29
|
+
case 'Identifier':
|
|
30
|
+
names.add(pattern.name);
|
|
31
|
+
break;
|
|
32
|
+
case 'ObjectPattern':
|
|
33
|
+
for (const property of pattern.properties) {
|
|
34
|
+
add_pattern_names(property.type === 'RestElement' ? property : property.value, names);
|
|
35
|
+
}
|
|
36
|
+
break;
|
|
37
|
+
case 'ArrayPattern':
|
|
38
|
+
for (const element of pattern.elements) add_pattern_names(element, names);
|
|
39
|
+
break;
|
|
40
|
+
case 'AssignmentPattern':
|
|
41
|
+
add_pattern_names(pattern.left, names);
|
|
42
|
+
break;
|
|
43
|
+
case 'RestElement':
|
|
44
|
+
add_pattern_names(pattern.argument, names);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Top-level names that only name a type, which TypeScript erases along with
|
|
51
|
+
* an export of them.
|
|
52
|
+
*
|
|
53
|
+
* @param {Program} ast
|
|
54
|
+
* @returns {Set<string>}
|
|
55
|
+
*/
|
|
56
|
+
function get_type_only_names(ast) {
|
|
57
|
+
/** @type {Set<string>} */
|
|
58
|
+
const types = new Set();
|
|
59
|
+
/** @type {Set<string>} */
|
|
60
|
+
const values = new Set();
|
|
61
|
+
for (const statement of ast.body) {
|
|
62
|
+
const node = /** @type {any} */ (
|
|
63
|
+
statement.type === 'ExportNamedDeclaration' && statement.declaration
|
|
64
|
+
? statement.declaration
|
|
65
|
+
: statement
|
|
66
|
+
);
|
|
67
|
+
switch (node.type) {
|
|
68
|
+
case 'TSInterfaceDeclaration':
|
|
69
|
+
case 'TSTypeAliasDeclaration':
|
|
70
|
+
types.add(node.id.name);
|
|
71
|
+
break;
|
|
72
|
+
case 'ImportDeclaration':
|
|
73
|
+
for (const specifier of node.specifiers) {
|
|
74
|
+
const names =
|
|
75
|
+
node.importKind === 'type' || specifier.importKind === 'type' ? types : values;
|
|
76
|
+
names.add(specifier.local.name);
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
case 'VariableDeclaration':
|
|
80
|
+
for (const declarator of node.declarations) add_pattern_names(declarator.id, values);
|
|
81
|
+
break;
|
|
82
|
+
case 'FunctionDeclaration':
|
|
83
|
+
case 'ClassDeclaration':
|
|
84
|
+
case 'TSEnumDeclaration':
|
|
85
|
+
case 'TSModuleDeclaration':
|
|
86
|
+
if (node.id?.type === 'Identifier') values.add(node.id.name);
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const name of values) types.delete(name);
|
|
91
|
+
return types;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether the module has a runtime default export, which `export *` does not
|
|
96
|
+
* re-export.
|
|
97
|
+
*
|
|
98
|
+
* @param {Program} ast
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
function has_default_export(ast) {
|
|
102
|
+
const type_only = get_type_only_names(ast);
|
|
103
|
+
return ast.body.some((node) => {
|
|
104
|
+
const export_kind = /** @type {any} */ (node).exportKind;
|
|
105
|
+
if (export_kind === 'type') return false;
|
|
106
|
+
switch (node.type) {
|
|
107
|
+
case 'ExportDefaultDeclaration': {
|
|
108
|
+
const declaration = /** @type {any} */ (node.declaration);
|
|
109
|
+
if (declaration.type === 'Identifier') return !type_only.has(declaration.name);
|
|
110
|
+
return (
|
|
111
|
+
declaration.type !== 'TSInterfaceDeclaration' && declaration.type !== 'TSDeclareFunction'
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
case 'ExportAllDeclaration':
|
|
115
|
+
return node.exported != null && export_name(node.exported) === 'default';
|
|
116
|
+
case 'ExportNamedDeclaration':
|
|
117
|
+
return node.specifiers.some(
|
|
118
|
+
(specifier) =>
|
|
119
|
+
/** @type {any} */ (specifier).exportKind !== 'type' &&
|
|
120
|
+
export_name(specifier.exported) === 'default' &&
|
|
121
|
+
(node.source != null || !type_only.has(String(export_name(specifier.local)))),
|
|
122
|
+
);
|
|
123
|
+
default:
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Emits the module that a `'use client'` `.tsrx` file is imported as: the
|
|
131
|
+
* directive and a re-export of the compiled module, which the Turbopack config
|
|
132
|
+
* helper compiles for the client query.
|
|
133
|
+
*
|
|
134
|
+
* @this {LoaderContext}
|
|
135
|
+
* @param {string} source
|
|
136
|
+
* @returns {void}
|
|
137
|
+
*/
|
|
138
|
+
export default function tsrx_react_turbopack_client_boundary_loader(source) {
|
|
139
|
+
const callback = this.async();
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
const ast = parse(source, this.resourcePath);
|
|
143
|
+
const specifier = JSON.stringify(`./${path.basename(this.resourcePath)}${CLIENT_QUERY}`);
|
|
144
|
+
let output = '';
|
|
145
|
+
for (const node of ast.body) {
|
|
146
|
+
const directive = 'directive' in node && node.directive;
|
|
147
|
+
if (!directive) break;
|
|
148
|
+
if (directive === 'use client') output += `'use client';\n`;
|
|
149
|
+
}
|
|
150
|
+
output += `export * from ${specifier};\n`;
|
|
151
|
+
if (has_default_export(ast)) output += `export { default } from ${specifier};\n`;
|
|
152
|
+
callback(null, output);
|
|
153
|
+
} catch (/** @type {any} */ err) {
|
|
154
|
+
callback(err);
|
|
155
|
+
}
|
|
156
|
+
}
|
package/src/index.js
CHANGED
|
@@ -11,8 +11,19 @@ const __dirname = path.dirname(__filename);
|
|
|
11
11
|
const TURBOPACK_JS_LOADER = path.join(__dirname, 'loader.js');
|
|
12
12
|
const TURBOPACK_CSS_LOADER = path.join(__dirname, 'css-loader.js');
|
|
13
13
|
const TURBOPACK_PLATFORM_LOADER = path.join(__dirname, 'platform-loader.js');
|
|
14
|
+
const TURBOPACK_CLIENT_BOUNDARY_LOADER = path.join(__dirname, 'client-boundary-loader.js');
|
|
14
15
|
const DEFAULT_RESOLVE_EXTENSIONS = ['.tsrx', '.tsx', '.ts', '.jsx', '.js', '.mjs', '.json'];
|
|
15
16
|
const CSS_QUERY = '?tsrx-css&lang.css';
|
|
17
|
+
const CLIENT_QUERY = '?tsrx-client&lang.tsx';
|
|
18
|
+
// Whitespace and comments between the tokens of a directive prologue.
|
|
19
|
+
const PROLOGUE_GAP = String.raw`(?:\s|\/\/[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)*`;
|
|
20
|
+
// A directive string, which may contain escapes.
|
|
21
|
+
const DIRECTIVE_STRING = String.raw`(?:'(?:[^'\\\n]|\\[\s\S])*'|"(?:[^"\\\n]|\\[\s\S])*")`;
|
|
22
|
+
// A `'use client'` directive in the directive prologue, after any comments
|
|
23
|
+
// and other directives. The client boundary loader checks the parsed module.
|
|
24
|
+
const CLIENT_DIRECTIVE = new RegExp(
|
|
25
|
+
`^${PROLOGUE_GAP}(?:${DIRECTIVE_STRING}${PROLOGUE_GAP}(?:;${PROLOGUE_GAP})?)*(?:'use client'|"use client")`,
|
|
26
|
+
);
|
|
16
27
|
const PLATFORM_SOURCE_GLOBS = [
|
|
17
28
|
'*.js',
|
|
18
29
|
'*.jsx',
|
|
@@ -68,9 +79,35 @@ export function create_tsrx_react_turbopack_rule(options = {}) {
|
|
|
68
79
|
};
|
|
69
80
|
}
|
|
70
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Next turns a `'use client'` module into a client reference that imports
|
|
84
|
+
* `./<file name>`, and for a renamed module that is the renamed name, such as
|
|
85
|
+
* `./App.tsrx.tsx`, which does not exist. This rule renames the module to
|
|
86
|
+
* `App.tsrx?tsrx-client&lang.tsx` instead, so the client reference resolves
|
|
87
|
+
* to `App.tsrx` with the client query. The renamed module re-exports that
|
|
88
|
+
* compiled module, so the client reference and direct imports from client
|
|
89
|
+
* modules share one module instance.
|
|
90
|
+
*
|
|
91
|
+
* @returns {{ condition: { all: any[] }, loaders: string[], as: string }}
|
|
92
|
+
*/
|
|
93
|
+
export function create_tsrx_react_turbopack_client_rule() {
|
|
94
|
+
return {
|
|
95
|
+
condition: {
|
|
96
|
+
all: [
|
|
97
|
+
{ not: 'foreign' },
|
|
98
|
+
{ not: { query: CSS_QUERY } },
|
|
99
|
+
{ not: { query: CLIENT_QUERY } },
|
|
100
|
+
{ content: CLIENT_DIRECTIVE },
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
loaders: [TURBOPACK_CLIENT_BOUNDARY_LOADER],
|
|
104
|
+
as: `*${CLIENT_QUERY}`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
71
108
|
/**
|
|
72
109
|
* @param {TsrxReactTurbopackOptions} [options]
|
|
73
|
-
* @returns {{ condition: { all: any[] }, loaders: Array<string | { loader: string, options: TsrxReactTurbopackOptions }>,
|
|
110
|
+
* @returns {{ condition: { all: any[] }, loaders: Array<string | { loader: string, options: TsrxReactTurbopackOptions }>, as: string }}
|
|
74
111
|
*/
|
|
75
112
|
export function create_tsrx_react_turbopack_css_rule(options = {}) {
|
|
76
113
|
options = normalize_options(options);
|
|
@@ -79,7 +116,10 @@ export function create_tsrx_react_turbopack_css_rule(options = {}) {
|
|
|
79
116
|
all: [{ not: 'foreign' }, { query: CSS_QUERY }],
|
|
80
117
|
},
|
|
81
118
|
loaders: [with_loader_options(TURBOPACK_CSS_LOADER, options)],
|
|
82
|
-
type: 'css'
|
|
119
|
+
// Renaming the module to `*.css` (instead of setting `type: 'css'`) routes
|
|
120
|
+
// it through Next's own CSS rule, which links the stylesheet into the page
|
|
121
|
+
// when a server component imports it.
|
|
122
|
+
as: '*.css',
|
|
83
123
|
};
|
|
84
124
|
}
|
|
85
125
|
|
|
@@ -124,6 +164,8 @@ function merge_resolve_extensions(resolve_extensions) {
|
|
|
124
164
|
*/
|
|
125
165
|
function merge_tsrx_rule(existing_rule, options) {
|
|
126
166
|
const rules = [
|
|
167
|
+
// Renames the module, so it must run before the compile rule.
|
|
168
|
+
create_tsrx_react_turbopack_client_rule(),
|
|
127
169
|
create_tsrx_react_turbopack_rule(options),
|
|
128
170
|
create_tsrx_react_turbopack_css_rule(options),
|
|
129
171
|
];
|
|
@@ -137,6 +179,7 @@ function merge_tsrx_rule(existing_rule, options) {
|
|
|
137
179
|
*
|
|
138
180
|
* The helper installs loader-backed `*.tsrx` rules that compile TSRX to TSX,
|
|
139
181
|
* route component-local `<style>` blocks through a sibling virtual CSS import,
|
|
182
|
+
* give `'use client'` modules a client boundary module Next can reference,
|
|
140
183
|
* and then hand the TSX output back to Turbopack so Next's React pipeline can
|
|
141
184
|
* finish the JSX transform.
|
|
142
185
|
*
|
package/src/loader.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/** @import { Platform, RuntimeImportMode } from '@tsrx/react' */
|
|
2
2
|
|
|
3
|
+
import path from 'node:path';
|
|
3
4
|
import { compile } from '@tsrx/react';
|
|
4
5
|
|
|
5
6
|
const CSS_QUERY = '?tsrx-css&lang.css';
|
|
@@ -9,14 +10,16 @@ const CSS_QUERY = '?tsrx-css&lang.css';
|
|
|
9
10
|
* module's own imports so the stylesheets of imported themes are added to the
|
|
10
11
|
* graph first and a rule in the applying module wins at equal specificity.
|
|
11
12
|
* Top-level directives such as `'use client'` stay first, and nothing above
|
|
12
|
-
* the import moves, so the compiler's source map still applies.
|
|
13
|
+
* the import moves, so the compiler's source map still applies. The specifier
|
|
14
|
+
* is relative because Turbopack does not resolve absolute import paths.
|
|
13
15
|
*
|
|
14
16
|
* @param {string} code
|
|
15
17
|
* @param {string} resource_path
|
|
16
18
|
* @returns {string}
|
|
17
19
|
*/
|
|
18
20
|
function append_css_import(code, resource_path) {
|
|
19
|
-
|
|
21
|
+
const specifier = `./${path.basename(resource_path)}${CSS_QUERY}`;
|
|
22
|
+
return `${code}\nimport ${JSON.stringify(specifier)};\n`;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
/**
|
package/types/index.d.ts
CHANGED
|
@@ -36,18 +36,28 @@ export interface TsrxReactTurbopackRule {
|
|
|
36
36
|
as: '*.tsx';
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
export interface TsrxReactTurbopackClientRule {
|
|
40
|
+
condition: {
|
|
41
|
+
all: Array<unknown>;
|
|
42
|
+
};
|
|
43
|
+
loaders: string[];
|
|
44
|
+
as: '*?tsrx-client&lang.tsx';
|
|
45
|
+
}
|
|
46
|
+
|
|
39
47
|
export interface TsrxReactTurbopackCssRule {
|
|
40
48
|
condition: {
|
|
41
49
|
all: Array<unknown>;
|
|
42
50
|
};
|
|
43
51
|
loaders: Array<string | TsrxReactTurbopackLoader>;
|
|
44
|
-
|
|
52
|
+
as: '*.css';
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
export declare function create_tsrx_react_turbopack_rule(
|
|
48
56
|
options?: TsrxReactTurbopackOptions,
|
|
49
57
|
): TsrxReactTurbopackRule;
|
|
50
58
|
|
|
59
|
+
export declare function create_tsrx_react_turbopack_client_rule(): TsrxReactTurbopackClientRule;
|
|
60
|
+
|
|
51
61
|
export declare function create_tsrx_react_turbopack_css_rule(
|
|
52
62
|
options?: TsrxReactTurbopackOptions,
|
|
53
63
|
): TsrxReactTurbopackCssRule;
|