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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19d480c
fix(web): Fix importing .ts/.tsx files in web with directory-named-im…
dac09 26dda97
Update tests
dac09 b8cfa25
Update tests | Directly use resolveFile
dac09 640a7d7
Fix index preference | Add new fixture for ts with index
dac09 cf087ad
fix(test): Normalise paths
dac09 5fbc986
Merge branch 'main' into fix/ts-imports-web
thedavidprice File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
Empty file.
Empty file.
34 changes: 21 additions & 13 deletions
34
packages/core/src/babelPlugins/__tests__/babel-plugin-redwood-directory-named-imports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { resolveFile, ensurePosixPath } from '@redwoodjs/internal' | ||
import path from 'path' | ||
|
||
import type { PluginObj, types } from '@babel/core' | ||
|
@@ -6,13 +7,20 @@ const getNewPath = (value: string, filename: string) => { | |
const dirname = path.dirname(value) | ||
const basename = path.basename(value) | ||
|
||
const newImportPath = [dirname, basename, basename].join('/') | ||
const indexImportPath = [dirname, basename, 'index'].join('/') | ||
|
||
try { | ||
require.resolve(path.resolve(path.dirname(filename), newImportPath)) | ||
return newImportPath | ||
} catch (e) { | ||
return null | ||
const indexImportPathResolved = resolveFile( | ||
path.join(path.dirname(filename), indexImportPath) | ||
) | ||
|
||
if (indexImportPathResolved) { | ||
// If babel can resolve this path with the index file | ||
return indexImportPathResolved | ||
} else { | ||
// If there isn't a index file | ||
const newImportPath = [dirname, basename, basename].join('/') | ||
|
||
return resolveFile(path.join(path.dirname(filename), newImportPath)) | ||
} | ||
} | ||
|
||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
p.node.source = newSource | ||
}, | ||
|
||
|
@@ -61,7 +69,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)) | ||
// @ts-expect-error - TypeDef must be outdated. | ||
p.node.source = newSource | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.