Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add devDependencies option #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -37,6 +37,7 @@ export default {
dependencies: true,
packagePath: path.resolve('./packages/module/package.json'),
peerDependencies: false,
devDependencies: false,
}),
],
};
Expand Down Expand Up @@ -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`.
5 changes: 5 additions & 0 deletions __fixtures__/dev-deps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import foo from 'foo';
import bar from 'bar';
import baz from 'baz';

export default `${foo}${bar}${baz}`;
6 changes: 6 additions & 0 deletions __fixtures__/dev-deps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"devDependencies": {
"foo": "^0.0.0",
"bar": "^0.0.0"
}
}
27 changes: 27 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
"
`;
11 changes: 11 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 = [];
Expand All @@ -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;
}

Expand All @@ -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 });
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -13,7 +13,8 @@
"external",
"auto",
"dependencies",
"peerDependencies"
"peerDependencies",
"devDependencies"
],
"author": "Steven Benisek <[email protected]>",
"homepage": "https://github.com/stevenbenisek/rollup-plugin-auto-external",
Expand Down