我創建了一個庫(https://github.com/TeemuKoivisto/svelte-tree-view),我在另一個專案中作為Svelte組件匯入。它在本地鏈接時運行良好(yarn link svelte-tree-view),但當我在node_modules中使用npm下載的版本時,我得到了這個極其奇怪的錯誤:
[!](插件svelte) ValidationError。The$ prefix is reserved, and cannot be used for variable and import name
. /node_modules/svelte-tree-。 view/src/TreeView。 啟示
77: rootElementStore.set(rootElement)。
78: });
79: var $$$$$$$$ = null。
^ = null.
80: var $$vars$ = [$$props, rootElement, $treeStore, treeStore, TreeNode_svelte_1. default];</script>
這是很難除錯的,因為外面沒有太多的討論或博文。無論如何,我讓自己感到驚訝,并最終用一個相當簡單的解決方案解決了這個問題,我想發這個帖子來幫助其他人。以下是與這個問題有關的最重要的庫:
"rollup": "^2.56.3",
"rollup-plugin-svelte": "^7.1.0",
"rollup-plugin-ts": "^1.4.2",/span>
"svelte": "^3.42.6",
"svelte-preprocess": "^4.9.4",
"tslib": "^2.3.1",
"typecript": "^4.4.3"
uj5u.com熱心網友回復:
好吧,答案是在你的svelte.config.js中添加typescript塊,定義了一個tsconfigFile。你為什么需要它,我還不太清楚,但我很高興它能發揮作用。
完整的svelte.config.js:
const autoPreprocess = require('svelte-preprocess')
const preprocessOptions = {
scss: { prependData: `@import 'src/global.scss';` },
typescript: {
tsconfigFile: './tsconfig.json'。
}
}
module.exports = {
preprocess: autoPreprocess(preprocessOptions)。
預處理選項
}
編輯:更仔細地觀察這個問題,顯然不建議創建TypeScript Svelte庫。https://github.com/sveltejs/component-template/issues/29#issuecomment-905481560這意味著我必須在這方面浪費更多的時間。哦,人類...
uj5u.com熱心網友回復:
你所得到的特定錯誤很可能在svelte-preprocess4.9.5版本中被修復。它可能沒有找到你的tsconfig.json,因為它試圖從檔案中找到,也許你是在一個monorepo中,你的node_modules是在有問題的tsconfig.json專案之上。
但是正如你所說,這一切都歸結為提供一個未經處理的庫版本,這是不應該的,因為你迫使每個想要使用你的庫的人設定 TypeScript 預處理 你遇到像你這樣的奇怪錯誤。
幸運的是,SvelteKit通過其打包命令為你提供了保障。它將對Svelte檔案進行預處理(之后是JS/HTML/CSS),也會處理TS->JS轉譯。它還會輸出這些檔案的型別定義檔案(d.ts)。所以你可以繼續使用TS撰寫你的庫,并為大家提供一個可消費的包。更多資訊。https://kit.svelte.dev/docs#packaging
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/333042.html
標籤:
上一篇:如何正確設定垂直滾動的css
