Browse Source

BREAKING: API methods take a single argument, and external modules must be declared as such

contingency-plan
Rich Harris 10 years ago
parent
commit
cdeace10ca
  1. 5
      README.md
  2. 7
      bin/runRollup.js
  3. 2
      gobblefile.js
  4. 15
      src/Bundle.js
  5. 17
      src/rollup.js
  6. 5
      src/utils/ensureArray.js
  7. 6
      src/utils/load.js
  8. 19
      src/utils/resolvePath.js
  9. 5
      test/function/allows-external-modules-from-nested-module/_config.js
  10. 7
      test/function/import-default-from-external/_config.js
  11. 7
      test/function/import-named-from-external/_config.js
  12. 5
      test/function/import-namespace-from-external-module-renamed/_config.js
  13. 7
      test/function/import-namespace-from-external-module/_config.js
  14. 5
      test/function/imports-are-deconflicted-b/_config.js
  15. 5
      test/function/imports-are-deconflicted/_config.js
  16. 5
      test/function/shadowed-external-export/_config.js
  17. 18
      test/test.js

5
README.md

@ -91,7 +91,10 @@ This is not a trivial task. There are almost certainly a great many complex edge
The example below is aspirational. It isn't yet implemented - it exists in the name of [README driven development](http://tom.preston-werner.com/2010/08/23/readme-driven-development.html).
```js
rollup.rollup( 'app.js', {
rollup.rollup({
// The bundle's starting point
entry: 'app.js',
// Override the default path resolution
resolvePath: function ( importee, importer ) {
// return a string or a falsy value - if falsy,

7
bin/runRollup.js

@ -33,13 +33,16 @@ function bundle ( options, method ) {
handleError({ code: 'MISSING_INPUT_OPTION' });
}
return rollup.rollup( options.input ).then( function ( bundle ) {
return rollup.rollup({
entry: options.input
}).then( function ( bundle ) {
var generateOptions = {
dest: options.output,
format: options.format
};
if ( options.output ) {
return bundle.write( options.output, generateOptions );
return bundle.write( generateOptions );
}
if ( options.sourcemap && options.sourcemap !== 'inline' ) {

2
gobblefile.js

@ -1,6 +1,6 @@
var gobble = require( 'gobble' );
var selfhost = 1;
var selfhost = 0;
module.exports = selfhost ?
gobble( 'src' )

15
src/Bundle.js

@ -6,7 +6,9 @@ import Module from './Module';
import ExternalModule from './ExternalModule';
import finalisers from './finalisers/index';
import makeLegalIdentifier from './utils/makeLegalIdentifier';
import ensureArray from './utils/ensureArray';
import { defaultResolver } from './utils/resolvePath';
import { defaultLoader } from './utils/load';
function badExports ( option, keys ) {
throw new Error( `'${option}' was specified for options.exports, but entry module has following exports: ${keys.join(', ')}` );
@ -18,6 +20,15 @@ export default class Bundle {
this.base = dirname( this.entryPath );
this.resolvePath = options.resolvePath || defaultResolver;
this.load = options.load || defaultLoader;
this.resolvePathOptions = {
external: ensureArray( options.external )
};
this.loadOptions = {
transform: ensureArray( options.transform )
};
this.entryModule = null;
this.modulePromises = {};
@ -28,7 +39,7 @@ export default class Bundle {
}
fetchModule ( importee, importer ) {
return Promise.resolve( importer === null ? importee : this.resolvePath( importee, importer ) )
return Promise.resolve( importer === null ? importee : this.resolvePath( importee, importer, this.resolvePathOptions ) )
.then( path => {
if ( !path ) {
// external module
@ -42,7 +53,7 @@ export default class Bundle {
}
if ( !has( this.modulePromises, path ) ) {
this.modulePromises[ path ] = readFile( path, { encoding: 'utf-8' })
this.modulePromises[ path ] = Promise.resolve( this.load( path, this.loadOptions ) )
.then( source => {
const module = new Module({
path,

17
src/rollup.js

@ -5,16 +5,21 @@ import Bundle from './Bundle';
let SOURCEMAPPING_URL = 'sourceMa';
SOURCEMAPPING_URL += 'ppingURL';
export function rollup ( entry, options = {} ) {
const bundle = new Bundle({
entry,
resolvePath: options.resolvePath
});
export function rollup ( options ) {
if ( !options || !options.entry ) {
throw new Error( 'You must supply options.entry to rollup' );
}
const bundle = new Bundle( options );
return bundle.build().then( () => {
return {
generate: options => bundle.generate( options ),
write: ( dest, options = {} ) => {
write: options => {
if ( !options || !options.dest ) {
throw new Error( 'You must supply options.dest to bundle.write' );
}
let { code, map } = bundle.generate({
dest,
format: options.format,

5
src/utils/ensureArray.js

@ -0,0 +1,5 @@
export default function ensureArray ( thing ) {
if ( Array.isArray( thing ) ) return thing;
if ( thing == undefined ) return [];
return [ thing ];
}

6
src/utils/load.js

@ -0,0 +1,6 @@
import { readFileSync } from 'sander';
export function defaultLoader ( path, options ) {
// TODO support plugins and transformers?
return readFileSync( path, { encoding: 'utf-8' });
}

19
src/utils/resolvePath.js

@ -1,11 +1,22 @@
import { dirname, isAbsolute, resolve } from 'path';
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 resolveExternal( importee, importer, options );
}
return resolve( dirname( importer ), importee ).replace( /\.js$/, '' ) + '.js';
}
}
function resolveExternal ( id, importer, options ) {
// for now, only node_modules is supported, and only jsnext:main
throw new Error( "TODO" );
}

5
test/function/allows-external-modules-from-nested-module/_config.js

@ -1,3 +1,6 @@
module.exports = {
description: 'imports external modules from nested internal modules'
description: 'imports external modules from nested internal modules',
options: {
external: [ 'path' ]
}
};

7
test/function/import-default-from-external/_config.js

@ -1,3 +1,6 @@
module.exports = {
description: 'imports default from external module'
};
description: 'imports default from external module',
options: {
external: [ 'path' ]
}
};

7
test/function/import-named-from-external/_config.js

@ -1,3 +1,6 @@
module.exports = {
description: 'imports names from an external module'
};
description: 'imports names from an external module',
options: {
external: [ 'path' ]
}
};

5
test/function/import-namespace-from-external-module-renamed/_config.js

@ -1,3 +1,6 @@
module.exports = {
description: 'imports a namespace from an external module and renames it'
description: 'imports a namespace from an external module and renames it',
options: {
external: [ 'path' ]
}
};

7
test/function/import-namespace-from-external-module/_config.js

@ -1,3 +1,6 @@
module.exports = {
description: 'imports a namespace from an external module'
};
description: 'imports a namespace from an external module',
options: {
external: [ 'path' ]
}
};

5
test/function/imports-are-deconflicted-b/_config.js

@ -1,3 +1,6 @@
module.exports = {
description: 'deconflicts imports (redux)'
description: 'deconflicts imports (redux)',
options: {
external: [ 'path' ]
}
};

5
test/function/imports-are-deconflicted/_config.js

@ -1,3 +1,6 @@
module.exports = {
description: 'deconflicts imports'
description: 'deconflicts imports',
options: {
external: [ 'path' ]
}
};

5
test/function/shadowed-external-export/_config.js

@ -1,3 +1,6 @@
module.exports = {
description: 'external modules are not shadowed'
description: 'external modules are not shadowed',
options: {
external: [ 'path' ]
}
};

18
test/test.js

@ -54,7 +54,11 @@ describe( 'rollup', function () {
}
( config.skip ? it.skip : config.solo ? it.only : it )( dir, function () {
return rollup.rollup( FUNCTION + '/' + dir + '/main.js', extend( {}, config.options ) )
var options = extend( {}, config.options, {
entry: FUNCTION + '/' + dir + '/main.js'
})
return rollup.rollup( options )
.then( function ( bundle ) {
var unintendedError;
@ -142,7 +146,11 @@ describe( 'rollup', function () {
config = { description: dir };
}
var bundlePromise = rollup.rollup( FORM + '/' + dir + '/main.js', extend( {}, config.options ) );
var options = extend( {}, config.options, {
entry: FORM + '/' + dir + '/main.js'
});
var bundlePromise = rollup.rollup( options );
PROFILES.forEach( function ( profile ) {
( config.skip ? it.skip : config.solo ? it.only : it )( 'generates ' + profile.format, function () {
@ -172,7 +180,11 @@ describe( 'rollup', function () {
describe( dir, function () {
var config = require( SOURCEMAPS + '/' + dir + '/_config' );
var bundlePromise = rollup.rollup( SOURCEMAPS + '/' + dir + '/main.js', extend( {}, config.options ) );
var options = extend( {}, config.options, {
entry: SOURCEMAPS + '/' + dir + '/main.js'
});
var bundlePromise = rollup.rollup( options );
PROFILES.forEach( function ( profile ) {
( config.skip ? it.skip : config.solo ? it.only : it )( 'generates ' + profile.format, function () {

Loading…
Cancel
Save