Skip to content

Commit

Permalink
Fix absolute and tilde paths (#1324)
Browse files Browse the repository at this point in the history
Fix addURLDependency resolution in Asset.js

Minimal reproducible repo: https://github.com/marcosbozzani/parcel-bug-tilde-abs-paths

Just clone the repo, `yarn install` and `yarn tilde` or `yarn slash`

Tilde error: `Cannot resolve dependency './~\style.css'`
Slash error: `Cannot resolve dependency './..\..\..\..\..\..\`
  • Loading branch information
marcosbozzani authored and Jasper De Moor committed May 23, 2018
1 parent 099a98e commit 1614918
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const isURL = require('./utils/is-url');
const config = require('./utils/config');
const syncPromise = require('./utils/syncPromise');
const logger = require('./Logger');
const Resolver = require('./Resolver');

let ASSET_ID = 1;

Expand Down Expand Up @@ -41,6 +42,7 @@ class Asset {
this.endTime = 0;
this.buildTime = 0;
this.bundledSize = 0;
this.resolver = new Resolver(options);
}

shouldInvalidate() {
Expand Down Expand Up @@ -91,14 +93,22 @@ class Asset {
}

const parsed = URL.parse(url);
const resolved = path.resolve(
path.dirname(from),
decodeURIComponent(parsed.pathname)
);
this.addDependency(
'./' + path.relative(path.dirname(this.name), resolved),
Object.assign({dynamic: true}, opts)
);
let depName;
let resolved;
let dir = path.dirname(from);
const filename = decodeURIComponent(parsed.pathname);

if (filename[0] === '~' || filename[0] === '/') {
if (dir === '.') {
dir = this.options.rootDir;
}
depName = resolved = this.resolver.resolveFilename(filename, dir);
} else {
resolved = path.resolve(dir, filename);
depName = './' + path.relative(path.dirname(this.name), resolved);
}

this.addDependency(depName, Object.assign({dynamic: true}, opts));

parsed.pathname = this.options.parser
.getAsset(resolved, this.options)
Expand Down
15 changes: 15 additions & 0 deletions test/asset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const Asset = require('../src/Asset');
const {bundle} = require('./utils');

Expand Down Expand Up @@ -76,5 +77,19 @@ describe('Asset', () => {
`${bundleName}?bar#baz`
);
});

it('should resolve slash', () => {
asset.dependencies.clear();
assert.strictEqual(asset.addURLDependency('/foo'), bundleName);
const key = path.resolve('/root/dir/foo');
assert(asset.dependencies.has(key));
});

it('should resolve tilde', () => {
asset.dependencies.clear();
assert.strictEqual(asset.addURLDependency('~/foo'), bundleName);
const key = path.normalize('/root/dir/foo');
assert(asset.dependencies.has(key));
});
});
});

0 comments on commit 1614918

Please sign in to comment.