ember-cli-typescript icon indicating copy to clipboard operation
ember-cli-typescript copied to clipboard

Does not work with monorepo / yarn workspaces

Open gossi opened this issue 6 years ago • 10 comments

I do have a setup with a monorepo / yarn workspace and lerna. Mostly identical with ember-decorators repo. For reference I added lerna.json and package.json (for the workspace) as reference below. The structure typically looks like this:

[root]
|- packages
|-- packageA
|-- packageB
|- node_modules
|- package.json
`- lerna.json

So, when packageA requires ember-cli-typescript it is installed to [root]/node_modules/ember-cli-typescript and so is typescript. Hence the watcher is setup to watch in the folder [root]/packages/packageA/node_modules/*** which doesn't exist.

As such the watcher tries to endlessly find something that doesn't exist. There should be some mechanism first to detect, wether this is a yarn workspace and an which folders to look for (it still can be the package related node_modules folder).

// lerna.json
{
  "lerna": "2.11.0",
  "npmClient": "yarn",
  "useWorkspaces": true,
  "packages": [
    "packages/*"
  ],
  "version": "0.0.0"
}
// package.json
{
	"private": true,
	"devDependencies": {
		"lerna": "^2.11.0"
	},
	"workspaces": [
		"packages/*"
	]
}

gossi avatar Jul 07 '18 14:07 gossi

Have you tried the branch in #223?

chriskrycho avatar Jul 07 '18 19:07 chriskrycho

After getting it the compilation step to work, there's another issue with the current setup (previously mentioned on slack).

If one has a workspace with multiple packages (A, B), where A depends on B and both of them use cli-typescript, then typescript files from B aren't compiled because:

error TS6059: File '/path/to/projectB/addon/utils/something.ts' is not under 'rootDir' '/path/to/projectA'. 'rootDir' is expected to contain all source files.

Basically typescript expects any typescript file to lie under A which isn't the case as B is in the workspace root.

workspace-root
 |- A
 |- B

This is the case with yarn as yarn workspaces simply link node_modules. I'm not sure if the same behavior exists when using lerna/npnm

makepanic avatar Jul 25 '18 18:07 makepanic

Is there any solution to this? Followed the same path, with #223 compilation started to work, but I stuck on converting ember addons in yarn workspace to typescript. Having the TS6059 issue when building ts project that depends on ts addons.

loginovma avatar Aug 02 '18 09:08 loginovma

I'm not sure what work (if any) will be necessary on this end to support monorepos, but you might start by experimenting with the new Project References feature in TS3.0.

dfreeman avatar Aug 02 '18 14:08 dfreeman

@gossi @loginovma were you able to derive a solution from the suggestions above or otherwise? I'm setting up a similar monorepo with lerna and want to see what the community has arrived at

theseyi avatar Oct 25 '18 21:10 theseyi

I currently solve it by not using ember-cli-typescript for compilation, but ember-cli-babel@7 with the TypeScript transform.

// ember-cli-build.js or index.js in addons

  {
    'ember-cli-babel': {
      includePolyfill: true,
      extensions: ['js', 'ts']
    },
    babel: {
      sourceMaps: 'inline',
      plugins: [
        '@babel/transform-typescript',
        ['@babel/proposal-decorators', { legacy: true }],
        ['@babel/proposal-class-properties', { loose: true }],
        '@babel/proposal-object-rest-spread'
      ]
    }
  }

However, instead of manually configuring Babel, I recommend trying out https://github.com/typed-ember/ember-cli-typescript/pull/333, which will essentially do the same, but better. 😄

And then in the tsconfig.js I manually setup paths for IntelliSense:

  {
    "paths": {
      "client/tests/*": ["tests/*"],
      "client/mirage/*": ["mirage/*"],
      "client/*": ["app/*"],
      "@clark/some-addon/*": ["../client-packages/addons/some-addon/addon/*"],
      "@clark/another-addon/*": ["../client-packages/addons/another-addon/addon/*"],
      // ...
      "*": ["types/*"]
    }
  }

I was gonna write a script to automate the generation of these paths, but I couldn't be be bothered to do so, yet. 😄

buschtoens avatar Oct 26 '18 08:10 buschtoens

I'll be the first to say I haven't given any real deep thought to the details of this setup, but I threw together a workspace-based repo and everything appeared to work pretty nicely out of the box (beyond needing to set up some tsconfig.json paths) with the current ember-cli-typescript@2 beta.

https://github.com/dfreeman/ember-workspace-playground

dfreeman avatar Oct 26 '18 21:10 dfreeman

Awesome! @dfreeman any concrete ETA on when ember-cli-typescript@2 will be released? Thanks for clarifying @buschtoens

theseyi avatar Oct 26 '18 22:10 theseyi

As annoying as I know this is, the answer is "when it's ready." You can track progress over on #333 🙂

We're still getting some real-world burn in with the new implementation, and we also have a few more things to clean up both in the code and in terms of documenting the migration process.

dfreeman avatar Oct 26 '18 22:10 dfreeman

@buschtoens I'm confused why you need to specify the the paths to your other modules because when you run a yarn install at the root of your monorepo, it will create symlinks in the node_modules directory to each of your packages in the mono repo (rather than the actual, built library files). Given that, it's supposed to "just work."

icfantv avatar Mar 12 '19 14:03 icfantv

ember-cli-typescript itself works fine with Yarn (and nom and pnpm) workspaces and has since v2. Additionally, we now recommend that people switch to using ember-cli-babel and running glint or tsc directly. In that case, Glint and TypeScript’s built-in support for project references comes “for free”.

chriskrycho avatar Sep 28 '23 21:09 chriskrycho