Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(middleware): added manual file type option #2846

Merged
merged 7 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ try {
TYPE_SCRIPT_AVAILABLE = true
} catch (e) {}

var Pattern = function (pattern, served, included, watched, nocache) {
var Pattern = function (pattern, served, included, watched, nocache, type) {
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.nocache = helper.isDefined(nocache) ? nocache : false
this.weight = helper.mmPatternWeight(pattern)
this.type = type
}

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

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 @@ -184,7 +184,8 @@ List.prototype._refresh = function () {

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

if (file.doNotCache) {
log.debug('Not preprocessing "%s" due to nocache')
Expand Down
4 changes: 3 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, type) {
// used for serving (processed path, eg some/file.coffee -> some/file.coffee.js)
this.path = path

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

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

this.type = type
}

File.prototype.toString = function () {
Expand Down
5 changes: 3 additions & 2 deletions 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 fileType = file.type

if (!files.included.hasOwnProperty(i)) {
continue
Expand All @@ -193,12 +194,12 @@ var createKarmaMiddleware = function (

scriptUrls.push(filePath)

if (fileExt === '.css') {
if (fileExt === '.css' || fileType === 'css') {
scriptTags.push(util.format(LINK_TAG_CSS, filePath))
continue
}

if (fileExt === '.html') {
if (fileExt === '.html' || fileType === 'html') {
scriptTags.push(util.format(LINK_TAG_HTML, filePath))
continue
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 208, scriptType should be fileType || SCRIPT_TYPE[fileExt] || 'text/javascript'. I think that would let us strip the code out of #2834 and just merge its tests.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub sadly wouldn't let me comment on the correct line.

Expand Down
38 changes: 28 additions & 10 deletions test/unit/middleware/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('middleware.karma', () => {
var nextSpy
var response

var MockFile = function (path, sha) {
File.call(this, path)
var MockFile = function (path, sha, type) {
File.call(this, path, undefined, undefined, type)
this.sha = sha || 'sha-default'
}

Expand Down Expand Up @@ -212,12 +212,14 @@ describe('middleware.karma', () => {
it('should serve context.html with replaced link tags', (done) => {
includedFiles([
new MockFile('/first.css', 'sha007'),
new MockFile('/second.html', 'sha678')
new MockFile('/second.html', 'sha678'),
new MockFile('/third', 'sha111', 'css'),
new MockFile('/fourth', 'sha222', 'html')
])

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CONTEXT\n<link type="text/css" href="/__proxy__/__karma__/absolute/first.css?sha007" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/second.html?sha678" rel="import">')
expect(response).to.beServedAs(200, 'CONTEXT\n<link type="text/css" href="/__proxy__/__karma__/absolute/first.css?sha007" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/second.html?sha678" rel="import">\n<link type="text/css" href="/__proxy__/__karma__/absolute/third?sha111" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/fourth?sha222" rel="import">')
done()
})

Expand All @@ -244,12 +246,16 @@ describe('middleware.karma', () => {
new MockFile('/some/abc/a.css', 'sha1'),
new MockFile('/base/path/b.css', 'sha2'),
new MockFile('/some/abc/c.html', 'sha3'),
new MockFile('/base/path/d.html', 'sha4')
new MockFile('/base/path/d.html', 'sha4'),
new MockFile('/some/abc/e', 'sha5', 'css'),
new MockFile('/base/path/f', 'sha6', 'css'),
new MockFile('/some/abc/g', 'sha7', 'html'),
new MockFile('/base/path/h', 'sha8', 'html')
])

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CONTEXT\n<link type="text/css" href="/__proxy__/__karma__/absolute/some/abc/a.css?sha1" rel="stylesheet">\n<link type="text/css" href="/__proxy__/__karma__/base/b.css?sha2" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/some/abc/c.html?sha3" rel="import">\n<link href="/__proxy__/__karma__/base/d.html?sha4" rel="import">')
expect(response).to.beServedAs(200, 'CONTEXT\n<link type="text/css" href="/__proxy__/__karma__/absolute/some/abc/a.css?sha1" rel="stylesheet">\n<link type="text/css" href="/__proxy__/__karma__/base/b.css?sha2" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/some/abc/c.html?sha3" rel="import">\n<link href="/__proxy__/__karma__/base/d.html?sha4" rel="import">\n<link type="text/css" href="/__proxy__/__karma__/absolute/some/abc/e?sha5" rel="stylesheet">\n<link type="text/css" href="/__proxy__/__karma__/base/f?sha6" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/some/abc/g?sha7" rel="import">\n<link href="/__proxy__/__karma__/base/h?sha8" rel="import">')
done()
})

Expand All @@ -261,7 +267,11 @@ describe('middleware.karma', () => {
new MockFile('/some/abc/a.css', 'sha1'),
new MockFile('/base/path/b.css', 'sha2'),
new MockFile('/some/abc/c.html', 'sha3'),
new MockFile('/base/path/d.html', 'sha4')
new MockFile('/base/path/d.html', 'sha4'),
new MockFile('/some/abc/e', 'sha5', 'css'),
new MockFile('/base/path/f', 'sha6', 'css'),
new MockFile('/some/abc/g', 'sha7', 'html'),
new MockFile('/base/path/h', 'sha8', 'html')
])

response.once('end', () => {
Expand All @@ -271,7 +281,11 @@ describe('middleware.karma', () => {
'/__proxy__/__karma__/absolute/some/abc/a.css?sha1',
'/__proxy__/__karma__/base/b.css?sha2',
'/__proxy__/__karma__/absolute/some/abc/c.html?sha3',
'/__proxy__/__karma__/base/d.html?sha4'
'/__proxy__/__karma__/base/d.html?sha4',
'/__proxy__/__karma__/absolute/some/abc/e?sha5',
'/__proxy__/__karma__/base/f?sha6',
'/__proxy__/__karma__/absolute/some/abc/g?sha7',
'/__proxy__/__karma__/base/h?sha8'
]
}))
done()
Expand Down Expand Up @@ -361,12 +375,16 @@ describe('middleware.karma', () => {
new MockFile('/first.css'),
new MockFile('/base/path/b.css'),
new MockFile('/second.html'),
new MockFile('/base/path/d.html')
new MockFile('/base/path/d.html'),
new MockFile('/third', null, 'css'),
new MockFile('/base/path/f', null, 'css'),
new MockFile('/fourth', null, 'html'),
new MockFile('/base/path/g', null, 'html')
])

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'DEBUG\n<link type="text/css" href="/__proxy__/__karma__/absolute/first.css" rel="stylesheet">\n<link type="text/css" href="/__proxy__/__karma__/base/b.css" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/second.html" rel="import">\n<link href="/__proxy__/__karma__/base/d.html" rel="import">')
expect(response).to.beServedAs(200, 'DEBUG\n<link type="text/css" href="/__proxy__/__karma__/absolute/first.css" rel="stylesheet">\n<link type="text/css" href="/__proxy__/__karma__/base/b.css" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/second.html" rel="import">\n<link href="/__proxy__/__karma__/base/d.html" rel="import">\n<link type="text/css" href="/__proxy__/__karma__/absolute/third" rel="stylesheet">\n<link type="text/css" href="/__proxy__/__karma__/base/f" rel="stylesheet">\n<link href="/__proxy__/__karma__/absolute/fourth" rel="import">\n<link href="/__proxy__/__karma__/base/g" rel="import">')
done()
})

Expand Down