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

Add an option to wrap merged files to IIFE #27

Merged
merged 1 commit into from
Jul 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"jscs": "1.13.1",
"jshint": "2.7.0",
"mocha": "2.2.4",
"mock-enb": "0.0.1",
"mock-enb": "0.0.2",
"mock-fs": "3.0.0",
"must": "0.12.0"
},
Expand Down
19 changes: 18 additions & 1 deletion techs/browser-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,26 @@
* nodeConfig.addTech(require('enb-diverse-js/techs/browser-js'));
* ```
*/
var EOL = require('os').EOL;
module.exports = require('enb/lib/build-flow').create()
.name('browser-js')
.target('target', '?.browser.js')
.useFileList(['vanilla.js', 'js', 'browser.js'])
.justJoinFilesWithComments()
.defineOption('iife', false)
.justJoinFiles(function (filename, contents) {
var relPath = this.node.relativePath(filename);

if (this._iife) {
contents = [
'(function(){',
contents,
'}());'
].join(EOL);
}
return [
'/* begin: ' + relPath + ' */',
contents,
'/* end: ' + relPath + ' *' + '/'
].join(EOL);
})
.createTech();
85 changes: 57 additions & 28 deletions test/techs/browser-js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,77 @@ var EOL = require('os').EOL,
mock = require('mock-fs'),
FileList = require('enb/lib/file-list'),
MockNode = require('mock-enb/lib/mock-node'),
browserJs = require('../../techs/browser-js');
browserJsTech = require('../../techs/browser-js');

describe('browser-js', function () {
var bundle,
fileList,
scheme;

afterEach(function () {
mock.restore();
});

it('must join files with comments', function () {
scheme = {
blocks: {
describe('must join files with comments', function () {
it('must join all files', function () {
var blocks = {
'block0.vanilla.js': 'Hello0',
'block1.browser.js': 'Hello1'
},
bundle: {}
};

mock(scheme);
reference = [
'/* begin: ../blocks/block0.vanilla.js */',
'Hello0',
'/* end: ../blocks/block0.vanilla.js */',
'/* begin: ../blocks/block1.browser.js */',
'Hello1',
'/* end: ../blocks/block1.browser.js */'
].join(EOL);

bundle = new MockNode('bundle');
fileList = new FileList();
return build(blocks)
.then(function (content) {
content[0].must.be(reference);
});
});
});

fileList.loadFromDirSync('blocks');
describe('code executes', function () {
var globals,
blocks = {
'block0.vanilla.js': 'var a = 1;',
'block1.browser.js': 'var a; global.TEST.push(a || 2);'
};

bundle.provideTechData('?.files', fileList);
beforeEach(function () {
globals = global.TEST = [];
});

var reference = [
'/* begin: ../blocks/block0.vanilla.js */',
'Hello0',
'/* end: ../blocks/block0.vanilla.js */',
'/* begin: ../blocks/block1.browser.js */',
'Hello1',
'/* end: ../blocks/block1.browser.js */'
].join(EOL);
it('code must executed in isolation', function () {
return build(blocks, { iife: true }, true)
.then(function () {
globals[0].must.be(2);
});
});

return bundle.runTechAndGetContent(browserJs)
.spread(function (content) {
content.toString().must.be(reference);
});
it('code must be executed in the same scoupe', function () {
return build(blocks, null, true)
.then(function () {
globals[0].must.be(1);
});
});
});
});

function build(blocks, options, isNeedRequire) {
mock({
blocks: blocks,
bundle: {}
});

var bundle = new MockNode('bundle'),
fileList = new FileList(),
testFunc;

fileList.loadFromDirSync('blocks');

bundle.provideTechData('?.files', fileList);

testFunc = isNeedRequire ? bundle.runTechAndRequire : bundle.runTechAndGetContent;

return testFunc.call(bundle, browserJsTech, options);
}
2 changes: 1 addition & 1 deletion test/techs/node-js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('node-js', function () {
'block.node.js': 'global.REQUIRED_TECHS.push("node-js");'
};

it('must join files all files', function () {
it('must join all files', function () {
return build(blocks)
.then(function () {
globals.must.include('vanilla-js');
Expand Down