Skip to content

Commit

Permalink
Introduced a simpler initialization syntax, deprecating the old
Browse files Browse the repository at this point in the history
  • Loading branch information
agronkabashi committed Feb 1, 2017
1 parent 9cadd2a commit 576f9b0
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 37 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# Version 1.1.0
* Simplified the syntax for initializing conditional. The older syntax is now deprecated.

```
// Before
plugins: [
conditional({
condition: process.env.buildTarget === "PROD",
plugins: [
plugin1(),
plugin2(),
...
]
})
]
// After
plugins: [
conditional(process.env.buildTarget === "PROD", [
plugin1(),
plugin2(),
...
])
]
```

# Version 1.0.1
* Minor hotpatch that updates the build files

# Version 1.0.0
* `plugin` property is replaced with `plugins` to simplify the setup where you need to run many plugins based on the same condition
```
Expand Down
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@ export default {
...
plugins: [
...
conditional({
condition: isProduction,
plugins: [
licence(),
strip(),
uglify(),
gzip()
]
})

conditional({
condition: !isProduction,
plugins: [
filesize(),
watch()
]
})
conditional(isProduction, [
licence(),
strip(),
uglify(),
gzip()
]),

conditional(!isProduction, [
filesize(),
watch()
])
]
})
```
Expand Down
20 changes: 16 additions & 4 deletions build/rollup-plugin-conditional.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ function getFirstExecutablePlugin(apiName, plugins, ...args) {
return plugins.reduce((result, plugin) => result || plugin[apiName] && plugin[apiName](...args), undefined);
}

var index = (options = {}) => {
if (!options.condition || !Array.isArray(options.plugins) || options.plugins.length === 0) {
return {};
var index = (arg1 = {}, arg2) => {
let condition = false;
let plugins = [];

if (Array.isArray(arg2)) {
condition = !!arg1;
plugins = arg2;
}
// Deprecated in 1.1.0
else if (typeof arg1 === "object") {
console.warn("This syntax is deprecated, please use conditional(condition, plugins) instead.");
condition = !!arg1.condition;
plugins = arg1.plugins;
}

const { plugins } = options;
if (!condition || !Array.isArray(plugins) || plugins.length === 0) {
return {};
}

const constructedApi = {};

Expand Down
20 changes: 16 additions & 4 deletions build/rollup-plugin-conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ function getFirstExecutablePlugin(apiName, plugins, ...args) {
return plugins.reduce((result, plugin) => result || plugin[apiName] && plugin[apiName](...args), undefined);
}

var index = (options = {}) => {
if (!options.condition || !Array.isArray(options.plugins) || options.plugins.length === 0) {
return {};
var index = (arg1 = {}, arg2) => {
let condition = false;
let plugins = [];

if (Array.isArray(arg2)) {
condition = !!arg1;
plugins = arg2;
}
// Deprecated in 1.1.0
else if (typeof arg1 === "object") {
console.warn("This syntax is deprecated, please use conditional(condition, plugins) instead.");
condition = !!arg1.condition;
plugins = arg1.plugins;
}

const { plugins } = options;
if (!condition || !Array.isArray(plugins) || plugins.length === 0) {
return {};
}

const constructedApi = {};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "rollup-plugin-conditional",
"version": "1.0.1",
"version": "1.1.0",
"description": "A proxy plugin for conditionally executing rollup plugins",
"main": "build/rollup-plugin-conditional.js",
"jsnext:main": "build/rollup-plugin-conditional.es.js",
"homepage": "https://github.com/AgronKAbashi/rollup-plugin-conditional",
"scripts": {
"clean": "rm -rf build",
"build": "rollup -c",
"publish": "npm run build && npm publish",
"start": "npm run clean -s && npm run build -s",
"test": "mocha --compilers js:babel-core/register"
},
Expand Down
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ function getFirstExecutablePlugin(apiName, plugins, ...args) {
return plugins.reduce((result, plugin) => result || plugin[apiName] && plugin[apiName](...args), undefined);
}

export default (options = {}) => {
if (!options.condition || !Array.isArray(options.plugins) || options.plugins.length === 0) {
return {};
export default (arg1 = {}, arg2) => {
let condition = false;
let plugins = [];

if (Array.isArray(arg2)) {
condition = !!arg1;
plugins = arg2;
}
// Deprecated in 1.1.0
else if (typeof arg1 === "object") {
console.warn("This syntax is deprecated, please use conditional(condition, plugins) instead.")
condition = !!arg1.condition
plugins = arg1.plugins
}

const { plugins } = options;
if (!condition || !Array.isArray(plugins) || plugins.length === 0) {
return {};
}

const constructedApi = {};

Expand Down
11 changes: 4 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ describe("rollup-plugin-conditional", () => {
let plugins;
let spies;

const createRollup = (condition, config) => {
const createRollup = (condition, plugins) => {
return rollup({
entry: "./test/fixtures/sourcecode",
plugins: [
conditional({
condition,
...config
})
conditional(condition, plugins)
]
});
};
Expand All @@ -33,7 +30,7 @@ describe("rollup-plugin-conditional", () => {
});

it("should run all the plugins if condition evaluates to true", () => {
return createRollup(true, {plugins}).then(() => {
return createRollup(true, plugins).then(() => {
assert(calculateSpyCallCount(spies) === plugins.length);
});
});
Expand All @@ -60,7 +57,7 @@ describe("rollup-plugin-conditional", () => {
});

it("should only execute the first plugin", () => {
return createRollup(true, {plugins}).then((args) => {
return createRollup(true, plugins).then((args) => {
assert(args.modules[0].code === "var transform1variable = true;")
});
})
Expand Down

0 comments on commit 576f9b0

Please sign in to comment.