Skip to content

Commit ec3b246

Browse files
committed
Convert file paths to urls relyng on internal karma implementation details
1 parent 56dab23 commit ec3b246

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ Example:
7171
import Foo from 'alias/foo';
7272

7373
// Rewrites to
74-
import Foo from '/Absolute/Path/To/alias-root/foo';
74+
import Foo from '/absolute/Path/To/alias-root/foo';
7575
```
7676

77+
Note `/absolute/` prefix. This is the web route Karma adds to all urls leading to absolute files. It is important to keep it to avoid referencing same file with different urls.
78+
7779
#### `customResolver`
7880
Default value: `null`
7981

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ const isLocalPath = (filename) =>
1414

1515
const hasExtension = (filename, extension) => filename.endsWith('.' + extension);
1616

17+
// This is copypasted from karma middleware
18+
const filePathToUrlPath = (filePath, basePath, urlRoot, proxyPath) => {
19+
if (filePath.startsWith(basePath)) {
20+
return proxyPath + urlRoot.substr(1) + 'base' + filePath.substr(basePath.length)
21+
}
22+
return proxyPath + urlRoot.substr(1) + 'absolute' + filePath
23+
};
24+
1725
const createModuleResolverPreprocessor = (karmaConfig, args = {}, config = {}, logger, helper) => {
1826
const log = logger.create('preprocessor:module-resolver');
1927
const options = helper.merge({}, defaultOptions, args, config);
@@ -41,7 +49,13 @@ const createModuleResolverPreprocessor = (karmaConfig, args = {}, config = {}, l
4149
for (const alias of sortedAliases) {
4250
if (result.startsWith(alias)) {
4351
const pathUnderAlias = path.relative(alias, result);
44-
result = path.resolve(options.aliases[alias], pathUnderAlias);
52+
const absolutePath = path.resolve(options.aliases[alias], pathUnderAlias);
53+
result = filePathToUrlPath(
54+
absolutePath,
55+
karmaConfig.basePath,
56+
karmaConfig.urlRoot,
57+
karmaConfig.upstreamProxy ? karmaConfig.upstreamProxy.path : '/'
58+
)
4559
}
4660
}
4761
}

0 commit comments

Comments
 (0)