Browse Source

keep non .js extensions, e.g. .json

better-aggressive
Rich-Harris 9 years ago
parent
commit
1ef3060e3a
  1. 13
      src/utils/defaults.js
  2. 2
      src/utils/path.js
  3. 14
      test/function/non-js-extensions/_config.js
  4. 3
      test/function/non-js-extensions/info.json
  5. 3
      test/function/non-js-extensions/main.js

13
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 ) {

2
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];
}

14
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;
}
}
}]
}
};

3
test/function/non-js-extensions/info.json

@ -0,0 +1,3 @@
{
"answer": 42
}

3
test/function/non-js-extensions/main.js

@ -0,0 +1,3 @@
import info from './info.json';
assert.equal( info.answer, 42 );
Loading…
Cancel
Save