【问题标题】:Reference Typescript project outside of the source folder在源文件夹之外引用 Typescript 项目
【发布时间】:2020-01-31 20:19:20
【问题描述】:

我在一个单一存储库中有 2 个 CRA 项目 - P1 和 P2。

root/
  projects/
    p1/
      package.json
      tsconfig.json
      src/
        shared/
          **/*.ts
    p2/
      package.json
      tsconfig.json
      src/
        **/*.ts

P1 包含一些共享基础架构源文件,我想在P2 中重复使用。

注意:我知道这种方法的缺点和负面影响,但是......

我尝试了什么:

  1. P2 中包含P1 并导入:

    • 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/
    
  2. 使用纱线工作区

    这个很简单。 Yarn does not allow 在项目根目录之外拥有工作区

    You cannot and must not reference a workspace that is located outside of this filesystem hierarchy.

  3. Typescript 3.0+ 项目参考:

    • p1 tsconfig.json 更新为

    "compilerOptions": { "composite": true }

    • 添加对P1 tsconfig.json的引用

    references: [ { "path": "{...}/p1/" ]

    • 使用tsc -b 构建P1 以生成声明文件

    • 现在我需要以某种方式导入它。

      • 创建tsconfig.paths.jsonconfigure paths 部分

      • 添加rootDir tp P1 tsconfig.json 以指向root/ 文件夹

      • 添加路径"@lib/*": ["&lt;relative-path-to-p1&gt;/p1/src/shared/*"]

    结果:

    Cannot find module: '@lib/....'. Make sure this package is installed

    我还看到 VSCode 可以正确识别我的配置并且 import intellisense 可以正常工作

  4. 使用react-app-rewired

    在我的 config-overrides.js 我添加了类似的东西

    module.exports = function override(config, env) {
      config.resolve.alias = {
        "@lib": path.resolve(__dirname, '...p1/src/shared/')
      }
    }
    

    这适用于项目中的所有别名。但就我而言,它失败了

    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/
    

【问题讨论】:

    标签: typescript create-react-app yarnpkg


    【解决方案1】:
    1. 添加customize-crareact-app-rewired

    2. 添加到引用的tsconfig.json

    {
      "compilerOptions": {
        ...
        "composite": true,
        "declaration": true,
        "declarationMap": true
    }
    
    1. 在基础项目中创建tsconfig.paths.json
    {
      "compilerOptions": {
        "baseUrl": "src",
        "paths": {
          "@alias/*": ["..relative/to/base/url/path/*"]
        }
      }
    }
    
    1. 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" }
      ]
    
    1. 在项目根目录下创建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()
    );
    
    
    1. package.json 中使用react-app-rewired 而不是react-scripts

    注意:这是修改后的代码,可能需要做一些小改动。

    【讨论】:

    • 如果 ../all/of/those/random/paths 您可以使用给定结构的正确路径,这将非常有帮助。
    • 请给我看看你的情况,也许我能帮上忙。它实际上是引用项目的相对路径。实际上,它在此代码中从相对路径构建绝对路径。你可以随意改变它
    【解决方案2】:

    所以,我不知道这是否是“正确”的方式,但我之前已经通过使用文件观察器(watchman、gulp 等)将项目 B 中的文件同步到共享文件夹中来做到这一点在项目 A 中。

    使用此方法时,请确保 .gitignore 复制的文件。

    项目 A
    |_ 包.json
    |_ 源
    ..|_ index.ts
    ..|_ 共享
    .....|_ someSharedFile.ts

    项目 B
    |_ gulpfile.js
    |_ 源
    ...|_ someSharedFile.ts

    https://css-tricks.com/gulp-for-beginners/

    --

    另一种选择是从项目 B 创建一个 NPM 包并将其安装到项目 A。

    【讨论】:

    • 感谢您提供此选项。我正在尝试保留一个文件副本(包括文件镜像)
    • "你可以将它移动到 src/ 中,或者从项目的 node_modules/ 中添加符号链接" 所以我从来没有尝试添加符号链接,但它可能值得一试。
    • 我也考虑过这种方法。这是我的应急计划。我认为可以用更直接的方法来解决这个参考,例如项目参考、工作区等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多