Skip to content

Commit

Permalink
fixes #927, when a link file (file that only requires other file) is …
Browse files Browse the repository at this point in the history
…part of the component, the generated link was incorrect
  • Loading branch information
davidfirst committed Apr 27, 2018
1 parent bdf65fd commit bae8adb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased]

- [#927](https://github.com/teambit/bit/issues/927) fix a case of link file (file that only requires other file) is part of the component
- improve bit-diff to show the output with colors
- fix bit-move of a directly imported dependency
- fix importing a different version of dependent when dependencies are not saved as components
Expand Down
37 changes: 37 additions & 0 deletions e2e/flows/es6-link-files.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,43 @@ describe('es6 components with link files', function () {
});
});

describe('when a component uses link file to import members AND that link file is part of the component', () => {
let utilIndexFixture;
before(() => {
helper.setNewLocalAndRemoteScopes();
helper.importCompiler();
const isArrayFixture = "export default function isArray() { return 'got is-array'; };";
helper.createFile('utils', 'is-array.js', isArrayFixture);
const isStringFixture = "export default function isString() { return 'got is-string'; };";
helper.createFile('utils', 'is-string.js', isStringFixture);
utilIndexFixture =
"export { default as isArray } from './is-array'; export { default as isString } from './is-string'; ";
helper.createFile('utils', 'index.js', utilIndexFixture);
// notice that in this case, the index.js file (link-file) is part of the component
helper.addComponentWithOptions('utils', { i: 'utils/misc' });
const fooBarFixture =
"import { isString, isArray } from '../utils'; export default function foo() { return isString() + ' and ' + isArray() + ' and got foo'; };";
helper.createComponentBarFoo(fooBarFixture);
helper.addComponentBarFoo();

helper.commitAllComponents();
helper.exportAllComponents();
});
describe('when importing the component', () => {
before(() => {
helper.reInitLocalScope();
helper.addRemoteScope();
helper.importComponent('bar/foo');
});
it('should generate the links correctly as if there was no link-file', () => {
const appJsFixture = "const barFoo = require('./components/bar/foo'); console.log(barFoo.default());";
fs.outputFileSync(path.join(helper.localScopePath, 'app.js'), appJsFixture);
const result = helper.runCmd('node app.js');
expect(result.trim()).to.equal('got is-string and got is-array and got foo');
});
});
});

describe('when the link file uses default-import and specific-import together', () => {
before(() => {
helper.setNewLocalAndRemoteScopes();
Expand Down
11 changes: 10 additions & 1 deletion src/consumer/component/dependencies-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,24 @@ Try to run "bit import ${componentId} --objects" to get the component saved in t

const processLinkFile = (originFile: string, linkFile: Object, isTestFile: boolean = false) => {
if (!linkFile.dependencies || R.isEmpty(linkFile.dependencies)) return;
const nonLinkFiles = [];
linkFile.dependencies.forEach((dependency) => {
const component = getComponentIdByDepFile(linkFile.file);
if (component.componentId) {
// the linkFile is already a component, no need to treat it differently than other depFile
processDepFile(originFile, linkFile.file, dependency.importSpecifiers, undefined, isTestFile);
// aggregate all dependencies using the same linkFile and ultimately run processDepFile
// with all importSpecifiers of that linkFile.
// also, delete the linkFile attribute of importSpecifiers so then once the component is
// imported and the link is generated, it won't be treated as a linkFile.
dependency.importSpecifiers.map(a => delete a.linkFile);
nonLinkFiles.push(dependency.importSpecifiers);
} else {
processDepFile(originFile, dependency.file, dependency.importSpecifiers, linkFile.file, isTestFile);
}
});
if (nonLinkFiles.length) {
processDepFile(originFile, linkFile.file, R.flatten(nonLinkFiles), undefined, isTestFile);
}
};

const processDepFiles = (
Expand Down

0 comments on commit bae8adb

Please sign in to comment.