I am currently working with Typescript, React, and Vite for my project.
This is how I have structured my project. I keep a tsconfig.json
file at the root level, and a separate public/tsconfig.worker.json
specifically for my public/serviceWorker.ts
file.
.
├── tsconfig.json
├── src/
│ └── constants/config.ts
└── public/
├── serviceWorker.ts
└── tsconfig.worker.json
Within my public/serviceWorker.ts
file, I import a variable from src/constants/config.ts
.
// public/serviceWorker.ts
import Variable from "../src/constants/config";
An error arises in the import statement as follows:
File '<path>/src/constants/config.ts' is not listed within the file list of project '<path>/public/tsconfig.worker.json'. Projects must list all files or use an 'include' pattern.
Here are snippets from my tsconfigs.
// root tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"useDefineForClassFields": true,
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"module": "ESNext",
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
// ...
},
"include": ["src", "svg.d.ts", "public/serviceWorker.ts"],
"references": [
{ "path": "./tsconfig.node.json" },
{ "path": "./public/tsconfig.worker.json" }
]
}
// public/tsconfig.worker.json
{
"compilerOptions": {
"rootDir": "../",
"composite": true,
"module": "es2022",
"noEmit": false,
"moduleResolution": "bundler",
"lib": ["WebWorker", "ES2022"],
}
}
Although I can successfully build using (
"build": "tsc && vite build"
), the output includes both serviceWorker.ts
and tsconfig.worker.json
files like so:
.
└── dist/
├── assets/
├── serviceWorker.ts
└── tsconfig.worker.json