Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from zacharygolba/fix-windows-file-paths
Browse files Browse the repository at this point in the history
fix: isFilePath not working on windows
  • Loading branch information
Rich-Harris authored Apr 10, 2017
2 parents 24a377d + 91e2581 commit 5c6144b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
"nyc": "^5.6.0",
"rimraf": "^2.5.2",
"rollup": "^0.25.4",
"rollup-babel-lib-bundler": "^2.2.4"
}
"rollup-babel-lib-bundler": "^2.2.4",
"slash": "^1.0.0"

This comment has been minimized.

Copy link
@lukeed

lukeed Apr 13, 2017

Contributor

This needs to be a dependency.

},
"dependencies": {}
}
25 changes: 21 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import path from 'path';
import { posix as path } from 'path';
import { platform } from 'os';
import fs from 'fs';

import slash from 'slash';

This comment has been minimized.

Copy link
@lukeed

lukeed Apr 13, 2017

Contributor

Because imported here.


const VOLUME = /^([A-Z]:)/;
const IS_WINDOWS = platform() === 'win32';

// Helper functions
const noop = () => null;
const matches = (key, importee) => {
Expand All @@ -24,6 +30,14 @@ const exists = uri => {
}
};

const normalizeId = id => {
if (IS_WINDOWS && typeof id === 'string') {
return slash(id.replace(VOLUME, ''));
}

return id;
};

export default function alias(options = {}) {
const hasResolve = Array.isArray(options.resolve);
const resolve = hasResolve ? options.resolve : ['.js'];
Expand All @@ -39,19 +53,22 @@ export default function alias(options = {}) {

return {
resolveId(importee, importer) {
const importeeId = normalizeId(importee);
const importerId = normalizeId(importer);

// First match is supposed to be the correct one
const toReplace = aliasKeys.find(key => matches(key, importee));
const toReplace = aliasKeys.find(key => matches(key, importeeId));

if (!toReplace) {
return null;
}

const entry = options[toReplace];

const updatedId = importee.replace(toReplace, entry);
const updatedId = importeeId.replace(toReplace, entry);

if (isFilePath(updatedId)) {
const directory = path.dirname(importer);
const directory = path.dirname(importerId);

// Resolve file names
const filePath = path.resolve(directory, updatedId);
Expand Down
13 changes: 8 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import test from 'ava';
import path from 'path';
import { posix as path } from 'path';
import { rollup } from 'rollup';
import alias from '../dist/rollup-plugin-alias';
import slash from 'slash';

const DIRNAME = slash(__dirname.replace(/^([A-Z]:)/, ''));

test(t => {
t.is(typeof alias, 'function');
Expand Down Expand Up @@ -90,9 +93,9 @@ test('Test for the resolve property', t => {
resolve: ['.js', '.jsx'],
});

const resolved = result.resolveId('ember', path.resolve(__dirname, './files/index.js'));
const resolved = result.resolveId('ember', path.resolve(DIRNAME, './files/index.js'));

t.is(resolved, path.resolve(__dirname, './files/folder/hipster.jsx'));
t.is(resolved, path.resolve(DIRNAME, './files/folder/hipster.jsx'));
});

test(t => {
Expand All @@ -110,9 +113,9 @@ test(t => {
resolve: './i/am/a/local/file',
});

const resolved = result.resolveId('resolve', path.resolve(__dirname, './files/index.js'));
const resolved = result.resolveId('resolve', path.resolve(DIRNAME, './files/index.js'));

t.is(resolved, path.resolve(__dirname, './files/i/am/a/local/file.js'));
t.is(resolved, path.resolve(DIRNAME, './files/i/am/a/local/file.js'));
});

// Tests in Rollup
Expand Down
13 changes: 1 addition & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1466,10 +1466,6 @@ esprima@2.7.x, esprima@^2.6.0, esprima@^2.7.1:
version "2.7.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"

esprima@^3.1.1:
version "3.1.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"

espurify@^1.6.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/espurify/-/espurify-1.7.0.tgz#1c5cf6cbccc32e6f639380bd4f991fab9ba9d226"
Expand Down Expand Up @@ -2184,20 +2180,13 @@ js-tokens@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"

js-yaml@3.6.1:
js-yaml@3.6.1, js-yaml@3.x, js-yaml@^3.5.1:
version "3.6.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30"
dependencies:
argparse "^1.0.7"
esprima "^2.6.0"

js-yaml@3.x, js-yaml@^3.5.1:
version "3.8.2"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.2.tgz#02d3e2c0f6beab20248d412c352203827d786721"
dependencies:
argparse "^1.0.7"
esprima "^3.1.1"

jsbn@~0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
Expand Down

0 comments on commit 5c6144b

Please sign in to comment.