Skip to content

Commit

Permalink
Try to use node resolution for module path
Browse files Browse the repository at this point in the history
This will allow assets to be in external modules.
Issue #3
  • Loading branch information
sveyret committed Sep 2, 2020
1 parent 5fc1a39 commit 94a65d0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Binaries
node_modules/
dist/
!__test__/node_modules

# Coverage
.nyc_output/
Expand Down
8 changes: 8 additions & 0 deletions __test__/node_modules/dummy/picture.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions __test__/package/success.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as pngImage from '../sub/folder/image.png'
import svgImage from '../image.svg'
import moduleImage from 'dummy/picture.svg'
import defaultImage from '../reexport'
import { image } from '../reexport'

export default function getPath(
type: 'fullImport' | 'defaultImport' | 'moduleImport' | 'defaultExport' | 'namedExport'
): string {
switch (type) {
case 'fullImport':
return pngImage
case 'defaultImport':
return svgImage
case 'moduleImport':
return moduleImage
case 'defaultExport':
return defaultImage
case 'namedExport':
return image
}
}
2 changes: 1 addition & 1 deletion __test__/success.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as pngImage from './sub/folder/image.png'
import svgImage from './image.svg'
import moduleImage from 'module/image.svg'
import moduleImage from 'dummy/picture.svg'
import defaultImage from './reexport'
import { image } from './reexport'

Expand Down
23 changes: 19 additions & 4 deletions src/context/AssetModuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class AssetModuleManager {

// Check if matching assets pattern
if (this.assetsMatch.test(moduleName)) {
return this.interpolateName(moduleName)
return this.interpolateName(this.findModulePath(moduleName))
}
}
return undefined
Expand All @@ -49,11 +49,10 @@ export default class AssetModuleManager {
/**
* Create the asset name using `targetName` template and given module name.
*
* @param moduleName - The name of module to use as interpolation source.
* @param modulePath - The path of module to use as interpolation source.
* @returns The asset name.
*/
private interpolateName(moduleName: string): string {
const modulePath = join(this.currentPath, moduleName)
private interpolateName(modulePath: string): string {
const parsed = parse(modulePath)
/* istanbul ignore next */
const ext = parsed.ext ? parsed.ext.substr(1) : 'bin'
Expand Down Expand Up @@ -91,4 +90,20 @@ export default class AssetModuleManager {
.replace(/\[folder\]/gi, folder)
return url
}

/**
* Find the module path, using default node resolution, or simply searching relative to current file.
*
* @param moduleName - The name of the module to find.
* @returns The module path.
*/
private findModulePath(moduleName: string): string {
try {
return require.resolve(moduleName, {
paths: [this.basePath],
})
} catch {
return join(this.currentPath, moduleName)
}
}
}
48 changes: 28 additions & 20 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('ts-transform-asset', function () {
result: {
fullImport: string
defaultImport: string
moduleImport: string
moduleImport: string | undefined
defaultExport: string
namedExport: string
}
Expand All @@ -26,7 +26,7 @@ describe('ts-transform-asset', function () {
result: {
fullImport: 'image.png',
defaultImport: 'image.svg',
moduleImport: 'image.svg',
moduleImport: 'picture.svg',
defaultExport: 'image.svg',
namedExport: 'image.svg',
},
Expand All @@ -36,16 +36,17 @@ describe('ts-transform-asset', function () {
result: {
fullImport: 'assets/image.png',
defaultImport: 'assets/image.svg',
moduleImport: 'assets/image.svg',
moduleImport: 'assets/picture.svg',
defaultExport: 'assets/image.svg',
namedExport: 'assets/image.svg',
},
},
default: {
rootDir: '__test__',
result: {
fullImport: '[hash].png',
defaultImport: 'b05767c238cb9f989cf3cd8180594878.svg',
moduleImport: '[hash].svg',
moduleImport: 'bee0f4fbbfd53e62289432b4a070cd03.svg',
defaultExport: 'b05767c238cb9f989cf3cd8180594878.svg',
namedExport: 'b05767c238cb9f989cf3cd8180594878.svg',
},
Expand All @@ -56,7 +57,8 @@ describe('ts-transform-asset', function () {
result: {
fullImport: 'sub/folder/folder_[hash]-[contenthash].png',
defaultImport: '_b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
moduleImport: 'module/module_[hash]-[contenthash].svg',
moduleImport:
'node_modules/dummy/dummy_bee0f4fbbfd53e62289432b4a070cd03-bee0f4fbbfd53e62289432b4a070cd03.svg',
defaultExport: '_b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
namedExport: '_b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
},
Expand All @@ -67,7 +69,7 @@ describe('ts-transform-asset', function () {
fullImport: '__test__/sub/folder/folder_[hash]-[contenthash].png',
defaultImport:
'__test__/__test___b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
moduleImport: '__test__/module/module_[hash]-[contenthash].svg',
moduleImport: undefined,
defaultExport:
'__test__/__test___b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
namedExport:
Expand Down Expand Up @@ -109,24 +111,30 @@ describe('ts-transform-asset', function () {
result.print()
})

it('should find full module import file', function () {
expect(result.requireContent('success')('fullImport')).to.equal(testCase.result.fullImport)
})
Array.of('success', 'package/success').forEach(file => {
describe(`...in file ${file}`, function () {
it('should find full module import file', function () {
expect(result.requireContent(file)('fullImport')).to.equal(testCase.result.fullImport)
})

it('should find default module import file', function () {
expect(result.requireContent('success')('defaultImport')).to.equal(testCase.result.defaultImport)
})
it('should find default module import file', function () {
expect(result.requireContent(file)('defaultImport')).to.equal(testCase.result.defaultImport)
})

it('should find external module file', function () {
expect(result.requireContent('success')('moduleImport')).to.equal(testCase.result.moduleImport)
})
if (testCase.result.moduleImport) {
it('should find external module file', function () {
expect(result.requireContent(file)('moduleImport')).to.equal(testCase.result.moduleImport)
})
}

it('should find default re-exported file', function () {
expect(result.requireContent('success')('defaultExport')).to.equal(testCase.result.defaultExport)
})
it('should find default re-exported file', function () {
expect(result.requireContent(file)('defaultExport')).to.equal(testCase.result.defaultExport)
})

it('should find named re-exported file', function () {
expect(result.requireContent('success')('namedExport')).to.equal(testCase.result.namedExport)
it('should find named re-exported file', function () {
expect(result.requireContent(file)('namedExport')).to.equal(testCase.result.namedExport)
})
})
})

it('should fail to require bad module', function () {
Expand Down

0 comments on commit 94a65d0

Please sign in to comment.