我有一个React / Redux打字稿项目A.我的团队决定将一些React组件和Redux代码拆分成一个NPM模块,所以我创建了另一个React / Redux TS项目B.
最初,当我尝试从A安装B时,由于类型重新声明而导致错误,因为A和B都依赖于相同类型的声明文件(react,redux等)。所以我将B的所有@types依赖项都移动为对等依赖项。这允许我从A中正确安装B.
但是,出于开发目的,我想从A npm link
到B,所以我不必经常重新编译并重新安装B.但是因为npm link
创建了一个符号链接,它指向整个B项目,包括我需要避免的类型定义。
有谁知道如何解决这个难题?
答案 0 :(得分:0)
这个问题与打字稿并不是特别相关,而是一个将两个javascript程序包链接在一起并防止库多次加载的普遍问题。解决此问题的一个重要因素取决于您使用的构建/捆绑程序。如果您只关心重复数据删除反应,我认为这个stackoverflow answer很好。
这里是使用webpack解决该问题的示例。
首先要确保子包中的所有共享依赖项都是devDependencies和peerDependencies,并且父包将它们设置为所需的依赖项和devDependencies。
A-package.json
WITH t1 AS (
SELECT keyword, domain
FROM domain_keywords
WHERE domain ='golflink.com'
GROUP BY domain, keyword
)
SELECT t1.domain AS 'Domain', t2.domain AS 'SimilarDomain', count(t2.keyword) AS 'SharedKeywordsNumber'
FROM t1, domain_keywords t2
WHERE t1.keyword = t2.keyword AND t1.domain != t2.domain
GROUP BY t1.domain, t2.domain
ORDER BY 3 DESC, 2
LIMIT 10
B-package.json
{
"dependencies": {
"B": "1.0.0",
"react": "x.x.x",
},
"devDependencies": {
"@types/react": "x.x.x"
}
}
如果从程序包A运行webpack,则需要确保在仅适用于程序包A的node_modules的情况下解析node_modules。
{
"version": "1.0.0",
"peerDependencies": {
"@types/react": "x.x.x",
"react": "x.x.x"
},
"devDependencies": {
"@types/react": "x.x.x",
"react": "x.x.x"
}
}
这也是使用react-app-rewired的另一种解决方案,
const path = require('path')
module.exports = {
// rest of your webpack condig...
resolve: {
modules: [path.resolve(__dirname, 'node_modules'), 'node_modules']
}
}