Skip to content

Commit

Permalink
fix(package-analyzer): infer index.js as main
Browse files Browse the repository at this point in the history
fixes #583
  • Loading branch information
JeroenVinke committed May 23, 2017
1 parent fd07344 commit f5c0ed1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/build/package-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,24 @@ function determineLoaderConfig(project, description) {
sourcePath = path.join(location, metadata.main);
}
} else {
sourcePath = path.join(location, metadata.main);
sourcePath = path.join(location, metadata.main || 'index');
}
} else {
sourcePath = path.join(location, 'index');
}

sourcePath = path.relative(path.resolve(project.paths.root), sourcePath);
sourcePath = sourcePath.endsWith('.js') ? sourcePath : sourcePath + '.js';

description.loaderConfig = {
name: description.name,
path: removeExtension(sourcePath)
};
if (fs.existsSync(sourcePath)) {
sourcePath = path.relative(path.resolve(project.paths.root), sourcePath);

description.loaderConfig = {
name: description.name,
path: removeExtension(sourcePath)
};
} else {
throw new Error(`The "${description.name}" package references a main file that does not exist: ${sourcePath}`);
}
}

function setLocation(project, description) {
Expand Down
39 changes: 39 additions & 0 deletions spec/lib/build/package-analyzer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ describe('The PackageAnalyzer', () => {
// setup mock package.json
const fsConfig = {};
fsConfig[path.join('node_modules/my-package', 'package.json')] = '{ "name": "my-package", "main": "index.js" }';
fsConfig[path.join('node_modules/my-package', 'index.js')] = 'some-content';
fsConfig[project.paths.root] = {};
mockfs(fsConfig);

Expand All @@ -127,6 +128,7 @@ describe('The PackageAnalyzer', () => {
// setup mock package.json
const fsConfig = {};
fsConfig[path.join('node_modules/my-package', 'package.json')] = '{ "name": "my-package", "main": "index.js" }';
fsConfig[path.join('node_modules/my-package', 'index.js')] = 'some-content';
fsConfig[project.paths.root] = {};
mockfs(fsConfig);

Expand All @@ -143,6 +145,7 @@ describe('The PackageAnalyzer', () => {
// setup mock package.json
const fsConfig = {};
let json = '{ "name": "my-package", "main": "index.js", "jspm": { "directories": { "dist": "foobar" }, "main": "my-main.js" } }';
fsConfig[path.join('node_modules/my-package/foobar', 'my-main.js')] = 'some-content';
fsConfig[path.join('node_modules/my-package', 'package.json')] = json;
fsConfig[project.paths.root] = {};
mockfs(fsConfig);
Expand All @@ -156,10 +159,29 @@ describe('The PackageAnalyzer', () => {
.catch(e => done.fail(e));
});

it('infers index.js as main file where package.json has no main property', done => {
// setup mock package.json
const fsConfig = {};
let json = '{ "name": "my-package" }';
fsConfig[path.join('node_modules/my-package', 'package.json')] = json;
fsConfig[path.join('node_modules/my-package', 'index.js')] = 'some-content';
fsConfig[project.paths.root] = {};
mockfs(fsConfig);

sut.analyze('my-package')
.then(description => {
expect(description.loaderConfig.name).toBe('my-package');
expect(description.loaderConfig.path).toBe('..\\node_modules\\my-package\\index');
done();
})
.catch(e => done.fail(e));
});

it('analyze() works when there is no package.json. Uses index.js as the main file', done => {
// setup mock package.json
const fsConfig = {};
fsConfig[path.join('node_modules/my-package')] = {};
fsConfig[path.join('node_modules/my-package', 'index.js')] = 'some-content';
fsConfig[project.paths.root] = {};
mockfs(fsConfig);

Expand All @@ -171,4 +193,21 @@ describe('The PackageAnalyzer', () => {
})
.catch(e => done.fail(e));
});

it('analyze() throws error when main file does not exist', done => {
// setup mock package.json
const fsConfig = {};
fsConfig[path.join('node_modules', 'my-package', 'package.json')] = '{ "main": "foo.js" }';
fsConfig[project.paths.root] = {};
mockfs(fsConfig);

let p = path.resolve(path.join('node_modules', 'my-package', 'foo.js'));

sut.analyze('my-package')
.then(() => done.fail('should have thrown an exception'))
.catch(e => {
expect(e.message).toBe(`The "my-package" package references a main file that does not exist: ${p}`);
done();
});
});
});

0 comments on commit f5c0ed1

Please sign in to comment.