Skip to content

Commit

Permalink
feat(config): add support for ES modules
Browse files Browse the repository at this point in the history
- Add support for ES modules via `esModule` property in file config object
  • Loading branch information
wesleycho committed May 4, 2017
1 parent 51b4f64 commit 0fe4f20
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions docs/config/02-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Each pattern is either a simple string or an object with four properties:
* **Default.** `true`
* **Description.** Should the files be served by Karma's webserver?

### `esModule`
* **Type.** Boolean
* **Default.** `false`
* **Description.** Should the files be loaded as ECMAScript modules?

### `nocache`
* **Type.** Boolean
* **Default.** `false`
Expand Down
8 changes: 5 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ try {
TYPE_SCRIPT_AVAILABLE = true
} catch (e) {}

var Pattern = function (pattern, served, included, watched, nocache) {
var Pattern = function (pattern, served, included, watched, nocache, esModule) {
this.pattern = pattern
this.served = helper.isDefined(served) ? served : true
this.included = helper.isDefined(included) ? included : true
this.watched = helper.isDefined(watched) ? watched : true
this.esModule = helper.isDefined(esModule) ? esModule : false
this.nocache = helper.isDefined(nocache) ? nocache : false
this.weight = helper.mmPatternWeight(pattern)
}
Expand All @@ -44,7 +45,7 @@ Pattern.prototype.compare = function (other) {
}

var UrlPattern = function (url) {
Pattern.call(this, url, false, true, false, false)
Pattern.call(this, url, false, true, false, false, false)
}

var createPatternObject = function (pattern) {
Expand All @@ -61,7 +62,8 @@ var createPatternObject = function (pattern) {
pattern.served,
pattern.included,
pattern.watched,
pattern.nocache)
pattern.nocache,
pattern.esModule)
}

log.warn('Invalid pattern %s!\n\tObject is missing "pattern" property.', pattern)
Expand Down
3 changes: 2 additions & 1 deletion lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ List.prototype._refresh = function () {

var mtime = mg.statCache[path].mtime
var doNotCache = patternObject.nocache
var file = new File(path, mtime, doNotCache)
var esModule = patternObject.esModule
var file = new File(path, mtime, doNotCache, esModule)

if (file.doNotCache) {
log.debug('Not preprocessing "%s" due to nocache')
Expand Down
3 changes: 2 additions & 1 deletion lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var _ = require('lodash')

// Constructor
var File = function (path, mtime, doNotCache) {
var File = function (path, mtime, doNotCache, esModule) {
// used for serving (processed path, eg some/file.coffee -> some/file.coffee.js)
this.path = path

Expand All @@ -23,6 +23,7 @@ var File = function (path, mtime, doNotCache) {
this.isUrl = false

this.doNotCache = _.isUndefined(doNotCache) ? false : doNotCache
this.esModule = esModule
}

File.prototype.toString = function () {
Expand Down
3 changes: 2 additions & 1 deletion lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ var createKarmaMiddleware = function (
var file = files.included[i]
var filePath = file.path
var fileExt = path.extname(filePath)
var esModule = fileExt.endsWith('.js') && file.esModule

if (!files.included.hasOwnProperty(i)) {
continue
Expand All @@ -204,7 +205,7 @@ var createKarmaMiddleware = function (
}

// The script tag to be placed
var scriptType = (SCRIPT_TYPE[fileExt] || 'text/javascript')
var scriptType = esModule ? 'module' : (SCRIPT_TYPE[fileExt] || 'text/javascript')

// In case there is a JavaScript version specified and this is a Firefox browser
if (jsVersion && jsVersion > 0 && isFirefox(request)) {
Expand Down

0 comments on commit 0fe4f20

Please sign in to comment.