Browse Source

Merge pull request #450 from rollup/gh-447

follow symlinks
gh-1132
Rich Harris 8 years ago
committed by GitHub
parent
commit
a804033655
  1. 3
      browser/fs.js
  2. 18
      src/utils/defaults.js
  3. 14
      src/utils/fs.js
  4. 4
      test/function/symlink/_config.js
  5. 1
      test/function/symlink/foo.js
  6. 2
      test/function/symlink/main.js
  7. 2
      test/function/symlink/symlinked/bar.js
  8. 3
      test/function/symlink/symlinked/baz.js

3
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' );

18
src/utils/defaults.js

@ -1,4 +1,4 @@
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';
@ -6,18 +6,24 @@ export function load ( id ) {
return readFileSync( id, 'utf-8' );
}
function addJsExtensionIfNecessary ( file ) {
function findFile ( file ) {
try {
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 ) && isFile( file ) ) return file;
if ( ~files.indexOf( `${name}.js` ) && isFile( `${file}.js` ) ) return `${file}.js`;
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 ) {

14
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 {
@ -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 );
@ -33,6 +26,3 @@ export function writeFile ( dest, data ) {
});
});
}
export const readdirSync = fs.readdirSync;
export const readFileSync = fs.readFileSync;

4
test/function/symlink/_config.js

@ -0,0 +1,4 @@
module.exports = {
skip: process.platform === 'win32',
description: 'follows symlinks'
};

1
test/function/symlink/foo.js

@ -0,0 +1 @@
symlinked/bar.js

2
test/function/symlink/main.js

@ -0,0 +1,2 @@
import foo from './foo.js';
assert.equal( foo, 'BAZ' );

2
test/function/symlink/symlinked/bar.js

@ -0,0 +1,2 @@
import baz from './baz.js';
export default baz();

3
test/function/symlink/symlinked/baz.js

@ -0,0 +1,3 @@
export default function () {
return 'BAZ';
}
Loading…
Cancel
Save