mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
10 years ago
16 changed files with 228 additions and 59 deletions
@ -1,7 +1,26 @@ |
|||
import { keys } from '../utils/object'; |
|||
|
|||
export default function es6 ( bundle, magicString, exportMode, options ) { |
|||
// TODO
|
|||
const introBlock = ''; |
|||
const exportBlock = ''; |
|||
const introBlock = ''; // TODO...
|
|||
|
|||
const exports = bundle.entryModule.exports; |
|||
const exportBlock = keys( exports ).map( exportedName => { |
|||
const specifier = exports[ exportedName ]; |
|||
|
|||
const canonicalName = bundle.entryModule.getCanonicalName( specifier.localName ); |
|||
|
|||
if ( exportedName === 'default' ) { |
|||
return `export default ${canonicalName};`; |
|||
} |
|||
|
|||
return exportedName === canonicalName ? |
|||
`export { ${exportedName} };` : |
|||
`export { ${canonicalName} as ${exportedName} };`; |
|||
}).join( '\n' ); |
|||
|
|||
if ( exportBlock ) { |
|||
magicString.append( '\n\n' + exportBlock ); |
|||
} |
|||
|
|||
return magicString.trim(); |
|||
} |
|||
|
@ -0,0 +1,5 @@ |
|||
export default function ensureArray ( thing ) { |
|||
if ( Array.isArray( thing ) ) return thing; |
|||
if ( thing == undefined ) return []; |
|||
return [ thing ]; |
|||
} |
@ -0,0 +1,10 @@ |
|||
import { readFileSync } from 'sander'; |
|||
|
|||
export function defaultLoader ( path, options ) { |
|||
// TODO support plugins e.g. !css and !json?
|
|||
const source = readFileSync( path, { encoding: 'utf-8' }); |
|||
|
|||
return options.transform.reduce( ( source, transformer ) => { |
|||
return transformer( source, path ); |
|||
}, source ); |
|||
} |
@ -1,11 +1,56 @@ |
|||
import { dirname, isAbsolute, resolve } from 'path'; |
|||
import { dirname, isAbsolute, resolve, parse } from 'path'; |
|||
import { readFileSync } from 'sander'; |
|||
|
|||
export function defaultResolver ( importee, importer ) { |
|||
export function defaultResolver ( importee, importer, options ) { |
|||
// absolute paths are left untouched
|
|||
if ( isAbsolute( importee ) ) return importee; |
|||
|
|||
// external modules stay external
|
|||
if ( importee[0] !== '.' ) return false; |
|||
// we try to resolve external modules
|
|||
if ( importee[0] !== '.' ) { |
|||
// unless we want to keep it external, that is
|
|||
if ( ~options.external.indexOf( importee ) ) return null; |
|||
|
|||
return options.resolveExternal( importee, importer, options ); |
|||
} |
|||
|
|||
return resolve( dirname( importer ), importee ).replace( /\.js$/, '' ) + '.js'; |
|||
} |
|||
|
|||
export function defaultExternalResolver ( id, importer, options ) { |
|||
// for now, only node_modules is supported, and only jsnext:main
|
|||
let parsed = parse( importer ); |
|||
let dir = parsed.dir; |
|||
|
|||
while ( dir !== parsed.root ) { |
|||
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})` ); |
|||
} |
|||
|
Loading…
Reference in new issue