I have 2 CRA projects in a mono-repo - P1 and P2.
root/
projects/
p1/
package.json
tsconfig.json
src/
shared/
**/*.ts
p2/
package.json
tsconfig.json
src/
**/*.ts
P1 contains some shared infrastructure source files, which I want to reuse in P2.
Note: I know about the disadvantages and negative sides of this approach, but...
What I tried:
Include
P1fromP2and import:P1 tsconfig.jsonmodified with"include": [..., "root/projects/p1/src/shared/**/*.ts]P2source file imports .ts files by relative path
Result:
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/Use yarn workspaces
This one is easy. Yarn does not allow to have workspaces outside of the project root
You cannot and must not reference a workspace that is located outside of this filesystem hierarchy.Typescript 3.0+ projects references:
- Update
p1 tsconfig.jsonwith
"compilerOptions": { "composite": true }- Add reference to
P1 tsconfig.json
references: [ { "path": "{...}/p1/" ]Build
P1withtsc -bto produce declaration filesnow I need to import it somehow.
create
tsconfig.paths.jsonto configurepathssectionadd
rootDirtpP1 tsconfig.jsonto point toroot/folderadd paths
"@lib/*": ["<relative-path-to-p1>/p1/src/shared/*"]
Result:
Cannot find module: '@lib/....'. Make sure this package is installedAlso I see that VSCode recognizes my configuration properly and
importintellisense works properly- Update
Use
react-app-rewiredin my
config-overrides.jsI added something likemodule.exports = function override(config, env) { config.resolve.alias = { "@lib": path.resolve(__dirname, '...p1/src/shared/') } }This works for all aliases inside the project. But in my case it fails with
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/