diff --git a/browser/fs.js b/browser/fs.js index be97954..dbb1f11 100644 --- a/browser/fs.js +++ b/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' ); diff --git a/src/utils/defaults.js b/src/utils/defaults.js index c746a7f..8db3c0b 100644 --- a/src/utils/defaults.js +++ b/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 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 ) { diff --git a/src/utils/fs.js b/src/utils/fs.js index 8a0e3e7..db790a9 100644 --- a/src/utils/fs.js +++ b/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; diff --git a/test/function/symlink/_config.js b/test/function/symlink/_config.js new file mode 100644 index 0000000..561b738 --- /dev/null +++ b/test/function/symlink/_config.js @@ -0,0 +1,4 @@ +module.exports = { + skip: process.platform === 'win32', + description: 'follows symlinks' +}; diff --git a/test/function/symlink/foo.js b/test/function/symlink/foo.js new file mode 120000 index 0000000..a4af385 --- /dev/null +++ b/test/function/symlink/foo.js @@ -0,0 +1 @@ +symlinked/bar.js \ No newline at end of file diff --git a/test/function/symlink/main.js b/test/function/symlink/main.js new file mode 100644 index 0000000..292b3b5 --- /dev/null +++ b/test/function/symlink/main.js @@ -0,0 +1,2 @@ +import foo from './foo.js'; +assert.equal( foo, 'BAZ' ); diff --git a/test/function/symlink/symlinked/bar.js b/test/function/symlink/symlinked/bar.js new file mode 100644 index 0000000..5d6afaa --- /dev/null +++ b/test/function/symlink/symlinked/bar.js @@ -0,0 +1,2 @@ +import baz from './baz.js'; +export default baz(); diff --git a/test/function/symlink/symlinked/baz.js b/test/function/symlink/symlinked/baz.js new file mode 100644 index 0000000..d08083e --- /dev/null +++ b/test/function/symlink/symlinked/baz.js @@ -0,0 +1,3 @@ +export default function () { + return 'BAZ'; +}