From ee5be6a7d8709e42821f6aeabbcad18a4af4dc66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Wed, 22 Apr 2020 17:56:02 +0200 Subject: [PATCH] feat(node-resolve): export defaults (#301) 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 --- packages/node-resolve/package.json | 2 + packages/node-resolve/src/index.js | 7 ++- packages/node-resolve/test/browser.js | 2 +- packages/node-resolve/test/dedupe-custom.js | 2 +- packages/node-resolve/test/dedupe.js | 2 +- .../node-resolve/test/get-package-info.js | 2 +- packages/node-resolve/test/jail.js | 2 +- packages/node-resolve/test/only.js | 2 +- packages/node-resolve/test/order.js | 2 +- packages/node-resolve/test/prefer-builtins.js | 2 +- packages/node-resolve/test/root-dir.js | 2 +- packages/node-resolve/test/symlinks.js | 2 +- packages/node-resolve/test/test.js | 2 +- packages/node-resolve/test/types.ts | 4 +- packages/node-resolve/types/index.d.ts | 9 ++- pnpm-lock.yaml | 55 +++++++++++++++---- 16 files changed, 72 insertions(+), 27 deletions(-) diff --git a/packages/node-resolve/package.json b/packages/node-resolve/package.json index bc6ac3a81..cf8d90c16 100755 --- a/packages/node-resolve/package.json +++ b/packages/node-resolve/package.json @@ -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" }, diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index 2573d2f3c..2378f568c 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -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'; @@ -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 = []; @@ -250,4 +253,4 @@ export default function nodeResolve(opts = {}) { return idToPackageInfo.get(id); } }; -} +}; diff --git a/packages/node-resolve/test/browser.js b/packages/node-resolve/test/browser.js index ae8ed9416..09023e238 100644 --- a/packages/node-resolve/test/browser.js +++ b/packages/node-resolve/test/browser.js @@ -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')); diff --git a/packages/node-resolve/test/dedupe-custom.js b/packages/node-resolve/test/dedupe-custom.js index 4e01f6220..11642fb71 100644 --- a/packages/node-resolve/test/dedupe-custom.js +++ b/packages/node-resolve/test/dedupe-custom.js @@ -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')); diff --git a/packages/node-resolve/test/dedupe.js b/packages/node-resolve/test/dedupe.js index 4de260b14..52579b0a2 100644 --- a/packages/node-resolve/test/dedupe.js +++ b/packages/node-resolve/test/dedupe.js @@ -5,7 +5,7 @@ const { rollup } = require('rollup'); const { testBundle } = require('../../../util/test'); -const nodeResolve = require('..'); +const { nodeResolve } = require('..'); process.chdir(join(__dirname, 'fixtures')); diff --git a/packages/node-resolve/test/get-package-info.js b/packages/node-resolve/test/get-package-info.js index 6de9ed7e7..38f2447ad 100644 --- a/packages/node-resolve/test/get-package-info.js +++ b/packages/node-resolve/test/get-package-info.js @@ -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')); diff --git a/packages/node-resolve/test/jail.js b/packages/node-resolve/test/jail.js index 27c50d346..299acc5f1 100644 --- a/packages/node-resolve/test/jail.js +++ b/packages/node-resolve/test/jail.js @@ -5,7 +5,7 @@ const { rollup } = require('rollup'); const { getImports } = require('../../../util/test'); -const nodeResolve = require('..'); +const { nodeResolve } = require('..'); process.chdir(join(__dirname, 'fixtures')); diff --git a/packages/node-resolve/test/only.js b/packages/node-resolve/test/only.js index 2f4209967..d983e5b61 100644 --- a/packages/node-resolve/test/only.js +++ b/packages/node-resolve/test/only.js @@ -5,7 +5,7 @@ const { rollup } = require('rollup'); const { getImports } = require('../../../util/test'); -const nodeResolve = require('..'); +const { nodeResolve } = require('..'); process.chdir(join(__dirname, 'fixtures')); diff --git a/packages/node-resolve/test/order.js b/packages/node-resolve/test/order.js index 30f0bc156..c65809f24 100644 --- a/packages/node-resolve/test/order.js +++ b/packages/node-resolve/test/order.js @@ -5,7 +5,7 @@ const { rollup } = require('rollup'); const { testBundle } = require('../../../util/test'); -const nodeResolve = require('..'); +const { nodeResolve } = require('..'); process.chdir(join(__dirname, 'fixtures')); diff --git a/packages/node-resolve/test/prefer-builtins.js b/packages/node-resolve/test/prefer-builtins.js index 3059a8973..dda3f5008 100644 --- a/packages/node-resolve/test/prefer-builtins.js +++ b/packages/node-resolve/test/prefer-builtins.js @@ -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')); diff --git a/packages/node-resolve/test/root-dir.js b/packages/node-resolve/test/root-dir.js index 837695c50..f200d3ca4 100644 --- a/packages/node-resolve/test/root-dir.js +++ b/packages/node-resolve/test/root-dir.js @@ -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')); diff --git a/packages/node-resolve/test/symlinks.js b/packages/node-resolve/test/symlinks.js index e3732ab7d..17af234e4 100644 --- a/packages/node-resolve/test/symlinks.js +++ b/packages/node-resolve/test/symlinks.js @@ -6,7 +6,7 @@ const { rollup } = require('rollup'); const { testBundle } = require('../../../util/test'); -const nodeResolve = require('..'); +const { nodeResolve } = require('..'); process.chdir(join(__dirname, 'fixtures')); diff --git a/packages/node-resolve/test/test.js b/packages/node-resolve/test/test.js index 6733864a1..ab2240b23 100755 --- a/packages/node-resolve/test/test.js +++ b/packages/node-resolve/test/test.js @@ -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')); diff --git a/packages/node-resolve/test/types.ts b/packages/node-resolve/test/types.ts index 5a8348cbd..c2be43c91 100755 --- a/packages/node-resolve/test/types.ts +++ b/packages/node-resolve/test/types.ts @@ -1,5 +1,5 @@ // @ts-check -import resolve from '..'; +import { nodeResolve } from '..'; /** @type {import("rollup").RollupOptions} */ const config = { @@ -10,7 +10,7 @@ const config = { name: 'MyModule' }, plugins: [ - resolve({ + nodeResolve({ browser: true, customResolveOptions: { moduleDirectory: 'js_modules' diff --git a/packages/node-resolve/types/index.d.ts b/packages/node-resolve/types/index.d.ts index 2ef6b59c9..39d48bd0f 100755 --- a/packages/node-resolve/types/index.d.ts +++ b/packages/node-resolve/types/index.d.ts @@ -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` @@ -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; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1fdeb13b7..cfb0a5569 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -256,15 +256,17 @@ importers: rollup-plugin-babel: ^4.4.0 packages/node-resolve: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.9_rollup@2.2.0 '@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.15.0 devDependencies: '@babel/core': 7.9.0 '@babel/preset-env': 7.9.0_@babel+core@7.9.0 - '@rollup/plugin-json': 4.0.2_rollup@2.2.0 + '@rollup/plugin-json': 4.0.3_rollup@2.2.0 es5-ext: 0.10.53 rollup: 2.2.0 rollup-plugin-babel: 4.3.3_@babel+core@7.9.0+rollup@2.2.0 @@ -278,6 +280,8 @@ importers: '@rollup/pluginutils': ^3.0.8 '@types/resolve': 0.0.8 builtin-modules: ^3.1.0 + deep-freeze: ^0.0.1 + deepmerge: ^4.2.2 es5-ext: ^0.10.53 is-module: ^1.0.0 resolve: ^1.14.2 @@ -1608,24 +1612,24 @@ packages: rollup: ^1.20.0 resolution: integrity: sha512-MPYGZr0qdbV5zZj8/2AuomVpnRVXRU5XKXb3HVniwRoRCreGlf5kOE081isNWeiLIi6IYkwTX9zE0/c7V8g81g== - /@rollup/plugin-json/4.0.2_rollup@2.2.0: + /@rollup/plugin-json/4.0.2_rollup@2.6.0: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 - rollup: 2.2.0 + '@rollup/pluginutils': 3.0.8_rollup@2.6.0 + rollup: 2.6.0 dev: true peerDependencies: rollup: ^1.20.0 resolution: integrity: sha512-t4zJMc98BdH42mBuzjhQA7dKh0t4vMJlUka6Fz0c+iO5IVnWaEMiYBy1uBj9ruHZzXBW23IPDGL9oCzBkQ9Udg== - /@rollup/plugin-json/4.0.2_rollup@2.6.0: + /@rollup/plugin-json/4.0.3_rollup@2.2.0: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.6.0 - rollup: 2.6.0 + '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + rollup: 2.2.0 dev: true peerDependencies: - rollup: ^1.20.0 + rollup: ^1.20.0 || ^2.0.0 resolution: - integrity: sha512-t4zJMc98BdH42mBuzjhQA7dKh0t4vMJlUka6Fz0c+iO5IVnWaEMiYBy1uBj9ruHZzXBW23IPDGL9oCzBkQ9Udg== + integrity: sha512-QMUT0HZNf4CX17LMdwaslzlYHUKTYGuuk34yYIgZrNdu+pMEfqMS55gck7HEeHBKXHM4cz5Dg1OVwythDdbbuQ== /@rollup/plugin-node-resolve/7.1.1: dependencies: '@rollup/pluginutils': 3.0.8 @@ -1758,6 +1762,19 @@ packages: rollup: ^1.20.0 resolution: integrity: sha512-rYGeAc4sxcZ+kPG/Tw4/fwJODC3IXHYDH4qusdN/b6aLw5LPUbzpecYbEJh4sVQGPFJxd2dBU4kc1H3oy9/bnw== + /@rollup/pluginutils/3.0.9_rollup@2.2.0: + dependencies: + '@types/estree': 0.0.39 + estree-walker: 1.0.1 + micromatch: 4.0.2 + rollup: 2.2.0 + dev: false + engines: + node: '>= 8.0.0' + peerDependencies: + rollup: ^1.20.0||^2.0.0 + resolution: + integrity: sha512-TLZavlfPAZYI7v33wQh4mTP6zojne14yok3DNSLcjoG/Hirxfkonn6icP5rrNWRn8nZsirJBFFpijVOJzkUHDg== /@samverschueren/stream-to-observable/0.3.0: dependencies: any-observable: 0.3.0 @@ -3193,10 +3210,20 @@ packages: node: '>=4.0.0' resolution: integrity: sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA== + /deep-freeze/0.0.1: + dev: false + resolution: + integrity: sha1-OgsABd4YZygZ39OM0x+RF5yJPoQ= /deep-is/0.1.3: dev: true resolution: integrity: sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ= + /deepmerge/4.2.2: + dev: false + engines: + node: '>=0.10.0' + resolution: + integrity: sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg== /default-require-extensions/2.0.0: dependencies: strip-bom: 3.0.0 @@ -5257,7 +5284,7 @@ packages: /micromatch/4.0.2: dependencies: braces: 3.0.2 - picomatch: 2.2.1 + picomatch: 2.2.2 engines: node: '>=8' resolution: @@ -5845,10 +5872,16 @@ packages: resolution: integrity: sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== /picomatch/2.2.1: + dev: true engines: node: '>=8.6' resolution: integrity: sha512-ISBaA8xQNmwELC7eOjqFKMESB2VIqt4PPDD0nsS95b/9dZXvVKOlz9keMSnoGGKcOHXfTvDD6WMaRoSc9UuhRA== + /picomatch/2.2.2: + engines: + node: '>=8.6' + resolution: + integrity: sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg== /pify/2.3.0: dev: true engines: