-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): refactor bundleResource in lib/server.js (#3029)
- Loading branch information
1 parent
c1a9567
commit 011a90c
Showing
9 changed files
with
156 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
test/e2e/support/error/under-test.js | ||
test/unit/fixtures/bundled.js | ||
static/karma.js | ||
static/context.js | ||
tmp/* |
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
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,27 @@ | ||
'use strict' | ||
const PathUtils = require('./path-utils') | ||
const fs = require('fs') | ||
const Promise = require('bluebird') | ||
|
||
const BundleUtils = { | ||
bundleResource (inPath, outPath) { | ||
return new Promise((resolve, reject) => { | ||
require('browserify')(inPath) | ||
.bundle() | ||
.pipe(fs.createWriteStream(outPath)) | ||
.once('finish', () => resolve()) | ||
.once('error', (e) => reject(e)) | ||
}) | ||
}, | ||
|
||
bundleResourceIfNotExist (inPath, outPath) { | ||
inPath = PathUtils.calculateAbsolutePath(inPath) | ||
outPath = PathUtils.calculateAbsolutePath(outPath) | ||
|
||
return fs.existsSync(outPath) | ||
? Promise.resolve() | ||
: BundleUtils.bundleResource(inPath, outPath) | ||
} | ||
} | ||
|
||
module.exports = BundleUtils |
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,9 +1,16 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
|
||
const PathUtils = { | ||
formatPathMapping (path, line, column) { | ||
return path + (line ? `:${line}` : '') + (column ? `:${column}` : '') | ||
}, | ||
|
||
calculateAbsolutePath (karmaRelativePath) { | ||
return path.join(__dirname, '..', '..', karmaRelativePath) | ||
} | ||
|
||
} | ||
|
||
module.exports = PathUtils |
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,42 @@ | ||
'use strict' | ||
|
||
const BundleUtils = require('../../../lib/utils/bundle-utils') | ||
const PathUtils = require('../../../lib/utils/path-utils') | ||
const FileUtils = require('../../../lib/utils/file-utils') | ||
const fs = require('fs') | ||
|
||
const sandbox = sinon.sandbox.create() | ||
|
||
describe('BundleUtils.bundleResource', () => { | ||
beforeEach(() => FileUtils.removeFileIfExists(PathUtils.calculateAbsolutePath('test/unit/fixtures/bundled.js'))) | ||
|
||
it('create bundle file in requested output path', (done) => { | ||
BundleUtils.bundleResource('test/unit/fixtures/format-error-root.js', 'test/unit/fixtures/bundled.js') | ||
.then(() => { | ||
expect(fs.existsSync(PathUtils.calculateAbsolutePath('test/unit/fixtures/bundled.js'))).to.be.true | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('BundleUtils.bundleResourceIfNotExist', () => { | ||
beforeEach(() => { | ||
sandbox.stub(BundleUtils, 'bundleResource').resolves() | ||
}) | ||
|
||
afterEach(() => sandbox.restore()) | ||
|
||
it('bundle resource when output path file not exists', () => { | ||
sandbox.stub(fs, 'existsSync').returns(false) | ||
|
||
BundleUtils.bundleResourceIfNotExist('context/main.js', 'static/context.js') | ||
expect(BundleUtils.bundleResource).to.have.been.called | ||
}) | ||
|
||
it('does not bundle resource when output path file exists', () => { | ||
sandbox.stub(fs, 'existsSync').returns(true) | ||
|
||
BundleUtils.bundleResourceIfNotExist('context/main.js', 'static/context.js') | ||
expect(BundleUtils.bundleResource).to.not.have.been.called | ||
}) | ||
}) |
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 @@ | ||
'use strict' | ||
const PathUtils = require('../../../lib/utils/path-utils') | ||
const fs = require('fs') | ||
|
||
describe('PathUtils.calculateAbsolutePath', () => { | ||
it('returns absolute path from karma project relative path', () => { | ||
expect(fs.existsSync(PathUtils.calculateAbsolutePath('logo/banner.png'))).to.be.true | ||
}) | ||
}) |