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

Add support for es2015 field #186

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/node_modules/entries/es2015-entry.js

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

3 changes: 2 additions & 1 deletion test/node_modules/entries/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/es2015/entry.js

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

3 changes: 3 additions & 0 deletions test/node_modules/es2015/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/es2015/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import es2015 from 'es2015';

export default es2015; // ES2015
3 changes: 3 additions & 0 deletions test/samples/prefer-es2015/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import entry from 'entries';

export default entry;
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down