Skip to content

Commit

Permalink
Merge pull request #450 from rollup/gh-447
Browse files Browse the repository at this point in the history
follow symlinks
  • Loading branch information
Rich-Harris committed Dec 20, 2016
2 parents 3f39de8 + 490d428 commit a804033
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 22 deletions.
3 changes: 2 additions & 1 deletion browser/fs.js
@@ -1,6 +1,7 @@
const nope = method => `Cannot use fs.${method} inside browser`;

export const isFile = () => false;
export const lstatSync = nope( 'lstatSync' );
export const readdirSync = nope( 'readdirSync' );
export const readFileSync = nope( 'readFileSync' );
export const realpathSync = nope( 'realpathSync' );
export const writeFile = nope( 'writeFile' );
24 changes: 15 additions & 9 deletions src/utils/defaults.js
@@ -1,23 +1,29 @@
import { isFile, readdirSync, readFileSync } from './fs.js';
import { lstatSync, readdirSync, readFileSync, realpathSync } from './fs.js'; // eslint-disable-line
import { basename, dirname, isAbsolute, resolve } from './path.js';
import { blank } from './object.js';

export function load ( id ) {
return readFileSync( id, 'utf-8' );
}

function addJsExtensionIfNecessary ( file ) {
function findFile ( file ) {
try {
const name = basename( file );
const files = readdirSync( dirname( file ) );

if ( ~files.indexOf( name ) && isFile( file ) ) return file;
if ( ~files.indexOf( `${name}.js` ) && isFile( `${file}.js` ) ) return `${file}.js`;
const stats = lstatSync( file );
if ( stats.isSymbolicLink() ) return findFile( realpathSync( file ) );
if ( stats.isFile() ) {
// check case
const name = basename( file );
const files = readdirSync( dirname( file ) );

if ( ~files.indexOf( name ) ) return file;
}
} catch ( err ) {
// noop
// suppress
}
}

return null;
function addJsExtensionIfNecessary ( file ) {
return findFile( file ) || findFile( file + '.js' );
}

export function resolveId ( importee, importer ) {
Expand Down
14 changes: 2 additions & 12 deletions src/utils/fs.js
@@ -1,6 +1,8 @@
import * as fs from 'fs';
import { dirname } from './path.js';

export * from 'fs';

function mkdirpath ( path ) {
const dir = dirname( path );
try {
Expand All @@ -11,15 +13,6 @@ function mkdirpath ( path ) {
}
}

export function isFile ( file ) {
try {
const stats = fs.statSync( file );
return stats.isFile();
} catch ( err ) {
return false;
}
}

export function writeFile ( dest, data ) {
return new Promise( ( fulfil, reject ) => {
mkdirpath( dest );
Expand All @@ -33,6 +26,3 @@ export function writeFile ( dest, data ) {
});
});
}

export const readdirSync = fs.readdirSync;
export const readFileSync = fs.readFileSync;
4 changes: 4 additions & 0 deletions test/function/symlink/_config.js
@@ -0,0 +1,4 @@
module.exports = {
skip: process.platform === 'win32',
description: 'follows symlinks'
};
1 change: 1 addition & 0 deletions test/function/symlink/foo.js
2 changes: 2 additions & 0 deletions test/function/symlink/main.js
@@ -0,0 +1,2 @@
import foo from './foo.js';
assert.equal( foo, 'BAZ' );
2 changes: 2 additions & 0 deletions test/function/symlink/symlinked/bar.js
@@ -0,0 +1,2 @@
import baz from './baz.js';
export default baz();
3 changes: 3 additions & 0 deletions test/function/symlink/symlinked/baz.js
@@ -0,0 +1,3 @@
export default function () {
return 'BAZ';
}

0 comments on commit a804033

Please sign in to comment.