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 directory named imports #1364

Merged
merged 3 commits into from Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,53 +1,52 @@
import * as babel from '@babel/core'
import { ensurePosixPath } from '@redwoodjs/internal'

import namedDirectory from '../babel-plugin-redwood-directory-named-import'

// So tests pass on Windows too
const POSIX_DIRNAME = ensurePosixPath(__dirname)

const testCases = [
// Directory named imports
{
input: 'import pew from "./__fixtures__/directory-named-imports/Module"',
output: `import pew from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/Module/Module.js";`,
input:
'import { ImpModule } from "./__fixtures__/directory-named-imports/Module"',
output:
'import { ImpModule } from "./__fixtures__/directory-named-imports/Module/Module";',
},
// Directory named imports TSX
{
input: 'import pew from "./__fixtures__/directory-named-imports/TSX"',
output: `import pew from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/TSX/TSX.tsx";`,
input:
'import { ImpTSX } from "./__fixtures__/directory-named-imports/TSX"',
output: `import { ImpTSX } from "./__fixtures__/directory-named-imports/TSX/TSX";`,
},
// Directory named exports
{
input:
'export { pew } from "./__fixtures__/directory-named-imports/Module"',
output: `export { pew } from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/Module/Module.js";`,
'export { ExpModule } from "./__fixtures__/directory-named-imports/Module"',
output: `export { ExpModule } from "./__fixtures__/directory-named-imports/Module/Module";`,
},
// Gives preferences to `index.*`
{
input:
'export { pew } from "./__fixtures__/directory-named-imports/indexModule"',
output: `export { pew } from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/indexModule/index.js";`,
'export { ExpIndex } from "./__fixtures__/directory-named-imports/indexModule"',
output: `export { ExpIndex } from "./__fixtures__/directory-named-imports/indexModule/index";`,
},
{
input:
'export { pew } from "./__fixtures__/directory-named-imports/TSWithIndex"',
output: `export { pew } from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/TSWithIndex/index.js";`,
'export { TSWithIndex } from "./__fixtures__/directory-named-imports/TSWithIndex"',
output: `export { TSWithIndex } from "./__fixtures__/directory-named-imports/TSWithIndex/index";`,
},
// Supports "*.ts"
{
input: 'export { pew } from "./__fixtures__/directory-named-imports/TS"',
output: `export { pew } from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/TS/TS.ts";`,
output: `export { pew } from "./__fixtures__/directory-named-imports/TS/TS";`,
},
// Supports "*.tsx"
{
input: 'export { pew } from "./__fixtures__/directory-named-imports/TSX"',
output: `export { pew } from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/TSX/TSX.tsx";`,
output: `export { pew } from "./__fixtures__/directory-named-imports/TSX/TSX";`,
},
// Supports "*.jsx"
{
input: 'export { pew } from "./__fixtures__/directory-named-imports/JSX"',
output: `export { pew } from "${POSIX_DIRNAME}/__fixtures__/directory-named-imports/JSX/JSX.jsx";`,
output: `export { pew } from "./__fixtures__/directory-named-imports/JSX/JSX";`,
},
]

Expand Down
@@ -1,27 +1,25 @@
import { resolveFile, ensurePosixPath } from '@redwoodjs/internal'
import { resolveFile } from '@redwoodjs/internal'
import path from 'path'

import type { PluginObj, types } from '@babel/core'

const getNewPath = (value: string, filename: string) => {
const getNewPath = (value: string, filename: string): string | null => {
const dirname = path.dirname(value)
const basename = path.basename(value)

// We try to resolve `index.[js*|ts*]` modules first,
// since that's the desired default behaviour
const indexImportPath = [dirname, basename, 'index'].join('/')

const indexImportPathResolved = resolveFile(
path.join(path.dirname(filename), indexImportPath)
)

if (indexImportPathResolved) {
// If babel can resolve this path with the index file
return indexImportPathResolved
if (resolveFile(path.resolve(path.dirname(filename), indexImportPath))) {
return indexImportPath
} else {
// If there isn't a index file
const newImportPath = [dirname, basename, basename].join('/')

return resolveFile(path.join(path.dirname(filename), newImportPath))
// No index file found, so try to import the directory-named-module instead
const dirnameImportPath = [dirname, basename, basename].join('/')
if (resolveFile(path.resolve(path.dirname(filename), dirnameImportPath))) {
return dirnameImportPath
}
}
return null
}

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

const newPath = getNewPath(value, <string>filename)
if (!newPath) return
const newSource = t.stringLiteral(ensurePosixPath(newPath))
const newSource = t.stringLiteral(newPath)
p.node.source = newSource
},

Expand All @@ -62,14 +60,14 @@ export default function ({ types: t }: { types: typeof types }): PluginObj {
// We only need this plugin in the module could not be found.
try {
require.resolve(value)
return // ABORT
return // ABORT, since the file was resolved
} catch {
// CONTINUE...
}

const newPath = getNewPath(value, <string>filename)
if (!newPath) return
const newSource = t.stringLiteral(ensurePosixPath(newPath))
const newSource = t.stringLiteral(newPath)
// @ts-expect-error - TypeDef must be outdated.
p.node.source = newSource
},
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/src/paths.ts
Expand Up @@ -96,7 +96,7 @@ export const getBaseDirFromFile = (file: string) => {
*/
export const resolveFile = (
filePath: string,
extensions: string[] = ['.tsx', '.ts', '.js', '.jsx']
extensions: string[] = ['.js', '.tsx', '.ts', '.jsx']
): string | null => {
for (const extension of extensions) {
const p = `${filePath}${extension}`
Expand Down