Skip to content

Commit

Permalink
Update PR
Browse files Browse the repository at this point in the history
  • Loading branch information
gela committed Jul 22, 2015
1 parent 76464e0 commit f2e3329
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 53 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"enb": ">= 0.8.22"
},
"devDependencies": {
"chai": "^3.2.0",
"chai-as-promised": "^5.1.0",
"enb": ">= 0.8.22",
"istanbul": "0.3.14",
"jscs": "1.13.1",
Expand All @@ -46,8 +48,8 @@
"coveralls": "npm i coveralls && npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls"
},
"dependencies": {
"browserify": "^10.2.6",
"vow": "^0.4.10",
"vow-node": "^0.3.0"
"browserify": "10.2.6",
"vow": "0.4.10",
"vow-node": "0.3.0"
}
}
36 changes: 18 additions & 18 deletions techs/node-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ module.exports = require('enb/lib/build-flow').create()
' }',
'};'
].join(EOL),
devMode = this._devMode || '',
bundled = this._bundled || '';
devMode = this._devMode || '';

if (bundled) {
if (this._bundled) {
var files = sourceFiles.map(function (file) {
var relPath = node.relativePath(file.fullname);
return relPath;
return node.relativePath(file.fullname);
}),
browserifyOptions = {
basedir: node.getDir()
basedir: node.getDir(),
builtins: [],
bundleExternal: false
},
renderer = browserify(files, browserifyOptions),
bundle = promisify(renderer.bundle.bind(renderer));
Expand All @@ -64,18 +64,18 @@ module.exports = require('enb/lib/build-flow').create()
.then(function (buf) {
return buf.toString('utf-8');
});
} else {
return [
devMode && dropRequireCacheFunc,
sourceFiles.map(function (file) {
var relPath = node.relativePath(file.fullname);

return [
devMode && 'dropRequireCache(require, require.resolve("' + relPath + '"));',
'require("' + relPath + '");'
].join(EOL);
}).join(EOL)
].join(EOL);
}

return [
devMode && dropRequireCacheFunc,
sourceFiles.map(function (file) {
var relPath = node.relativePath(file.fullname);

return [
devMode && 'dropRequireCache(require, require.resolve("' + relPath + '"));',
'require("' + relPath + '");'
].join(EOL);
}).join(EOL)
].join(EOL);
})
.createTech();
112 changes: 80 additions & 32 deletions test/techs/node-js.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require('chai')
.use(require('chai-as-promised'))
.should();

var fs = require('fs'),
path = require('path'),
mock = require('mock-fs'),
Expand All @@ -19,24 +23,33 @@ describe('node-js', function () {

describe('join files', function () {
var scheme = {
blocks: {
'block.vanilla.js': 'global.REQUIRED_TECHS.push("vanilla-js");',
'block.node.js': 'global.REQUIRED_TECHS.push("node-js");'
}
};
blocks: {
'block.vanilla.js': 'global.REQUIRED_TECHS.push("vanilla-js");',
'block.node.js': 'global.REQUIRED_TECHS.push("node-js");'
}
},
targetPath = path.resolve('./bundle/bundle.node.js');

afterEach(function () {
dropRequireCache(require, targetPath);
});

it('must join all files', function () {
return build(scheme)
.then(function () {
globals.must.include('vanilla-js');
globals.must.include('node-js');
require(targetPath);

globals.should.include('vanilla-js');
globals.should.include('node-js');
});
});

it('must join `vanilla.js` file after `node.js` file', function () {
return build(scheme)
.then(function () {
globals.indexOf('node-js').must.be.above(globals.indexOf('vanilla-js'));
require(targetPath);

globals.indexOf('node-js').should.be.above(globals.indexOf('vanilla-js'));
});
});
});
Expand All @@ -58,96 +71,117 @@ describe('node-js', function () {
it('must drop require cache by default', function () {
return build(scheme)
.then(function () {
require(targetPath);
fs.writeFileSync(blockPath, 'global.REQUIRED_TECHS.push("fake-node-js");');

dropRequireCache(require, targetPath);
require(targetPath);

globals.must.include('fake-node-js');
globals.should.include('fake-node-js');
});
});

it('must not drop require cache in production mode', function () {
return build(scheme, { devMode: false })
.then(function () {
require(targetPath);
fs.writeFileSync(blockPath, 'global.REQUIRED_TECHS.push("fake-node-js");');

dropRequireCache(require, targetPath);
require(targetPath);

globals.must.include('node-js');
globals.must.have.length(1);
globals.should.include('node-js');
globals.should.have.length(1);
});
});
});

describe('bundled', function () {
var targetPath = path.resolve('./bundle/bundle.node.js');

afterEach(function () {
dropRequireCache(require, targetPath);
});

it('must run code from bundle', function () {
var scheme = {
blocks: {
'block.vanilla.js': 'global.REQUIRED_TECHS.push("vanilla-js");',
'block.node.js': 'global.REQUIRED_TECHS.push("node-js");'
}
};

return build(scheme, { bundled: true })
.then(function () {
scheme.blocks = {};
require(targetPath);
deleteFolderRecursive('./blocks/');
require(targetPath);

scheme.blocks.must.be.empty;
globals.must.include('node-js');
globals.must.include('vanilla-js');
});
});

it('must not requre base modules', function () {
it('must not reveal node_modules', function () {
var scheme = {
blocks: {
'block.vanilla.js': 'var vow = require("vow");'
'block.node.js': 'global.__fake = require("fake");'
},
// jscs:disable
node_modules: {
vow: {
'index.js': 'module.exports = "vow";'
fake: {
'index.js': 'module.exports = "fake";'
}
}
// jscs:enable
};

return build(scheme, { bundled: true })
.then(function () {
var bundle = fs.readFileSync(targetPath, 'utf-8');
dropRequireCache(require, targetPath);
require(targetPath);

global.__fake.should.be.equal('fake');
});
});

it('must not browserify base node modules', function () {
global.__type = 'fake';
var scheme = {
blocks: {
'block.node.js': 'global.__type = require("os").type();'
}
};

return build(scheme, { bundled: true })
.then(function () {
dropRequireCache(require, targetPath);
require(targetPath);
bundle.must.include('var vow = require(\"vow\");');

global.__type.should.not.equal('fake');
global.__type.should.not.equal('Browser');
});
});

it('must fail if base modules does not exist', function () {
it('must fail if node_modules does not exist', function () {
var scheme = {
blocks: {
'block.vanilla.js': 'var vow = require("vow");'
'block.node.js': 'require("fake");'
},
// jscs:disable
node_modules: {
vow: {
'index.js': 'module.exports = "vow";'
fake: {
'index.js': 'module.exports = "fake";'
}
}
// jscs:enable
};

return build(scheme, { bundled: true })
.then(function () {
var bundle = fs.readFileSync(targetPath, 'utf-8');
dropRequireCache(require, require.resolve('fake'));
deleteFolderRecursive('./node_modules');

dropRequireCache(require, targetPath);
scheme['node_modules'] = {};
require(targetPath);
bundle.must.include('Cannot find module');
(function () {
require(targetPath);
}).should.throw(/fake/);
});
});
});
Expand All @@ -164,5 +198,19 @@ function build(scheme, options) {

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

return bundle.runTechAndRequire(NodeJsTech, options);
return bundle.runTech(NodeJsTech, options);
}

function deleteFolderRecursive (path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file) {
var curPath = path + '/' + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}

0 comments on commit f2e3329

Please sign in to comment.