diff --git a/README.md b/README.md index 51d307c..45d3a1f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ export default { name: 'MyModule', plugins: [ resolve({ + // use "es2015" field for ES6 module if possible + es2015: true, // Default: false + // use "module" field for ES6 module if possible module: true, // Default: true diff --git a/src/index.js b/src/index.js index 6b59116..8005111 100644 --- a/src/index.js +++ b/src/index.js @@ -40,6 +40,7 @@ function cachedIsFile (file, cb) { const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents))); export default function nodeResolve ( options = {} ) { + const useEs2015 = options.es2015 === true; const useModule = options.module !== false; const useMain = options.main !== false; const useJsnext = options.jsnext === true; @@ -59,8 +60,8 @@ export default function nodeResolve ( options = {} ) { throw new Error( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' ); } - if ( !useModule && !useMain && !useJsnext ) { - throw new Error( `At least one of options.module, options.main or options.jsnext must be true` ); + if ( !useEs2015 && !useModule && !useMain && !useJsnext ) { + throw new Error( `At least one of options.es2015, options.module, options.main or options.jsnext must be true` ); } let preserveSymlinks; @@ -135,11 +136,13 @@ export default function nodeResolve ( options = {} ) { if (options.browser && typeof pkg[ 'browser' ] === 'string') { pkg[ 'main' ] = pkg[ 'browser' ]; + } else if ( useEs2015 && pkg[ 'es2015' ] ) { + pkg[ 'main' ] = pkg[ 'es2015' ]; } else if ( useModule && pkg[ 'module' ] ) { pkg[ 'main' ] = pkg[ 'module' ]; } else if ( useJsnext && pkg[ 'jsnext:main' ] ) { pkg[ 'main' ] = pkg[ 'jsnext:main' ]; - } else if ( ( useJsnext || useModule ) && !useMain ) { + } else if ( ( useEs2015 || useJsnext || useModule ) && !useMain ) { disregardResult = true; } return pkg; diff --git a/test/node_modules/entries/es2015-entry.js b/test/node_modules/entries/es2015-entry.js new file mode 100644 index 0000000..ca93c5b --- /dev/null +++ b/test/node_modules/entries/es2015-entry.js @@ -0,0 +1 @@ +export default 'ES2015-ENTRY'; diff --git a/test/node_modules/entries/package.json b/test/node_modules/entries/package.json index 155bbf8..55d6b80 100644 --- a/test/node_modules/entries/package.json +++ b/test/node_modules/entries/package.json @@ -1,5 +1,6 @@ { "main": "main-entry.js", "module": "module-entry.js", - "jsnext:main": "jsnext-entry.js" + "jsnext:main": "jsnext-entry.js", + "es2015": "es2015-entry.js" } diff --git a/test/node_modules/es2015/entry.js b/test/node_modules/es2015/entry.js new file mode 100644 index 0000000..2fe8897 --- /dev/null +++ b/test/node_modules/es2015/entry.js @@ -0,0 +1 @@ +export default 'ES2015'; diff --git a/test/node_modules/es2015/package.json b/test/node_modules/es2015/package.json new file mode 100644 index 0000000..e592029 --- /dev/null +++ b/test/node_modules/es2015/package.json @@ -0,0 +1,3 @@ +{ + "es2015": "entry.js" +} diff --git a/test/samples/es2015/main.js b/test/samples/es2015/main.js new file mode 100644 index 0000000..4bb71d4 --- /dev/null +++ b/test/samples/es2015/main.js @@ -0,0 +1,3 @@ +import es2015 from 'es2015'; + +export default es2015; // ES2015 diff --git a/test/samples/prefer-es2015/main.js b/test/samples/prefer-es2015/main.js new file mode 100644 index 0000000..c5a945d --- /dev/null +++ b/test/samples/prefer-es2015/main.js @@ -0,0 +1,3 @@ +import entry from 'entries'; + +export default entry; diff --git a/test/test.js b/test/test.js index fbf6c2f..718bdd3 100644 --- a/test/test.js +++ b/test/test.js @@ -47,6 +47,17 @@ describe( 'rollup-plugin-node-resolve', function () { }); }); + it( 'finds a module with es2015', function () { + return rollup.rollup({ + input: 'samples/es2015/main.js', + plugins: [ + nodeResolve({ es2015: true }) + ] + }).then( executeBundle ).then( module => { + assert.equal( module.exports, 'ES2015' ); + }); + }); + it( 'finds and converts a basic CommonJS module', function () { return rollup.rollup({ input: 'samples/commonjs/main.js', @@ -445,6 +456,17 @@ describe( 'rollup-plugin-node-resolve', function () { }); }); + it( 'prefers es2015 field over module, jsnext:main and main', () => { + return rollup.rollup({ + input: 'samples/prefer-es2015/main.js', + plugins: [ + nodeResolve({ es2015: true, jsnext: true, preferBuiltins: false }) + ] + }).then( executeBundle ).then( module => { + assert.equal( module.exports, 'ES2015-ENTRY' ); + }); + }); + it('finds and uses an .mjs module', function () { return rollup.rollup({ input: 'samples/module-mjs/main.js',