我有這個(gè)檔案結(jié)構(gòu):
有人可以幫我知道如何透過(guò)設(shè)定 Vite 來(lái)存取 pdf.js 嗎?
我嘗試過(guò):
在vite.config.js中,匯出預(yù)設(shè)的defineConfig:
plugins: [ laravel({ input: [ 'node_modules/pdfjs-dist/build/pdf.js' ], refresh: true, }), ],
我在我的 app.blade.php 中使用了 @vite:
@vite([ 'node_modules/pdfjs-dist/build/pdf.js' ])
我得到了這個(gè)錯(cuò)誤: 無(wú)法在 Vite 清單中找到檔案:node_modules/pdfjs-dist/build/pdf.js。
誰(shuí)能幫我解釋一下嗎?
從你的問(wèn)題中我看出你似乎正在使用 Laravel(很好,因?yàn)槲乙埠苁煜ぃ?
我建議您查看 Laravel 文檔,特別是新安裝 Laravel 時(shí)開(kāi)箱即用的 vite 配置。透過(guò)遵循他們指定的目錄約定,您將避免嘗試打破他們的模式而帶來(lái)無(wú)盡的麻煩。查看 Laravel 文件的以下部分將是一個(gè)很好的起點(diǎn): https://laravel.com/docs/10.x/vite#main-內(nèi)容
#在理想的世界中,您將遵循Laravel 假設(shè)的相同約定...即您的專案有一個(gè)resources/js
資料夾,這是您通常儲(chǔ)存專案的javascript 的位置(例如app例如.js
)。
只要您在應(yīng)用程式中需要的地方導(dǎo)入javascript 文件,最好的解決方案可能是使用resources/js/app.js
作為您的Vite“輸入”,並在Vite Blade 指令中引用它好吧(如resources/js/app.js
)。然後,Vite 將在您的應(yīng)用程式需要時(shí)匯入該依賴項(xiàng)。
# Your vite.config.js excerpt would look like: plugins: [ laravel({ input: [ 'resources/js/app.js' ], refresh: true, }), ], # Your @vite blade directive would look like: @vite(['resources/js/app.js']) # Your import call (wherever you're using this dependency) might look like: import 'pdfjs-dist/build/pdf.js';
希望有幫助!