Skip to content
This repository has been archived by the owner on Jul 10, 2021. It is now read-only.

Commit

Permalink
feat: handle ../ paths
Browse files Browse the repository at this point in the history
  • Loading branch information
karlprieb committed Feb 23, 2021
1 parent 51da961 commit 470b358
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
18 changes: 12 additions & 6 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { existsSync, lstatSync } = require('fs')
const { resolve, extname, dirname } = require('path')

const isNodeModule = module => {
if (module === '../') {
return false
}

try {
require.resolve(module)
return true
Expand Down Expand Up @@ -40,16 +44,18 @@ const makeDeclaration =
const hasModuleExt = extname(module).length
const newModuleName = hasModuleExt ? module.slice(0, -extname(module).length) : module

const pathLiteral =
existsSync(dirPath) &&
lstatSync(dirPath).isDirectory()
? `${module}/index.${extension}`
: `${newModuleName}.${extension}`
const pathLiteral = () => {
if (existsSync(dirPath) && lstatSync(dirPath).isDirectory()) {
return `${module}${newModuleName.endsWith('/') ? '' : '/'}index.${extension}`
}

return `${newModuleName}.${extension}`
}

path.replaceWith(
declaration(
...args(path),
stringLiteral(pathLiteral)
stringLiteral(pathLiteral())
)
)
}
Expand Down
40 changes: 32 additions & 8 deletions tests/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Replace should add the custom extension to export statements 1`] = `
"export { something } from \\"./lib/something.jsx\\";
"export { oneBackLevel } from \\"../index.jsx\\";
export { twoBackLevel } from \\"../../index.jsx\\";
export { somethingBack } from \\"../lib/something.jsx\\";
export { something } from \\"./lib/something.jsx\\";
export { something as another } from \\"./lib/something.jsx\\";
export * as anotherModule from \\"./lib/something.jsx\\";
export * from \\"./lib/something.jsx\\";
Expand All @@ -13,7 +16,10 @@ export * as replacer_something2 from './lib/something.ts';"
`;

exports[`Replace should add the custom extension to import statements 1`] = `
"import { export1, export2 as alias2 } from \\"./lib/something.jsx\\";
"import { oneBackLevel } from \\"../index.jsx\\";
import { twoBackLevel } from \\"../../index.jsx\\";
import { somethingBack } from \\"../lib/something.jsx\\";
import { export1, export2 as alias2 } from \\"./lib/something.jsx\\";
import { something } from \\"./lib/something.jsx\\";
import { something as other } from \\"./lib/something.jsx\\";
import anotherImport from \\"./lib/something.jsx\\";
Expand All @@ -29,7 +35,10 @@ import * as replacer_Something from './lib/something.ts';"
`;

exports[`Replace should add the default extension to export statements 1`] = `
"export { something } from \\"./lib/something.js\\";
"export { oneBackLevel } from \\"../index.js\\";
export { twoBackLevel } from \\"../../index.js\\";
export { somethingBack } from \\"../lib/something.js\\";
export { something } from \\"./lib/something.js\\";
export { something as another } from \\"./lib/something.js\\";
export * as anotherModule from \\"./lib/something.js\\";
export * from \\"./lib/something.js\\";
Expand All @@ -41,7 +50,10 @@ export * as replacer_something2 from './lib/something.ts';"
`;

exports[`Replace should add the default extension to import statements 1`] = `
"import { export1, export2 as alias2 } from \\"./lib/something.js\\";
"import { oneBackLevel } from \\"../index.js\\";
import { twoBackLevel } from \\"../../index.js\\";
import { somethingBack } from \\"../lib/something.js\\";
import { export1, export2 as alias2 } from \\"./lib/something.js\\";
import { something } from \\"./lib/something.js\\";
import { something as other } from \\"./lib/something.js\\";
import anotherImport from \\"./lib/something.js\\";
Expand All @@ -57,7 +69,10 @@ import * as replacer_Something from './lib/something.ts';"
`;

exports[`Replace should add the replace custom extension to export statements 1`] = `
"export { something } from \\"./lib/something.jsx\\";
"export { oneBackLevel } from \\"../index.jsx\\";
export { twoBackLevel } from \\"../../index.jsx\\";
export { somethingBack } from \\"../lib/something.jsx\\";
export { something } from \\"./lib/something.jsx\\";
export { something as another } from \\"./lib/something.jsx\\";
export * as anotherModule from \\"./lib/something.jsx\\";
export * from \\"./lib/something.jsx\\";
Expand All @@ -69,7 +84,10 @@ export * as replacer_something2 from \\"./lib/something.jsx\\";"
`;

exports[`Replace should add the replace custom extension to import statements 1`] = `
"import { export1, export2 as alias2 } from \\"./lib/something.jsx\\";
"import { oneBackLevel } from \\"../index.jsx\\";
import { twoBackLevel } from \\"../../index.jsx\\";
import { somethingBack } from \\"../lib/something.jsx\\";
import { export1, export2 as alias2 } from \\"./lib/something.jsx\\";
import { something } from \\"./lib/something.jsx\\";
import { something as other } from \\"./lib/something.jsx\\";
import anotherImport from \\"./lib/something.jsx\\";
Expand All @@ -85,7 +103,10 @@ import * as replacer_Something from \\"./lib/something.jsx\\";"
`;

exports[`Replace should add the replace default extension to export statements 1`] = `
"export { something } from \\"./lib/something.js\\";
"export { oneBackLevel } from \\"../index.js\\";
export { twoBackLevel } from \\"../../index.js\\";
export { somethingBack } from \\"../lib/something.js\\";
export { something } from \\"./lib/something.js\\";
export { something as another } from \\"./lib/something.js\\";
export * as anotherModule from \\"./lib/something.js\\";
export * from \\"./lib/something.js\\";
Expand All @@ -97,7 +118,10 @@ export * as replacer_something2 from \\"./lib/something.js\\";"
`;

exports[`Replace should add the replace default extension to import statements 1`] = `
"import { export1, export2 as alias2 } from \\"./lib/something.js\\";
"import { oneBackLevel } from \\"../index.js\\";
import { twoBackLevel } from \\"../../index.js\\";
import { somethingBack } from \\"../lib/something.js\\";
import { export1, export2 as alias2 } from \\"./lib/something.js\\";
import { something } from \\"./lib/something.js\\";
import { something as other } from \\"./lib/something.js\\";
import anotherImport from \\"./lib/something.js\\";
Expand Down
6 changes: 6 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const syntaxTypescript = require('@babel/plugin-syntax-typescript')
const plugin = require('../src/plugin.js')

const importStatements = `
import { oneBackLevel } from '../'
import { twoBackLevel } from '../..'
import { somethingBack } from '../lib/something'
import { export1 , export2 as alias2 } from './lib/something'
import { something } from './lib/something'
import { something as other } from './lib/something'
Expand All @@ -21,6 +24,9 @@ import * as replacer_Something from './lib/something.ts'
`

const exportStatements = `
export { oneBackLevel } from '../'
export { twoBackLevel } from '../../'
export { somethingBack } from '../lib/something'
export { something } from './lib/something'
export { something as another } from './lib/something'
export * as anotherModule from './lib/something'
Expand Down

0 comments on commit 470b358

Please sign in to comment.