-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(*): break up into individual modules (#474)
this should make it much easier to comprehend and write tests for karma-webpack. there is one change in how the KarmaWebpackController is managed, we now instantiate this in the preprocessor phase and propagate the value within the karma config object as a private variable. This allows for breaking the framework and preprocessor into separates modules and has the added benefit of being able to run multiple times in a given session without sharing mutable state. This allows integrations tests to be run in parallel as well as multiple times which was previously not possible. Fixes N/A
- Loading branch information
Showing
20 changed files
with
264 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = require('./karma-webpack'); | ||
module.exports = require('./karma/plugin'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function KW_Framework(config) { | ||
// This controller is instantiated and set during the preprocessor phase. | ||
const controller = config.__karmaWebpackController; | ||
const commonsPath = path.join(controller.outputPath, 'commons.js'); | ||
const runtimePath = path.join(controller.outputPath, 'runtime.js'); | ||
|
||
// make sure tmp folder exists | ||
if (!fs.existsSync(controller.outputPath)) { | ||
fs.mkdirSync(controller.outputPath); | ||
} | ||
|
||
// create dummy files for commons.js and runtime.js so they get included by karma | ||
fs.closeSync(fs.openSync(commonsPath, 'w')); | ||
fs.closeSync(fs.openSync(runtimePath, 'w')); | ||
|
||
// register for karma | ||
config.files.unshift({ | ||
pattern: commonsPath, | ||
included: true, | ||
served: true, | ||
watched: false, | ||
}); | ||
config.files.unshift({ | ||
pattern: runtimePath, | ||
included: true, | ||
served: true, | ||
watched: false, | ||
}); | ||
} | ||
|
||
KW_Framework.$inject = ['config']; | ||
|
||
module.exports = KW_Framework; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const KW_Framework = require('../karma-webpack/framework'); | ||
const KW_Preprocessor = require('../karma-webpack/preprocessor'); | ||
|
||
const KW_KarmaPlugin = { | ||
'preprocessor:webpack': ['factory', KW_Preprocessor], | ||
'framework:webpack': ['factory', KW_Framework], | ||
}; | ||
|
||
module.exports = KW_KarmaPlugin; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const path = require('path'); | ||
const os = require('os'); | ||
|
||
function create() { | ||
return { | ||
mode: 'development', | ||
output: { | ||
filename: '[name].js', | ||
// eslint-disable-next-line prettier/prettier | ||
path: path.join(os.tmpdir(), '_karma_webpack_') + Math.floor(Math.random() * 1000000), | ||
}, | ||
stats: { | ||
modules: false, | ||
colors: true, | ||
}, | ||
watch: false, | ||
optimization: { | ||
runtimeChunk: 'single', | ||
splitChunks: { | ||
chunks: 'all', | ||
minSize: 0, | ||
cacheGroups: { | ||
commons: { | ||
name: 'commons', | ||
chunks: 'all', | ||
minChunks: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
plugins: [], | ||
}; | ||
} | ||
|
||
module.exports = { create }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const fs = require('fs'); | ||
|
||
class KW_WebpackPlugin { | ||
constructor(options) { | ||
this.karmaEmitter = options.karmaEmitter; | ||
this.controller = options.controller; | ||
} | ||
|
||
apply(compiler) { | ||
this.compiler = compiler; | ||
|
||
// webpack bundles are finished | ||
compiler.hooks.done.tap('KW_WebpackPlugin', async (stats) => { | ||
// read generated file content and store for karma preprocessor | ||
this.controller.bundlesContent = {}; | ||
stats.toJson().assets.forEach((webpackFileObj) => { | ||
const filePath = `${compiler.options.output.path}/${ | ||
webpackFileObj.name | ||
}`; | ||
this.controller.bundlesContent[webpackFileObj.name] = fs.readFileSync( | ||
filePath, | ||
'utf-8' | ||
); | ||
}); | ||
|
||
// karma refresh | ||
this.karmaEmitter.refreshFiles(); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = KW_WebpackPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.