Skip to content

Commit

Permalink
Merge pull request #1 from showpad/separate-options
Browse files Browse the repository at this point in the history
Separate options for rollup and bundle
  • Loading branch information
klaascuvelier committed Dec 14, 2015
2 parents ca2c63d + 13d1209 commit ccbab7b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 2.0.0
**BREAKING** the options for the preprocessor are now specified in 2 separate object, `rollup` and `bundle`


# 1.0.0
Initial version
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,36 @@ npm install karma-rollup-preprocessor --save-dev


# Configuration
See [rollup wiki](https://github.com/rollup/rollup/wiki) for more details.
The `rollupPreprocessor` configuration is optional. (You'll need to install more dependencies)
The `rollupPreprocessor` configuration is optional. (You'll need to install more dependencies). It takes two keys: `rollup` and
`bundle`.

`rollup` is the configuration object for `rollup` (See [rollup.rollup](https://github.com/rollup/rollup/wiki/JavaScript-API#rolluprollup-options-) for more details).

`bundle` is the configuration object used when generating the bundle (See [bundle.generate](https://github.com/rollup/rollup/wiki/JavaScript-API#bundlegenerate-options-) for more details)
*Notice* this is preprocessor and does not write a file or return the bundle, only the content of the processed file gets changed.
So when adding the `sourceMaps` options, `inline` is the only logical value.


## Example
```js
module.exports = function (config) {
config.set({
preprocessors: {
'test/main.js': ['rollup']
},
rollupPreprocessor: {
plugins: [
require('rollup-plugin-babel')({
presets: [
require('babel-preset-es2015-rollup')
]
})
]
rollup: {
plugins: [
require('rollup-plugin-babel')({
presets: [
require('babel-preset-es2015-rollup')
]
})
]
},
bundle: {
sourceMap: 'inline'
}
}
});
};
Expand Down
16 changes: 9 additions & 7 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ module.exports = function (config)

// specify the config for the rollup pre-processor: run babel plugin on the code
rollupPreprocessor: {
plugins: [
require('rollup-plugin-babel')({
presets: [
require('babel-preset-es2015-rollup')
]
})
]
rollup: {
plugins: [
require('rollup-plugin-babel')({
presets: [
require('babel-preset-es2015-rollup')
]
})
]
}
},

// load necessary plugins
Expand Down
19 changes: 16 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ function createPreprocessor (args, config, logger, helper)
var log = logger.create('preprocessor.rollup');
config = config || {};

var rollupConfig = config.rollup || {};
var bundleConfig = config.bundle || {};

function preprocess (content, file, done)
{
log.debug('Processing "%s".', file.originalPath);

try {
config.entry = file.originalPath;
rollupConfig.entry = file.originalPath;

rollup
.rollup(config)
.rollup(rollupConfig)
.then(function (bundle)
{
var processed = bundle.generate({format: 'es6'}).code;
if (!bundleConfig.hasOwnProperty('format')) {
bundleConfig.format = 'es6';
}

var generated = bundle.generate(bundleConfig);
var processed = generated.code;

if (bundleConfig.sourceMap === 'inline') {
var url = generated.map.toUrl();
processed += "\n" + '//# sourceMappingURL=' + url;
}

done(null, processed);
})
.catch(function (error)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "karma-rollup-preprocessor",
"version": "1.0.0",
"version": "2.0.0",
"description": "A rollup preprocessor for Karma",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit ccbab7b

Please sign in to comment.