Skip to content

Commit

Permalink
Refactor for upstream breaking changes
Browse files Browse the repository at this point in the history
Library source updated for Rollup v1.0.0 API (Closes #42)
  • Loading branch information
jlmakes committed Jan 3, 2019
1 parent a073f5b commit 2113471
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

- Support for Rollup `1.0.0` [#42](https://github.com/jlmakes/karma-rollup-preprocessor/issues/42)

### Changed

- **Breaking:** Upgrade Rollup peer dependency to `>= 1.0.0`
Expand Down
87 changes: 43 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const path = require("path");
const rollup = require("rollup").rollup;
const rollup = require("rollup");
const chokidar = require("chokidar");

function createPreprocessor(options, preconfig, basePath, emitter, logger) {
Expand All @@ -10,40 +10,39 @@ function createPreprocessor(options, preconfig, basePath, emitter, logger) {

let cache;

return (content, file, done) => {
const config = Object.assign({}, options, preconfig.options, {
input: file.path,
cache
});
rollup(config)
.then(bundle => {
cache = bundle.cache;
watch.capture(bundle);
log.info("Generating bundle");
return bundle.generate(config);
})
.then(({ code, map }) => {
file.sourceMap = map;

const sourcemap =
(config.output && config.output.sourcemap) || config.sourcemap;

const output =
sourcemap === "inline"
? (code += `\n//# sourceMappingURL=${map.toUrl()}\n`)
: code;

done(null, output);
})
.catch(error => {
const location = path.relative(basePath, file.path);
log.error(
"Error processing “%s”\n\n%s\n",
location,
error.stack || error.message
);
done(error, null);
return async function preprocessor(content, file, done) {
try {
const config = Object.assign({}, options, preconfig.options, {
input: file.path,
cache
});

const bundle = await rollup.rollup(config);
cache = bundle.cache;
watch.capture(cache.modules);

const { output } = await bundle.generate(config);

for (const result of output) {
if (!result.isAsset) {
const { code, map } = result;
const { sourcemap } = config.output;

file.sourceMap = map;

const processed =
sourcemap === "inline"
? code + `\n//# sourceMappingURL=${map.toUrl()}\n`
: code;

done(null, processed);
}
}
} catch (error) {
const location = path.relative(basePath, file.path);
log.error("Failed to process ./%s\n\n%s\n", location, error.stack);
done(error, null);
}
};
}

Expand All @@ -58,26 +57,26 @@ function Watch(emitter, log) {
emitter.on("run_start", () => this.start());
}

Watch.prototype.capture = function(bundle) {
Watch.prototype.capture = function(modules) {
this.buffer.clear();
bundle.modules.forEach(m => this.buffer.add(m.id));
modules.forEach(module => this.buffer.add(module.id));
};

Watch.prototype.clean = function() {
this.watchList.forEach(m => {
if (!this.buffer.has(m)) {
this.watch.unwatch(m);
this.watchList.delete(m);
this.watchList.forEach(module => {
if (!this.buffer.has(module)) {
this.watch.unwatch(module);
this.watchList.delete(module);
}
});
};

Watch.prototype.start = function() {
this.clean();
this.buffer.forEach(m => {
if (!this.watchList.has(m) && !m.includes("\u0000")) {
this.watch.add(m);
this.watchList.add(m);
this.buffer.forEach(module => {
if (!this.watchList.has(module) && !module.includes("\u0000")) {
this.watch.add(module);
this.watchList.add(module);
}
});
};
Expand Down

0 comments on commit 2113471

Please sign in to comment.