Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

injectScript is not defined #126

Open
farmercz opened this issue Aug 14, 2023 · 1 comment
Open

injectScript is not defined #126

farmercz opened this issue Aug 14, 2023 · 1 comment

Comments

@farmercz
Copy link

版本:
"vite": "4.3.9",
"@types/node": "20.3.1",
"@types/react": "18.2.8",
"@types/react-dom": "18.2.4",
"vite-plugin-html": "^3.2.0"

vite.config.ts配置:
plugins: [
react(),
createHtmlPlugin({
entry: 'src/main.tsx',
inject:{
data:{
injectScript:<script src="./inject.js"></script>
},
tags: [
{
injectTo: 'body-prepend',
tag: 'div',
attrs: {
id: 'tag',
},
},
],
}
}),
],
index.html中使用:
<%- injectScript %>

本地dev时出现错误:

<title>Error</title> <script type="module">
          import { ErrorOverlay } from '/@vite/client'
          document.body.appendChild(new ErrorOverlay({"message":"ejs:8\n    6|     \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\r\n    7|     \u003ctitle>人才档案\u003c/title>\r\n >> 8|     \u003c%- injectScript %>\r\n    9| \r\n    10|   \u003c/head>\r\n    11|   \u003cbody>\r\n\ninjectScript is not defined","stack":"    at eval (eval at compile (D:\\reactProject\\talents-profile\\node_modules\\.pnpm\\ejs@3.1.9\\node_modules\\ejs\\lib\\ejs.js:673:12), \u003canonymous>:12:17)\n    at anonymous (D:\\reactProject\\talents-profile\\node_modules\\.pnpm\\ejs@3.1.9\\node_modules\\ejs\\lib\\ejs.js:703:17)\n    at Object.exports.render (D:\\reactProject\\talents-profile\\node_modules\\.pnpm\\ejs@3.1.9\\node_modules\\ejs\\lib\\ejs.js:425:37)\n    at renderHtml (D:\\reactProject\\talents-profile\\node_modules\\.pnpm\\vite-plugin-html@3.2.0_vite@4.3.9\\node_modules\\vite-plugin-html\\dist\\index.cjs:232:26)\n    at transform (D:\\reactProject\\talents-profile\\node_modules\\.pnpm\\vite-plugin-html@3.2.0_vite@4.3.9\\node_modules\\vite-plugin-html\\dist\\index.cjs:154:29)\n    at applyHtmlTransforms (file:///D:/reactProject/talents-profile/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.1_less@4.1.3/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:37683:27)\n    at async viteIndexHtmlMiddleware (file:///D:/reactProject/talents-profile/node_modules/.pnpm/vite@4.3.9_@types+node@20.3.1_less@4.1.3/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:61791:28)","frame":""}))
        
    </script>
</head>
<body></body>
@zhnoah
Copy link

zhnoah commented Nov 20, 2023

injectScript: '<script src="./inject.js"></script>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants