Skip to content

Commit 7469a15

Browse files
Jasper De Moordevongovett
authored andcommitted
Package.json can't be found bugfix (#170)
* add path traversing for package.json, plugin bug * improve * fix * add another test
1 parent dba452a commit 7469a15

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

β€Žsrc/Bundler.jsβ€Ž

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,20 @@ class Bundler extends EventEmitter {
8888
this.packagers.add(type, packager);
8989
}
9090

91-
loadPlugins() {
91+
loadPlugins(location = this.mainFile) {
92+
let pkg;
93+
try {
94+
pkg = localRequire('./package.json', location);
95+
} catch (err) {
96+
if (err.code === 'MODULE_NOT_FOUND' && location.length > 1) {
97+
return this.loadPlugins(Path.join(location, '..'));
98+
}
99+
}
92100
try {
93-
let pkg = localRequire('./package.json', this.mainFile);
94101
let deps = Object.assign({}, pkg.dependencies, pkg.devDependencies);
95102
for (let dep in deps) {
96103
if (dep.startsWith('parcel-plugin-')) {
97-
localRequire(dep, this.mainFile)(this);
104+
localRequire(dep, location)(this);
98105
}
99106
}
100107
} catch (err) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./test.txt');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

β€Žtest/plugins.jsβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@ describe('plugins', function() {
1414
let output = run(b);
1515
assert.equal(output, 'hello world');
1616
});
17+
18+
it('should load package.json from parent tree', async function() {
19+
let b = await bundle(__dirname + '/integration/plugins/sub-folder/index.js');
20+
21+
assertBundleTree(b, {
22+
name: 'index.js',
23+
assets: ['index.js', 'test.txt'],
24+
childBundles: []
25+
});
26+
27+
let output = run(b);
28+
assert.equal(output, 'hello world');
29+
});
1730
});

0 commit comments

Comments
Β (0)