Skip to content

Commit

Permalink
Use browser relative path algorithm for chunks (#2902)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 6, 2019
1 parent b1df517 commit 7179390
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
11 changes: 6 additions & 5 deletions browser/path.ts
Expand Up @@ -37,16 +37,17 @@ export function relative(from: string, to: string) {
const fromParts = from.split(/[/\\]/).filter(Boolean);
const toParts = to.split(/[/\\]/).filter(Boolean);

if (fromParts[0] === '.') fromParts.shift();
if (toParts[0] === '.') toParts.shift();

while (fromParts[0] && toParts[0] && fromParts[0] === toParts[0]) {
fromParts.shift();
toParts.shift();
}

while (toParts[0] === '.' || toParts[0] === '..') {
const toPart = toParts.shift();
if (toPart === '..') {
fromParts.pop();
}
while (toParts[0] === '..' && fromParts.length > 0) {
toParts.shift();
fromParts.pop();
}

while (fromParts.pop()) {
Expand Down
3 changes: 2 additions & 1 deletion src/Chunk.ts
@@ -1,5 +1,6 @@
import sha256 from 'hash.js/lib/hash/sha/256';
import MagicString, { Bundle as MagicStringBundle, SourceMap } from 'magic-string';
import { relative } from '../browser/path';
import ExportDefaultDeclaration from './ast/nodes/ExportDefaultDeclaration';
import FunctionDeclaration from './ast/nodes/FunctionDeclaration';
import { UNDEFINED_EXPRESSION } from './ast/values';
Expand Down Expand Up @@ -28,7 +29,7 @@ import { error } from './utils/error';
import { sortByExecutionOrder } from './utils/executionOrder';
import getIndentString from './utils/getIndentString';
import { makeLegal } from './utils/identifierHelpers';
import { basename, dirname, isAbsolute, normalize, relative, resolve } from './utils/path';
import { basename, dirname, isAbsolute, normalize, resolve } from './utils/path';
import relativeId, { getAliasName } from './utils/relativeId';
import renderChunk from './utils/renderChunk';
import { RenderOptions } from './utils/renderHelpers';
Expand Down
1 change: 1 addition & 0 deletions test/function/index.js
Expand Up @@ -51,6 +51,7 @@ runTestSuiteWithSamples('function', path.resolve(__dirname, 'samples'), (dir, co
path.basename(dir) + ': ' + config.description,
() => {
if (config.show) console.group(path.basename(dir));
if (config.before) config.before();

process.chdir(dir);
const warnings = [];
Expand Down
23 changes: 23 additions & 0 deletions test/function/samples/relative-outside-external/_config.js
@@ -0,0 +1,23 @@
const assert = require('assert');
const cwd = process.cwd;

module.exports = {
description: 'correctly resolves relative external imports from outside directories',
options: {
external() {
return true;
}
},
before() {
process.cwd = () => '/';
},
context: {
require(id) {
return id;
}
},
exports(exports) {
process.cwd = cwd;
assert.strictEqual(exports.value, '../../../test.js');
}
};
1 change: 1 addition & 0 deletions test/function/samples/relative-outside-external/main.js
@@ -0,0 +1 @@
export {default as value} from '../../../test.js';

0 comments on commit 7179390

Please sign in to comment.