From c11807f1ef0c7bec8799af3b9bfe99b32b676a83 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Thu, 6 Jun 2019 10:13:13 +0200 Subject: [PATCH] Use browser relative path algorithm for chunks --- browser/path.ts | 11 +++++---- src/Chunk.ts | 3 ++- test/function/index.js | 1 + .../relative-outside-external/_config.js | 23 +++++++++++++++++++ .../samples/relative-outside-external/main.js | 1 + 5 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 test/function/samples/relative-outside-external/_config.js create mode 100644 test/function/samples/relative-outside-external/main.js diff --git a/browser/path.ts b/browser/path.ts index a774121edcf..19be8664082 100644 --- a/browser/path.ts +++ b/browser/path.ts @@ -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()) { diff --git a/src/Chunk.ts b/src/Chunk.ts index 23816cbe925..2803e1f536e 100644 --- a/src/Chunk.ts +++ b/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'; @@ -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'; diff --git a/test/function/index.js b/test/function/index.js index 04436fa3004..939ccb61b9d 100644 --- a/test/function/index.js +++ b/test/function/index.js @@ -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 = []; diff --git a/test/function/samples/relative-outside-external/_config.js b/test/function/samples/relative-outside-external/_config.js new file mode 100644 index 00000000000..1f9b807717d --- /dev/null +++ b/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'); + } +}; diff --git a/test/function/samples/relative-outside-external/main.js b/test/function/samples/relative-outside-external/main.js new file mode 100644 index 00000000000..5cccc1d766f --- /dev/null +++ b/test/function/samples/relative-outside-external/main.js @@ -0,0 +1 @@ +export {default as value} from '../../../test.js';