Skip to content

Commit

Permalink
fix: allow for deep nested webpack options
Browse files Browse the repository at this point in the history
  • Loading branch information
daKmoR committed Dec 10, 2018
1 parent adfe2ff commit 048f6ee
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
48 changes: 36 additions & 12 deletions lib/KarmaWebpackController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require('fs');
const os = require('os');

const webpack = require('webpack');
const merge = require('merge');

class KarmaSyncPlugin {
constructor(options) {
Expand Down Expand Up @@ -64,6 +65,7 @@ const defaultWebpackOptions = {
},
},
},
plugins: [],
// Something like this will be auto added by this.configure()
// entry: {
// 'foo-one.test.js': 'path/to/test/foo-one.test.js',
Expand All @@ -75,36 +77,54 @@ const defaultWebpackOptions = {
};

class KarmaWebpackController {
constructor() {
this.isActive = false;
this.bundlesContent = {};
this.__debounce = false;
set webpackOptions(options) {
this.__webpackOptions = options;
}

configure(webpackOptions, karmaOptions) {
this.__webpackOptions = {
...defaultWebpackOptions,
...webpackOptions,
};
get webpackOptions() {
return this.__webpackOptions;
}

set karmaEmitter(emitter) {
this.__karmaEmitter = emitter;

this.__webpackOptions.plugins = this.__webpackOptions.plugins || [];
this.__webpackOptions.plugins.push(
new KarmaSyncPlugin({
karmaEmitter: karmaOptions.emitter,
karmaEmitter: emitter,
controller: this,
})
);

karmaOptions.emitter.on('exit', (done) => {
emitter.on('exit', (done) => {
this.onKarmaExit();
done();
});
}

get karmaEmitter() {
return this.__karmaEmitter;
}

get outputPath() {
return this.__webpackOptions.output.path;
}

constructor() {
this.isActive = false;
this.bundlesContent = {};
this.__debounce = false;
this.__webpackOptions = defaultWebpackOptions;
}

updateWebpackOptions(newOptions) {
this.__webpackOptions = merge.recursive(
true,
this.__webpackOptions,
newOptions
);
}

async bundle() {
if (this.isActive === false && this.__debounce === false) {
console.log('Webpack bundling...');
Expand Down Expand Up @@ -161,4 +181,8 @@ class KarmaWebpackController {
}
}

module.exports = KarmaWebpackController;
module.exports = {
KarmaSyncPlugin,
KarmaWebpackController,
defaultWebpackOptions,
};
17 changes: 7 additions & 10 deletions lib/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs');

const glob = require('glob');

const KarmaWebpackController = require('./KarmaWebpackController');
const { KarmaWebpackController } = require('./KarmaWebpackController');

const controller = new KarmaWebpackController();

Expand Down Expand Up @@ -57,15 +57,12 @@ function preprocessorsToWebpackEntries(preprocessors) {
function preprocessorFactory(config, emitter) {
// one time setup
if (controller.isActive === false) {
controller.configure(
{
entry: preprocessorsToWebpackEntries(config.preprocessors),
watch: config.autoWatch,
},
{
emitter,
}
);
controller.updateWebpackOptions({
entry: preprocessorsToWebpackEntries(config.preprocessors),
watch: config.autoWatch,
});
controller.updateWebpackOptions(config.webpack);
controller.karmaEmitter = emitter;
}

return async function processFile(content, file, done) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"webpack": "^4.0.0"
},
"dependencies": {
"glob": "^7.1.3"
"glob": "^7.1.3",
"merge": "^1.2.1"
},
"devDependencies": {
"@commitlint/cli": "^7.1.1",
Expand Down
26 changes: 26 additions & 0 deletions test/KarmaWebpackController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {
KarmaWebpackController,
defaultWebpackOptions,
} = require('../lib/KarmaWebpackController');

describe('KarmaWebpackController', () => {
it('applies the default webpackOptions', () => {
const controller = new KarmaWebpackController();
expect(controller.webpackOptions).toEqual(defaultWebpackOptions);
});

it('can provide custom nested webpackOptions', () => {
const controller = new KarmaWebpackController();
controller.updateWebpackOptions({
output: {
path: 'foo',
publicPath: 'bar',
},
});
expect(controller.webpackOptions.output.path).toBe('foo');
expect(controller.webpackOptions.output.publicPath).toBe('bar');
expect(controller.webpackOptions.output.filename).toBe(
defaultWebpackOptions.output.filename
);
});
});

0 comments on commit 048f6ee

Please sign in to comment.