Skip to content

Commit

Permalink
Package.json can't be found bugfix (#170)
Browse files Browse the repository at this point in the history
* add path traversing for package.json, plugin bug

* improve

* fix

* add another test
  • Loading branch information
Jasper De Moor authored and devongovett committed Dec 12, 2017
1 parent dba452a commit 7469a15
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,20 @@ class Bundler extends EventEmitter {
this.packagers.add(type, packager);
}

loadPlugins() {
loadPlugins(location = this.mainFile) {
let pkg;
try {
pkg = localRequire('./package.json', location);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND' && location.length > 1) {
return this.loadPlugins(Path.join(location, '..'));
}
}
try {
let pkg = localRequire('./package.json', this.mainFile);
let deps = Object.assign({}, pkg.dependencies, pkg.devDependencies);
for (let dep in deps) {
if (dep.startsWith('parcel-plugin-')) {
localRequire(dep, this.mainFile)(this);
localRequire(dep, location)(this);
}
}
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions test/integration/plugins/sub-folder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./test.txt');
1 change: 1 addition & 0 deletions test/integration/plugins/sub-folder/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
13 changes: 13 additions & 0 deletions test/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@ describe('plugins', function() {
let output = run(b);
assert.equal(output, 'hello world');
});

it('should load package.json from parent tree', async function() {
let b = await bundle(__dirname + '/integration/plugins/sub-folder/index.js');

assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'test.txt'],
childBundles: []
});

let output = run(b);
assert.equal(output, 'hello world');
});
});

0 comments on commit 7469a15

Please sign in to comment.