Node.jsのコンソールアプリでviteを使用して静的ファイルをコピーしたいですがうまくいかなくて困っています。
vite.config.jsのvite-plugin-static-copyの部分をコメントアウトしていて、 npm run buildを実行すると正しくビルドするのですが、
resourcesフォルダをコピーしたく、vite-plugin-static-copyを使用するとimportでエラーします。
console
1X [ERROR] "vite-plugin-static-copy" resolved to an ESM file. ESM file cannot be loaded by `require`
Node.jsのVersionは20を使用しています。
tsconfig.jsのmoduleは "module": "commonjs" を指定しています。
今回はコンソールアプリでdependenciesをあえて使わないようにしているため、devDependenciesのみとなっており、node_modulesnのコピーは不要としています。
わかるかたいらっしゃいましたらお願いします。
vite.config.js
1import { defineConfig, normalizePath } from 'vite' 2import path from 'node:path' 3// 読み込めない 4// import { viteStaticCopy } from 'vite-plugin-static-copy' 5 6// https://vitejs.dev/config/ 7// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/typedef 8export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => { 9 return { 10 build: { 11 assetsDir: '', 12 outDir: normalizePath(path.resolve(__dirname, 'dist')), 13 sourcemap: 'inline', 14 copyPublicDir: true, 15 emptyOutDir: true, 16 minify: false, 17 reportCompressedSize: false, 18 rollupOptions: { 19 input: normalizePath(path.resolve(__dirname, 'src/index.ts')), 20 output: { 21 entryFileNames: '[name].js' 22 } 23 } 24 }, 25 plugins: [ 26 /* 27 viteStaticCopy({ 28 targets: [ 29 { 30 src: 'src/resources', 31 dest: 'dist' 32 } 33 ] 34 }) 35 */ 36 ] 37 } 38})
package.json
1{ 2 "name": "test", 3 "version": "0.0.0", 4 "private": true, 5 "scripts": { 6 "dev": "nodemon ./src/index.ts", 7 "build": "vite build", 8 "start": "node ./dist/index.js", 9 "lint": "eslint src/**/*" 10 }, 11 "devDependencies": { 12 "@types/node": "^20.12.8", 13 "@typescript-eslint/eslint-plugin": "^7.7.0", 14 "@typescript-eslint/parser": "^7.7.0", 15 "eslint": "^8.57.0", 16 "eslint-config-prettier": "^9.1.0", 17 "nodemon": "^3.1.0", 18 "prettier": "^3.2.5", 19 "ts-node": "^10.9.1", 20 "ts-node-dev": "^2.0.0", 21 "typescript": "^4.9.5", 22 "vite": "^5.2.11", 23 "vite-plugin-static-copy": "^1.0.4" 24 } 25}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。