From 2ccb1b809d4460134b8df045a96f6480fb38df9f Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 31 Oct 2015 12:15:06 -0400 Subject: [PATCH] handle filenames with .foo, where foo is not an extension --- src/utils/defaults.js | 18 +++++++++++------- src/utils/fs.js | 9 +++++++++ test/function/non-extension-dot/_config.js | 3 +++ test/function/non-extension-dot/foo.bar.js | 1 + test/function/non-extension-dot/main.js | 2 ++ 5 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 test/function/non-extension-dot/_config.js create mode 100644 test/function/non-extension-dot/foo.bar.js create mode 100644 test/function/non-extension-dot/main.js diff --git a/src/utils/defaults.js b/src/utils/defaults.js index 2cb6cd2..fef4f8a 100644 --- a/src/utils/defaults.js +++ b/src/utils/defaults.js @@ -1,26 +1,30 @@ -import { readFileSync } from './fs.js'; +import { isFile, readFileSync } from './fs.js'; import { dirname, extname, isAbsolute, resolve } from './path.js'; export function load ( id ) { return readFileSync( id, 'utf-8' ); } -function addExt ( id ) { - if ( !extname( id ) ) id += '.js'; - return id; +function addJsExtensionIfNecessary ( file ) { + if ( isFile( file ) ) return file; + + file += '.js'; + if ( isFile( file ) ) return file; + + return null; } export function resolveId ( importee, importer ) { // absolute paths are left untouched - if ( isAbsolute( importee ) ) return addExt( importee ); + if ( isAbsolute( importee ) ) return addJsExtensionIfNecessary( importee ); // if this is the entry point, resolve against cwd - if ( importer === undefined ) return resolve( process.cwd(), addExt( importee ) ); + if ( importer === undefined ) return addJsExtensionIfNecessary( resolve( process.cwd(), importee ) ); // external modules are skipped at this stage if ( importee[0] !== '.' ) return null; - return resolve( dirname( importer ), addExt( importee ) ); + return addJsExtensionIfNecessary( resolve( dirname( importer ), importee ) ); } export function onwarn ( msg ) { diff --git a/src/utils/fs.js b/src/utils/fs.js index 7a24c24..d153099 100644 --- a/src/utils/fs.js +++ b/src/utils/fs.js @@ -12,6 +12,15 @@ 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 ); diff --git a/test/function/non-extension-dot/_config.js b/test/function/non-extension-dot/_config.js new file mode 100644 index 0000000..3f29d79 --- /dev/null +++ b/test/function/non-extension-dot/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'adds .js to module paths with non-extension dots in them' +}; diff --git a/test/function/non-extension-dot/foo.bar.js b/test/function/non-extension-dot/foo.bar.js new file mode 100644 index 0000000..c20d0fa --- /dev/null +++ b/test/function/non-extension-dot/foo.bar.js @@ -0,0 +1 @@ +export default 'fubar'; diff --git a/test/function/non-extension-dot/main.js b/test/function/non-extension-dot/main.js new file mode 100644 index 0000000..eaf0ac2 --- /dev/null +++ b/test/function/non-extension-dot/main.js @@ -0,0 +1,2 @@ +import status from './foo.bar'; +assert.equal( status, 'fubar' );