Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #14 from rollup/gh-8
Browse files Browse the repository at this point in the history
support browser field
  • Loading branch information
Rich-Harris committed Dec 30, 2015
2 parents 45bb5e9 + 93df9c9 commit 4d5bf63
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 61 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"description": "Bundle third-party dependencies in node_modules",
"version": "1.1.0",
"devDependencies": {
"babel-preset-es2015": "^6.3.13",
"babel-preset-es2015-rollup": "^1.0.0",
"babel-register": "^6.3.13",
"es5-ext": "^0.10.11",
"eslint": "^1.7.3",
"mocha": "^2.3.3",
Expand All @@ -16,7 +18,7 @@
"main": "dist/rollup-plugin-npm.cjs.js",
"jsnext:main": "dist/rollup-plugin-npm.es6.js",
"scripts": {
"test": "mocha",
"test": "mocha --compilers js:babel-register",
"pretest": "npm run build",
"build": "rollup -c -f cjs -o dist/rollup-plugin-npm.cjs.js && rollup -c -f es6 -o dist/rollup-plugin-npm.es6.js",
"prepublish": "npm test"
Expand All @@ -27,6 +29,7 @@
"README.md"
],
"dependencies": {
"browser-resolve": "^1.11.0",
"builtin-modules": "^1.1.0",
"resolve": "^1.1.6"
},
Expand Down
1 change: 1 addition & 0 deletions src/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
31 changes: 23 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { dirname } from 'path';
import { dirname, resolve } from 'path';
import builtins from 'builtin-modules';
import resolve from 'resolve';
import nodeResolve from 'resolve';
import browserResolve from 'browser-resolve';

const COMMONJS_BROWSER_EMPTY = nodeResolve.sync( 'browser-resolve/empty.js', __dirname );
const ES6_BROWSER_EMPTY = resolve( __dirname, '../src/empty.js' );

export default function npm ( options ) {
options = options || {};

const skip = options.skip || [];
const useMain = options.main !== false;

const resolveId = options.browser ? browserResolve : nodeResolve;

return {
resolveId( importee, importer ) {
resolveId ( importee, importer ) {
const parts = importee.split( /[\/\\]/ );
const id = parts.shift();

if ( ~skip.indexOf(id) ) return null;

// disregard entry modules and builtins
if ( !importer || ~builtins.indexOf( importee ) ) return null;
// disregard entry module
if ( !importer ) return null;

return new Promise( ( accept, reject ) => {
resolve(
resolveId(
importee,
{
basedir: dirname( importer ),
packageFilter( pkg ) {
packageFilter ( pkg ) {
const id = pkg[ 'name' ];
if ( options.jsnext ) {
const main = pkg[ 'jsnext:main' ];
Expand All @@ -38,7 +44,16 @@ export default function npm ( options ) {
return pkg;
}
},
( err, resolved ) => err ? reject( err ) : accept( resolved )
( err, resolved ) => {
if ( err ) {
reject( err );
} else {
if ( resolved === COMMONJS_BROWSER_EMPTY ) resolved = ES6_BROWSER_EMPTY;
if ( ~builtins.indexOf( resolved ) ) resolved = null;

accept( resolved );
}
}
);
});
}
Expand Down
3 changes: 3 additions & 0 deletions test/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015-rollup" ]
}
1 change: 1 addition & 0 deletions test/node_modules/isomorphic-object/browser-dep.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/node_modules/isomorphic-object/browser-test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/node_modules/isomorphic-object/browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/node_modules/isomorphic-object/dep.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/node_modules/isomorphic-object/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/node_modules/isomorphic-object/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/node_modules/isomorphic/browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/node_modules/isomorphic/node.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/node_modules/isomorphic/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/node_modules/test/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/node_modules/uses-buffer/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/node_modules/uses-buffer/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/samples/browser-false/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import thing from 'uses-buffer';

assert.deepEqual( thing, {} );
2 changes: 2 additions & 0 deletions test/samples/browser-object/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { env, dep, test } from 'isomorphic-object';
export { env, dep, test };
3 changes: 3 additions & 0 deletions test/samples/browser/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import env from 'isomorphic';

export default env;
2 changes: 2 additions & 0 deletions test/samples/builtins/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { sep } from 'path';
export default sep;
159 changes: 107 additions & 52 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var path = require( 'path' );
var assert = require( 'assert' );
var rollup = require( 'rollup' );
var commonjs = require( 'rollup-plugin-commonjs' );
Expand All @@ -6,23 +7,27 @@ var npm = require( '..' );

process.chdir( __dirname );

function executeBundle ( bundle ) {
const generated = bundle.generate({
format: 'cjs'
});

const fn = new Function ( 'module', 'exports', 'assert', generated.code );
let module = { exports: {} };

fn( module, module.exports, assert );

return module;
}

describe( 'rollup-plugin-npm', function () {
it( 'finds a module with jsnext:main', function () {
return rollup.rollup({
entry: 'samples/jsnext/main.js',
plugins: [
npm({ jsnext: true })
]
}).then( function ( bundle ) {
var generated = bundle.generate({
format: 'cjs'
});

var fn = new Function ( 'module', generated.code );
var module = {};

fn( module );

}).then( executeBundle ).then( module => {
assert.equal( module.exports, '2H' );
});
});
Expand All @@ -34,18 +39,9 @@ describe( 'rollup-plugin-npm', function () {
npm({ main: true }),
commonjs()
]
}).then( function ( bundle ) {
var generated = bundle.generate({
format: 'cjs'
});

var fn = new Function ( 'module', generated.code );
var module = {};

fn( module );

}).then( executeBundle ).then( module => {
assert.equal( module.exports, 'It works!' );
})
});
});

it( 'handles a trailing slash', function () {
Expand All @@ -55,18 +51,9 @@ describe( 'rollup-plugin-npm', function () {
npm({ main: true }),
commonjs()
]
}).then( function ( bundle ) {
var generated = bundle.generate({
format: 'cjs'
});

var fn = new Function ( 'module', generated.code );
var module = {};

fn( module );

}).then( executeBundle ).then( module => {
assert.equal( module.exports, 'It works!' );
})
});
});

it( 'finds a file inside a package directory', function () {
Expand All @@ -76,16 +63,7 @@ describe( 'rollup-plugin-npm', function () {
npm(),
babel()
]
}).then( function ( bundle ) {
var generated = bundle.generate({
format: 'cjs'
});

var fn = new Function ( 'module', generated.code );
var module = {};

fn( module );

}).then( executeBundle ).then( module => {
assert.equal( module.exports, 'FOO' );
});
});
Expand All @@ -96,16 +74,7 @@ describe( 'rollup-plugin-npm', function () {
plugins: [
npm()
]
}).then( function ( bundle ) {
var generated = bundle.generate({
format: 'cjs'
});

var fn = new Function ( 'module', generated.code );
var module = {};

fn( module );

}).then( executeBundle ).then( module => {
assert.equal( module.exports, 42 );
});
});
Expand Down Expand Up @@ -142,4 +111,90 @@ describe( 'rollup-plugin-npm', function () {
assert.ok( generated.code.indexOf( 'encode' ) < 0 );
});
});

it( 'disregards top-level browser field by default', function () {
return rollup.rollup({
entry: 'samples/browser/main.js',
plugins: [
npm({
main: true,
browser: false
})
]
}).then( executeBundle ).then( module => {
assert.equal( module.exports, 'node' );
});
});

it( 'allows use of the top-level browser field', function () {
return rollup.rollup({
entry: 'samples/browser/main.js',
plugins: [
npm({
main: true,
browser: true
})
]
}).then( executeBundle ).then( module => {
assert.equal( module.exports, 'browser' );
});
});

it( 'disregards object browser field by default', function () {
return rollup.rollup({
entry: 'samples/browser-object/main.js',
plugins: [
npm({
main: true,
browser: false
})
]
}).then( executeBundle ).then( module => {
assert.equal( module.exports.env, 'node' );
assert.equal( module.exports.dep, 'node-dep' );
assert.equal( module.exports.test, 42 );
});
});

it( 'allows use of the object browser field', function () {
return rollup.rollup({
entry: 'samples/browser-object/main.js',
plugins: [
npm({
main: true,
browser: true
})
]
}).then( executeBundle ).then( module => {
assert.equal( module.exports.env, 'browser' );
assert.equal( module.exports.dep, 'browser-dep' );
assert.equal( module.exports.test, 43 );
});
});

it( 'supports `false` in browser field', function () {
return rollup.rollup({
entry: 'samples/browser-false/main.js',
plugins: [
npm({
main: true,
browser: true
})
]
}).then( executeBundle );
});

it( 'skips builtins', function () {
return rollup.rollup({
entry: 'samples/builtins/main.js',
plugins: [ npm() ]
}).then( bundle => {
const { code } = bundle.generate({ format: 'cjs' });
const fn = new Function ( 'module', 'exports', 'require', code );

fn( module, module.exports, id => require( id ) );

assert.equal( module.exports, path.sep );
});
});
});

0 comments on commit 4d5bf63

Please sign in to comment.