Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use browser relative path algorithm for chunks #2902

Merged
merged 1 commit into from Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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';