Skip to content

Commit

Permalink
feat(node-resolve): export defaults (rollup#301)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

The plugin function is no longer the default export. Moving forward, there are two named exports:

```js
{ DEFAULTS, nodeResolve }
```

* feat(node-resolve): Export defaults

* Wrap module

* Don't export DEFAULT_OPTIONS directly

* Create cjs-wrapper

* Named export

* Freeze defaults

* Deep freeze defaults

* Deep merge defaults
  • Loading branch information
MichaelDeBoey authored and LarsDenBakker committed Sep 12, 2020
1 parent a04f4a8 commit 5901cfb
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 27 deletions.
2 changes: 2 additions & 0 deletions packages/node-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
"@rollup/pluginutils": "^3.0.8",
"@types/resolve": "0.0.8",
"builtin-modules": "^3.1.0",
"deep-freeze": "^0.0.1",
"deepmerge": "^4.2.2",
"is-module": "^1.0.0",
"resolve": "^1.14.2"
},
Expand Down
7 changes: 5 additions & 2 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { dirname, normalize, resolve, sep } from 'path';

import builtinList from 'builtin-modules';
import deepFreeze from 'deep-freeze';
import deepMerge from 'deepmerge';
import isModule from 'is-module';

import { isDirCached, isFileCached, readCachedFile } from './cache';
Expand All @@ -25,8 +27,9 @@ const defaults = {
extensions: ['.mjs', '.js', '.json', '.node'],
resolveOnly: []
};
export const DEFAULTS = deepFreeze(deepMerge({}, defaults));

export default function nodeResolve(opts = {}) {
export const nodeResolve = (opts = {}) => {
const options = Object.assign({}, defaults, opts);
const { customResolveOptions, extensions, jail } = options;
const warnings = [];
Expand Down Expand Up @@ -250,4 +253,4 @@ export default function nodeResolve(opts = {}) {
return idToPackageInfo.get(id);
}
};
}
};
2 changes: 1 addition & 1 deletion packages/node-resolve/test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const commonjs = require('rollup-plugin-commonjs');

const { testBundle } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/dedupe-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { rollup } = require('rollup');

const { testBundle } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures', 'custom-resolve-options'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/dedupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { rollup } = require('rollup');

const { testBundle } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/get-package-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const test = require('ava');
const { rollup } = require('rollup');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(path.join(__dirname, 'fixtures'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/jail.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { rollup } = require('rollup');

const { getImports } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/only.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { rollup } = require('rollup');

const { getImports } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { rollup } = require('rollup');

const { testBundle } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/prefer-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { rollup } = require('rollup');

const { getImports, testBundle } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/root-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { rollup } = require('rollup');

const { testBundle } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures', 'monorepo-dedupe', 'packages', 'package-a'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/symlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { rollup } = require('rollup');

const { testBundle } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures'));

Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const commonjs = require('rollup-plugin-commonjs');

const { getCode, getImports, testBundle } = require('../../../util/test');

const nodeResolve = require('..');
const { nodeResolve } = require('..');

process.chdir(join(__dirname, 'fixtures'));

Expand Down
4 changes: 2 additions & 2 deletions packages/node-resolve/test/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import resolve from '..';
import { nodeResolve } from '..';

/** @type {import("rollup").RollupOptions} */
const config = {
Expand All @@ -10,7 +10,7 @@ const config = {
name: 'MyModule'
},
plugins: [
resolve({
nodeResolve({
browser: true,
customResolveOptions: {
moduleDirectory: 'js_modules'
Expand Down
9 changes: 8 additions & 1 deletion packages/node-resolve/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Plugin } from 'rollup';
import { AsyncOpts } from 'resolve';

export const DEFAULTS: {
customResolveOptions: {};
dedupe: [];
extensions: ['.mjs', '.js', '.json', '.node'];
resolveOnly: [];
};

export interface Options {
/**
* If `true`, instructs the plugin to use the `"browser"` property in `package.json`
Expand Down Expand Up @@ -81,4 +88,4 @@ export interface Options {
/**
* Locate modules using the Node resolution algorithm, for using third party modules in node_modules
*/
export default function nodeResolve(options?: Options): Plugin;
export const nodeResolve: (options?: Options) => Plugin;
55 changes: 44 additions & 11 deletions pnpm-lock.yaml

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

0 comments on commit 5901cfb

Please sign in to comment.