Skip to content

Commit 5f29c9f

Browse files
authored
Merge pull request #32 from parcel-bundler/windows
Fix path handling on windows
2 parents 5b5d5fe + db88b2e commit 5f29c9f

13 files changed

+81
-35
lines changed

.mocharc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"require": ["@babel/register"]
3+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@parcel/source-map",
3-
"version": "2.0.0-alpha.4.14",
3+
"version": "2.0.0-alpha.4.15",
44
"main": "./dist/node.js",
55
"browser": "./dist/wasm-browser.js",
66
"license": "MIT",
@@ -61,6 +61,7 @@
6161
"@babel/core": "^7.9.0",
6262
"@babel/preset-env": "^7.9.5",
6363
"@babel/preset-flow": "^7.9.0",
64+
"@babel/register": "^7.11.5",
6465
"cross-env": "^7.0.2",
6566
"flow-bin": "^0.123.0",
6667
"flow-copy-source": "^2.0.9",

src/SourceMap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class SourceMap {
2121
* @param projectRoot root directory of the project, this is to ensure all source paths are relative to this path
2222
*/
2323
constructor(projectRoot: string = '/') {
24-
this.projectRoot = normalizePath(projectRoot);
24+
this.projectRoot = projectRoot;
2525
}
2626

2727
/**

src/utils.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,39 @@ import type { VLQMap, SourceMapStringifyOptions } from './types';
33

44
import path from 'path';
55

6+
// For some reason path.isAbsolute barely works... Regex to the rescue?
7+
// Apparently windows stuff is under `path.win32`, so yeah windows makes stuff complicated :)
8+
const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:){0,1}[\\/]+/;
9+
const PATH_SEPARATOR_REGEX = /\\/g;
10+
611
export function generateInlineMap(map: string): string {
712
return `data:application/json;charset=utf-8;base64,${Buffer.from(map).toString('base64')}`;
813
}
914

10-
export function normalizePath(filepath: string): string {
11-
filepath = filepath.replace(/\\/g, '/');
12-
13-
// Prefix relative paths with ./ as it makes it more clear and probably prevents issues
14-
if (filepath.length > 0 && filepath[0] !== '.' && !path.isAbsolute(filepath)) {
15-
filepath = `./${filepath}`;
16-
}
15+
export function isAbsolute(filepath: string): boolean {
16+
return ABSOLUTE_PATH_REGEX.test(filepath);
17+
}
1718

18-
return filepath;
19+
export function normalizePath(filepath: string): string {
20+
return filepath.replace(ABSOLUTE_PATH_REGEX, '/').replace(PATH_SEPARATOR_REGEX, '/');
1921
}
2022

2123
export function relatifyPath(filepath: string, rootDir: string): string {
22-
// Sourcemaps are made for web, so replace backslashes with regular slashes
24+
rootDir = normalizePath(rootDir);
2325
filepath = normalizePath(filepath);
2426

2527
// Make root paths relative to the rootDir
2628
if (filepath[0] === '/') {
27-
filepath = normalizePath(path.relative(rootDir, filepath));
29+
filepath = path.relative(rootDir, filepath);
2830
}
2931

30-
return filepath;
32+
// Prefix relative paths with ./ as it makes it more clear and probably prevents issues
33+
if (filepath[0] !== '.') {
34+
filepath = `./${filepath}`;
35+
}
36+
37+
// Sourcemaps are made for web, so replace backslashes with regular slashes
38+
return normalizePath(filepath);
3139
}
3240

3341
export async function partialVlqMapToSourceMap(

test/append.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const assert = require('assert');
2-
const SourceMap = require('.').default;
1+
import assert from 'assert';
2+
import SourceMap from '.';
33

44
const SIMPLE_SOURCE_MAP = {
55
version: 3,

test/basic.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const assert = require('assert');
2-
const SourceMap = require('.').default;
1+
import assert from 'assert';
2+
import SourceMap from '.';
33

44
const SIMPLE_SOURCE_MAP = {
55
version: 3,

test/empty-map.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const assert = require('assert');
2-
const { generateEmptyMap } = require('.').default;
1+
import assert from 'assert';
2+
import SourceMap from '.';
33

44
describe('SourceMap - Empty Map', () => {
55
it('Should be able to create a 1 to 1 map from a sourcefile', async () => {
6-
let map = generateEmptyMap({
6+
let map = SourceMap.generateEmptyMap({
77
projectRoot: '/test-root',
88
sourceName: '/test-root/index.js',
99
sourceContent: `
@@ -44,7 +44,7 @@ describe('SourceMap - Empty Map', () => {
4444
});
4545

4646
it('Should be able to create a 1 to 1 map from a sourcefile with a lineOffset', async () => {
47-
let map = generateEmptyMap({
47+
let map = SourceMap.generateEmptyMap({
4848
projectRoot: '/test-root',
4949
sourceName: 'index.js',
5050
sourceContent: `

test/extend.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const assert = require('assert');
2-
const SourceMap = require('.').default;
1+
import assert from 'assert';
2+
import SourceMap from '.';
33

44
describe('SourceMap - Extend Map', () => {
55
it('Basic extending', async function () {

test/find.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const assert = require('assert');
2-
const SourceMap = require('.').default;
1+
import assert from 'assert';
2+
import SourceMap from '.';
33

44
describe('SourceMap - Find', () => {
55
it('Should be able to find closest mapping to a generated position', async () => {

test/formats.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const assert = require('assert');
2-
const SourceMap = require('.').default;
1+
import assert from 'assert';
2+
import SourceMap from '.';
33

44
const SIMPLE_SOURCE_MAP = {
55
version: 3,
@@ -79,10 +79,10 @@ describe('SourceMap - Formats', () => {
7979
});
8080

8181
it('Should make all sourcePaths web friendly aka no windows backslashes', async () => {
82-
let map = new SourceMap('\\Users\\test\\');
82+
let map = new SourceMap('C:\\Users\\test\\');
8383
map.addRawMappings({
8484
mappings: SIMPLE_SOURCE_MAP.mappings,
85-
sources: ['\\Users\\test\\helloworld.coffee'],
85+
sources: ['C:\\Users\\test\\helloworld.coffee'],
8686
names: SIMPLE_SOURCE_MAP.names,
8787
});
8888

test/inline-source.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const assert = require('assert');
2-
const fs = require('fs-extra');
3-
const path = require('path');
4-
const SourceMap = require('.').default;
1+
import assert from 'assert';
2+
import fs from 'fs-extra';
3+
import path from 'path';
4+
import SourceMap from '.';
55

66
const SIMPLE_SOURCE_MAP = {
77
version: 3,

test/utils.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import assert from 'assert';
2+
import { normalizePath, relatifyPath } from '../src/utils';
3+
4+
describe('Utilities', () => {
5+
it('Relative path - WIN32', async () => {
6+
let result = relatifyPath('D:\\test\\sub-dir\\file.js', 'D:\\test\\sub-dir\\');
7+
assert.equal(result, './file.js');
8+
});
9+
10+
it('Relative path - POSIX', async () => {
11+
let result = relatifyPath('\\test\\sub-dir\\file.js', '\\test\\sub-dir\\');
12+
assert.equal(result, './file.js');
13+
});
14+
15+
it('Relative path - POSIX', async () => {
16+
let result = relatifyPath('/test/sub-dir/file.js', '/test/sub-dir/');
17+
assert.equal(result, './file.js');
18+
});
19+
20+
it('Normalize path - WIN32', async () => {
21+
let result = normalizePath('D:\\test\\sub-dir\\file.js');
22+
assert.equal(result, '/test/sub-dir/file.js');
23+
});
24+
25+
it('Normalize path - POSIX', async () => {
26+
let result = normalizePath('\\test\\sub-dir\\file.js');
27+
assert.equal(result, '/test/sub-dir/file.js');
28+
});
29+
30+
it('Normalize path - POSIX', async () => {
31+
let result = normalizePath('/test/sub-dir/file.js');
32+
assert.equal(result, '/test/sub-dir/file.js');
33+
});
34+
});

test/vlq.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const assert = require('assert');
2-
const SourceMap = require('.').default;
1+
import assert from 'assert';
2+
import SourceMap from '.';
33

44
const SIMPLE_SOURCE_MAP = {
55
version: 3,

0 commit comments

Comments
 (0)