From 392faa9930be7191bf44ba1bded5d7aec06e480e Mon Sep 17 00:00:00 2001 From: Andrew Leedham Date: Mon, 4 May 2020 14:11:18 +0100 Subject: [PATCH 1/2] Add devDependencies option --- index.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index db7bce7..4ce1043 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,17 @@ -const getBuiltins = require('builtins'); -const path = require('path'); -const readPkg = require('read-pkg'); -const safeResolve = require('safe-resolve'); -const semver = require('semver'); +const getBuiltins = require("builtins"); +const path = require("path"); +const readPkg = require("read-pkg"); +const safeResolve = require("safe-resolve"); +const semver = require("semver"); module.exports = ({ builtins = true, dependencies = true, packagePath, peerDependencies = true, + devDependencies = false, } = {}) => ({ - name: 'auto-external', + name: "auto-external", options(opts) { const pkg = readPkg.sync(packagePath); let ids = []; @@ -23,14 +24,18 @@ module.exports = ({ ids = ids.concat(Object.keys(pkg.peerDependencies)); } + if (devDependencies && pkg.devDependencies) { + ids = ids.concat(Object.keys(pkg.devDependencies)); + } + if (builtins) { ids = ids.concat(getBuiltins(semver.valid(builtins))); } ids = ids.map(safeResolve).filter(Boolean); - const external = id => { - if (typeof opts.external === 'function' && opts.external(id)) { + const external = (id) => { + if (typeof opts.external === "function" && opts.external(id)) { return true; } @@ -50,7 +55,7 @@ module.exports = ({ const resolvedDirname = path.dirname(resolvedPath); - return ids.some(idx => resolvedDirname.startsWith(path.dirname(idx))); + return ids.some((idx) => resolvedDirname.startsWith(path.dirname(idx))); }; return Object.assign({}, opts, { external }); From 06c721e934ace2ef09896dfe70c444e344c54074 Mon Sep 17 00:00:00 2001 From: Andrew Leedham Date: Mon, 4 May 2020 17:14:32 +0100 Subject: [PATCH 2/2] Add docs + testing for devDependencies option --- README.md | 7 ++++++- __fixtures__/dev-deps/index.js | 5 +++++ __fixtures__/dev-deps/package.json | 6 ++++++ __tests__/__snapshots__/index.js.snap | 27 +++++++++++++++++++++++++++ __tests__/index.js | 11 +++++++++++ package.json | 5 +++-- 6 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 __fixtures__/dev-deps/index.js create mode 100644 __fixtures__/dev-deps/package.json diff --git a/README.md b/README.md index 3263035..d51e746 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://travis-ci.org/stevenbenisek/rollup-plugin-auto-external.svg?branch=master)](https://travis-ci.org/stevenbenisek/rollup-plugin-auto-external) -> [Rollup](https://rollupjs.org/) plugin to automatically exclude package.json dependencies and peerDependencies from your bundle. +> [Rollup](https://rollupjs.org/) plugin to automatically exclude package.json dependencies from your bundle. ## Install @@ -37,6 +37,7 @@ export default { dependencies: true, packagePath: path.resolve('./packages/module/package.json'), peerDependencies: false, + devDependencies: false, }), ], }; @@ -90,3 +91,7 @@ Rollup will complain if `builtins` is present, and the build target is a browser #### `peerDependencies` `boolean`: defaults to `true`. + +#### `devDependencies` + +`boolean`: defaults to `false`. diff --git a/__fixtures__/dev-deps/index.js b/__fixtures__/dev-deps/index.js new file mode 100644 index 0000000..001fea5 --- /dev/null +++ b/__fixtures__/dev-deps/index.js @@ -0,0 +1,5 @@ +import foo from 'foo'; +import bar from 'bar'; +import baz from 'baz'; + +export default `${foo}${bar}${baz}`; diff --git a/__fixtures__/dev-deps/package.json b/__fixtures__/dev-deps/package.json new file mode 100644 index 0000000..9eb4bd7 --- /dev/null +++ b/__fixtures__/dev-deps/package.json @@ -0,0 +1,6 @@ +{ + "devDependencies": { + "foo": "^0.0.0", + "bar": "^0.0.0" + } +} diff --git a/__tests__/__snapshots__/index.js.snap b/__tests__/__snapshots__/index.js.snap index 87d5df1..0d0509b 100644 --- a/__tests__/__snapshots__/index.js.snap +++ b/__tests__/__snapshots__/index.js.snap @@ -346,6 +346,18 @@ export default index; " `; +exports[`autoExternal(options) should handle enabling devDependencies 1`] = ` +"import foo from 'foo'; +import bar from 'bar'; + +var baz = 'baz'; + +var index = \`\${foo}\${bar}\${baz}\`; + +export default index; +" +`; + exports[`autoExternal(options) should handle extending external array 1`] = ` "import foo from 'foo'; import bar from 'bar'; @@ -379,3 +391,18 @@ var index = \`\${foo}\${bar}\${baz}\`; export default index; " `; + +exports[`autoExternal(options) should not add devDependencies by default 1`] = ` +"var foo = 'foo'; + +var bar = 'bar'; + +var bar_1 = bar; + +var baz = 'baz'; + +var index = \`\${foo}\${bar_1}\${baz}\`; + +export default index; +" +`; diff --git a/__tests__/index.js b/__tests__/index.js index 4c7cca6..d1575b6 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -56,6 +56,17 @@ describe('autoExternal(options)', () => { { peerDependencies: false } )); + it('should not add devDependencies by default', () => + bundle({ + input: path.resolve(__dirname, '../__fixtures__/dev-deps/index.js'), + })); + + it('should handle enabling devDependencies', () => + bundle( + { input: path.resolve(__dirname, '../__fixtures__/dev-deps/index.js') }, + { devDependencies: true } + )); + it('should add builtins by default', () => bundle({ input: path.resolve(__dirname, '../__fixtures__/builtins/index.js'), diff --git a/package.json b/package.json index e4f99ef..c153534 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rollup-plugin-auto-external", "version": "3.0.0-alpha.0", - "description": "Rollup plugin to automatically exclude package.json dependencies and peerDependencies from your bundle", + "description": "Rollup plugin to automatically exclude package.json dependencies from your bundle", "main": "index.js", "scripts": { "prepublishOnly": "npm test", @@ -13,7 +13,8 @@ "external", "auto", "dependencies", - "peerDependencies" + "peerDependencies", + "devDependencies" ], "author": "Steven Benisek ", "homepage": "https://github.com/stevenbenisek/rollup-plugin-auto-external",