@tsrx/turbopack-plugin-react 0.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/LICENSE +21 -0
- package/package.json +40 -0
- package/src/css-loader.js +28 -0
- package/src/index.js +100 -0
- package/src/loader.js +128 -0
- package/types/index.d.ts +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dominic Gannaway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsrx/turbopack-plugin-react",
|
|
3
|
+
"description": "Turbopack integration for @tsrx/react",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Dominic Gannaway",
|
|
6
|
+
"version": "0.0.1",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/Ripple-TS/ripple.git",
|
|
14
|
+
"directory": "packages/turbopack-plugin-react"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./types/index.d.ts",
|
|
19
|
+
"default": "./src/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@tsrx/react": "0.0.7"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"next": ">=15.3.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependenciesMeta": {
|
|
29
|
+
"next": {
|
|
30
|
+
"optional": true
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"src",
|
|
38
|
+
"types"
|
|
39
|
+
]
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { compile } from '@tsrx/react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {{
|
|
5
|
+
* resourcePath: string,
|
|
6
|
+
* async: () => (err: unknown, output?: string | null, map?: unknown) => void,
|
|
7
|
+
* }} LoaderContext
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Re-runs the `@tsrx/react` compiler against the `.tsrx` source to extract
|
|
12
|
+
* the scoped CSS emitted by its `<style>` block. Invoked when Turbopack
|
|
13
|
+
* resolves the sibling `?tsrx-css&lang.css` import prepended by the JS loader.
|
|
14
|
+
*
|
|
15
|
+
* @this {LoaderContext}
|
|
16
|
+
* @param {string} source
|
|
17
|
+
* @returns {void}
|
|
18
|
+
*/
|
|
19
|
+
export default function tsrx_react_turbopack_css_loader(source) {
|
|
20
|
+
const callback = this.async();
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const { css } = compile(source, this.resourcePath);
|
|
24
|
+
callback(null, css?.code ?? '');
|
|
25
|
+
} catch (/** @type {any} */ err) {
|
|
26
|
+
callback(err);
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
|
|
4
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
5
|
+
const __dirname = path.dirname(__filename);
|
|
6
|
+
|
|
7
|
+
const TURBOPACK_JS_LOADER = path.join(__dirname, 'loader.js');
|
|
8
|
+
const TURBOPACK_CSS_LOADER = path.join(__dirname, 'css-loader.js');
|
|
9
|
+
const DEFAULT_RESOLVE_EXTENSIONS = ['.tsrx', '.tsx', '.ts', '.jsx', '.js', '.mjs', '.json'];
|
|
10
|
+
const CSS_QUERY = '?tsrx-css&lang.css';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {{
|
|
14
|
+
* turbopack?: {
|
|
15
|
+
* root?: string,
|
|
16
|
+
* rules?: Record<string, any>,
|
|
17
|
+
* resolveAlias?: Record<string, any>,
|
|
18
|
+
* resolveExtensions?: string[],
|
|
19
|
+
* debugIds?: boolean,
|
|
20
|
+
* },
|
|
21
|
+
* [key: string]: any,
|
|
22
|
+
* }} NextTurbopackConfig
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @returns {{ condition: { all: any[] }, loaders: string[], as: string }}
|
|
27
|
+
*/
|
|
28
|
+
export function create_tsrx_react_turbopack_rule() {
|
|
29
|
+
return {
|
|
30
|
+
condition: {
|
|
31
|
+
all: [{ not: 'foreign' }, { not: { query: CSS_QUERY } }],
|
|
32
|
+
},
|
|
33
|
+
loaders: [TURBOPACK_JS_LOADER],
|
|
34
|
+
as: '*.tsx',
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @returns {{ condition: { all: any[] }, loaders: string[], type: string }}
|
|
40
|
+
*/
|
|
41
|
+
export function create_tsrx_react_turbopack_css_rule() {
|
|
42
|
+
return {
|
|
43
|
+
condition: {
|
|
44
|
+
all: [{ not: 'foreign' }, { query: CSS_QUERY }],
|
|
45
|
+
},
|
|
46
|
+
loaders: [TURBOPACK_CSS_LOADER],
|
|
47
|
+
type: 'css',
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param {string[] | undefined} resolve_extensions
|
|
53
|
+
* @returns {string[]}
|
|
54
|
+
*/
|
|
55
|
+
function merge_resolve_extensions(resolve_extensions) {
|
|
56
|
+
const merged = resolve_extensions ? [...resolve_extensions] : [...DEFAULT_RESOLVE_EXTENSIONS];
|
|
57
|
+
if (!merged.includes('.tsrx')) {
|
|
58
|
+
merged.unshift('.tsrx');
|
|
59
|
+
}
|
|
60
|
+
return merged;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {any} existing_rule
|
|
65
|
+
* @returns {any}
|
|
66
|
+
*/
|
|
67
|
+
function merge_tsrx_rule(existing_rule) {
|
|
68
|
+
const rules = [create_tsrx_react_turbopack_rule(), create_tsrx_react_turbopack_css_rule()];
|
|
69
|
+
if (!existing_rule) return rules;
|
|
70
|
+
return Array.isArray(existing_rule) ? [...rules, ...existing_rule] : [...rules, existing_rule];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Merge the Turbopack settings needed for `.tsrx` React modules into a Next.js
|
|
75
|
+
* config object.
|
|
76
|
+
*
|
|
77
|
+
* The helper installs loader-backed `*.tsrx` rules that compile TSRX to TSX,
|
|
78
|
+
* route component-local `<style>` blocks through a sibling virtual CSS import,
|
|
79
|
+
* and then hand the TSX output back to Turbopack so Next's React pipeline can
|
|
80
|
+
* finish the JSX transform.
|
|
81
|
+
*
|
|
82
|
+
* @param {NextTurbopackConfig} [next_config]
|
|
83
|
+
* @returns {NextTurbopackConfig}
|
|
84
|
+
*/
|
|
85
|
+
export function tsrxReactTurbopack(next_config = {}) {
|
|
86
|
+
const turbopack = next_config.turbopack ?? {};
|
|
87
|
+
const rules = { ...(turbopack.rules ?? {}) };
|
|
88
|
+
rules['*.tsrx'] = merge_tsrx_rule(rules['*.tsrx']);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
...next_config,
|
|
92
|
+
turbopack: {
|
|
93
|
+
...turbopack,
|
|
94
|
+
rules,
|
|
95
|
+
resolveExtensions: merge_resolve_extensions(turbopack.resolveExtensions),
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default tsrxReactTurbopack;
|
package/src/loader.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { compile } from '@tsrx/react';
|
|
2
|
+
|
|
3
|
+
const CSS_QUERY = '?tsrx-css&lang.css';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {string} source
|
|
7
|
+
* @param {number} index
|
|
8
|
+
* @returns {number}
|
|
9
|
+
*/
|
|
10
|
+
function skip_whitespace_and_comments(source, index) {
|
|
11
|
+
while (index < source.length) {
|
|
12
|
+
const char = source[index];
|
|
13
|
+
if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
|
|
14
|
+
index++;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (char === '/' && source[index + 1] === '/') {
|
|
18
|
+
index += 2;
|
|
19
|
+
while (index < source.length && source[index] !== '\n') {
|
|
20
|
+
index++;
|
|
21
|
+
}
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (char === '/' && source[index + 1] === '*') {
|
|
25
|
+
index += 2;
|
|
26
|
+
while (index < source.length && !(source[index] === '*' && source[index + 1] === '/')) {
|
|
27
|
+
index++;
|
|
28
|
+
}
|
|
29
|
+
index = Math.min(index + 2, source.length);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
return index;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {string} source
|
|
39
|
+
* @param {number} index
|
|
40
|
+
* @returns {number}
|
|
41
|
+
*/
|
|
42
|
+
function read_directive_statement(source, index) {
|
|
43
|
+
const quote = source[index];
|
|
44
|
+
if (quote !== '"' && quote !== "'") {
|
|
45
|
+
return -1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let cursor = index + 1;
|
|
49
|
+
let value = '';
|
|
50
|
+
while (cursor < source.length) {
|
|
51
|
+
const char = source[cursor];
|
|
52
|
+
if (char === '\\') {
|
|
53
|
+
value += source.slice(cursor, cursor + 2);
|
|
54
|
+
cursor += 2;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (char === quote) {
|
|
58
|
+
cursor++;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
value += char;
|
|
62
|
+
cursor++;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (cursor > source.length || !value.startsWith('use ')) {
|
|
66
|
+
return -1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
cursor = skip_whitespace_and_comments(source, cursor);
|
|
70
|
+
if (source[cursor] === ';') {
|
|
71
|
+
cursor++;
|
|
72
|
+
}
|
|
73
|
+
return skip_whitespace_and_comments(source, cursor);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {string} code
|
|
78
|
+
* @param {string} resource_path
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
function prepend_css_import(code, resource_path) {
|
|
82
|
+
const css_import = `import ${JSON.stringify(resource_path + CSS_QUERY)};\n`;
|
|
83
|
+
const initial_index = skip_whitespace_and_comments(code, 0);
|
|
84
|
+
let insertion_index = initial_index;
|
|
85
|
+
let scan_index = initial_index;
|
|
86
|
+
|
|
87
|
+
while (scan_index < code.length) {
|
|
88
|
+
const next_index = read_directive_statement(code, scan_index);
|
|
89
|
+
if (next_index === -1) {
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
insertion_index = next_index;
|
|
93
|
+
scan_index = next_index;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return `${code.slice(0, insertion_index)}${css_import}${code.slice(insertion_index)}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @typedef {{
|
|
101
|
+
* resourcePath: string,
|
|
102
|
+
* async: () => (err: unknown, output?: string | null, map?: unknown) => void,
|
|
103
|
+
* }} LoaderContext
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Compile `.tsrx` files to TSX for consumption by Turbopack's built-in
|
|
108
|
+
* TypeScript/React pipeline. When a component-local `<style>` block is
|
|
109
|
+
* present, prepend an import to a sibling virtual CSS resource that is handled
|
|
110
|
+
* by the Turbopack config helper's query-targeted CSS rule.
|
|
111
|
+
*
|
|
112
|
+
* @this {LoaderContext}
|
|
113
|
+
* @param {string} source
|
|
114
|
+
* @returns {void}
|
|
115
|
+
*/
|
|
116
|
+
export default function tsrx_react_turbopack_loader(source) {
|
|
117
|
+
const callback = this.async();
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
const { code, map, css } = compile(source, this.resourcePath);
|
|
121
|
+
const output = css ? prepend_css_import(code, this.resourcePath) : code;
|
|
122
|
+
const output_map = css ? null : map;
|
|
123
|
+
|
|
124
|
+
callback(null, output, /** @type {any} */ (output_map ?? undefined));
|
|
125
|
+
} catch (/** @type {any} */ err) {
|
|
126
|
+
callback(err);
|
|
127
|
+
}
|
|
128
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface NextTurbopackConfig {
|
|
2
|
+
turbopack?: {
|
|
3
|
+
root?: string;
|
|
4
|
+
rules?: Record<string, unknown>;
|
|
5
|
+
resolveAlias?: Record<string, unknown>;
|
|
6
|
+
resolveExtensions?: string[];
|
|
7
|
+
debugIds?: boolean;
|
|
8
|
+
};
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface TsrxReactTurbopackRule {
|
|
13
|
+
condition: {
|
|
14
|
+
all: Array<unknown>;
|
|
15
|
+
};
|
|
16
|
+
loaders: string[];
|
|
17
|
+
as: '*.tsx';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TsrxReactTurbopackCssRule {
|
|
21
|
+
condition: {
|
|
22
|
+
all: Array<unknown>;
|
|
23
|
+
};
|
|
24
|
+
loaders: string[];
|
|
25
|
+
type: 'css';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export declare function create_tsrx_react_turbopack_rule(): TsrxReactTurbopackRule;
|
|
29
|
+
|
|
30
|
+
export declare function create_tsrx_react_turbopack_css_rule(): TsrxReactTurbopackCssRule;
|
|
31
|
+
|
|
32
|
+
export declare function tsrxReactTurbopack(next_config?: NextTurbopackConfig): NextTurbopackConfig;
|
|
33
|
+
|
|
34
|
+
export default tsrxReactTurbopack;
|