Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): support paths in compiler options #1584

Merged
merged 7 commits into from
Aug 14, 2019

Conversation

simonhaenisch
Copy link
Contributor

@simonhaenisch simonhaenisch commented Jun 1, 2019

Someone was asking about why path aliases in the compilerOptions section of tsconfig didn't work in Stencil, and I ended up trying to solve it.

First, I created rollup-plugin-typescript-paths, which uses nodeModuleNameResolver from the typescript api to resolve imports using the path aliases (it returns the transpiled .js counterpart of the .ts file).

Then I realized that I had an issue with const enums: Stencil removes the paths field from the compiler options entirely (in transpile-service.ts), which causes the import statements of any const enums to remain in the file even though they have disappeared during transpilation (like interfaces).

To solve this I decided to implement the resolving of path aliases in Stencil itself, because it's relatively simple and the only reason I wrote my plugin was for use with Stencil (though it should theoretically work in other contexts).

To try it out:

  • npm link in stencil
  • npm init stencil app foobar
  • cd foobar, npm link @stencil/core
  • change files:
// tsconfig.json
{
  // ...
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@foobar": ["src/global/app"]
    }
  }
}
// src/global/app.ts

export const foobar = 'foobar';
// src/components/app-home/app-home.tsx

import { foobar } from '@foobar';

render() {
  return <div>{foobar}</div>;
}

BTW I tried writing a test but didn't have enough time to figure out how to change the compiler options within the test.

@simonhaenisch simonhaenisch marked this pull request as ready for review June 14, 2019 13:24
@simonhaenisch simonhaenisch changed the title feat(compiler): consider paths in compiler options feat(compiler): support paths in compiler options Jun 14, 2019
@leoafarias
Copy link

Just a bump on this.. spent a lot more that I care to admit trying to see if was an issue on my end or not.

@rainzhao2000
Copy link

Bump for sure, awesome of you to implement a solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants