I'm struggling to incorporate NPM 7 workspaces into a Typescript Expo project. The goal is to maintain the standard Expo structure, with the root App.tsx
file, while segregating certain code sections into workspaces.
I'm facing challenges compiling the TS code within the workspaces. Despite numerous attempts to configure the TS and/or Webpack settings, I haven't been successful. Here's a simplified file layout to replicate the issue:
package.json
tsconfig.json
App.tsx
/packages
/core
index.ts
package.json
The crucial part of the main ./package.json
{
"main": "node_modules/expo/AppEntry.js",
"workspaces": [
"packages/*"
],
"scripts": {...},
"dependencies": {...},
"devDependencies": {...},
"private": true
}
The minimal ./tsconfig.json
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"strict": true
}
}
The simple ./packages/core/package.json
{
"name": "core",
"private": true
}
In ./packages/core/index.ts
, there's just an exported log()
function for demonstration purposes
export function log(s: string) {
console.log(s)
}
Lastly, in ./App.tsx
, the function is imported and called
import { log } from 'core'
log('hello')
//...
Compilation Issue
When attempting to build for web (e.g., using expo build:web
), the following error occurs:
✖ Expo Webpack
Compiled with some errors in 1.62s
Failed to compile.
[path]/node_modules/core/index.ts 1:21
Module parse failed: Unexpected token (1:21)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export function log(s: string) {
| console.log(s)
| }
This error is expected since the /node_modules
directory is intentionally excluded.
Therefore, my query is what measures should be taken to successfully compile the workspaces code? Ideally, it should be seamlessly integrated, akin to regular project files without precompilation. Is this attainable?
Failed Resolution Attempts
I primarily focused on altering the tsconfig to resolve the issue, although its impact remains questionable
#1
Tried adding
"include": ["./node_modules/core"],
in ./tsconfig.json
. However, it proved ineffective as the error persisted.
#2
Created /packages/core/tsconfig.json
containing the composite option:
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"strict": true,
"composite": true
}
}
Referenced this in the root tsconfig.json
:
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"strict": true
},
"references": [{ "path": "./node_modules/core" }]
// or even with:
"references": [{ "path": "./packages/core" }]
}
Yet, these efforts didn't yield any positive outcome as the same error persisted.
Your assistance is greatly appreciated.