embroider-build / ember-auto-import

Zero config import from npm packages
Other
360 stars 108 forks source link

Incompatibility with ember-cli-typescript v2 #155

Closed josemarluedke closed 5 years ago

josemarluedke commented 5 years ago

With the latest release of ember-auto-import, we can now use ember-auto-import and ember-cli-tyepscript v2 which uses babel 7 for TypeScript.

In my initial tests, I noticed that if a TS file is importing a module, that ember-auto-import should include to the bundle, it is not including anymore after v2.

This bug probably is here, but I might be wrong. Let me know if this a bug on ember-cli-typescript.

I have putted a reproduction of the error in this repo: https://github.com/josemarluedke/--ts-v2-demo

You can see that after the last commit, which adds ember-cli-typescript v2, the import does not work anymore.

ef4 commented 5 years ago

After a bit of poking I realized that I think I should be looking at the v2 branch of ember-cli-typescript?

Right now ember-auto-import installs its analysis preprocessor before ember-cli-babel, because we want to see the imports before they get transpiled out. ember-cli-typescript runs within ember-cli-babel as a babel plugin, so by the time the TS is available as JS, it misses getting analyzed.

I see two options. The first is to move ember-auto-import's analyzer to itself be a babel plugin. This would be good and would possibly even have performance benefits. The challenge is that our caching needs to become exactly as durable as ember-cli-babel's, because we would only notice imports via side-effect.

The other option is to make ember-auto-import explicitly aware of how to analyze typescript files. The TS files are already present in the trees that we analyze, theoretically this code could also setup a parser that understands typescript. The downside of this approach is that it would add typescript as a runtime dependency of everyone using auto-import, and it would introduce another extra parsing step.

ef4 commented 5 years ago

@dfreeman pointed out that since ember-cli-typescript already configures babel 7 to support TS syntax, all that should be required is respecting the configured extensions in ember-cli-babel.

boris-petrov commented 5 years ago

@ef4 - thank you for the Babel 7 support, you're awesome!

We also hit the same issue as this one here. I tried putting:

    'ember-cli-babel': {
      extensions: ['.ts'],
    },

In ember-cli-build.js and the error about the missing imports has gone away but now my frontend.js file seems to not be compiled - there are import and export statements everywhere. You can also try this in @josemarluedke's reproduction repo. Any ideas why?

Thank you for the support!

buschtoens commented 5 years ago

@boris-petrov Setting extensions: ['.ts'] is actually wrong, because

  1. it should be extensions: ['ts'] (or rather ['js', 'ts']), without the leading .
  2. this means that only *.ts files get processed, but you also have *.js files
  3. this is automatically done by ember-cli-typescript@beta for you: https://github.com/typed-ember/ember-cli-typescript/blob/v2.0.0-beta.3/ts/addon.ts#L126-L137

What @ef4 was referring to is that ember-auto-import should read the options.ember-cli-babel.extensions key and analyze all files with matching extensions, because it currently only analyzes *.js. ☺️

boris-petrov commented 5 years ago

@buschtoens - thanks for the answer! I added .ts because that's what I saw in some examples somewhere. :)

So I am to understand that I should wait for a fix from ember-auto-import, right?