source: imaps-frontend/node_modules/@vitejs/plugin-react/dist/index.cjs@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 11.5 KB
RevLine 
[d565449]1'use strict';
2
3const vite = require('vite');
4const fs = require('node:fs');
5const path = require('node:path');
6const node_module = require('node:module');
7
8var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
9function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
11const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
12const path__default = /*#__PURE__*/_interopDefaultCompat(path);
13
14const runtimePublicPath = "/@react-refresh";
15const _require = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
16const reactRefreshDir = path__default.dirname(
17 _require.resolve("react-refresh/package.json")
18);
19const runtimeFilePath = path__default.join(
20 reactRefreshDir,
21 "cjs/react-refresh-runtime.development.js"
22);
23const runtimeCode = `
24const exports = {}
25${fs__default.readFileSync(runtimeFilePath, "utf-8")}
26${fs__default.readFileSync(_require.resolve("./refreshUtils.js"), "utf-8")}
27export default exports
28`;
29const preambleCode = `
30import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
31RefreshRuntime.injectIntoGlobalHook(window)
32window.$RefreshReg$ = () => {}
33window.$RefreshSig$ = () => (type) => type
34window.__vite_plugin_react_preamble_installed__ = true
35`;
36const sharedHeader = `
37import RefreshRuntime from "${runtimePublicPath}";
38
39const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
40`.replace(/\n+/g, "");
41const functionHeader = `
42let prevRefreshReg;
43let prevRefreshSig;
44
45if (import.meta.hot && !inWebWorker) {
46 if (!window.__vite_plugin_react_preamble_installed__) {
47 throw new Error(
48 "@vitejs/plugin-react can't detect preamble. Something is wrong. " +
49 "See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201"
50 );
51 }
52
53 prevRefreshReg = window.$RefreshReg$;
54 prevRefreshSig = window.$RefreshSig$;
55 window.$RefreshReg$ = (type, id) => {
56 RefreshRuntime.register(type, __SOURCE__ + " " + id)
57 };
58 window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
59}`.replace(/\n+/g, "");
60const functionFooter = `
61if (import.meta.hot && !inWebWorker) {
62 window.$RefreshReg$ = prevRefreshReg;
63 window.$RefreshSig$ = prevRefreshSig;
64}`;
[0c6b92a]65const sharedFooter = (id) => `
[d565449]66if (import.meta.hot && !inWebWorker) {
67 RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
[0c6b92a]68 RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(
69 id
70)}, currentExports);
[d565449]71 import.meta.hot.accept((nextExports) => {
72 if (!nextExports) return;
[0c6b92a]73 const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify(
74 id
75)}, currentExports, nextExports);
[d565449]76 if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
77 });
78 });
79}`;
80function addRefreshWrapper(code, id) {
[0c6b92a]81 return sharedHeader + functionHeader.replace("__SOURCE__", JSON.stringify(id)) + code + functionFooter + sharedFooter(id);
[d565449]82}
83function addClassComponentRefreshWrapper(code, id) {
[0c6b92a]84 return sharedHeader + code + sharedFooter(id);
[d565449]85}
86
87let babel;
88async function loadBabel() {
89 if (!babel) {
90 babel = await import('@babel/core');
91 }
92 return babel;
93}
94const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
95const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
96const defaultIncludeRE = /\.[tj]sx?$/;
97const tsRE = /\.tsx?$/;
98function viteReact(opts = {}) {
99 let devBase = "/";
100 const filter = vite.createFilter(opts.include ?? defaultIncludeRE, opts.exclude);
101 const jsxImportSource = opts.jsxImportSource ?? "react";
102 const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
103 const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`;
104 let isProduction = true;
105 let projectRoot = process.cwd();
106 let skipFastRefresh = false;
107 let runPluginOverrides;
108 let staticBabelOptions;
109 const importReactRE = /\bimport\s+(?:\*\s+as\s+)?React\b/;
110 const viteBabel = {
111 name: "vite:react-babel",
112 enforce: "pre",
113 config() {
114 if (opts.jsxRuntime === "classic") {
115 return {
116 esbuild: {
117 jsx: "transform"
118 }
119 };
120 } else {
121 return {
122 esbuild: {
123 jsx: "automatic",
124 jsxImportSource: opts.jsxImportSource
125 },
126 optimizeDeps: { esbuildOptions: { jsx: "automatic" } }
127 };
128 }
129 },
130 configResolved(config) {
131 devBase = config.base;
132 projectRoot = config.root;
133 isProduction = config.isProduction;
134 skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
135 if ("jsxPure" in opts) {
136 config.logger.warnOnce(
137 "[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly."
138 );
139 }
140 const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(defined);
141 if (hooks.length > 0) {
142 runPluginOverrides = (babelOptions, context) => {
143 hooks.forEach((hook) => hook(babelOptions, context, config));
144 };
145 } else if (typeof opts.babel !== "function") {
146 staticBabelOptions = createBabelOptions(opts.babel);
147 }
148 },
149 async transform(code, id, options) {
150 if (id.includes("/node_modules/"))
151 return;
152 const [filepath] = id.split("?");
153 if (!filter(filepath))
154 return;
155 const ssr = options?.ssr === true;
156 const babelOptions = (() => {
157 if (staticBabelOptions)
158 return staticBabelOptions;
159 const newBabelOptions = createBabelOptions(
160 typeof opts.babel === "function" ? opts.babel(id, { ssr }) : opts.babel
161 );
162 runPluginOverrides?.(newBabelOptions, { id, ssr });
163 return newBabelOptions;
164 })();
165 const plugins = [...babelOptions.plugins];
166 const isJSX = filepath.endsWith("x");
167 const useFastRefresh = !skipFastRefresh && !ssr && (isJSX || (opts.jsxRuntime === "classic" ? importReactRE.test(code) : code.includes(jsxImportDevRuntime) || code.includes(jsxImportRuntime)));
168 if (useFastRefresh) {
169 plugins.push([
170 await loadPlugin("react-refresh/babel"),
171 { skipEnvCheck: true }
172 ]);
173 }
174 if (opts.jsxRuntime === "classic" && isJSX) {
175 if (!isProduction) {
176 plugins.push(
177 await loadPlugin("@babel/plugin-transform-react-jsx-self"),
178 await loadPlugin("@babel/plugin-transform-react-jsx-source")
179 );
180 }
181 }
182 if (!plugins.length && !babelOptions.presets.length && !babelOptions.configFile && !babelOptions.babelrc) {
183 return;
184 }
185 const parserPlugins = [...babelOptions.parserOpts.plugins];
186 if (!filepath.endsWith(".ts")) {
187 parserPlugins.push("jsx");
188 }
189 if (tsRE.test(filepath)) {
190 parserPlugins.push("typescript");
191 }
192 const babel2 = await loadBabel();
193 const result = await babel2.transformAsync(code, {
194 ...babelOptions,
195 root: projectRoot,
196 filename: id,
197 sourceFileName: filepath,
198 // Required for esbuild.jsxDev to provide correct line numbers
199 // This crates issues the react compiler because the re-order is too important
200 // People should use @babel/plugin-transform-react-jsx-development to get back good line numbers
[0c6b92a]201 retainLines: getReactCompilerPlugin(plugins) != null ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
[d565449]202 parserOpts: {
203 ...babelOptions.parserOpts,
204 sourceType: "module",
205 allowAwaitOutsideFunction: true,
206 plugins: parserPlugins
207 },
208 generatorOpts: {
209 ...babelOptions.generatorOpts,
[0c6b92a]210 // import attributes parsing available without plugin since 7.26
211 importAttributesKeyword: "with",
[d565449]212 decoratorsBeforeExport: true
213 },
214 plugins,
215 sourceMaps: true
216 });
217 if (result) {
218 let code2 = result.code;
219 if (useFastRefresh) {
220 if (refreshContentRE.test(code2)) {
221 code2 = addRefreshWrapper(code2, id);
222 } else if (reactCompRE.test(code2)) {
223 code2 = addClassComponentRefreshWrapper(code2, id);
224 }
225 }
226 return { code: code2, map: result.map };
227 }
228 }
229 };
[0c6b92a]230 const dependencies = [
231 "react",
232 "react-dom",
233 jsxImportDevRuntime,
234 jsxImportRuntime
235 ];
[d565449]236 const staticBabelPlugins = typeof opts.babel === "object" ? opts.babel?.plugins ?? [] : [];
[0c6b92a]237 const reactCompilerPlugin = getReactCompilerPlugin(staticBabelPlugins);
238 if (reactCompilerPlugin != null) {
239 const reactCompilerRuntimeModule = getReactCompilerRuntimeModule(reactCompilerPlugin);
240 dependencies.push(reactCompilerRuntimeModule);
[d565449]241 }
242 const viteReactRefresh = {
243 name: "vite:react-refresh",
244 enforce: "pre",
245 config: (userConfig) => ({
246 build: silenceUseClientWarning(userConfig),
247 optimizeDeps: {
248 include: dependencies
249 },
250 resolve: {
251 dedupe: ["react", "react-dom"]
252 }
253 }),
254 resolveId(id) {
255 if (id === runtimePublicPath) {
256 return id;
257 }
258 },
259 load(id) {
260 if (id === runtimePublicPath) {
261 return runtimeCode;
262 }
263 },
264 transformIndexHtml() {
265 if (!skipFastRefresh)
266 return [
267 {
268 tag: "script",
269 attrs: { type: "module" },
270 children: preambleCode.replace(`__BASE__`, devBase)
271 }
272 ];
273 }
274 };
275 return [viteBabel, viteReactRefresh];
276}
277viteReact.preambleCode = preambleCode;
278const silenceUseClientWarning = (userConfig) => ({
279 rollupOptions: {
280 onwarn(warning, defaultHandler) {
281 if (warning.code === "MODULE_LEVEL_DIRECTIVE" && warning.message.includes("use client")) {
282 return;
283 }
[0c6b92a]284 if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) {
285 return;
286 }
[d565449]287 if (userConfig.build?.rollupOptions?.onwarn) {
288 userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
289 } else {
290 defaultHandler(warning);
291 }
292 }
293 }
294});
295const loadedPlugin = /* @__PURE__ */ new Map();
296function loadPlugin(path) {
297 const cached = loadedPlugin.get(path);
298 if (cached)
299 return cached;
300 const promise = import(path).then((module) => {
301 const value = module.default || module;
302 loadedPlugin.set(path, value);
303 return value;
304 });
305 loadedPlugin.set(path, promise);
306 return promise;
307}
308function createBabelOptions(rawOptions) {
309 var _a;
310 const babelOptions = {
311 babelrc: false,
312 configFile: false,
313 ...rawOptions
314 };
315 babelOptions.plugins || (babelOptions.plugins = []);
316 babelOptions.presets || (babelOptions.presets = []);
317 babelOptions.overrides || (babelOptions.overrides = []);
318 babelOptions.parserOpts || (babelOptions.parserOpts = {});
319 (_a = babelOptions.parserOpts).plugins || (_a.plugins = []);
320 return babelOptions;
321}
322function defined(value) {
323 return value !== void 0;
324}
[0c6b92a]325function getReactCompilerPlugin(plugins) {
326 return plugins.find(
[d565449]327 (p) => p === "babel-plugin-react-compiler" || Array.isArray(p) && p[0] === "babel-plugin-react-compiler"
328 );
329}
[0c6b92a]330function getReactCompilerRuntimeModule(plugin) {
331 let moduleName = "react/compiler-runtime";
332 if (Array.isArray(plugin)) {
333 if (plugin[1]?.target === "17" || plugin[1]?.target === "18") {
334 moduleName = "react-compiler-runtime";
335 } else if (typeof plugin[1]?.runtimeModule === "string") {
336 moduleName = plugin[1]?.runtimeModule;
337 }
338 }
339 return moduleName;
[d565449]340}
341
342module.exports = viteReact;
343module.exports.default = viteReact;
Note: See TracBrowser for help on using the repository browser.