Skip to content

Commit b56c38c

Browse files
authored
Merge pull request #5 from slune-org/module
Try to use node resolution for module path
2 parents 5fc1a39 + 94a65d0 commit b56c38c

File tree

6 files changed

+79
-25
lines changed

6 files changed

+79
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Binaries
22
node_modules/
33
dist/
4+
!__test__/node_modules
45

56
# Coverage
67
.nyc_output/

__test__/node_modules/dummy/picture.svg

Lines changed: 8 additions & 0 deletions
Loading

__test__/package/success.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as pngImage from '../sub/folder/image.png'
2+
import svgImage from '../image.svg'
3+
import moduleImage from 'dummy/picture.svg'
4+
import defaultImage from '../reexport'
5+
import { image } from '../reexport'
6+
7+
export default function getPath(
8+
type: 'fullImport' | 'defaultImport' | 'moduleImport' | 'defaultExport' | 'namedExport'
9+
): string {
10+
switch (type) {
11+
case 'fullImport':
12+
return pngImage
13+
case 'defaultImport':
14+
return svgImage
15+
case 'moduleImport':
16+
return moduleImage
17+
case 'defaultExport':
18+
return defaultImage
19+
case 'namedExport':
20+
return image
21+
}
22+
}

__test__/success.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as pngImage from './sub/folder/image.png'
22
import svgImage from './image.svg'
3-
import moduleImage from 'module/image.svg'
3+
import moduleImage from 'dummy/picture.svg'
44
import defaultImage from './reexport'
55
import { image } from './reexport'
66

src/context/AssetModuleManager.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class AssetModuleManager {
4040

4141
// Check if matching assets pattern
4242
if (this.assetsMatch.test(moduleName)) {
43-
return this.interpolateName(moduleName)
43+
return this.interpolateName(this.findModulePath(moduleName))
4444
}
4545
}
4646
return undefined
@@ -49,11 +49,10 @@ export default class AssetModuleManager {
4949
/**
5050
* Create the asset name using `targetName` template and given module name.
5151
*
52-
* @param moduleName - The name of module to use as interpolation source.
52+
* @param modulePath - The path of module to use as interpolation source.
5353
* @returns The asset name.
5454
*/
55-
private interpolateName(moduleName: string): string {
56-
const modulePath = join(this.currentPath, moduleName)
55+
private interpolateName(modulePath: string): string {
5756
const parsed = parse(modulePath)
5857
/* istanbul ignore next */
5958
const ext = parsed.ext ? parsed.ext.substr(1) : 'bin'
@@ -91,4 +90,20 @@ export default class AssetModuleManager {
9190
.replace(/\[folder\]/gi, folder)
9291
return url
9392
}
93+
94+
/**
95+
* Find the module path, using default node resolution, or simply searching relative to current file.
96+
*
97+
* @param moduleName - The name of the module to find.
98+
* @returns The module path.
99+
*/
100+
private findModulePath(moduleName: string): string {
101+
try {
102+
return require.resolve(moduleName, {
103+
paths: [this.basePath],
104+
})
105+
} catch {
106+
return join(this.currentPath, moduleName)
107+
}
108+
}
94109
}

src/index.spec.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('ts-transform-asset', function () {
1515
result: {
1616
fullImport: string
1717
defaultImport: string
18-
moduleImport: string
18+
moduleImport: string | undefined
1919
defaultExport: string
2020
namedExport: string
2121
}
@@ -26,7 +26,7 @@ describe('ts-transform-asset', function () {
2626
result: {
2727
fullImport: 'image.png',
2828
defaultImport: 'image.svg',
29-
moduleImport: 'image.svg',
29+
moduleImport: 'picture.svg',
3030
defaultExport: 'image.svg',
3131
namedExport: 'image.svg',
3232
},
@@ -36,16 +36,17 @@ describe('ts-transform-asset', function () {
3636
result: {
3737
fullImport: 'assets/image.png',
3838
defaultImport: 'assets/image.svg',
39-
moduleImport: 'assets/image.svg',
39+
moduleImport: 'assets/picture.svg',
4040
defaultExport: 'assets/image.svg',
4141
namedExport: 'assets/image.svg',
4242
},
4343
},
4444
default: {
45+
rootDir: '__test__',
4546
result: {
4647
fullImport: '[hash].png',
4748
defaultImport: 'b05767c238cb9f989cf3cd8180594878.svg',
48-
moduleImport: '[hash].svg',
49+
moduleImport: 'bee0f4fbbfd53e62289432b4a070cd03.svg',
4950
defaultExport: 'b05767c238cb9f989cf3cd8180594878.svg',
5051
namedExport: 'b05767c238cb9f989cf3cd8180594878.svg',
5152
},
@@ -56,7 +57,8 @@ describe('ts-transform-asset', function () {
5657
result: {
5758
fullImport: 'sub/folder/folder_[hash]-[contenthash].png',
5859
defaultImport: '_b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
59-
moduleImport: 'module/module_[hash]-[contenthash].svg',
60+
moduleImport:
61+
'node_modules/dummy/dummy_bee0f4fbbfd53e62289432b4a070cd03-bee0f4fbbfd53e62289432b4a070cd03.svg',
6062
defaultExport: '_b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
6163
namedExport: '_b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
6264
},
@@ -67,7 +69,7 @@ describe('ts-transform-asset', function () {
6769
fullImport: '__test__/sub/folder/folder_[hash]-[contenthash].png',
6870
defaultImport:
6971
'__test__/__test___b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
70-
moduleImport: '__test__/module/module_[hash]-[contenthash].svg',
72+
moduleImport: undefined,
7173
defaultExport:
7274
'__test__/__test___b05767c238cb9f989cf3cd8180594878-b05767c238cb9f989cf3cd8180594878.svg',
7375
namedExport:
@@ -109,24 +111,30 @@ describe('ts-transform-asset', function () {
109111
result.print()
110112
})
111113

112-
it('should find full module import file', function () {
113-
expect(result.requireContent('success')('fullImport')).to.equal(testCase.result.fullImport)
114-
})
114+
Array.of('success', 'package/success').forEach(file => {
115+
describe(`...in file ${file}`, function () {
116+
it('should find full module import file', function () {
117+
expect(result.requireContent(file)('fullImport')).to.equal(testCase.result.fullImport)
118+
})
115119

116-
it('should find default module import file', function () {
117-
expect(result.requireContent('success')('defaultImport')).to.equal(testCase.result.defaultImport)
118-
})
120+
it('should find default module import file', function () {
121+
expect(result.requireContent(file)('defaultImport')).to.equal(testCase.result.defaultImport)
122+
})
119123

120-
it('should find external module file', function () {
121-
expect(result.requireContent('success')('moduleImport')).to.equal(testCase.result.moduleImport)
122-
})
124+
if (testCase.result.moduleImport) {
125+
it('should find external module file', function () {
126+
expect(result.requireContent(file)('moduleImport')).to.equal(testCase.result.moduleImport)
127+
})
128+
}
123129

124-
it('should find default re-exported file', function () {
125-
expect(result.requireContent('success')('defaultExport')).to.equal(testCase.result.defaultExport)
126-
})
130+
it('should find default re-exported file', function () {
131+
expect(result.requireContent(file)('defaultExport')).to.equal(testCase.result.defaultExport)
132+
})
127133

128-
it('should find named re-exported file', function () {
129-
expect(result.requireContent('success')('namedExport')).to.equal(testCase.result.namedExport)
134+
it('should find named re-exported file', function () {
135+
expect(result.requireContent(file)('namedExport')).to.equal(testCase.result.namedExport)
136+
})
137+
})
130138
})
131139

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

0 commit comments

Comments
 (0)