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

fix(rollup-plugin): plugin now correctly resolves relative ts imports #1516

Merged
merged 3 commits into from
Sep 26, 2019
Merged

fix(rollup-plugin): plugin now correctly resolves relative ts imports #1516

merged 3 commits into from
Sep 26, 2019

Conversation

wes-goulet
Copy link
Contributor

Details

The rollup-plugin was failing to resolve relative imports if using Typescript. It was always assuming a .js file extension. This PR fixes that bug and adds a test to ensure TS relative imports work.

Does this PR introduce breaking changes?

  • No, it does not introduce breaking changes.

The PR fulfills these requirements:

  • Have tests for the proposed changes been added? ✅
  • Have you followed these instructions to clearly describe the issue being fixed or feature enhanced? ✅

const normalizedPath = path.resolve(path.dirname(importer), importee);
const absPath = pluginUtils.addExtension(normalizedPath);
const absPath = pluginUtils.addExtension(normalizedPath, ext);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is assuming that the importer dictates the importee's extension. What if you're specifying the extension explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, I guess I don't usually see imports specifying the extension (import foo from ./foo.js) so didn't think of that... I will update to check if importee has extension specified then if not assume it's the same as importer (since most .ts files will import other .ts files and most .js files will import other .js files). Sound good?

Copy link
Contributor

@caridy caridy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apapko or @pmdartus or @diervo should also take a look at this.

@@ -51,14 +51,15 @@ module.exports = function rollupLwcCompiler(pluginOptions = {}) {

// Normalize relative import to absolute import
if (importee.startsWith('.') && importer) {
const ext = path.extname(importee) || path.extname(importer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wes566 why are you checking the extension of both importer and importee?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So first it checks if the importee specifies an extensions... so if you had import foo from ./foo.js it would use .js... whereas if the import statement said import foo from ./foo then it will match the importer's extension.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if your entry file has a '.js' extension, however your imported file, by mistake, has a .ts extension?
// entry.js
import foo from './foo' // foo points to foo.ts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok what we are saying is that if the file you are in is TS, by we will try to match it, but if your relative file is for whatever weird reason.
So yes we should throw

import { api, LightningElement } from "lwc";

export default class Foo extends LightningElement {
// @ts-ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the ignore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it my editor will complain "typescript does not support experimental decorators"... I could fix it by adding a tsconfig.json... but that feels like overkill for a simple test so it's easier to just ts-ignore this line

});
});
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would like to see a mixed case of js importing ts or vice versa to ensure the scenario is handled correctly

Copy link
Contributor

@diervo diervo Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should actually throw if you are have a mix since it makes no sense or whatsoever.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would definitely simplify things... I wouldn't go as far as saying it makes "no sense" since mixing TS and JS is pretty common in brownfield projects that are trying to move to typescript slowly - after all, the typescript compiler has an option allowJs so it's very much a supported scenario from Typescripts point-of-view.

Having said that, mixing TS and JS is more of a feature to consider adding in the future if there is demand... the goal of this PR is to simply fix the bug of "my TS file cannot relatively import another TS file". So I like the idea for now of just throwing if TS and JS are mixed. I'll update the PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using LWC you are writing new things, so there is not upgrade path :) I think the logic is fine as it is but if you can throw when the missmatch happens it will avoid us to have to support that case all togeher,

@diervo
Copy link
Contributor

diervo commented Sep 25, 2019

@wes566 make sure we do throw if we detect a explicit missmatch between importer and importee extension and I will merge it.

@diervo
Copy link
Contributor

diervo commented Sep 26, 2019

I think is your first PR, so congrats @wes566 ! and thank you

@diervo diervo merged commit d6423f9 into salesforce:master Sep 26, 2019
@wes-goulet wes-goulet deleted the ts-fix branch September 26, 2019 21:23
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