This question: "Cannot find module" error when using TypeScript 3 Project References was somewhat helpful.
So was this one: Project references in TypeScript 3 with separate `outDir`
But I am still having trouble getting this working efficiently with VSCode and React.
Project structure:
common tsconfig:
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"rootDir": ".",
"composite": true,
"outDir": "build"
},
"references": []
}
client tsconfig:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
},
"include": [
"src",
],
"references": [
{
"path": "../common"
}
]
}
server tsconfig
{
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"esnext"
],
"module": "commonjs",
"sourceMap": true,
"outDir": "build",
"allowJs": true,
"moduleResolution": "node",
"strict": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"baseUrl": ".",
},
"include": [
"src/**/*"
],
"references": [
{
"path": "../common"
}
]
}
I am using create-react-app in client
. This has caused compilation issues. I believe I have solved this with create-app-rewired
and customize-cra
using this for config-overrides.js
const { override, removeModuleScopePlugin, getBabelLoader } = require("customize-cra");
const path = require("path");
const addCommon = (config) => {
const loader = getBabelLoader(config, false);
const commonPath = path.normalize(path.join(process.cwd(), "../common")).replace(/\\/g, "\\");
loader.include = [loader.include, commonPath];
return config;
};
module.exports = override(
addCommon,
removeModuleScopePlugin(),
);
In common, I have created a file index.ts
which exports code from every file.
export * from "./messages/toServer/AuthMessage";
for example.
On the front-end and back-end, I can import code (to use) with a handwritten import statement:
import { AuthMessage } from "../../../common/build/messages/toServer/AuthMessage";
VSCode doesn't give any suggestions, but it accepts the import statement assuming the code is built.
I run tsc -b -w
in common. This seems to work with manual imports.
It is possible to have:
AuthMessage
Guidance here: https://www.typescriptlang.org/docs/handbook/project-references.html#guidance is very confusing. Any help with an explanation here is appreciated.
Other solutions?
src
of both projects. It's not clean, and I can see some disasters with refactoring and an IDE attempting to re-negotiate import paths if the program is running...Any help is appreciated.
It appears to be working. The steps I took...
Create a base tsconfig in the project directory. (This is documented)
{
"references": [
{
"path": "./common"
}
],
"files": []
}
Add "extends": "../tsconfig"
to both sub project tsconfigs.
export everything from all the files in common/src
. (using /common/index.ts
)
Update import paths to the common directory import { ClassName } from "../../common"
(not individual build folders)
Run tsc -b
in non-react projects for compilation. After very simple initial testing, it looks like my rewired react config just picks up changes to common
. The config-overrides.js
is a necessity.
This does appear to work in VSCode, however, after testing, I agree auto-import is superior in WebStorm.
When creating new files in common
, they must be exported in /common/index.ts
WebStorm auto import will give you a clue, VSCode is a little less obvious.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With