dee*_*zen 5 typescript create-react-app yarnpkg
我在单一回购中有 2 个 CRA 项目 - P1 和 P2。
root/
projects/
p1/
package.json
tsconfig.json
src/
shared/
**/*.ts
p2/
package.json
tsconfig.json
src/
**/*.ts
Run Code Online (Sandbox Code Playgroud)
P1包含一些共享基础设施源文件,我想在P2.
注意:我知道这种方法的缺点和负面影响,但是......
我试过的:
包括P1从P2和导入:
P1 tsconfig.json 修改为
"include": [..., "root/projects/p1/src/shared/**/*.ts]
P2 源文件通过相对路径导入 .ts 文件结果:
You attempted to import <path> which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/
Run Code Online (Sandbox Code Playgroud)使用纱线工作区
这个很容易。Yarn不允许在项目根目录之外拥有工作区
You cannot and must not reference a workspace that is located outside of this filesystem hierarchy.
Typescript 3.0+ 项目参考:
p1 tsconfig.json与"compilerOptions": { "composite": true }
P1 tsconfig.jsonreferences: [ { "path": "{...}/p1/" ]
Build P1withtsc -b生成声明文件
现在我需要以某种方式导入它。
创建tsconfig.paths.json于配置 paths部分
添加rootDirtpP1 tsconfig.json以指向root/文件夹
添加路径 "@lib/*": ["<relative-path-to-p1>/p1/src/shared/*"]
结果:
Cannot find module: '@lib/....'. Make sure this package is installed
我还看到 VSCode 正确识别我的配置并且import智能感知正常工作
用 react-app-rewired
在我的config-overrides.js我添加了类似的东西
module.exports = function override(config, env) {
config.resolve.alias = {
"@lib": path.resolve(__dirname, '...p1/src/shared/')
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于项目内的所有别名。但就我而言,它失败了
You attempted to import <root>/p1/src/shared... which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/
Run Code Online (Sandbox Code Playgroud)添加customize-cra并react-app-rewired打包
添加到引用tsconfig.json:
{
"compilerOptions": {
...
"composite": true,
"declaration": true,
"declarationMap": true
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.paths.json在基础项目中创建{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@alias/*": ["..relative/to/base/url/path/*"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json这样的行:{
"extends": "./tsconfig.paths.json",
"compilerOptions": {
"baseUrl": "<the same value as in paths file>"
},
"references": [
{ "path": "../relative/path/to/referenced/project/tsconfig.json/file" }
]
Run Code Online (Sandbox Code Playgroud)
config-override.js在项目根目录下创建文件const { override, removeModuleScopePlugin, getBabelLoader, addWebpackAlias } = require("customize-cra");
const path = require("path");
const fs = require('fs');
const updateAliases = (config) => {
const aliases = {
"@alias": ["absolute/path/to/base/url"]
};
return addWebpackAlias(aliases)(config);
};
const updateIncludes = (config) => {
const loader = getBabelLoader(config, false);
const commonPath = path.normalize(path.join(process.cwd(), "../relative/path/to/referenced/project/tsconfig.json/file")).replace(/\\/g, "\\");
loader.include = [loader.include, commonPath];
return config;
};
module.exports = override(
updateAliases,
updateIncludes,
removeModuleScopePlugin()
);
Run Code Online (Sandbox Code Playgroud)
react-app-rewired而不是react-scripts在package.json注意:这是修改后的代码,因此可能需要一些细微的更改。
| 归档时间: |
|
| 查看次数: |
7960 次 |
| 最近记录: |