Fix vite react variable issue

This commit is contained in:
ckt1031 2023-07-08 07:52:37 +00:00
parent 3ed9a219c7
commit 9bf98ab53a
2 changed files with 24 additions and 39 deletions

View File

@ -17,10 +17,9 @@
"semantic-ui-react": "^2.1.4" "semantic-ui-react": "^2.1.4"
}, },
"scripts": { "scripts": {
"start": "react-scripts start", "start": "vite preview --host 0.0.0.0",
"build": "react-scripts build", "build": "vite build",
"test": "react-scripts test", "dev": "vite dev"
"eject": "react-scripts eject"
}, },
"eslintConfig": { "eslintConfig": {
"extends": [ "extends": [

View File

@ -1,44 +1,30 @@
import fs from "node:fs";
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react'
import * as esbuild from "esbuild"; import * as esbuild from "esbuild";
import fs from 'fs/promises';
const sourceJSPattern = /\/src\/.*\.js$/; export default defineConfig(() => ({
const rollupPlugin = (matchers) => ({ plugins: [react()],
name: "js-in-jsx", esbuild: {
load(id) { loader: "jsx",
if (matchers.some(matcher => matcher.test(id))) { include: /src\/.*\.jsx?$/,
const file = fs.readFileSync(id, { encoding: "utf-8" }); // loader: "tsx",
return esbuild.transformSync(file, { loader: "jsx" }); // include: /src\/.*\.[tj]sx?$/,
} exclude: [],
}
});
export default defineConfig({
plugins: [
react()
],
build: {
outDir: "./build",
rollupOptions: {
plugins: [
rollupPlugin([sourceJSPattern])
],
},
commonjsOptions: {
transformMixedEsModules: true,
},
}, },
optimizeDeps: { optimizeDeps: {
esbuildOptions: { esbuildOptions: {
loader: { plugins: [
".js": "jsx", {
}, name: "load-js-files-as-jsx",
}, setup(build) {
}, build.onLoad({ filter: /src\/.*\.js$/ }, async (args) => ({
esbuild: {
loader: "jsx", loader: "jsx",
include: [sourceJSPattern], contents: await fs.readFile(args.path, "utf8"),
exclude: [], }));
}, },
}); },
],
},
},
}));