Skip to content
This repository was archived by the owner on Dec 4, 2022. It is now read-only.

Commit aee2d36

Browse files
authored
fix dependency resolution of . and .. to not be identified as custom-resolved used (#105)
* fix teambit/bit#1734, recognize "." and ".." partials as relative paths * avoid assuming that custom-resolve was used only based on the relative-path and the existence of the config. instead, explicitly mark custom-resolved as such. * bump version
1 parent f160ffa commit aee2d36

File tree

7 files changed

+89
-41
lines changed

7 files changed

+89
-41
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [unreleased]
99

10+
## [2.0.12-angular.3] - 2019-06-14
11+
12+
- fix dependency resolution of `.` and `..` to not be identified as custom-resolved used.
13+
1014
## [2.0.12-angular.2] - 2019-06-10
1115

1216
- add rxjs package

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bit-javascript",
3-
"version": "2.0.12-angular.2",
3+
"version": "2.0.12-angular.3",
44
"scripts": {
55
"flow": "flow; test $? -eq 0 -o $? -eq 2",
66
"lint": "eslint src && flow check || true",

src/dependency-builder/dependency-tree/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ module.exports._getDependencies = function (config) {
139139
if (!isDependenciesArray && dependenciesRaw[dependency].importSpecifiers) {
140140
pathMap.importSpecifiers = dependenciesRaw[dependency].importSpecifiers;
141141
}
142-
if (!isRelativeImport(dependency) && config.resolveConfig) {
143-
// is includes also packages, which actually don't use customResolve, however, they will be
144-
// filtered out later.
142+
if (cabinetParams.wasCustomResolveUsed) {
145143
pathMap.isCustomResolveUsed = true;
146144
}
147145

src/dependency-builder/dependency-tree/index.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require('../../utils');
1616
require('../../utils/is-relative-import');
1717
require('../../dependency-builder/detectives/detective-css-and-preprocessors');
1818
require('../../dependency-builder/detectives/detective-typescript');
19+
require('../../dependency-builder/detectives/parser-helper');
1920

2021
const dependencyTree = rewire('./');
2122
const fixtures = path.resolve(`${__dirname}/../../../fixtures/dependency-tree`);
@@ -918,4 +919,52 @@ describe('dependencyTree', function () {
918919
expect(visited[filename].missing).to.be.undefined;
919920
});
920921
});
922+
describe('resolve config when the dependency is "."', () => {
923+
it('should not set the dependency with isCustomResolveUsed=true', () => {
924+
mockfs({
925+
[`${__dirname}/src`]: {
926+
'foo.js': "require('.');",
927+
'index.js': 'module.exports = {}'
928+
}
929+
});
930+
const directory = path.normalize(`${__dirname}/src`);
931+
const filename = path.normalize(`${directory}/foo.js`);
932+
const config = {
933+
filename,
934+
directory,
935+
pathMap: [],
936+
resolveConfig: { aliases: { something: 'anything' } }
937+
};
938+
dependencyTree(config);
939+
const pathMapRecord = config.pathMap.find(f => f.file === filename);
940+
expect(pathMapRecord.dependencies).to.have.lengthOf(1);
941+
const dependency = pathMapRecord.dependencies[0];
942+
expect(dependency).to.not.have.property('isCustomResolveUsed');
943+
});
944+
});
945+
describe('resolve config when the dependency is ".."', () => {
946+
it('should not set the dependency with isCustomResolveUsed=true', () => {
947+
mockfs({
948+
[`${__dirname}/src`]: {
949+
'index.js': 'module.exports = {}',
950+
bar: {
951+
'foo.js': "require('..');"
952+
}
953+
}
954+
});
955+
const directory = path.normalize(`${__dirname}/src`);
956+
const filename = path.normalize(`${directory}/bar/foo.js`);
957+
const config = {
958+
filename,
959+
directory,
960+
pathMap: [],
961+
resolveConfig: { aliases: { something: 'anything' } }
962+
};
963+
dependencyTree(config);
964+
const pathMapRecord = config.pathMap.find(f => f.file === filename);
965+
expect(pathMapRecord.dependencies).to.have.lengthOf(1);
966+
const dependency = pathMapRecord.dependencies[0];
967+
expect(dependency).to.not.have.property('isCustomResolveUsed');
968+
});
969+
});
921970
});

src/dependency-builder/filing-cabinet/index.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ type Options = {
5252
isScript?: boolean, // relevant for Vue files
5353
ast?: string,
5454
ext?: string,
55-
content?: string
55+
content?: string,
56+
wasCustomResolveUsed?: boolean
5657
};
5758

5859
module.exports = function cabinet(options: Options) {
@@ -163,18 +164,7 @@ module.exports._getJSType = function (options) {
163164
* @return {String}
164165
*/
165166
function jsLookup(options: Options) {
166-
const {
167-
configPath,
168-
partial,
169-
directory,
170-
config,
171-
webpackConfig,
172-
filename,
173-
ast,
174-
isScript,
175-
content,
176-
resolveConfig
177-
} = options;
167+
const { configPath, partial, directory, config, webpackConfig, filename, ast, isScript, content } = options;
178168
const type = module.exports._getJSType({
179169
config,
180170
webpackConfig,
@@ -204,7 +194,7 @@ function jsLookup(options: Options) {
204194
case 'es6':
205195
default:
206196
debug(`using commonjs resolver ${type}`);
207-
return commonJSLookup(partial, filename, directory, resolveConfig);
197+
return commonJSLookup(options);
208198
}
209199
}
210200

@@ -225,7 +215,10 @@ function cssPreprocessorLookup(options: Options) {
225215
const { filename, partial, directory, resolveConfig } = options;
226216
if (resolveConfig && !isRelativeImport(partial)) {
227217
const result = resolveNonRelativePath(partial, filename, directory, resolveConfig);
228-
if (result) return result;
218+
if (result) {
219+
options.wasCustomResolveUsed = true;
220+
return result;
221+
}
229222
}
230223
if (partial.startsWith('~') && !partial.startsWith('~/')) {
231224
// webpack syntax for resolving a module from node_modules
@@ -239,10 +232,10 @@ function cssPreprocessorLookup(options: Options) {
239232
}
240233

241234
function tsLookup(options: Options) {
242-
const { partial, filename, directory, resolveConfig } = options;
235+
const { partial, filename } = options;
243236
if (partial[0] !== '.') {
244237
// when a path is not relative, use the standard commonJS lookup
245-
return commonJSLookup(partial, filename, directory, resolveConfig);
238+
return commonJSLookup(options);
246239
}
247240
debug('performing a typescript lookup');
248241

@@ -262,7 +255,7 @@ function tsLookup(options: Options) {
262255
if (!resolvedModule) {
263256
// ts.resolveModuleName doesn't always work, fallback to commonJSLookup
264257
debug('failed resolving with tsLookup, trying commonJSLookup');
265-
return commonJSLookup(partial, filename, directory, resolveConfig);
258+
return commonJSLookup(options);
266259
}
267260
debug('ts resolved module: ', resolvedModule);
268261
const result = resolvedModule ? resolvedModule.resolvedFileName : '';
@@ -291,8 +284,9 @@ function resolveNonRelativePath(partial, filename, directory, resolveConfig) {
291284
* @param {String} directory
292285
* @return {String}
293286
*/
294-
function commonJSLookup(partial, filename, directory, resolveConfig) {
295-
directory = path.dirname(filename); // node_modules should be propagated from the file location backwards
287+
function commonJSLookup(options: Options) {
288+
const { filename, resolveConfig } = options;
289+
const directory = path.dirname(filename); // node_modules should be propagated from the file location backwards
296290
// Need to resolve partials within the directory of the module, not filing-cabinet
297291
const moduleLookupDir = path.join(directory, 'node_modules');
298292

@@ -302,6 +296,7 @@ function commonJSLookup(partial, filename, directory, resolveConfig) {
302296

303297
// Make sure the partial is being resolved to the filename's context
304298
// 3rd party modules will not be relative
299+
let partial = options.partial;
305300
if (partial[0] === '.') {
306301
partial = path.resolve(path.dirname(filename), partial);
307302
}
@@ -319,6 +314,7 @@ function commonJSLookup(partial, filename, directory, resolveConfig) {
319314
if (!isRelativeImport(partial) && resolveConfig) {
320315
debug(`trying to resolve using resolveConfig ${JSON.stringify(resolveConfig)}`);
321316
result = resolveNonRelativePath(partial, filename, directory, resolveConfig);
317+
if (result) options.wasCustomResolveUsed = true;
322318
} else {
323319
debug(`could not resolve ${partial}`);
324320
}

src/dependency-builder/lookups/vue-lookup/index.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@ module.exports = function (options) {
1717
const { script, styles } = compiler.parseComponent(fileContent.toString(), { pad: 'line' });
1818
if (isScript) {
1919
const scriptExt = script.lang ? languageMap[script.lang] || script.lang : DEFAULT_SCRIPT_LANG;
20-
return cabinet({
21-
partial,
22-
filename,
23-
directory: path.dirname(filename),
24-
content: script.content,
25-
resolveConfig,
26-
ext: `.${scriptExt}` || path.extname(partial)
27-
});
20+
return cabinet(
21+
Object.assign(options, {
22+
directory: path.dirname(filename),
23+
content: script.content,
24+
ast: null,
25+
ext: `.${scriptExt}` || path.extname(partial)
26+
})
27+
);
2828
}
2929
const stylesResult = styles.map((style) => {
3030
const styleExt = style.lang ? languageMap[style.lang] || style.lang : DEFAULT_STYLE_LANG;
31-
return cabinet({
32-
partial,
33-
filename: `${path.join(path.dirname(filename), path.parse(filename).name)}.${styleExt}`,
34-
directory: path.dirname(filename),
35-
content: style.content,
36-
resolveConfig,
37-
ext: `.${styleExt}`
38-
});
31+
return cabinet(
32+
Object.assign(options, {
33+
filename: `${path.join(path.dirname(filename), path.parse(filename).name)}.${styleExt}`,
34+
directory: path.dirname(filename),
35+
content: style.content,
36+
ast: null,
37+
ext: `.${styleExt}`
38+
})
39+
);
3940
});
4041
return stylesResult[0];
4142
};

src/utils/is-relative-import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
* End quote.
1212
*/
1313
export default function isRelativeImport(str: string): boolean {
14-
return str.startsWith('./') || str.startsWith('../') || str.startsWith('/');
14+
return str.startsWith('./') || str.startsWith('../') || str.startsWith('/') || str === '.' || str === '..';
1515
}

0 commit comments

Comments
 (0)