vite-plugin-rnw 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/README.md +90 -0
- package/dist/index.cjs +405 -0
- package/dist/index.d.cts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +380 -0
- package/package.json +64 -0
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# vite-plugin-rnw
|
|
2
|
+
|
|
3
|
+
A vite plugin for React Native Web projects. (edited from a version of the react plugin)
|
|
4
|
+
|
|
5
|
+
- enable [Fast Refresh](https://www.npmjs.com/package/react-refresh) in development (requires react >= 16.9)
|
|
6
|
+
- use the [automatic JSX runtime](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html)
|
|
7
|
+
- use custom Babel plugins/presets
|
|
8
|
+
- small installation size
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
// vite.config.js
|
|
12
|
+
import { defineConfig } from "vite";
|
|
13
|
+
import rnw from "vite-plugin-rnw";
|
|
14
|
+
|
|
15
|
+
export default defineConfig({
|
|
16
|
+
plugins: [rnw()],
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Options
|
|
21
|
+
|
|
22
|
+
### include/exclude
|
|
23
|
+
|
|
24
|
+
Includes `.js`, `.jsx`, `.ts` & `.tsx` by default. This option can be used to add fast refresh to other files:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import { defineConfig } from "vite";
|
|
28
|
+
import rnw from "vite-plugin-rnw";
|
|
29
|
+
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
plugins: [rnw({ include: /\.(js|jsx|ts|tsx)$/ })],
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
> `node_modules` can be processed by this plugin when needed
|
|
36
|
+
> the excludes default is `/\/node_modules\/(?!react-native|@react-native|expo|@expo)/;`
|
|
37
|
+
|
|
38
|
+
### jsxImportSource
|
|
39
|
+
|
|
40
|
+
Control where the JSX factory is imported from. Default to `'react'`
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
rnw({ jsxImportSource: "nativewind" });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### babel
|
|
47
|
+
|
|
48
|
+
The `babel` option lets you add plugins, presets, and [other configuration](https://babeljs.io/docs/en/options) to the Babel transformation performed on each included file.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
rnw({
|
|
52
|
+
babel: {
|
|
53
|
+
presets: [...],
|
|
54
|
+
// Your plugins run before any built-in transform (eg: Fast Refresh)
|
|
55
|
+
plugins: [...],
|
|
56
|
+
// Use .babelrc files
|
|
57
|
+
babelrc: true,
|
|
58
|
+
// Use babel.config.js files
|
|
59
|
+
configFile: true,
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
#### Proposed syntax
|
|
65
|
+
|
|
66
|
+
If you are using ES syntax that are still in proposal status (e.g. class properties), you can selectively enable them with the `babel.parserOpts.plugins` option:
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
rnw({
|
|
70
|
+
babel: {
|
|
71
|
+
parserOpts: {
|
|
72
|
+
plugins: ["decorators-legacy"],
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
This option does not enable _code transformation_. That is handled by esbuild.
|
|
79
|
+
|
|
80
|
+
**Note:** TypeScript syntax is handled automatically.
|
|
81
|
+
|
|
82
|
+
Here's the [complete list of Babel parser plugins](https://babeljs.io/docs/en/babel-parser#ecmascript-proposalshttpsgithubcombabelproposals).
|
|
83
|
+
|
|
84
|
+
## Consistent components exports
|
|
85
|
+
|
|
86
|
+
For React refresh to work correctly, your file should only export React components. You can find a good explanation in the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works).
|
|
87
|
+
|
|
88
|
+
If an incompatible change in exports is found, the module will be invalidated and HMR will propagate. To make it easier to export simple constants alongside your component, the module is only invalidated when their value changes.
|
|
89
|
+
|
|
90
|
+
You can catch mistakes and get more detailed warning with this [eslint rule](https://github.com/ArnaudBarre/eslint-plugin-react-refresh).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
const node_path = __toESM(require("node:path"));
|
|
26
|
+
const node_url = __toESM(require("node:url"));
|
|
27
|
+
const node_fs = __toESM(require("node:fs"));
|
|
28
|
+
const vite = __toESM(require("vite"));
|
|
29
|
+
const vite_plugin_commonjs = __toESM(require("vite-plugin-commonjs"));
|
|
30
|
+
const __rolldown_pluginutils = __toESM(require("@rolldown/pluginutils"));
|
|
31
|
+
|
|
32
|
+
//#region src/refresh-utils.ts
|
|
33
|
+
const runtimePublicPath = "/@react-refresh";
|
|
34
|
+
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
|
|
35
|
+
const refreshContentRE = /\$RefreshReg\$\(/;
|
|
36
|
+
const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(1)}";
|
|
37
|
+
injectIntoGlobalHook(window);
|
|
38
|
+
window.$RefreshReg$ = () => {};
|
|
39
|
+
window.$RefreshSig$ = () => (type) => type;`;
|
|
40
|
+
const getPreambleCode = (base) => preambleCode.replace("__BASE__", base);
|
|
41
|
+
const avoidSourceMapOption = Symbol();
|
|
42
|
+
function addRefreshWrapper(code, map, pluginName, id, reactRefreshHost = "") {
|
|
43
|
+
const hasRefresh = refreshContentRE.test(code);
|
|
44
|
+
const onlyReactComp = !hasRefresh && reactCompRE.test(code);
|
|
45
|
+
const normalizedMap = map === avoidSourceMapOption ? null : map;
|
|
46
|
+
if (!hasRefresh && !onlyReactComp) return {
|
|
47
|
+
code,
|
|
48
|
+
map: normalizedMap
|
|
49
|
+
};
|
|
50
|
+
const avoidSourceMap = map === avoidSourceMapOption;
|
|
51
|
+
const newMap = typeof normalizedMap === "string" ? JSON.parse(normalizedMap) : normalizedMap;
|
|
52
|
+
let newCode = code;
|
|
53
|
+
if (hasRefresh) {
|
|
54
|
+
const refreshHead = removeLineBreaksIfNeeded(`let prevRefreshReg;
|
|
55
|
+
let prevRefreshSig;
|
|
56
|
+
|
|
57
|
+
if (import.meta.hot && !inWebWorker) {
|
|
58
|
+
if (!window.$RefreshReg$) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
"${pluginName} can't detect preamble. Something is wrong."
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
prevRefreshReg = window.$RefreshReg$;
|
|
65
|
+
prevRefreshSig = window.$RefreshSig$;
|
|
66
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg(${JSON.stringify(id)});
|
|
67
|
+
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
`, avoidSourceMap);
|
|
71
|
+
newCode = `${refreshHead}${newCode}
|
|
72
|
+
|
|
73
|
+
if (import.meta.hot && !inWebWorker) {
|
|
74
|
+
window.$RefreshReg$ = prevRefreshReg;
|
|
75
|
+
window.$RefreshSig$ = prevRefreshSig;
|
|
76
|
+
}
|
|
77
|
+
`;
|
|
78
|
+
if (newMap) newMap.mappings = ";".repeat(16) + newMap.mappings;
|
|
79
|
+
}
|
|
80
|
+
const sharedHead = removeLineBreaksIfNeeded(`import * as RefreshRuntime from "${reactRefreshHost}${runtimePublicPath}";
|
|
81
|
+
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
|
82
|
+
|
|
83
|
+
`, avoidSourceMap);
|
|
84
|
+
newCode = `${sharedHead}${newCode}
|
|
85
|
+
|
|
86
|
+
if (import.meta.hot && !inWebWorker) {
|
|
87
|
+
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
88
|
+
RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(id)}, currentExports);
|
|
89
|
+
import.meta.hot.accept((nextExports) => {
|
|
90
|
+
if (!nextExports) return;
|
|
91
|
+
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify(id)}, currentExports, nextExports);
|
|
92
|
+
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
`;
|
|
97
|
+
if (newMap) newMap.mappings = ";;;" + newMap.mappings;
|
|
98
|
+
return {
|
|
99
|
+
code: newCode,
|
|
100
|
+
map: newMap
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function removeLineBreaksIfNeeded(code, enabled) {
|
|
104
|
+
return enabled ? code.replace(/\n/g, "") : code;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/index.ts
|
|
109
|
+
const silenceUseClientWarning = (userConfig) => ({ rollupOptions: { onwarn(warning, defaultHandler) {
|
|
110
|
+
var _userConfig$build;
|
|
111
|
+
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && (warning.message.includes("use client") || warning.message.includes("use server"))) return;
|
|
112
|
+
if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) return;
|
|
113
|
+
if ((_userConfig$build = userConfig.build) === null || _userConfig$build === void 0 || (_userConfig$build = _userConfig$build.rollupOptions) === null || _userConfig$build === void 0 ? void 0 : _userConfig$build.onwarn) userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
|
114
|
+
else defaultHandler(warning);
|
|
115
|
+
} } });
|
|
116
|
+
const _dirname = (0, node_path.dirname)((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
|
|
117
|
+
const refreshRuntimePath = (0, node_path.join)(_dirname, "refresh-runtime.js");
|
|
118
|
+
let babel;
|
|
119
|
+
async function loadBabel() {
|
|
120
|
+
if (!babel) babel = await import("@babel/core");
|
|
121
|
+
return babel;
|
|
122
|
+
}
|
|
123
|
+
const extensions = [
|
|
124
|
+
".web.js",
|
|
125
|
+
".web.ts",
|
|
126
|
+
".web.tsx",
|
|
127
|
+
".web.mjs",
|
|
128
|
+
".web.cjs",
|
|
129
|
+
".js",
|
|
130
|
+
".jsx",
|
|
131
|
+
".json",
|
|
132
|
+
".ts",
|
|
133
|
+
".tsx",
|
|
134
|
+
".mjs",
|
|
135
|
+
".cjs"
|
|
136
|
+
];
|
|
137
|
+
const defaultIncludeRE = /\.[tj]sx?$/;
|
|
138
|
+
const defaultExcludeRE = /\/node_modules\/(?!react-native|@react-native|expo|@expo)/;
|
|
139
|
+
const tsRE = /\.tsx?$/;
|
|
140
|
+
const getInitialOptions = (opts) => {
|
|
141
|
+
if (opts.jsxRuntime === "classic") if ("rolldownVersion" in vite) return { oxc: { jsx: {
|
|
142
|
+
runtime: "classic",
|
|
143
|
+
development: false
|
|
144
|
+
} } };
|
|
145
|
+
else return { esbuild: { jsx: "transform" } };
|
|
146
|
+
else return {
|
|
147
|
+
esbuild: {
|
|
148
|
+
jsx: "automatic",
|
|
149
|
+
jsxImportSource: opts.jsxImportSource
|
|
150
|
+
},
|
|
151
|
+
optimizeDeps: "rolldownVersion" in vite ? { rollupOptions: { jsx: { mode: "automatic" } } } : { esbuildOptions: { jsx: "automatic" } }
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
function viteReactNativeWeb(opts = {}) {
|
|
155
|
+
var _opts$babel;
|
|
156
|
+
const include = opts.include ?? defaultIncludeRE;
|
|
157
|
+
const exclude = opts.exclude ?? defaultExcludeRE;
|
|
158
|
+
const filter = (0, vite.createFilter)(include, exclude);
|
|
159
|
+
const jsxImportSource = opts.jsxImportSource ?? "react";
|
|
160
|
+
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
|
|
161
|
+
const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`;
|
|
162
|
+
let runningInVite = false;
|
|
163
|
+
let isProduction = true;
|
|
164
|
+
let projectRoot = process.cwd();
|
|
165
|
+
let skipFastRefresh = true;
|
|
166
|
+
let runPluginOverrides;
|
|
167
|
+
let staticBabelOptions;
|
|
168
|
+
const importReactRE = /\bimport\s+(?:\*\s+as\s+)?React\b/;
|
|
169
|
+
const viteBabel = {
|
|
170
|
+
name: "vite:react-native-web-babel",
|
|
171
|
+
enforce: "pre",
|
|
172
|
+
config(_userConfig, env) {
|
|
173
|
+
var _initialOptions$optim;
|
|
174
|
+
const development = env.mode === "development";
|
|
175
|
+
const initialOptions = getInitialOptions(opts);
|
|
176
|
+
return {
|
|
177
|
+
...initialOptions,
|
|
178
|
+
define: {
|
|
179
|
+
global: "window",
|
|
180
|
+
DEV: JSON.stringify(development),
|
|
181
|
+
"global.__x": {},
|
|
182
|
+
_frameTimestamp: void 0,
|
|
183
|
+
_WORKLET: false,
|
|
184
|
+
__DEV__: JSON.stringify(development),
|
|
185
|
+
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || env.mode),
|
|
186
|
+
"global.Error": "Error"
|
|
187
|
+
},
|
|
188
|
+
optimizeDeps: {
|
|
189
|
+
...initialOptions.optimizeDeps,
|
|
190
|
+
include: ["react-native-reanimated"],
|
|
191
|
+
esbuildOptions: {
|
|
192
|
+
...(_initialOptions$optim = initialOptions.optimizeDeps) === null || _initialOptions$optim === void 0 ? void 0 : _initialOptions$optim.esbuildOptions,
|
|
193
|
+
resolveExtensions: extensions,
|
|
194
|
+
loader: { ".js": "jsx" }
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
resolve: {
|
|
198
|
+
extensions,
|
|
199
|
+
alias: { "react-native": "react-native-web" }
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
},
|
|
203
|
+
configResolved(config) {
|
|
204
|
+
runningInVite = true;
|
|
205
|
+
projectRoot = config.root;
|
|
206
|
+
isProduction = config.isProduction;
|
|
207
|
+
skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
|
|
208
|
+
if ("jsxPure" in opts) config.logger.warnOnce("[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly.");
|
|
209
|
+
const hooks = config.plugins.map((plugin) => {
|
|
210
|
+
var _plugin$api;
|
|
211
|
+
return (_plugin$api = plugin.api) === null || _plugin$api === void 0 ? void 0 : _plugin$api.reactBabel;
|
|
212
|
+
}).filter(defined);
|
|
213
|
+
if (hooks.length > 0) runPluginOverrides = (babelOptions, context) => {
|
|
214
|
+
hooks.forEach((hook) => hook(babelOptions, context, config));
|
|
215
|
+
};
|
|
216
|
+
else if (typeof opts.babel !== "function") staticBabelOptions = createBabelOptions(opts.babel);
|
|
217
|
+
},
|
|
218
|
+
options(options) {
|
|
219
|
+
if (!runningInVite) {
|
|
220
|
+
options.jsx = {
|
|
221
|
+
mode: opts.jsxRuntime,
|
|
222
|
+
importSource: opts.jsxImportSource
|
|
223
|
+
};
|
|
224
|
+
return options;
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
transform: {
|
|
228
|
+
filter: { id: {
|
|
229
|
+
include: (0, __rolldown_pluginutils.makeIdFiltersToMatchWithQuery)(include),
|
|
230
|
+
exclude: [...exclude ? (0, __rolldown_pluginutils.makeIdFiltersToMatchWithQuery)(ensureArray(exclude)) : []]
|
|
231
|
+
} },
|
|
232
|
+
async handler(code, id, options) {
|
|
233
|
+
const [filepath] = id.split("?");
|
|
234
|
+
if (!filepath || !filter(filepath)) return;
|
|
235
|
+
const ssr = (options === null || options === void 0 ? void 0 : options.ssr) === true;
|
|
236
|
+
const babelOptions = (() => {
|
|
237
|
+
if (staticBabelOptions) return staticBabelOptions;
|
|
238
|
+
const newBabelOptions = createBabelOptions(typeof opts.babel === "function" ? opts.babel(id, { ssr }) : opts.babel);
|
|
239
|
+
runPluginOverrides === null || runPluginOverrides === void 0 || runPluginOverrides(newBabelOptions, {
|
|
240
|
+
id,
|
|
241
|
+
ssr
|
|
242
|
+
});
|
|
243
|
+
return newBabelOptions;
|
|
244
|
+
})();
|
|
245
|
+
const plugins = [...babelOptions.plugins];
|
|
246
|
+
const isJSX = filepath.endsWith("x");
|
|
247
|
+
const useFastRefresh = !skipFastRefresh && !ssr && (isJSX || (opts.jsxRuntime === "classic" ? importReactRE.test(code) : code.includes(jsxImportDevRuntime) || code.includes(jsxImportRuntime)));
|
|
248
|
+
if (useFastRefresh) plugins.push([await loadPlugin("react-refresh/babel"), { skipEnvCheck: true }]);
|
|
249
|
+
if (opts.jsxRuntime === "classic" && isJSX) {
|
|
250
|
+
if (!isProduction) plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
|
|
251
|
+
}
|
|
252
|
+
const parserPlugins = [...babelOptions.parserOpts.plugins];
|
|
253
|
+
if (!filepath.endsWith(".ts")) parserPlugins.push("jsx");
|
|
254
|
+
if (tsRE.test(filepath)) parserPlugins.push("typescript");
|
|
255
|
+
const babel$1 = await loadBabel();
|
|
256
|
+
let toTransform = code;
|
|
257
|
+
if (isProduction && id.includes("node_modules/react-native-reanimated") && id.includes("ReanimatedModule/js-reanimated/webUtils") && code.includes("require")) toTransform = `
|
|
258
|
+
|
|
259
|
+
import createReactDOMStyle from 'react-native-web/dist/exports/StyleSheet/compiler/createReactDOMStyle';
|
|
260
|
+
import {
|
|
261
|
+
createTextShadowValue,
|
|
262
|
+
createTransformValue,
|
|
263
|
+
} from 'react-native-web/dist/exports/StyleSheet/preprocess';
|
|
264
|
+
|
|
265
|
+
export { createReactDOMStyle, createTextShadowValue, createTransformValue };
|
|
266
|
+
`;
|
|
267
|
+
const result = await babel$1.transformAsync(toTransform, {
|
|
268
|
+
...babelOptions,
|
|
269
|
+
root: projectRoot,
|
|
270
|
+
filename: id,
|
|
271
|
+
sourceFileName: filepath,
|
|
272
|
+
retainLines: getReactCompilerPlugin(plugins) != null ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
|
|
273
|
+
parserOpts: {
|
|
274
|
+
...babelOptions.parserOpts,
|
|
275
|
+
sourceType: "module",
|
|
276
|
+
allowAwaitOutsideFunction: true,
|
|
277
|
+
plugins: parserPlugins
|
|
278
|
+
},
|
|
279
|
+
generatorOpts: {
|
|
280
|
+
...babelOptions.generatorOpts,
|
|
281
|
+
importAttributesKeyword: "with",
|
|
282
|
+
decoratorsBeforeExport: true
|
|
283
|
+
},
|
|
284
|
+
plugins,
|
|
285
|
+
sourceMaps: true
|
|
286
|
+
});
|
|
287
|
+
if (result) {
|
|
288
|
+
if (!useFastRefresh) return {
|
|
289
|
+
code: result.code,
|
|
290
|
+
map: result.map
|
|
291
|
+
};
|
|
292
|
+
return addRefreshWrapper(result.code, result.map, "@vitejs/plugin-react", id, opts.reactRefreshHost);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const dependencies = [
|
|
298
|
+
"react",
|
|
299
|
+
"react-dom",
|
|
300
|
+
jsxImportDevRuntime,
|
|
301
|
+
jsxImportRuntime
|
|
302
|
+
];
|
|
303
|
+
const staticBabelPlugins = typeof opts.babel === "object" ? ((_opts$babel = opts.babel) === null || _opts$babel === void 0 ? void 0 : _opts$babel.plugins) ?? [] : [];
|
|
304
|
+
const reactCompilerPlugin = getReactCompilerPlugin(staticBabelPlugins);
|
|
305
|
+
if (reactCompilerPlugin != null) {
|
|
306
|
+
const reactCompilerRuntimeModule = getReactCompilerRuntimeModule(reactCompilerPlugin);
|
|
307
|
+
dependencies.push(reactCompilerRuntimeModule);
|
|
308
|
+
}
|
|
309
|
+
const viteReactRefresh = {
|
|
310
|
+
name: "vite:react-native-web-refresh",
|
|
311
|
+
enforce: "pre",
|
|
312
|
+
config: (userConfig) => ({
|
|
313
|
+
build: silenceUseClientWarning(userConfig),
|
|
314
|
+
optimizeDeps: { include: dependencies },
|
|
315
|
+
resolve: { dedupe: ["react", "react-dom"] }
|
|
316
|
+
}),
|
|
317
|
+
resolveId: {
|
|
318
|
+
filter: { id: (0, __rolldown_pluginutils.exactRegex)(runtimePublicPath) },
|
|
319
|
+
handler(id) {
|
|
320
|
+
if (id === runtimePublicPath) return id;
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
load: {
|
|
324
|
+
filter: { id: (0, __rolldown_pluginutils.exactRegex)(runtimePublicPath) },
|
|
325
|
+
handler(id) {
|
|
326
|
+
if (id === runtimePublicPath) return (0, node_fs.readFileSync)(refreshRuntimePath, "utf-8").replace(/__README_URL__/g, "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react");
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
transformIndexHtml(_, config) {
|
|
330
|
+
if (!skipFastRefresh) return [{
|
|
331
|
+
tag: "script",
|
|
332
|
+
attrs: { type: "module" },
|
|
333
|
+
children: getPreambleCode(config.server.config.base)
|
|
334
|
+
}];
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
return [
|
|
338
|
+
{
|
|
339
|
+
name: "treat-js-files-as-jsx",
|
|
340
|
+
async transform(code, id) {
|
|
341
|
+
if (!id.match(/\.js$/)) return null;
|
|
342
|
+
return vite.transformWithEsbuild(code, id, {
|
|
343
|
+
loader: "jsx",
|
|
344
|
+
jsx: "automatic"
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
},
|
|
348
|
+
(0, vite_plugin_commonjs.default)(),
|
|
349
|
+
viteBabel,
|
|
350
|
+
viteReactRefresh
|
|
351
|
+
];
|
|
352
|
+
}
|
|
353
|
+
viteReactNativeWeb.preambleCode = preambleCode;
|
|
354
|
+
const loadedPlugin = /* @__PURE__ */ new Map();
|
|
355
|
+
function loadPlugin(path) {
|
|
356
|
+
const cached = loadedPlugin.get(path);
|
|
357
|
+
if (cached) return cached;
|
|
358
|
+
const promise = import(path).then((module$1) => {
|
|
359
|
+
const value = module$1.default || module$1;
|
|
360
|
+
loadedPlugin.set(path, value);
|
|
361
|
+
return value;
|
|
362
|
+
});
|
|
363
|
+
loadedPlugin.set(path, promise);
|
|
364
|
+
return promise;
|
|
365
|
+
}
|
|
366
|
+
function createBabelOptions(rawOptions) {
|
|
367
|
+
var _babelOptions$parserO;
|
|
368
|
+
const babelOptions = {
|
|
369
|
+
babelrc: false,
|
|
370
|
+
configFile: false,
|
|
371
|
+
...rawOptions
|
|
372
|
+
};
|
|
373
|
+
babelOptions.plugins || (babelOptions.plugins = []);
|
|
374
|
+
babelOptions.presets || (babelOptions.presets = []);
|
|
375
|
+
babelOptions.overrides || (babelOptions.overrides = []);
|
|
376
|
+
babelOptions.parserOpts || (babelOptions.parserOpts = {});
|
|
377
|
+
(_babelOptions$parserO = babelOptions.parserOpts).plugins || (_babelOptions$parserO.plugins = []);
|
|
378
|
+
return babelOptions;
|
|
379
|
+
}
|
|
380
|
+
function defined(value) {
|
|
381
|
+
return value !== void 0;
|
|
382
|
+
}
|
|
383
|
+
function getReactCompilerPlugin(plugins) {
|
|
384
|
+
return plugins.find((p) => p === "babel-plugin-react-compiler" || Array.isArray(p) && p[0] === "babel-plugin-react-compiler");
|
|
385
|
+
}
|
|
386
|
+
function getReactCompilerRuntimeModule(plugin) {
|
|
387
|
+
let moduleName = "react/compiler-runtime";
|
|
388
|
+
if (Array.isArray(plugin)) {
|
|
389
|
+
var _plugin$, _plugin$2, _plugin$3;
|
|
390
|
+
if (((_plugin$ = plugin[1]) === null || _plugin$ === void 0 ? void 0 : _plugin$.target) === "17" || ((_plugin$2 = plugin[1]) === null || _plugin$2 === void 0 ? void 0 : _plugin$2.target) === "18") moduleName = "react-compiler-runtime";
|
|
391
|
+
else if (typeof ((_plugin$3 = plugin[1]) === null || _plugin$3 === void 0 ? void 0 : _plugin$3.runtimeModule) === "string") {
|
|
392
|
+
var _plugin$4;
|
|
393
|
+
moduleName = (_plugin$4 = plugin[1]) === null || _plugin$4 === void 0 ? void 0 : _plugin$4.runtimeModule;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return moduleName;
|
|
397
|
+
}
|
|
398
|
+
function ensureArray(value) {
|
|
399
|
+
return Array.isArray(value) ? value : [value];
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
//#endregion
|
|
403
|
+
exports.default = viteReactNativeWeb;
|
|
404
|
+
exports.silenceUseClientWarning = silenceUseClientWarning;
|
|
405
|
+
module.exports.default = module.exports
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ParserOptions, TransformOptions } from "@babel/core";
|
|
2
|
+
import { BuildOptions, Plugin, ResolvedConfig, UserConfig } from "vite";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
declare const silenceUseClientWarning: (userConfig: UserConfig) => BuildOptions;
|
|
6
|
+
interface Options {
|
|
7
|
+
include?: string | RegExp | Array<string | RegExp>;
|
|
8
|
+
exclude?: string | RegExp | Array<string | RegExp>;
|
|
9
|
+
/**
|
|
10
|
+
* Control where the JSX factory is imported from.
|
|
11
|
+
* https://esbuild.github.io/api/#jsx-import-source
|
|
12
|
+
* @default 'react'
|
|
13
|
+
*/
|
|
14
|
+
jsxImportSource?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Note: Skipping React import with classic runtime is not supported from v4
|
|
17
|
+
* @default "automatic"
|
|
18
|
+
*/
|
|
19
|
+
jsxRuntime?: "classic" | "automatic";
|
|
20
|
+
/**
|
|
21
|
+
* Babel configuration applied in both dev and prod.
|
|
22
|
+
*/
|
|
23
|
+
babel?: BabelOptions | ((id: string, options: {
|
|
24
|
+
ssr?: boolean;
|
|
25
|
+
}) => BabelOptions);
|
|
26
|
+
/**
|
|
27
|
+
* React Fast Refresh runtime URL prefix.
|
|
28
|
+
* Useful in a module federation context to enable HMR by specifying
|
|
29
|
+
* the host application URL in the Vite config of a remote application.
|
|
30
|
+
* @example
|
|
31
|
+
* reactRefreshHost: 'http://localhost:3000'
|
|
32
|
+
*/
|
|
33
|
+
reactRefreshHost?: string;
|
|
34
|
+
/**
|
|
35
|
+
* If set, disables the recommendation to use `@vitejs/plugin-react-oxc`
|
|
36
|
+
*/
|
|
37
|
+
disableOxcRecommendation?: boolean;
|
|
38
|
+
}
|
|
39
|
+
type BabelOptions = Omit<TransformOptions, "ast" | "filename" | "root" | "sourceFileName" | "sourceMaps" | "inputSourceMap">;
|
|
40
|
+
/**
|
|
41
|
+
* The object type used by the `options` passed to plugins with
|
|
42
|
+
* an `api.reactBabel` method.
|
|
43
|
+
*/
|
|
44
|
+
interface ReactBabelOptions extends BabelOptions {
|
|
45
|
+
plugins: Extract<BabelOptions["plugins"], any[]>;
|
|
46
|
+
presets: Extract<BabelOptions["presets"], any[]>;
|
|
47
|
+
overrides: Extract<BabelOptions["overrides"], any[]>;
|
|
48
|
+
parserOpts: ParserOptions & {
|
|
49
|
+
plugins: Extract<ParserOptions["plugins"], any[]>;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
type ReactBabelHook = (babelConfig: ReactBabelOptions, context: ReactBabelHookContext, config: ResolvedConfig) => void;
|
|
53
|
+
type ReactBabelHookContext = {
|
|
54
|
+
ssr: boolean;
|
|
55
|
+
id: string;
|
|
56
|
+
};
|
|
57
|
+
type ViteReactPluginApi = {
|
|
58
|
+
/**
|
|
59
|
+
* Manipulate the Babel options of `@vitejs/plugin-react`
|
|
60
|
+
*/
|
|
61
|
+
reactBabel?: ReactBabelHook;
|
|
62
|
+
};
|
|
63
|
+
declare function viteReactNativeWeb(opts?: Options): Plugin[];
|
|
64
|
+
declare namespace viteReactNativeWeb {
|
|
65
|
+
var preambleCode: string;
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { BabelOptions, Options, ReactBabelOptions, ViteReactPluginApi, viteReactNativeWeb as default, silenceUseClientWarning };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { BuildOptions, Plugin, ResolvedConfig, UserConfig } from "vite";
|
|
2
|
+
import { ParserOptions, TransformOptions } from "@babel/core";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
declare const silenceUseClientWarning: (userConfig: UserConfig) => BuildOptions;
|
|
6
|
+
interface Options {
|
|
7
|
+
include?: string | RegExp | Array<string | RegExp>;
|
|
8
|
+
exclude?: string | RegExp | Array<string | RegExp>;
|
|
9
|
+
/**
|
|
10
|
+
* Control where the JSX factory is imported from.
|
|
11
|
+
* https://esbuild.github.io/api/#jsx-import-source
|
|
12
|
+
* @default 'react'
|
|
13
|
+
*/
|
|
14
|
+
jsxImportSource?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Note: Skipping React import with classic runtime is not supported from v4
|
|
17
|
+
* @default "automatic"
|
|
18
|
+
*/
|
|
19
|
+
jsxRuntime?: "classic" | "automatic";
|
|
20
|
+
/**
|
|
21
|
+
* Babel configuration applied in both dev and prod.
|
|
22
|
+
*/
|
|
23
|
+
babel?: BabelOptions | ((id: string, options: {
|
|
24
|
+
ssr?: boolean;
|
|
25
|
+
}) => BabelOptions);
|
|
26
|
+
/**
|
|
27
|
+
* React Fast Refresh runtime URL prefix.
|
|
28
|
+
* Useful in a module federation context to enable HMR by specifying
|
|
29
|
+
* the host application URL in the Vite config of a remote application.
|
|
30
|
+
* @example
|
|
31
|
+
* reactRefreshHost: 'http://localhost:3000'
|
|
32
|
+
*/
|
|
33
|
+
reactRefreshHost?: string;
|
|
34
|
+
/**
|
|
35
|
+
* If set, disables the recommendation to use `@vitejs/plugin-react-oxc`
|
|
36
|
+
*/
|
|
37
|
+
disableOxcRecommendation?: boolean;
|
|
38
|
+
}
|
|
39
|
+
type BabelOptions = Omit<TransformOptions, "ast" | "filename" | "root" | "sourceFileName" | "sourceMaps" | "inputSourceMap">;
|
|
40
|
+
/**
|
|
41
|
+
* The object type used by the `options` passed to plugins with
|
|
42
|
+
* an `api.reactBabel` method.
|
|
43
|
+
*/
|
|
44
|
+
interface ReactBabelOptions extends BabelOptions {
|
|
45
|
+
plugins: Extract<BabelOptions["plugins"], any[]>;
|
|
46
|
+
presets: Extract<BabelOptions["presets"], any[]>;
|
|
47
|
+
overrides: Extract<BabelOptions["overrides"], any[]>;
|
|
48
|
+
parserOpts: ParserOptions & {
|
|
49
|
+
plugins: Extract<ParserOptions["plugins"], any[]>;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
type ReactBabelHook = (babelConfig: ReactBabelOptions, context: ReactBabelHookContext, config: ResolvedConfig) => void;
|
|
53
|
+
type ReactBabelHookContext = {
|
|
54
|
+
ssr: boolean;
|
|
55
|
+
id: string;
|
|
56
|
+
};
|
|
57
|
+
type ViteReactPluginApi = {
|
|
58
|
+
/**
|
|
59
|
+
* Manipulate the Babel options of `@vitejs/plugin-react`
|
|
60
|
+
*/
|
|
61
|
+
reactBabel?: ReactBabelHook;
|
|
62
|
+
};
|
|
63
|
+
declare function viteReactNativeWeb(opts?: Options): Plugin[];
|
|
64
|
+
declare namespace viteReactNativeWeb {
|
|
65
|
+
var preambleCode: string;
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { BabelOptions, Options, ReactBabelOptions, ViteReactPluginApi, viteReactNativeWeb as default, silenceUseClientWarning };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import * as vite from "vite";
|
|
5
|
+
import { createFilter } from "vite";
|
|
6
|
+
import commonjs from "vite-plugin-commonjs";
|
|
7
|
+
import { exactRegex, makeIdFiltersToMatchWithQuery } from "@rolldown/pluginutils";
|
|
8
|
+
|
|
9
|
+
//#region src/refresh-utils.ts
|
|
10
|
+
const runtimePublicPath = "/@react-refresh";
|
|
11
|
+
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
|
|
12
|
+
const refreshContentRE = /\$RefreshReg\$\(/;
|
|
13
|
+
const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(1)}";
|
|
14
|
+
injectIntoGlobalHook(window);
|
|
15
|
+
window.$RefreshReg$ = () => {};
|
|
16
|
+
window.$RefreshSig$ = () => (type) => type;`;
|
|
17
|
+
const getPreambleCode = (base) => preambleCode.replace("__BASE__", base);
|
|
18
|
+
const avoidSourceMapOption = Symbol();
|
|
19
|
+
function addRefreshWrapper(code, map, pluginName, id, reactRefreshHost = "") {
|
|
20
|
+
const hasRefresh = refreshContentRE.test(code);
|
|
21
|
+
const onlyReactComp = !hasRefresh && reactCompRE.test(code);
|
|
22
|
+
const normalizedMap = map === avoidSourceMapOption ? null : map;
|
|
23
|
+
if (!hasRefresh && !onlyReactComp) return {
|
|
24
|
+
code,
|
|
25
|
+
map: normalizedMap
|
|
26
|
+
};
|
|
27
|
+
const avoidSourceMap = map === avoidSourceMapOption;
|
|
28
|
+
const newMap = typeof normalizedMap === "string" ? JSON.parse(normalizedMap) : normalizedMap;
|
|
29
|
+
let newCode = code;
|
|
30
|
+
if (hasRefresh) {
|
|
31
|
+
const refreshHead = removeLineBreaksIfNeeded(`let prevRefreshReg;
|
|
32
|
+
let prevRefreshSig;
|
|
33
|
+
|
|
34
|
+
if (import.meta.hot && !inWebWorker) {
|
|
35
|
+
if (!window.$RefreshReg$) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
"${pluginName} can't detect preamble. Something is wrong."
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
prevRefreshReg = window.$RefreshReg$;
|
|
42
|
+
prevRefreshSig = window.$RefreshSig$;
|
|
43
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg(${JSON.stringify(id)});
|
|
44
|
+
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
`, avoidSourceMap);
|
|
48
|
+
newCode = `${refreshHead}${newCode}
|
|
49
|
+
|
|
50
|
+
if (import.meta.hot && !inWebWorker) {
|
|
51
|
+
window.$RefreshReg$ = prevRefreshReg;
|
|
52
|
+
window.$RefreshSig$ = prevRefreshSig;
|
|
53
|
+
}
|
|
54
|
+
`;
|
|
55
|
+
if (newMap) newMap.mappings = ";".repeat(16) + newMap.mappings;
|
|
56
|
+
}
|
|
57
|
+
const sharedHead = removeLineBreaksIfNeeded(`import * as RefreshRuntime from "${reactRefreshHost}${runtimePublicPath}";
|
|
58
|
+
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
|
59
|
+
|
|
60
|
+
`, avoidSourceMap);
|
|
61
|
+
newCode = `${sharedHead}${newCode}
|
|
62
|
+
|
|
63
|
+
if (import.meta.hot && !inWebWorker) {
|
|
64
|
+
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
65
|
+
RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(id)}, currentExports);
|
|
66
|
+
import.meta.hot.accept((nextExports) => {
|
|
67
|
+
if (!nextExports) return;
|
|
68
|
+
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify(id)}, currentExports, nextExports);
|
|
69
|
+
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
`;
|
|
74
|
+
if (newMap) newMap.mappings = ";;;" + newMap.mappings;
|
|
75
|
+
return {
|
|
76
|
+
code: newCode,
|
|
77
|
+
map: newMap
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function removeLineBreaksIfNeeded(code, enabled) {
|
|
81
|
+
return enabled ? code.replace(/\n/g, "") : code;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/index.ts
|
|
86
|
+
const silenceUseClientWarning = (userConfig) => ({ rollupOptions: { onwarn(warning, defaultHandler) {
|
|
87
|
+
var _userConfig$build;
|
|
88
|
+
if (warning.code === "MODULE_LEVEL_DIRECTIVE" && (warning.message.includes("use client") || warning.message.includes("use server"))) return;
|
|
89
|
+
if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) return;
|
|
90
|
+
if ((_userConfig$build = userConfig.build) === null || _userConfig$build === void 0 || (_userConfig$build = _userConfig$build.rollupOptions) === null || _userConfig$build === void 0 ? void 0 : _userConfig$build.onwarn) userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
|
|
91
|
+
else defaultHandler(warning);
|
|
92
|
+
} } });
|
|
93
|
+
const _dirname = dirname(fileURLToPath(import.meta.url));
|
|
94
|
+
const refreshRuntimePath = join(_dirname, "refresh-runtime.js");
|
|
95
|
+
let babel;
|
|
96
|
+
async function loadBabel() {
|
|
97
|
+
if (!babel) babel = await import("@babel/core");
|
|
98
|
+
return babel;
|
|
99
|
+
}
|
|
100
|
+
const extensions = [
|
|
101
|
+
".web.js",
|
|
102
|
+
".web.ts",
|
|
103
|
+
".web.tsx",
|
|
104
|
+
".web.mjs",
|
|
105
|
+
".web.cjs",
|
|
106
|
+
".js",
|
|
107
|
+
".jsx",
|
|
108
|
+
".json",
|
|
109
|
+
".ts",
|
|
110
|
+
".tsx",
|
|
111
|
+
".mjs",
|
|
112
|
+
".cjs"
|
|
113
|
+
];
|
|
114
|
+
const defaultIncludeRE = /\.[tj]sx?$/;
|
|
115
|
+
const defaultExcludeRE = /\/node_modules\/(?!react-native|@react-native|expo|@expo)/;
|
|
116
|
+
const tsRE = /\.tsx?$/;
|
|
117
|
+
const getInitialOptions = (opts) => {
|
|
118
|
+
if (opts.jsxRuntime === "classic") if ("rolldownVersion" in vite) return { oxc: { jsx: {
|
|
119
|
+
runtime: "classic",
|
|
120
|
+
development: false
|
|
121
|
+
} } };
|
|
122
|
+
else return { esbuild: { jsx: "transform" } };
|
|
123
|
+
else return {
|
|
124
|
+
esbuild: {
|
|
125
|
+
jsx: "automatic",
|
|
126
|
+
jsxImportSource: opts.jsxImportSource
|
|
127
|
+
},
|
|
128
|
+
optimizeDeps: "rolldownVersion" in vite ? { rollupOptions: { jsx: { mode: "automatic" } } } : { esbuildOptions: { jsx: "automatic" } }
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
function viteReactNativeWeb(opts = {}) {
|
|
132
|
+
var _opts$babel;
|
|
133
|
+
const include = opts.include ?? defaultIncludeRE;
|
|
134
|
+
const exclude = opts.exclude ?? defaultExcludeRE;
|
|
135
|
+
const filter = createFilter(include, exclude);
|
|
136
|
+
const jsxImportSource = opts.jsxImportSource ?? "react";
|
|
137
|
+
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
|
|
138
|
+
const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`;
|
|
139
|
+
let runningInVite = false;
|
|
140
|
+
let isProduction = true;
|
|
141
|
+
let projectRoot = process.cwd();
|
|
142
|
+
let skipFastRefresh = true;
|
|
143
|
+
let runPluginOverrides;
|
|
144
|
+
let staticBabelOptions;
|
|
145
|
+
const importReactRE = /\bimport\s+(?:\*\s+as\s+)?React\b/;
|
|
146
|
+
const viteBabel = {
|
|
147
|
+
name: "vite:react-native-web-babel",
|
|
148
|
+
enforce: "pre",
|
|
149
|
+
config(_userConfig, env) {
|
|
150
|
+
var _initialOptions$optim;
|
|
151
|
+
const development = env.mode === "development";
|
|
152
|
+
const initialOptions = getInitialOptions(opts);
|
|
153
|
+
return {
|
|
154
|
+
...initialOptions,
|
|
155
|
+
define: {
|
|
156
|
+
global: "window",
|
|
157
|
+
DEV: JSON.stringify(development),
|
|
158
|
+
"global.__x": {},
|
|
159
|
+
_frameTimestamp: void 0,
|
|
160
|
+
_WORKLET: false,
|
|
161
|
+
__DEV__: JSON.stringify(development),
|
|
162
|
+
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || env.mode),
|
|
163
|
+
"global.Error": "Error"
|
|
164
|
+
},
|
|
165
|
+
optimizeDeps: {
|
|
166
|
+
...initialOptions.optimizeDeps,
|
|
167
|
+
include: ["react-native-reanimated"],
|
|
168
|
+
esbuildOptions: {
|
|
169
|
+
...(_initialOptions$optim = initialOptions.optimizeDeps) === null || _initialOptions$optim === void 0 ? void 0 : _initialOptions$optim.esbuildOptions,
|
|
170
|
+
resolveExtensions: extensions,
|
|
171
|
+
loader: { ".js": "jsx" }
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
resolve: {
|
|
175
|
+
extensions,
|
|
176
|
+
alias: { "react-native": "react-native-web" }
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
},
|
|
180
|
+
configResolved(config) {
|
|
181
|
+
runningInVite = true;
|
|
182
|
+
projectRoot = config.root;
|
|
183
|
+
isProduction = config.isProduction;
|
|
184
|
+
skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
|
|
185
|
+
if ("jsxPure" in opts) config.logger.warnOnce("[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly.");
|
|
186
|
+
const hooks = config.plugins.map((plugin) => {
|
|
187
|
+
var _plugin$api;
|
|
188
|
+
return (_plugin$api = plugin.api) === null || _plugin$api === void 0 ? void 0 : _plugin$api.reactBabel;
|
|
189
|
+
}).filter(defined);
|
|
190
|
+
if (hooks.length > 0) runPluginOverrides = (babelOptions, context) => {
|
|
191
|
+
hooks.forEach((hook) => hook(babelOptions, context, config));
|
|
192
|
+
};
|
|
193
|
+
else if (typeof opts.babel !== "function") staticBabelOptions = createBabelOptions(opts.babel);
|
|
194
|
+
},
|
|
195
|
+
options(options) {
|
|
196
|
+
if (!runningInVite) {
|
|
197
|
+
options.jsx = {
|
|
198
|
+
mode: opts.jsxRuntime,
|
|
199
|
+
importSource: opts.jsxImportSource
|
|
200
|
+
};
|
|
201
|
+
return options;
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
transform: {
|
|
205
|
+
filter: { id: {
|
|
206
|
+
include: makeIdFiltersToMatchWithQuery(include),
|
|
207
|
+
exclude: [...exclude ? makeIdFiltersToMatchWithQuery(ensureArray(exclude)) : []]
|
|
208
|
+
} },
|
|
209
|
+
async handler(code, id, options) {
|
|
210
|
+
const [filepath] = id.split("?");
|
|
211
|
+
if (!filepath || !filter(filepath)) return;
|
|
212
|
+
const ssr = (options === null || options === void 0 ? void 0 : options.ssr) === true;
|
|
213
|
+
const babelOptions = (() => {
|
|
214
|
+
if (staticBabelOptions) return staticBabelOptions;
|
|
215
|
+
const newBabelOptions = createBabelOptions(typeof opts.babel === "function" ? opts.babel(id, { ssr }) : opts.babel);
|
|
216
|
+
runPluginOverrides === null || runPluginOverrides === void 0 || runPluginOverrides(newBabelOptions, {
|
|
217
|
+
id,
|
|
218
|
+
ssr
|
|
219
|
+
});
|
|
220
|
+
return newBabelOptions;
|
|
221
|
+
})();
|
|
222
|
+
const plugins = [...babelOptions.plugins];
|
|
223
|
+
const isJSX = filepath.endsWith("x");
|
|
224
|
+
const useFastRefresh = !skipFastRefresh && !ssr && (isJSX || (opts.jsxRuntime === "classic" ? importReactRE.test(code) : code.includes(jsxImportDevRuntime) || code.includes(jsxImportRuntime)));
|
|
225
|
+
if (useFastRefresh) plugins.push([await loadPlugin("react-refresh/babel"), { skipEnvCheck: true }]);
|
|
226
|
+
if (opts.jsxRuntime === "classic" && isJSX) {
|
|
227
|
+
if (!isProduction) plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
|
|
228
|
+
}
|
|
229
|
+
const parserPlugins = [...babelOptions.parserOpts.plugins];
|
|
230
|
+
if (!filepath.endsWith(".ts")) parserPlugins.push("jsx");
|
|
231
|
+
if (tsRE.test(filepath)) parserPlugins.push("typescript");
|
|
232
|
+
const babel$1 = await loadBabel();
|
|
233
|
+
let toTransform = code;
|
|
234
|
+
if (isProduction && id.includes("node_modules/react-native-reanimated") && id.includes("ReanimatedModule/js-reanimated/webUtils") && code.includes("require")) toTransform = `
|
|
235
|
+
|
|
236
|
+
import createReactDOMStyle from 'react-native-web/dist/exports/StyleSheet/compiler/createReactDOMStyle';
|
|
237
|
+
import {
|
|
238
|
+
createTextShadowValue,
|
|
239
|
+
createTransformValue,
|
|
240
|
+
} from 'react-native-web/dist/exports/StyleSheet/preprocess';
|
|
241
|
+
|
|
242
|
+
export { createReactDOMStyle, createTextShadowValue, createTransformValue };
|
|
243
|
+
`;
|
|
244
|
+
const result = await babel$1.transformAsync(toTransform, {
|
|
245
|
+
...babelOptions,
|
|
246
|
+
root: projectRoot,
|
|
247
|
+
filename: id,
|
|
248
|
+
sourceFileName: filepath,
|
|
249
|
+
retainLines: getReactCompilerPlugin(plugins) != null ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
|
|
250
|
+
parserOpts: {
|
|
251
|
+
...babelOptions.parserOpts,
|
|
252
|
+
sourceType: "module",
|
|
253
|
+
allowAwaitOutsideFunction: true,
|
|
254
|
+
plugins: parserPlugins
|
|
255
|
+
},
|
|
256
|
+
generatorOpts: {
|
|
257
|
+
...babelOptions.generatorOpts,
|
|
258
|
+
importAttributesKeyword: "with",
|
|
259
|
+
decoratorsBeforeExport: true
|
|
260
|
+
},
|
|
261
|
+
plugins,
|
|
262
|
+
sourceMaps: true
|
|
263
|
+
});
|
|
264
|
+
if (result) {
|
|
265
|
+
if (!useFastRefresh) return {
|
|
266
|
+
code: result.code,
|
|
267
|
+
map: result.map
|
|
268
|
+
};
|
|
269
|
+
return addRefreshWrapper(result.code, result.map, "@vitejs/plugin-react", id, opts.reactRefreshHost);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
const dependencies = [
|
|
275
|
+
"react",
|
|
276
|
+
"react-dom",
|
|
277
|
+
jsxImportDevRuntime,
|
|
278
|
+
jsxImportRuntime
|
|
279
|
+
];
|
|
280
|
+
const staticBabelPlugins = typeof opts.babel === "object" ? ((_opts$babel = opts.babel) === null || _opts$babel === void 0 ? void 0 : _opts$babel.plugins) ?? [] : [];
|
|
281
|
+
const reactCompilerPlugin = getReactCompilerPlugin(staticBabelPlugins);
|
|
282
|
+
if (reactCompilerPlugin != null) {
|
|
283
|
+
const reactCompilerRuntimeModule = getReactCompilerRuntimeModule(reactCompilerPlugin);
|
|
284
|
+
dependencies.push(reactCompilerRuntimeModule);
|
|
285
|
+
}
|
|
286
|
+
const viteReactRefresh = {
|
|
287
|
+
name: "vite:react-native-web-refresh",
|
|
288
|
+
enforce: "pre",
|
|
289
|
+
config: (userConfig) => ({
|
|
290
|
+
build: silenceUseClientWarning(userConfig),
|
|
291
|
+
optimizeDeps: { include: dependencies },
|
|
292
|
+
resolve: { dedupe: ["react", "react-dom"] }
|
|
293
|
+
}),
|
|
294
|
+
resolveId: {
|
|
295
|
+
filter: { id: exactRegex(runtimePublicPath) },
|
|
296
|
+
handler(id) {
|
|
297
|
+
if (id === runtimePublicPath) return id;
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
load: {
|
|
301
|
+
filter: { id: exactRegex(runtimePublicPath) },
|
|
302
|
+
handler(id) {
|
|
303
|
+
if (id === runtimePublicPath) return readFileSync(refreshRuntimePath, "utf-8").replace(/__README_URL__/g, "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react");
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
transformIndexHtml(_, config) {
|
|
307
|
+
if (!skipFastRefresh) return [{
|
|
308
|
+
tag: "script",
|
|
309
|
+
attrs: { type: "module" },
|
|
310
|
+
children: getPreambleCode(config.server.config.base)
|
|
311
|
+
}];
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
return [
|
|
315
|
+
{
|
|
316
|
+
name: "treat-js-files-as-jsx",
|
|
317
|
+
async transform(code, id) {
|
|
318
|
+
if (!id.match(/\.js$/)) return null;
|
|
319
|
+
return vite.transformWithEsbuild(code, id, {
|
|
320
|
+
loader: "jsx",
|
|
321
|
+
jsx: "automatic"
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
commonjs(),
|
|
326
|
+
viteBabel,
|
|
327
|
+
viteReactRefresh
|
|
328
|
+
];
|
|
329
|
+
}
|
|
330
|
+
viteReactNativeWeb.preambleCode = preambleCode;
|
|
331
|
+
const loadedPlugin = /* @__PURE__ */ new Map();
|
|
332
|
+
function loadPlugin(path) {
|
|
333
|
+
const cached = loadedPlugin.get(path);
|
|
334
|
+
if (cached) return cached;
|
|
335
|
+
const promise = import(path).then((module) => {
|
|
336
|
+
const value = module.default || module;
|
|
337
|
+
loadedPlugin.set(path, value);
|
|
338
|
+
return value;
|
|
339
|
+
});
|
|
340
|
+
loadedPlugin.set(path, promise);
|
|
341
|
+
return promise;
|
|
342
|
+
}
|
|
343
|
+
function createBabelOptions(rawOptions) {
|
|
344
|
+
var _babelOptions$parserO;
|
|
345
|
+
const babelOptions = {
|
|
346
|
+
babelrc: false,
|
|
347
|
+
configFile: false,
|
|
348
|
+
...rawOptions
|
|
349
|
+
};
|
|
350
|
+
babelOptions.plugins || (babelOptions.plugins = []);
|
|
351
|
+
babelOptions.presets || (babelOptions.presets = []);
|
|
352
|
+
babelOptions.overrides || (babelOptions.overrides = []);
|
|
353
|
+
babelOptions.parserOpts || (babelOptions.parserOpts = {});
|
|
354
|
+
(_babelOptions$parserO = babelOptions.parserOpts).plugins || (_babelOptions$parserO.plugins = []);
|
|
355
|
+
return babelOptions;
|
|
356
|
+
}
|
|
357
|
+
function defined(value) {
|
|
358
|
+
return value !== void 0;
|
|
359
|
+
}
|
|
360
|
+
function getReactCompilerPlugin(plugins) {
|
|
361
|
+
return plugins.find((p) => p === "babel-plugin-react-compiler" || Array.isArray(p) && p[0] === "babel-plugin-react-compiler");
|
|
362
|
+
}
|
|
363
|
+
function getReactCompilerRuntimeModule(plugin) {
|
|
364
|
+
let moduleName = "react/compiler-runtime";
|
|
365
|
+
if (Array.isArray(plugin)) {
|
|
366
|
+
var _plugin$, _plugin$2, _plugin$3;
|
|
367
|
+
if (((_plugin$ = plugin[1]) === null || _plugin$ === void 0 ? void 0 : _plugin$.target) === "17" || ((_plugin$2 = plugin[1]) === null || _plugin$2 === void 0 ? void 0 : _plugin$2.target) === "18") moduleName = "react-compiler-runtime";
|
|
368
|
+
else if (typeof ((_plugin$3 = plugin[1]) === null || _plugin$3 === void 0 ? void 0 : _plugin$3.runtimeModule) === "string") {
|
|
369
|
+
var _plugin$4;
|
|
370
|
+
moduleName = (_plugin$4 = plugin[1]) === null || _plugin$4 === void 0 ? void 0 : _plugin$4.runtimeModule;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return moduleName;
|
|
374
|
+
}
|
|
375
|
+
function ensureArray(value) {
|
|
376
|
+
return Array.isArray(value) ? value : [value];
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
//#endregion
|
|
380
|
+
export { viteReactNativeWeb as default, silenceUseClientWarning };
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.0.1",
|
|
3
|
+
"name": "vite-plugin-rnw",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"author": "Daniel Williams",
|
|
9
|
+
"description": "Vite plugin for React Native Web",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"vite",
|
|
12
|
+
"vite-plugin",
|
|
13
|
+
"react-native-web",
|
|
14
|
+
"react-native"
|
|
15
|
+
],
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/dannyhw/vite-plugin-rnw.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/dannyhw/vite-plugin-rnw/issues"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"dev": "tsdown --watch",
|
|
34
|
+
"build": "tsdown",
|
|
35
|
+
"prepublishOnly": "npm run build"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": "^14.18.0 || >=16.0.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^22",
|
|
42
|
+
"tsdown": "^0.12.9",
|
|
43
|
+
"vite": "^7.0.4",
|
|
44
|
+
"babel-plugin-react-compiler": "19.1.0-rc.2",
|
|
45
|
+
"react": "^19.1.0",
|
|
46
|
+
"react-dom": "^19.1.0",
|
|
47
|
+
"rolldown": "1.0.0-beta.24"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"typescript": "^5",
|
|
51
|
+
"vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0",
|
|
52
|
+
"react-native-web": "*"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"vite-plugin-commonjs": "^0.10.4",
|
|
56
|
+
"@babel/plugin-transform-modules-commonjs": "^7.27.1",
|
|
57
|
+
"@babel/core": "^7.28.0",
|
|
58
|
+
"@babel/plugin-transform-react-jsx-self": "^7.27.1",
|
|
59
|
+
"@babel/plugin-transform-react-jsx-source": "^7.27.1",
|
|
60
|
+
"@rolldown/pluginutils": "1.0.0-beta.24",
|
|
61
|
+
"@types/babel__core": "^7.20.5",
|
|
62
|
+
"react-refresh": "^0.17.0"
|
|
63
|
+
}
|
|
64
|
+
}
|