Skip to content

Commit

Permalink
Merge pull request #32 from parcel-bundler/windows
Browse files Browse the repository at this point in the history
Fix path handling on windows
  • Loading branch information
DeMoorJasper authored Sep 7, 2020
2 parents 5b5d5fe + db88b2e commit 5f29c9f
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"require": ["@babel/register"]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@parcel/source-map",
"version": "2.0.0-alpha.4.14",
"version": "2.0.0-alpha.4.15",
"main": "./dist/node.js",
"browser": "./dist/wasm-browser.js",
"license": "MIT",
Expand Down Expand Up @@ -61,6 +61,7 @@
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"@babel/preset-flow": "^7.9.0",
"@babel/register": "^7.11.5",
"cross-env": "^7.0.2",
"flow-bin": "^0.123.0",
"flow-copy-source": "^2.0.9",
Expand Down
2 changes: 1 addition & 1 deletion src/SourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class SourceMap {
* @param projectRoot root directory of the project, this is to ensure all source paths are relative to this path
*/
constructor(projectRoot: string = '/') {
this.projectRoot = normalizePath(projectRoot);
this.projectRoot = projectRoot;
}

/**
Expand Down
30 changes: 19 additions & 11 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,39 @@ import type { VLQMap, SourceMapStringifyOptions } from './types';

import path from 'path';

// For some reason path.isAbsolute barely works... Regex to the rescue?
// Apparently windows stuff is under `path.win32`, so yeah windows makes stuff complicated :)
const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:){0,1}[\\/]+/;
const PATH_SEPARATOR_REGEX = /\\/g;

export function generateInlineMap(map: string): string {
return `data:application/json;charset=utf-8;base64,${Buffer.from(map).toString('base64')}`;
}

export function normalizePath(filepath: string): string {
filepath = filepath.replace(/\\/g, '/');

// Prefix relative paths with ./ as it makes it more clear and probably prevents issues
if (filepath.length > 0 && filepath[0] !== '.' && !path.isAbsolute(filepath)) {
filepath = `./${filepath}`;
}
export function isAbsolute(filepath: string): boolean {
return ABSOLUTE_PATH_REGEX.test(filepath);
}

return filepath;
export function normalizePath(filepath: string): string {
return filepath.replace(ABSOLUTE_PATH_REGEX, '/').replace(PATH_SEPARATOR_REGEX, '/');
}

export function relatifyPath(filepath: string, rootDir: string): string {
// Sourcemaps are made for web, so replace backslashes with regular slashes
rootDir = normalizePath(rootDir);
filepath = normalizePath(filepath);

// Make root paths relative to the rootDir
if (filepath[0] === '/') {
filepath = normalizePath(path.relative(rootDir, filepath));
filepath = path.relative(rootDir, filepath);
}

return filepath;
// Prefix relative paths with ./ as it makes it more clear and probably prevents issues
if (filepath[0] !== '.') {
filepath = `./${filepath}`;
}

// Sourcemaps are made for web, so replace backslashes with regular slashes
return normalizePath(filepath);
}

export async function partialVlqMapToSourceMap(
Expand Down
4 changes: 2 additions & 2 deletions test/append.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const SourceMap = require('.').default;
import assert from 'assert';
import SourceMap from '.';

const SIMPLE_SOURCE_MAP = {
version: 3,
Expand Down
4 changes: 2 additions & 2 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const SourceMap = require('.').default;
import assert from 'assert';
import SourceMap from '.';

const SIMPLE_SOURCE_MAP = {
version: 3,
Expand Down
8 changes: 4 additions & 4 deletions test/empty-map.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const assert = require('assert');
const { generateEmptyMap } = require('.').default;
import assert from 'assert';
import SourceMap from '.';

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

it('Should be able to create a 1 to 1 map from a sourcefile with a lineOffset', async () => {
let map = generateEmptyMap({
let map = SourceMap.generateEmptyMap({
projectRoot: '/test-root',
sourceName: 'index.js',
sourceContent: `
Expand Down
4 changes: 2 additions & 2 deletions test/extend.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const SourceMap = require('.').default;
import assert from 'assert';
import SourceMap from '.';

describe('SourceMap - Extend Map', () => {
it('Basic extending', async function () {
Expand Down
4 changes: 2 additions & 2 deletions test/find.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const SourceMap = require('.').default;
import assert from 'assert';
import SourceMap from '.';

describe('SourceMap - Find', () => {
it('Should be able to find closest mapping to a generated position', async () => {
Expand Down
8 changes: 4 additions & 4 deletions test/formats.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const SourceMap = require('.').default;
import assert from 'assert';
import SourceMap from '.';

const SIMPLE_SOURCE_MAP = {
version: 3,
Expand Down Expand Up @@ -79,10 +79,10 @@ describe('SourceMap - Formats', () => {
});

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

Expand Down
8 changes: 4 additions & 4 deletions test/inline-source.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert');
const fs = require('fs-extra');
const path = require('path');
const SourceMap = require('.').default;
import assert from 'assert';
import fs from 'fs-extra';
import path from 'path';
import SourceMap from '.';

const SIMPLE_SOURCE_MAP = {
version: 3,
Expand Down
34 changes: 34 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from 'assert';
import { normalizePath, relatifyPath } from '../src/utils';

describe('Utilities', () => {
it('Relative path - WIN32', async () => {
let result = relatifyPath('D:\\test\\sub-dir\\file.js', 'D:\\test\\sub-dir\\');
assert.equal(result, './file.js');
});

it('Relative path - POSIX', async () => {
let result = relatifyPath('\\test\\sub-dir\\file.js', '\\test\\sub-dir\\');
assert.equal(result, './file.js');
});

it('Relative path - POSIX', async () => {
let result = relatifyPath('/test/sub-dir/file.js', '/test/sub-dir/');
assert.equal(result, './file.js');
});

it('Normalize path - WIN32', async () => {
let result = normalizePath('D:\\test\\sub-dir\\file.js');
assert.equal(result, '/test/sub-dir/file.js');
});

it('Normalize path - POSIX', async () => {
let result = normalizePath('\\test\\sub-dir\\file.js');
assert.equal(result, '/test/sub-dir/file.js');
});

it('Normalize path - POSIX', async () => {
let result = normalizePath('/test/sub-dir/file.js');
assert.equal(result, '/test/sub-dir/file.js');
});
});
4 changes: 2 additions & 2 deletions test/vlq.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const SourceMap = require('.').default;
import assert from 'assert';
import SourceMap from '.';

const SIMPLE_SOURCE_MAP = {
version: 3,
Expand Down

0 comments on commit 5f29c9f

Please sign in to comment.