Browse Source

handle filenames with .foo, where foo is not an extension

better-aggressive
Rich-Harris 9 years ago
parent
commit
2ccb1b809d
  1. 18
      src/utils/defaults.js
  2. 9
      src/utils/fs.js
  3. 3
      test/function/non-extension-dot/_config.js
  4. 1
      test/function/non-extension-dot/foo.bar.js
  5. 2
      test/function/non-extension-dot/main.js

18
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'; import { dirname, extname, isAbsolute, resolve } from './path.js';
export function load ( id ) { export function load ( id ) {
return readFileSync( id, 'utf-8' ); return readFileSync( id, 'utf-8' );
} }
function addExt ( id ) { function addJsExtensionIfNecessary ( file ) {
if ( !extname( id ) ) id += '.js'; if ( isFile( file ) ) return file;
return id;
file += '.js';
if ( isFile( file ) ) return file;
return null;
} }
export function resolveId ( importee, importer ) { export function resolveId ( importee, importer ) {
// absolute paths are left untouched // 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 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 // external modules are skipped at this stage
if ( importee[0] !== '.' ) return null; if ( importee[0] !== '.' ) return null;
return resolve( dirname( importer ), addExt( importee ) ); return addJsExtensionIfNecessary( resolve( dirname( importer ), importee ) );
} }
export function onwarn ( msg ) { export function onwarn ( msg ) {

9
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 ) { export function writeFile ( dest, data ) {
return new Promise( ( fulfil, reject ) => { return new Promise( ( fulfil, reject ) => {
mkdirpath( dest ); mkdirpath( dest );

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

1
test/function/non-extension-dot/foo.bar.js

@ -0,0 +1 @@
export default 'fubar';

2
test/function/non-extension-dot/main.js

@ -0,0 +1,2 @@
import status from './foo.bar';
assert.equal( status, 'fubar' );
Loading…
Cancel
Save