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(web): Fix importing .ts/.tsx files in web with directory-named-import plugin #1332

Merged
merged 6 commits into from
Oct 15, 2020

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented Oct 13, 2020

As title :)

Resolves #1290
Resolves #1214

todo

  • Add another test for typescript imports
  • Make sure it prefers index.{js/ts} over module name

@dac09 dac09 requested a review from peterp October 13, 2020 12:52
@@ -9,7 +10,16 @@ const getNewPath = (value: string, filename: string) => {
const newImportPath = [dirname, basename, basename].join('/')

try {
Copy link
Contributor

@peterp peterp Oct 13, 2020

Choose a reason for hiding this comment

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

Since resolveFile returns null when the path isn't found, I think we can remove the try/catch blocks.

Suggested change
try {
return resolveFile(path.join(path.dirname(filename), newImportPath))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

@peterp
Copy link
Contributor

peterp commented Oct 13, 2020

oooh, 🦜🦆 with 🗻

@dac09
Copy link
Collaborator Author

dac09 commented Oct 13, 2020

Note: there is one failing test // Gives preferences to `index.* I'll fix this after lunch!

EDIT: done

@peterp
Copy link
Contributor

peterp commented Oct 13, 2020

Oh windows!

@peterp
Copy link
Contributor

peterp commented Oct 13, 2020

I think @Tobbe contributed an utility called poxisPath in @redwoodjs/internal

@dac09
Copy link
Collaborator Author

dac09 commented Oct 13, 2020

I think @Tobbe contributed an utility called poxisPath in @redwoodjs/internal

Giving that a go now 👍

@dac09
Copy link
Collaborator Author

dac09 commented Oct 14, 2020

@peterp ready for review. Finally got them windows paths! ✅

@@ -35,7 +43,7 @@ export default function ({ types: t }: { types: typeof types }): PluginObj {

const newPath = getNewPath(value, <string>filename)
if (!newPath) return
const newSource = t.stringLiteral(value.replace(value, newPath))
const newSource = t.stringLiteral(ensurePosixPath(newPath))
Copy link
Member

Choose a reason for hiding this comment

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

<3

{
input: 'import pew from "./__fixtures__/directory-named-imports/Module"',
output:
'import pew from "./__fixtures__/directory-named-imports/Module/Module"',
output: `import pew from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/Module/Module.js";`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Had a thought. Something feels off here, because imports in NodeJS are always in unix style. I guess it's because of the way that we get the absolute paths.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not exactly happy with this. There's nothing actionable, just thought I would mention it.

@peterp
Copy link
Contributor

peterp commented Oct 15, 2020

THanks <3!

@peterp peterp added this to the Next release milestone Oct 15, 2020
@peterp peterp merged commit c46c869 into redwoodjs:main Oct 15, 2020
@dac09 dac09 deleted the fix/ts-imports-web branch October 15, 2020 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants