Skip to content

Instantly share code, notes, and snippets.

@peterp
Last active December 11, 2021 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterp/31cafb73534ee7ebbd009ebc80f8b959 to your computer and use it in GitHub Desktop.
Save peterp/31cafb73534ee7ebbd009ebc80f8b959 to your computer and use it in GitHub Desktop.
import { transformSync } from '@swc/core'
import path from 'path'
import { AliasPathResolver } from './index'
test('named `src` imports', () => {
// create resolved path.
const out = transformSync(
`
import { woof } from 'src/lib/dog.ts'
`,
{
plugin: (m) =>
new AliasPathResolver({
aliasPath: __dirname,
// let's pretend file is in `src/services/dogService/dogService.ts`
sourcePath: path.join(__dirname, 'services/dogServices/index.ts'),
}).visitProgram(m),
}
)
expect(out.code).toMatchInlineSnapshot(`
"import { woof } from '../../lib/dog.ts';
"
`)
})
test('default `src` imports', () => {
// create resolved path.
const out = transformSync(
`
import X from 'src/lib/dog.ts'
`,
{
plugin: (m) =>
new AliasPathResolver({
aliasPath: __dirname,
// let's pretend file is in `src/services/dogService/dogService.ts`
sourcePath: path.join(__dirname, 'services/dogServices/index.ts'),
}).visitProgram(m),
}
)
expect(out.code).toMatchInlineSnapshot(`
"import X from '../../lib/dog.ts';
"
`)
})
test('named `src` exports', () => {
// create resolved path.
const out = transformSync(
`
export { woof } from 'src/lib/dog.ts'
`,
{
plugin: (m) =>
new AliasPathResolver({
aliasPath: __dirname,
// let's pretend file is in `src/services/dogService/dogService.ts`
sourcePath: path.join(__dirname, 'services/dogServices/index.ts'),
}).visitProgram(m),
}
)
expect(out.code).toMatchInlineSnapshot(`
"export { woof } from '../../lib/dog.ts';
"
`)
})
test('default `src` exports', () => {
// create resolved path.
const out = transformSync(
`
export * as Dog from 'src/lib/dog.ts'
`,
{
plugin: (m) =>
new AliasPathResolver({
aliasPath: __dirname,
// let's pretend file is in `src/services/dogService/dogService.ts`
sourcePath: path.join(__dirname, 'services/dogServices/index.ts'),
}).visitProgram(m),
}
)
expect(out.code).toMatchInlineSnapshot(`
"import * as _Dog from '../../lib/dog.ts';
export { _Dog as Dog };
"
`)
})
import {
ExportNamedDeclaration,
ImportDeclaration,
ModuleDeclaration,
} from '@swc/core'
import Visitor from '@swc/core/Visitor'
import path from 'path'
interface AliasPathOptions {
/** the absolute path to your `src` directory */
aliasPath: string
/** the path to the source file */
sourcePath: string
}
export class AliasPathResolver extends Visitor {
options: AliasPathOptions
constructor(options: AliasPathOptions) {
super()
this.options = options
}
replaceImportPath = (value: string) => {
const absAliasPath = path.join(this.options.aliasPath, value.substring(4))
let newImportPath = path.relative(
path.dirname(this.options.sourcePath),
absAliasPath
)
if (newImportPath.indexOf('.') !== 0) {
newImportPath = './' + newImportPath
}
return newImportPath
}
visitImportDeclaration(n: ImportDeclaration): ImportDeclaration {
if (n.source.value.startsWith('src/')) {
n.source.value = this.replaceImportPath(n.source.value)
}
return n
}
visitExportNamedDeclaration(n: ExportNamedDeclaration): ModuleDeclaration {
if (n?.source?.value.startsWith('src/')) {
n.source.value = this.replaceImportPath(n.source.value)
}
return n
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment