From 1ef3060e3a1dcc890cece0585a3ca67e91298cdc Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 24 Oct 2015 12:47:44 -0400 Subject: [PATCH] keep non .js extensions, e.g. .json --- src/utils/defaults.js | 13 +++++++++---- src/utils/path.js | 2 +- test/function/non-js-extensions/_config.js | 14 ++++++++++++++ test/function/non-js-extensions/info.json | 3 +++ test/function/non-js-extensions/main.js | 3 +++ 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 test/function/non-js-extensions/_config.js create mode 100644 test/function/non-js-extensions/info.json create mode 100644 test/function/non-js-extensions/main.js diff --git a/src/utils/defaults.js b/src/utils/defaults.js index 07bd31b..690ebfb 100644 --- a/src/utils/defaults.js +++ b/src/utils/defaults.js @@ -1,21 +1,26 @@ import { readFileSync } from './fs'; -import { dirname, isAbsolute, resolve } from './path'; +import { dirname, extname, isAbsolute, resolve } from './path'; export function load ( id ) { return readFileSync( id, 'utf-8' ); } +function addExt ( id ) { + if ( !extname( id ) ) id += '.js'; + return id; +} + export function resolveId ( importee, importer ) { // absolute paths are left untouched - if ( isAbsolute( importee ) ) return importee; + if ( isAbsolute( importee ) ) return addExt( importee ); // if this is the entry point, resolve against cwd - if ( importer === undefined ) return resolve( process.cwd(), importee ); + if ( importer === undefined ) return resolve( process.cwd(), addExt( importee ) ); // external modules are skipped at this stage if ( importee[0] !== '.' ) return null; - return resolve( dirname( importer ), importee ).replace( /\.js$/, '' ) + '.js'; + return resolve( dirname( importer ), addExt( importee ) ); } export function onwarn ( msg ) { diff --git a/src/utils/path.js b/src/utils/path.js index 269702b..5ef7f49 100644 --- a/src/utils/path.js +++ b/src/utils/path.js @@ -21,7 +21,7 @@ export function dirname ( path ) { } export function extname ( path ) { - const match = /\.[^\.]+$/.exec( path ); + const match = /\.[^\.]+$/.exec( basename( path ) ); if ( !match ) return ''; return match[0]; } diff --git a/test/function/non-js-extensions/_config.js b/test/function/non-js-extensions/_config.js new file mode 100644 index 0000000..26ccf26 --- /dev/null +++ b/test/function/non-js-extensions/_config.js @@ -0,0 +1,14 @@ +var path = require( 'path' ); + +module.exports = { + description: 'non .js extensions are preserved', + options: { + plugins: [{ + transform: function ( code, id ) { + if ( path.extname( id ) === '.json' ) { + return 'export default ' + code; + } + } + }] + } +}; diff --git a/test/function/non-js-extensions/info.json b/test/function/non-js-extensions/info.json new file mode 100644 index 0000000..5d18e67 --- /dev/null +++ b/test/function/non-js-extensions/info.json @@ -0,0 +1,3 @@ +{ + "answer": 42 +} diff --git a/test/function/non-js-extensions/main.js b/test/function/non-js-extensions/main.js new file mode 100644 index 0000000..790924c --- /dev/null +++ b/test/function/non-js-extensions/main.js @@ -0,0 +1,3 @@ +import info from './info.json'; + +assert.equal( info.answer, 42 );