-
Notifications
You must be signed in to change notification settings - Fork 6
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
Support CommonJS #28
Support CommonJS #28
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,16 @@ | |
* nodeConfig.addTech(require('enb-diverse-js/techs/node-js')); | ||
* ``` | ||
*/ | ||
var EOL = require('os').EOL; | ||
var EOL = require('os').EOL, | ||
browserify = require('browserify'), | ||
promisify = require('vow-node').promisify; | ||
|
||
module.exports = require('enb/lib/build-flow').create() | ||
.name('node-js') | ||
.target('target', '?.node.js') | ||
.useFileList(['vanilla.js', 'node.js']) | ||
.defineOption('devMode', true) | ||
.defineOption('bundled', false) | ||
.builder(function (sourceFiles) { | ||
var node = this.node, | ||
dropRequireCacheFunc = [ | ||
|
@@ -44,6 +48,24 @@ module.exports = require('enb/lib/build-flow').create() | |
].join(EOL), | ||
devMode = this._devMode || ''; | ||
|
||
if (this._bundled) { | ||
var files = sourceFiles.map(function (file) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. относительные пути нужны при любом значении There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tadatuta почему? Мы же их используем только в контексте этой опции. UPD: все, поняла, вынесу. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и то верно |
||
return node.relativePath(file.fullname); | ||
}), | ||
browserifyOptions = { | ||
basedir: node.getDir(), | ||
builtins: [], | ||
bundleExternal: false | ||
}, | ||
renderer = browserify(files, browserifyOptions), | ||
bundle = promisify(renderer.bundle.bind(renderer)); | ||
|
||
return bundle() | ||
.then(function (buf) { | ||
return buf.toString('utf-8'); | ||
}); | ||
} | ||
|
||
return [ | ||
devMode && dropRequireCacheFunc, | ||
sourceFiles.map(function (file) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
test/**/*.test.js | ||
--ui bdd | ||
--reporter dot | ||
--require must |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
require('chai') | ||
.use(require('chai-as-promised')) | ||
.should(); | ||
|
||
var fs = require('fs'), | ||
path = require('path'), | ||
mock = require('mock-fs'), | ||
FileList = require('enb/lib/file-list'), | ||
dropRequireCache = require('enb/lib/fs/drop-require-cache'), | ||
MockNode = require('mock-enb/lib/mock-node'), | ||
NodeJsTech = require('../../techs/node-js'); | ||
NodeJsTech = require('../../techs/node-js'), | ||
rimraf = require('rimraf'); | ||
|
||
describe('node-js', function () { | ||
var globals; | ||
|
@@ -18,30 +23,43 @@ describe('node-js', function () { | |
}); | ||
|
||
describe('join files', function () { | ||
var blocks = { | ||
'block.vanilla.js': 'global.REQUIRED_TECHS.push("vanilla-js");', | ||
'block.node.js': 'global.REQUIRED_TECHS.push("node-js");' | ||
}; | ||
var scheme = { | ||
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(blocks) | ||
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(blocks) | ||
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')); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('devMode', function () { | ||
var blocks = { | ||
'block.node.js': 'global.REQUIRED_TECHS.push("node-js");' | ||
var scheme = { | ||
blocks: { | ||
'block.node.js': 'global.REQUIRED_TECHS.push("node-js");' | ||
} | ||
}, | ||
blockPath = path.resolve('./blocks/block.node.js'), | ||
targetPath = path.resolve('./bundle/bundle.node.js'); | ||
|
@@ -52,37 +70,142 @@ describe('node-js', function () { | |
}); | ||
|
||
it('must drop require cache by default', function () { | ||
return build(blocks) | ||
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(blocks, { devMode: false }) | ||
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.should.include('node-js'); | ||
globals.should.have.length(1); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('bundled', function () { | ||
var targetPath = path.resolve('./bundle/bundle.node.js'); | ||
|
||
afterEach(function () { | ||
global.__fake = ''; | ||
dropRequireCache(require, targetPath); | ||
}); | ||
|
||
it('must run code from bundle in isolation of blocks', 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 () { | ||
rimraf.sync('./blocks/'); | ||
require(targetPath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Этот тест оставляет в памяти закэшированный модуль по пути к There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему |
||
|
||
globals.should.include('node-js'); | ||
globals.should.include('vanilla-js'); | ||
}); | ||
}); | ||
|
||
it('must use CommonJS module', function () { | ||
var scheme = { | ||
blocks: { | ||
'block.node.js': 'global.__fake = require("fake");' | ||
}, | ||
// jscs:disable | ||
node_modules: { | ||
fake: { | ||
'index.js': 'module.exports = "fake";' | ||
} | ||
} | ||
// jscs:enable | ||
}; | ||
|
||
return build(scheme, { bundled: true }) | ||
.then(function () { | ||
dropRequireCache(require, targetPath); | ||
require(targetPath); | ||
|
||
global.__fake.should.be.equal('fake'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Очистку можно вынести в |
||
}) | ||
.then(function () { | ||
dropRequireCache(require, require.resolve('fake')); | ||
}) | ||
.fail(function (err) { | ||
dropRequireCache(require, require.resolve('fake')); | ||
throw err; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут всё же надо подчистить за собой: .always(function () {
dropRequireCache(require, 'path/to/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); | ||
|
||
globals.must.include('node-js'); | ||
globals.must.have.length(1); | ||
global.__type.should.not.equal('fake'); | ||
global.__type.should.not.equal('Browser'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
}); | ||
|
||
it('must not reveal node_modules', function () { | ||
var scheme = { | ||
blocks: { | ||
'block.node.js': 'require("fake");' | ||
}, | ||
// jscs:disable | ||
node_modules: { | ||
fake: { | ||
'index.js': 'module.exports = "fake";' | ||
} | ||
} | ||
// jscs:enable | ||
}, | ||
pathToModule = require.resolve('fake'); | ||
|
||
return build(scheme, { bundled: true }) | ||
.then(function () { | ||
dropRequireCache(require, targetPath); | ||
rimraf.sync('./node_modules'); | ||
|
||
(function () { | ||
require(targetPath); | ||
}).should.throw(/fake/); | ||
}) | ||
.then(function () { | ||
dropRequireCache(require, pathToModule); | ||
}) | ||
.fail(function (err) { | ||
dropRequireCache(require, pathToModule); | ||
throw err; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут тоже надо очистить кэш. |
||
}); | ||
}); | ||
}); | ||
|
||
function build(blocks, options) { | ||
mock({ | ||
blocks: blocks, | ||
bundle: {} | ||
}); | ||
function build(scheme, options) { | ||
scheme.bundle = {}; | ||
mock(scheme); | ||
|
||
var bundle = new MockNode('bundle'), | ||
fileList = new FileList(); | ||
|
@@ -91,5 +214,5 @@ function build(blocks, options) { | |
|
||
bundle.provideTechData('?.files', fileList); | ||
|
||
return bundle.runTechAndRequire(NodeJsTech, options); | ||
return bundle.runTech(NodeJsTech, options); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blond, ты говорил, что для забандливания node.js стоит использовать
webpack
. почему все-таки остановились наbrowsefiry
, он ведь помимо разворачивания депендов еще и пытается добавить код, эмулирующий ноду для браузера?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Присоединяюсь к вопросу @tadatuta
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tadatuta, это настраивается.
Вообще
Browserify
стал получше с тех пор как я на него смотрел для сборки нодового кода. Аwebpack
довольно тяжолый с милионом плагинов даже из коробки, из-за чего сложнее тестировать и немного сложнее поддерживать.Но мне не сильно принципиально, если все захотят
webpack
. Просто именно для этой задачи не вижу особой пользы.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tavriaforever, кажется, что это в режиме по умолчанию, когда он все нативные модули превращает в js-код и подклеивает его. Если выставить нужные опции, чтобы нативные модули и модули из
node_modules
игнорировались, кол-во лишнего кода должно быть примерно одинаковым.@gela-d, проверь, это пожалуйста. Если
browserify
действительно генерит лишнего кода сильно больше даже с опциями — нужно использоватьwebpack
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
так а чем в итоге проверка-то закончилась?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gela-d сколько точно в цифрах?
@tadatuta на глаз было всё ок.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blond а скажите как померить?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Собрать файл, посмотреть на его вес и вычесть вес исходных файлов.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tadatuta @blond разница будет в 200B