@ -1,4 +1,5 @@
import { dirname , isAbsolute , resolve } from 'path' ;
import { dirname , isAbsolute , resolve } from 'path' ;
import { readFileSync } from 'sander' ;
export function defaultResolver ( importee , importer , options ) {
export function defaultResolver ( importee , importer , options ) {
// absolute paths are left untouched
// absolute paths are left untouched
@ -9,14 +10,46 @@ export function defaultResolver ( importee, importer, options ) {
// unless we want to keep it external, that is
// unless we want to keep it external, that is
if ( ~ options . external . indexOf ( importee ) ) return null ;
if ( ~ options . external . indexOf ( importee ) ) return null ;
return resolveExternal ( importee , importer , options ) ;
return options . resolveExternal ( importee , importer , options ) ;
}
}
return resolve ( dirname ( importer ) , importee ) . replace ( /\.js$/ , '' ) + '.js' ;
return resolve ( dirname ( importer ) , importee ) . replace ( /\.js$/ , '' ) + '.js' ;
}
}
function resolveExternal ( id , importer , options ) {
export function defaultExternalResolver ( id , importer , options ) {
// for now, only node_modules is supported, and only jsnext:main
// for now, only node_modules is supported, and only jsnext:main
let dir = dirname ( importer ) ;
throw new Error ( "TODO" ) ;
while ( dir !== '/' ) {
const pkgPath = resolve ( dir , 'node_modules' , id , 'package.json' ) ;
let pkgJson ;
try {
pkgJson = readFileSync ( pkgPath ) . toString ( ) ;
} catch ( err ) {
// noop
}
if ( pkgJson ) {
let pkg ;
try {
pkg = JSON . parse ( pkgJson ) ;
} catch ( err ) {
throw new Error ( ` Malformed JSON: ${ pkgPath } ` ) ;
}
const main = pkg [ 'jsnext:main' ] ;
if ( ! main ) {
throw new Error ( ` Package ${ id } does not have a jsnext:main field, and so cannot be included in your rollup. Try adding it as an external module instead (e.g. options.external = [' ${ id } ']). See https://github.com/rollup/rollup/wiki/jsnext:main for more info ` ) ;
}
return resolve ( dirname ( pkgPath ) , main ) . replace ( /\.js$/ , '' ) + '.js' ;
}
dir = dirname ( dir ) ;
}
throw new Error ( ` Could not find package ${ id } (required by ${ importer } ) ` ) ;
}
}