Skip to content

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

Merged
adamdbradley merged 7 commits intomasterfrom
support-typescript-paths
Aug 14, 2019
Merged

feat(compiler): support paths in compiler options#1584
adamdbradley merged 7 commits intomasterfrom
support-typescript-paths

Conversation

@simonhaenisch
Copy link
Copy Markdown
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
Copy Markdown

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
Copy Markdown

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