Skip to content

Commit bf183e1

Browse files
authored
Merge pull request #52 from andyhot/fix/config-rewrite
Fix config import rewriting to only affect the root config
2 parents 287811a + b82f0db commit bf183e1

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

lib/transforms/import-declarations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ function transformer(file, api) {
2222
var sourceRelativePath = file.fileInfo.sourceRelativePath;
2323

2424
if (sourceRelativePath.startsWith('tests/')) {
25-
importPath = path_utils.makeAbsolute(appName + '/app/' + sourceRelativePath, importPath);
25+
importPath = path_utils.makeAbsolute('app/' + sourceRelativePath, importPath, appName);
2626

2727
// see if we stayed within tests
2828
var inTestsPrefix = appName + '/app/tests/';
2929
if (importPath.startsWith(inTestsPrefix)) {
3030
importPath = appName + '/tests/' + importPath.substring(inTestsPrefix.length);
3131
}
3232
} else {
33-
importPath = path_utils.makeAbsolute(appName + '/' + sourceRelativePath, importPath);
33+
importPath = path_utils.makeAbsolute(sourceRelativePath, importPath, appName);
3434
}
3535
} else {
3636
// check if import path starts with appName and add /app

lib/utils/path.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ function makeRelative(from, to) {
1212
return path;
1313
}
1414

15-
function makeAbsolute(base, path) {
16-
var isConfigPath = path.indexOf('/config/') > 0;
17-
var baseDir = p.dirname('/' + base);
18-
if (isConfigPath) {
19-
baseDir += '/..';
15+
function makeAbsolute(base, path, appName) {
16+
var baseDir = p.dirname(`/${appName}/${base}`);
17+
18+
// check if path goes over the root config folder and adjust base
19+
var configIndex = path.indexOf('/config/');
20+
if (configIndex >= 0) {
21+
var configPath = p.resolve(baseDir, path.substring(0, configIndex) + '/config').substring(1);
22+
if (configPath == `${appName}/app/config`) {
23+
baseDir += '/..';
24+
}
2025
}
26+
2127
return p.resolve(baseDir, path).substring(1);
2228
}
2329

test/fixtures/config/input.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ module.exports = {
22
app: {
33
'app.js': 'import config from "./config/environment";',
44
utils: {
5-
'first.js': 'import config from "my-app/config/environment";',
6-
'second.js': 'import config from "../config/environment";'
5+
'first.js': 'import ConfigUtils from "./config/third";',
6+
'second.js': 'import config from "../config/environment";',
7+
config: {
8+
'third.js': 'import config from "../../config/environment";',
9+
}
710
}
811
},
912
config: {
@@ -13,7 +16,10 @@ module.exports = {
1316
unit: {
1417
utils: {
1518
'first-test.js': 'import config from "../../../config/environment";',
16-
'second-test.js': 'import config from "my-app/config/environment";'
19+
'second-test.js': 'import config from "my-app/config/environment";',
20+
config: {
21+
'third-test.js': 'import config from "my-app/config/environment";'
22+
}
1723
}
1824
}
1925
}

test/fixtures/config/output.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ module.exports = {
33
'main.js': 'import config from "../config/environment";',
44
utils: {
55
first: {
6-
'util.js': 'import config from "my-app/config/environment";',
6+
'util.js': 'import ConfigUtils from "../config/third/util";',
77
'util-unit-test.js': 'import config from "../../../config/environment";'
88
},
99
second: {
1010
'util.js': 'import config from "../../../config/environment";',
1111
'util-unit-test.js': 'import config from "my-app/config/environment";'
12+
},
13+
config: {
14+
third: {
15+
'util-unit-test.js': 'import config from "my-app/config/environment";',
16+
'util.js': 'import config from "../../../../config/environment";'
17+
}
1218
}
1319
}
1420
},

0 commit comments

Comments
 (0)