├── .gitignore ├── .jscs.js ├── .jshintignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTION.md ├── LICENSE.txt ├── README.md ├── api.en.md ├── api.ru.md ├── package.json ├── techs ├── browser-js.js └── node-js.js └── test ├── .jshintrc ├── mocha.opts └── techs ├── browser-js.test.js └── node-js.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | coverage 4 | -------------------------------------------------------------------------------- /.jscs.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | excludeFiles: [ 3 | 'node_modules', 4 | 'test/fixtures/.enb/tmp', 5 | 'test/fixtures/bundle', 6 | 'coverage' 7 | ], 8 | requireSpaceAfterKeywords: ['if', 'else', 'for', 'while', 'do', 'switch', 'return', 'try', 'catch'], 9 | requireSpaceBeforeBlockStatements: true, 10 | requireSpacesInConditionalExpression: true, 11 | requireSpacesInFunction: { 12 | beforeOpeningCurlyBrace: true 13 | }, 14 | requireSpacesInAnonymousFunctionExpression: { 15 | beforeOpeningRoundBrace: true 16 | }, 17 | disallowSpacesInNamedFunctionExpression: { 18 | beforeOpeningRoundBrace: true 19 | }, 20 | requireMultipleVarDecl: true, 21 | requireBlocksOnNewline: 1, 22 | disallowPaddingNewlinesInBlocks: true, 23 | disallowSpacesInsideArrayBrackets: 'nested', 24 | disallowSpacesInsideParentheses: true, 25 | requireSpacesInsideObjectBrackets: 'all', 26 | disallowQuotedKeysInObjects: 'allButReserved', 27 | disallowSpaceAfterObjectKeys: true, 28 | requireCommaBeforeLineBreak: true, 29 | requireOperatorBeforeLineBreak: true, 30 | disallowSpaceAfterPrefixUnaryOperators: true, 31 | disallowSpaceBeforePostfixUnaryOperators: true, 32 | requireSpaceBeforeBinaryOperators: true, 33 | requireSpaceAfterBinaryOperators: true, 34 | requireCamelCaseOrUpperCaseIdentifiers: true, 35 | disallowKeywords: ['with'], 36 | disallowMultipleLineStrings: true, 37 | disallowMultipleLineBreaks: true, 38 | validateLineBreaks: 'LF', 39 | validateQuoteMarks: { 40 | mark: '\'', 41 | escape: true 42 | }, 43 | disallowMixedSpacesAndTabs: true, 44 | disallowTrailingWhitespace: true, 45 | disallowKeywordsOnNewLine: ['else', 'catch'], 46 | requireLineFeedAtFileEnd: true, 47 | maximumLineLength: 120, 48 | requireCapitalizedConstructors: true, 49 | safeContextKeyword: ['_this'], 50 | disallowYodaConditions: true, 51 | validateJSDoc: { 52 | checkParamNames: true, 53 | checkRedundantParams: true, 54 | requireParamTypes: true 55 | }, 56 | requireSpaceAfterLineComment: true, 57 | disallowNewlineBeforeBlockStatements: true 58 | }; 59 | -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise": true, 3 | "eqeqeq": true, 4 | "forin": true, 5 | "freeze": true, 6 | "immed": true, 7 | "latedef": "nofunc", 8 | "noarg": true, 9 | "noempty": true, 10 | "nonbsp": true, 11 | "nonew": true, 12 | "undef": true, 13 | "unused": true, 14 | 15 | "node": true, 16 | 17 | "expr": true, 18 | "sub": true 19 | } 20 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .* 2 | node_modules 3 | test 4 | coverage 5 | appveyor.yml 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: node_js 4 | 5 | matrix: 6 | include: 7 | - node_js: iojs-v1 8 | - node_js: "0.10" 9 | - node_js: "0.12" 10 | env: COVERALLS=1 11 | 12 | after_success: 13 | - if [ "x$COVERALLS" = "x1" ]; then npm run coveralls; fi 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | История изменений 2 | ================= 3 | 4 | 1.1.1 5 | ----- 6 | 7 | ### Зависимости 8 | 9 | * Модуль `browserify@11.1.0` обновлен до версии `14.3.0`. 10 | * Модуль `enb-source-map@1.8.0` обновлен до версии `1.10.0`. 11 | * Модуль `uglify-js@2.4.24` обновлен до версии `2.8.22`. 12 | * Модуль `vow@0.4.10` обновлен до версии `0.4.15`. 13 | 14 | 1.1.0 15 | ----- 16 | 17 | * Добавлена поддержка `enb` версии `1.x` ([#45]). 18 | 19 | 1.0.0 20 | ----- 21 | 22 | ### Технологии 23 | 24 | * [ __*major*__ ] Удалена технология `vanilla-js` ([#13]). 25 | 26 | ### Новая функциональность 27 | 28 | * [Изоляция кода](README.md#Изоляция-кода-исходных-блоков) ([#6]). 29 | * [Минимизация кода](README.md#Минимизация-кода) ([#30]). 30 | * [Поддержка карт кода](README.md#source-maps) (source maps) ([#12]). 31 | * Сборка `Node.js`-кода в [один файл](api.ru.md#bundled) ([#17]). 32 | * [Поддержка YModules](api.ru.md#includeym) ([#31]) 33 | 34 | ### Опции технологий 35 | 36 | * В технологию [browser-js](api.ru.md#browser-js) добавлена опция [devMode](api.ru.md#devmode) ([#15]) 37 | 38 | ### Остальное 39 | 40 | * [ __*major*__ ] Пакет `enb-diverse-js` переименован в `enb-js` ([#14]). 41 | * Добавлены тесты на технологии `browser-js` и `node-js` ([#7]). 42 | * Обновлен jsdoc ([#23]). 43 | 44 | 0.1.0 45 | ----- 46 | 47 | ### Технологии 48 | 49 | * Добавлена технология `vanilla-js`. 50 | * Добавлена технология `browser-js`. 51 | * Добавлена технология `node-js`. 52 | 53 | [#45]: https://github.com/enb/enb-js/pull/45 54 | [#31]: https://github.com/enb/enb-js/issues/31 55 | [#30]: https://github.com/enb/enb-js/issues/30 56 | [#23]: https://github.com/enb/enb-js/issues/23 57 | [#17]: https://github.com/enb/enb-js/issues/17 58 | [#15]: https://github.com/enb/enb-js/issues/15 59 | [#14]: https://github.com/enb/enb-js/issues/14 60 | [#13]: https://github.com/enb/enb-js/issues/13 61 | [#12]: https://github.com/enb/enb-js/issues/12 62 | [#7]: https://github.com/enb/enb-js/issues/7 63 | [#6]: https://github.com/enb/enb-js/issues/6 64 | -------------------------------------------------------------------------------- /CONTRIBUTION.md: -------------------------------------------------------------------------------- 1 | Контрибьюторам 2 | ============== 3 | 4 | Этот документ описывает некоторые положения о совместной работе над проектом `enb-js`. 5 | 6 | Мейнтейнер проекта — [Андрей Абрамов (@blond)](https://github.com/blond). 7 | 8 | Проект развивается с помощью сообщества. Мейнтейнер мерджит пулл-реквесты, исправляет особо критичные баги. 9 | 10 | Контрибьютеры 11 | ------------- 12 | 13 | Список контрибьютеров данного проекта доступен по ссылке https://github.com/enb/enb-js/graphs/contributors. 14 | 15 | Вы так же можете получить его с помощью команды `git log --pretty=format:"%an <%ae>" | sort -u`. 16 | 17 | Пулл-реквесты 18 | ------------- 19 | 20 | Если вы что-то исправили или добавили в пакет `enb-js`, вы можете отправить пулл-реквест. Он будет рассмотрен мейнтейнером и, либо принят, либо прокомментирован для дальнейшей доработки, либо отклонен. 21 | 22 | Ошибки 23 | ------ 24 | 25 | Если вы нашли ошибку, недоделку, опечатку и вообще любой дефект в `enb-js`, пожалуйста, сообщите об этом, используя github-issues. 26 | И да. Чем подробнее вы опишите ошибку, тем быстрее её смогут воспроизвести и исправить, и тем больше ментейнеры будут вам признательны :) 27 | К сожалению, неизбежны ситуации, когда ошибка будет воспроизводиться только на вашем проекте или в вашем серверном окружении, и ментейнер не сможет не только исправить, а даже воспроизвести её. В таких случаях, мы будем верить, вы сами сможете найти нужное исправление и со временем предложить ментейнеру фикс. 28 | 29 | Фичи 30 | ---- 31 | 32 | Если вы придумали новую фичу, весьма вероятно, что её реализацию вам придётся писать самому. Но вы не постесняйтесь, спросите у ментейнеров имена разработчиков, которые хорошо разбираются в `enb-js` — с ними можно скооперироваться, чтобы реализовать вашу задумку. 33 | Если вы не можете её реализовать сами и не смогли заинтересовать других крутых разработчиков, но фича невероятно крутая — заведите таск в Issues, что-нибудь придумаем. 34 | В крайнем случае объясним вам, что хотеть эту фичу вам не нужно. Или что с ней нужно повременить. Но это в крайнем случае :) 35 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | © YANDEX LLC, 2014 2 | 3 | The Source Code called `enb-js` available at https://github.com/enb/enb-js is subject to the terms of the Mozilla Public License, v. 2.0 (hereinafter - MPL). The text of MPL is the following: 4 | 5 | Mozilla Public License, version 2.0 6 | 7 | 1. Definitions 8 | 9 | 1.1. "Contributor" 10 | 11 | means each individual or legal entity that creates, contributes to the 12 | creation of, or owns Covered Software. 13 | 14 | 1.2. "Contributor Version" 15 | 16 | means the combination of the Contributions of others (if any) used by a 17 | Contributor and that particular Contributor's Contribution. 18 | 19 | 1.3. "Contribution" 20 | 21 | means Covered Software of a particular Contributor. 22 | 23 | 1.4. "Covered Software" 24 | 25 | means Source Code Form to which the initial Contributor has attached the 26 | notice in Exhibit A, the Executable Form of such Source Code Form, and 27 | Modifications of such Source Code Form, in each case including portions 28 | thereof. 29 | 30 | 1.5. "Incompatible With Secondary Licenses" 31 | means 32 | 33 | a. that the initial Contributor has attached the notice described in 34 | Exhibit B to the Covered Software; or 35 | 36 | b. that the Covered Software was made available under the terms of 37 | version 1.1 or earlier of the License, but not also under the terms of 38 | a Secondary License. 39 | 40 | 1.6. "Executable Form" 41 | 42 | means any form of the work other than Source Code Form. 43 | 44 | 1.7. "Larger Work" 45 | 46 | means a work that combines Covered Software with other material, in a 47 | separate file or files, that is not Covered Software. 48 | 49 | 1.8. "License" 50 | 51 | means this document. 52 | 53 | 1.9. "Licensable" 54 | 55 | means having the right to grant, to the maximum extent possible, whether 56 | at the time of the initial grant or subsequently, any and all of the 57 | rights conveyed by this License. 58 | 59 | 1.10. "Modifications" 60 | 61 | means any of the following: 62 | 63 | a. any file in Source Code Form that results from an addition to, 64 | deletion from, or modification of the contents of Covered Software; or 65 | 66 | b. any new file in Source Code Form that contains any Covered Software. 67 | 68 | 1.11. "Patent Claims" of a Contributor 69 | 70 | means any patent claim(s), including without limitation, method, 71 | process, and apparatus claims, in any patent Licensable by such 72 | Contributor that would be infringed, but for the grant of the License, 73 | by the making, using, selling, offering for sale, having made, import, 74 | or transfer of either its Contributions or its Contributor Version. 75 | 76 | 1.12. "Secondary License" 77 | 78 | means either the GNU General Public License, Version 2.0, the GNU Lesser 79 | General Public License, Version 2.1, the GNU Affero General Public 80 | License, Version 3.0, or any later versions of those licenses. 81 | 82 | 1.13. "Source Code Form" 83 | 84 | means the form of the work preferred for making modifications. 85 | 86 | 1.14. "You" (or "Your") 87 | 88 | means an individual or a legal entity exercising rights under this 89 | License. For legal entities, "You" includes any entity that controls, is 90 | controlled by, or is under common control with You. For purposes of this 91 | definition, "control" means (a) the power, direct or indirect, to cause 92 | the direction or management of such entity, whether by contract or 93 | otherwise, or (b) ownership of more than fifty percent (50%) of the 94 | outstanding shares or beneficial ownership of such entity. 95 | 96 | 97 | 2. License Grants and Conditions 98 | 99 | 2.1. Grants 100 | 101 | Each Contributor hereby grants You a world-wide, royalty-free, 102 | non-exclusive license: 103 | 104 | a. under intellectual property rights (other than patent or trademark) 105 | Licensable by such Contributor to use, reproduce, make available, 106 | modify, display, perform, distribute, and otherwise exploit its 107 | Contributions, either on an unmodified basis, with Modifications, or 108 | as part of a Larger Work; and 109 | 110 | b. under Patent Claims of such Contributor to make, use, sell, offer for 111 | sale, have made, import, and otherwise transfer either its 112 | Contributions or its Contributor Version. 113 | 114 | 2.2. Effective Date 115 | 116 | The licenses granted in Section 2.1 with respect to any Contribution 117 | become effective for each Contribution on the date the Contributor first 118 | distributes such Contribution. 119 | 120 | 2.3. Limitations on Grant Scope 121 | 122 | The licenses granted in this Section 2 are the only rights granted under 123 | this License. No additional rights or licenses will be implied from the 124 | distribution or licensing of Covered Software under this License. 125 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 126 | Contributor: 127 | 128 | a. for any code that a Contributor has removed from Covered Software; or 129 | 130 | b. for infringements caused by: (i) Your and any other third party's 131 | modifications of Covered Software, or (ii) the combination of its 132 | Contributions with other software (except as part of its Contributor 133 | Version); or 134 | 135 | c. under Patent Claims infringed by Covered Software in the absence of 136 | its Contributions. 137 | 138 | This License does not grant any rights in the trademarks, service marks, 139 | or logos of any Contributor (except as may be necessary to comply with 140 | the notice requirements in Section 3.4). 141 | 142 | 2.4. Subsequent Licenses 143 | 144 | No Contributor makes additional grants as a result of Your choice to 145 | distribute the Covered Software under a subsequent version of this 146 | License (see Section 10.2) or under the terms of a Secondary License (if 147 | permitted under the terms of Section 3.3). 148 | 149 | 2.5. Representation 150 | 151 | Each Contributor represents that the Contributor believes its 152 | Contributions are its original creation(s) or it has sufficient rights to 153 | grant the rights to its Contributions conveyed by this License. 154 | 155 | 2.6. Fair Use 156 | 157 | This License is not intended to limit any rights You have under 158 | applicable copyright doctrines of fair use, fair dealing, or other 159 | equivalents. 160 | 161 | 2.7. Conditions 162 | 163 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 164 | Section 2.1. 165 | 166 | 167 | 3. Responsibilities 168 | 169 | 3.1. Distribution of Source Form 170 | 171 | All distribution of Covered Software in Source Code Form, including any 172 | Modifications that You create or to which You contribute, must be under 173 | the terms of this License. You must inform recipients that the Source 174 | Code Form of the Covered Software is governed by the terms of this 175 | License, and how they can obtain a copy of this License. You may not 176 | attempt to alter or restrict the recipients' rights in the Source Code 177 | Form. 178 | 179 | 3.2. Distribution of Executable Form 180 | 181 | If You distribute Covered Software in Executable Form then: 182 | 183 | a. such Covered Software must also be made available in Source Code Form, 184 | as described in Section 3.1, and You must inform recipients of the 185 | Executable Form how they can obtain a copy of such Source Code Form by 186 | reasonable means in a timely manner, at a charge no more than the cost 187 | of distribution to the recipient; and 188 | 189 | b. You may distribute such Executable Form under the terms of this 190 | License, or sublicense it under different terms, provided that the 191 | license for the Executable Form does not attempt to limit or alter the 192 | recipients' rights in the Source Code Form under this License. 193 | 194 | 3.3. Distribution of a Larger Work 195 | 196 | You may create and distribute a Larger Work under terms of Your choice, 197 | provided that You also comply with the requirements of this License for 198 | the Covered Software. If the Larger Work is a combination of Covered 199 | Software with a work governed by one or more Secondary Licenses, and the 200 | Covered Software is not Incompatible With Secondary Licenses, this 201 | License permits You to additionally distribute such Covered Software 202 | under the terms of such Secondary License(s), so that the recipient of 203 | the Larger Work may, at their option, further distribute the Covered 204 | Software under the terms of either this License or such Secondary 205 | License(s). 206 | 207 | 3.4. Notices 208 | 209 | You may not remove or alter the substance of any license notices 210 | (including copyright notices, patent notices, disclaimers of warranty, or 211 | limitations of liability) contained within the Source Code Form of the 212 | Covered Software, except that You may alter any license notices to the 213 | extent required to remedy known factual inaccuracies. 214 | 215 | 3.5. Application of Additional Terms 216 | 217 | You may choose to offer, and to charge a fee for, warranty, support, 218 | indemnity or liability obligations to one or more recipients of Covered 219 | Software. However, You may do so only on Your own behalf, and not on 220 | behalf of any Contributor. You must make it absolutely clear that any 221 | such warranty, support, indemnity, or liability obligation is offered by 222 | You alone, and You hereby agree to indemnify every Contributor for any 223 | liability incurred by such Contributor as a result of warranty, support, 224 | indemnity or liability terms You offer. You may include additional 225 | disclaimers of warranty and limitations of liability specific to any 226 | jurisdiction. 227 | 228 | 4. Inability to Comply Due to Statute or Regulation 229 | 230 | If it is impossible for You to comply with any of the terms of this License 231 | with respect to some or all of the Covered Software due to statute, 232 | judicial order, or regulation then You must: (a) comply with the terms of 233 | this License to the maximum extent possible; and (b) describe the 234 | limitations and the code they affect. Such description must be placed in a 235 | text file included with all distributions of the Covered Software under 236 | this License. Except to the extent prohibited by statute or regulation, 237 | such description must be sufficiently detailed for a recipient of ordinary 238 | skill to be able to understand it. 239 | 240 | 5. Termination 241 | 242 | 5.1. The rights granted under this License will terminate automatically if You 243 | fail to comply with any of its terms. However, if You become compliant, 244 | then the rights granted under this License from a particular Contributor 245 | are reinstated (a) provisionally, unless and until such Contributor 246 | explicitly and finally terminates Your grants, and (b) on an ongoing 247 | basis, if such Contributor fails to notify You of the non-compliance by 248 | some reasonable means prior to 60 days after You have come back into 249 | compliance. Moreover, Your grants from a particular Contributor are 250 | reinstated on an ongoing basis if such Contributor notifies You of the 251 | non-compliance by some reasonable means, this is the first time You have 252 | received notice of non-compliance with this License from such 253 | Contributor, and You become compliant prior to 30 days after Your receipt 254 | of the notice. 255 | 256 | 5.2. If You initiate litigation against any entity by asserting a patent 257 | infringement claim (excluding declaratory judgment actions, 258 | counter-claims, and cross-claims) alleging that a Contributor Version 259 | directly or indirectly infringes any patent, then the rights granted to 260 | You by any and all Contributors for the Covered Software under Section 261 | 2.1 of this License shall terminate. 262 | 263 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 264 | license agreements (excluding distributors and resellers) which have been 265 | validly granted by You or Your distributors under this License prior to 266 | termination shall survive termination. 267 | 268 | 6. Disclaimer of Warranty 269 | 270 | Covered Software is provided under this License on an "as is" basis, 271 | without warranty of any kind, either expressed, implied, or statutory, 272 | including, without limitation, warranties that the Covered Software is free 273 | of defects, merchantable, fit for a particular purpose or non-infringing. 274 | The entire risk as to the quality and performance of the Covered Software 275 | is with You. Should any Covered Software prove defective in any respect, 276 | You (not any Contributor) assume the cost of any necessary servicing, 277 | repair, or correction. This disclaimer of warranty constitutes an essential 278 | part of this License. No use of any Covered Software is authorized under 279 | this License except under this disclaimer. 280 | 281 | 7. Limitation of Liability 282 | 283 | Under no circumstances and under no legal theory, whether tort (including 284 | negligence), contract, or otherwise, shall any Contributor, or anyone who 285 | distributes Covered Software as permitted above, be liable to You for any 286 | direct, indirect, special, incidental, or consequential damages of any 287 | character including, without limitation, damages for lost profits, loss of 288 | goodwill, work stoppage, computer failure or malfunction, or any and all 289 | other commercial damages or losses, even if such party shall have been 290 | informed of the possibility of such damages. This limitation of liability 291 | shall not apply to liability for death or personal injury resulting from 292 | such party's negligence to the extent applicable law prohibits such 293 | limitation. Some jurisdictions do not allow the exclusion or limitation of 294 | incidental or consequential damages, so this exclusion and limitation may 295 | not apply to You. 296 | 297 | 8. Litigation 298 | 299 | Any litigation relating to this License may be brought only in the courts 300 | of a jurisdiction where the defendant maintains its principal place of 301 | business and such litigation shall be governed by laws of that 302 | jurisdiction, without reference to its conflict-of-law provisions. Nothing 303 | in this Section shall prevent a party's ability to bring cross-claims or 304 | counter-claims. 305 | 306 | 9. Miscellaneous 307 | 308 | This License represents the complete agreement concerning the subject 309 | matter hereof. If any provision of this License is held to be 310 | unenforceable, such provision shall be reformed only to the extent 311 | necessary to make it enforceable. Any law or regulation which provides that 312 | the language of a contract shall be construed against the drafter shall not 313 | be used to construe this License against a Contributor. 314 | 315 | 316 | 10. Versions of the License 317 | 318 | 10.1. New Versions 319 | 320 | Mozilla Foundation is the license steward. Except as provided in Section 321 | 10.3, no one other than the license steward has the right to modify or 322 | publish new versions of this License. Each version will be given a 323 | distinguishing version number. 324 | 325 | 10.2. Effect of New Versions 326 | 327 | You may distribute the Covered Software under the terms of the version 328 | of the License under which You originally received the Covered Software, 329 | or under the terms of any subsequent version published by the license 330 | steward. 331 | 332 | 10.3. Modified Versions 333 | 334 | If you create software not governed by this License, and you want to 335 | create a new license for such software, you may create and use a 336 | modified version of this License if you rename the license and remove 337 | any references to the name of the license steward (except to note that 338 | such modified license differs from this License). 339 | 340 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 341 | Licenses If You choose to distribute Source Code Form that is 342 | Incompatible With Secondary Licenses under the terms of this version of 343 | the License, the notice described in Exhibit B of this License must be 344 | attached. 345 | 346 | Exhibit A - Source Code Form License Notice 347 | 348 | This Source Code Form is subject to the 349 | terms of the Mozilla Public License, v. 350 | 2.0. If a copy of the MPL was not 351 | distributed with this file, You can 352 | obtain one at 353 | http://mozilla.org/MPL/2.0/. 354 | 355 | If it is not possible or desirable to put the notice in a particular file, 356 | then You may include the notice in a location (such as a LICENSE file in a 357 | relevant directory) where a recipient would be likely to look for such a 358 | notice. 359 | 360 | You may add additional accurate notices of copyright ownership. 361 | 362 | Exhibit B - "Incompatible With Secondary Licenses" Notice 363 | 364 | This Source Code Form is "Incompatible 365 | With Secondary Licenses", as defined by 366 | the Mozilla Public License, v. 2.0. 367 | 368 | 369 | A copy of the MPL is also available at http://mozilla.org/MPL/2.0/. 370 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | enb-js 2 | ====== 3 | 4 | [![NPM version](https://img.shields.io/npm/v/enb-js.svg?style=flat)](https://www.npmjs.org/package/enb-js) 5 | [![Build Status](https://img.shields.io/travis/enb/enb-js/master.svg?style=flat&label=tests)](https://travis-ci.org/enb/enb-js) 6 | [![Coverage Status](https://img.shields.io/coveralls/enb/enb-js.svg?style=flat)](https://coveralls.io/r/enb/enb-js?branch=master) 7 | [![Dependency Status](https://img.shields.io/david/enb/enb-js.svg?style=flat)](https://david-dm.org/enb/enb-js) 8 | 9 | Пакет предоставляет набор [ENB](https://ru.bem.info/tools/bem/enb-bem/)-технологий для сборки 10 | JavaScript-кода в проектах, построенных по [методологии БЭМ](https://ru.bem.info/method/). 11 | 12 | **Технологии пакета `enb-js`:** 13 | 14 | * [browser-js](api.ru.md#browser-js) 15 | * [node-js](api.ru.md#node-js) 16 | 17 | Принципы работы технологий и их API описаны в документе [API технологий](api.ru.md). 18 | 19 | ## Установка 20 | 21 | Установите пакет `enb-js`: 22 | 23 | ```sh 24 | $ npm install --save-dev enb-js 25 | ``` 26 | 27 | **Требования:** зависимость от пакета `enb` версии `0.16.0` или выше. 28 | 29 | 30 | - [Быстрый старт](#Быстрый-старт) 31 | - [Особенности технологий пакета](#Особенности-технологий-пакета) 32 | - [Изоморфный JavaScript](#Изоморфный-javascript) 33 | - [Отладка кода](#Отладка-кода) 34 | - [Source Maps](#source-maps) 35 | - [Режим разработки для Node.js](#Режим-разработки-для-nodejs) 36 | - [Изоляция кода исходных блоков](#Изоляция-кода-исходных-блоков) 37 | - [Минимизация кода](#Минимизация-кода) 38 | - [Лицензия](#Лицензия) 39 | 40 | 41 | 42 | 43 | Быстрый старт 44 | ------------- 45 | 46 | Чтобы собрать JS-код для исполнения в браузере, подключите технологию [browser-js](api.ru.md#browser-js), а для работы в `Node.js` — [node-js](api.ru.md#node-js). 47 | 48 | ```js 49 | var BrowserJsTech = require('enb-js/techs/browser-js'), 50 | NodeJsTech = require('enb-js/techs/node-js'), 51 | FileProvideTech = require('enb/techs/file-provider'), 52 | bemTechs = require('enb-bem-techs'); 53 | 54 | module.exports = function(config) { 55 | config.node('bundle', function(node) { 56 | // Получение списка файлов для сборки 57 | node.addTechs([ 58 | [FileProvideTech, { target: '?.bemdecl.js' }], 59 | [bemTechs.levels, { levels: ['blocks'] }], 60 | [bemTechs.deps], 61 | [bemTechs.files] 62 | ]); 63 | 64 | // Сборка JS-файл для работы в браузере 65 | node.addTech([BrowserJsTech, { /* опции технологии */ }]); 66 | node.addTarget('?.browser.js'); 67 | 68 | // Сборка JS-файл для работы в Node.js 69 | node.addTech([NodeJsTech, { /* опции технологии */ }]); 70 | node.addTarget('?.browser.js'); 71 | }); 72 | }; 73 | ``` 74 | 75 | Особенности технологий пакета 76 | ----------------------------- 77 | 78 | ### Изоморфный JavaScript 79 | 80 | Термин «изоморфность» означает возможность выполнения одного и того же кода как в браузере, так и в `Node.js`. 81 | 82 | Предполагается, что в файловой системе код блоков разделяется по файлам с разными расширениями, которые обозначают в какой среде может работать данный код: 83 | 84 | * `*.vanilla.js` — файлы, код которых может работать как в бразуере, так и в `Node.js`. 85 | * `*.js` и `*.browser.js` — файлы, код которых может работать только в браузере. 86 | * `*.node.js` — файлы, код которых может работать только в `Node.js`. 87 | 88 | ### Отладка кода 89 | 90 | #### Source Maps 91 | 92 | Для удобной отладки кода в браузере следует включить генерацию карт кода с помощью опции [sourcemaps](api.ru.md#sourcemaps). 93 | 94 | #### Режим разработки для Node.js 95 | 96 | Для сборки файлов, предназначенных для исполнения в Node.js, существует специальный режим, удобный для разработки. 97 | 98 | После сборки итоговый файл не содержит исходного кода блоков, а лишь подключает необходимые файлы с помощью `require`. 99 | 100 | ```js 101 | require('../blocks/utils/utils.node.js'); 102 | require('../blocks/page/page.node.js'); 103 | ``` 104 | 105 | При таком подходе сообщения об ошибках будут указывать на код из исходных файлов. Кроме того, при каждом выполнении собранного файла кэш Node.js не будет учитываться. Это позволяет видеть изменения без перезапуска Node.js. 106 | 107 | Настроить сборку для выкладки в production можно с помощью опций [devMode](api.ru.md#devmode) или [bundled](api.ru.md#bundled). 108 | 109 | ### Изоляция кода исходных блоков 110 | 111 | При обычной сборке JS-кода, предназначенного для работы в браузере, один блок может некорректно влиять на работу другого блока. 112 | 113 | ```js 114 | /* begin: blocks/greeting/greeting.js */ 115 | var message = 'Кукусики!'; 116 | 117 | onStart(function () { 118 | alert(message); 119 | }); 120 | /* end: blocks/greeting/greeting.js */ 121 | /* begin: blocks/parting/parting.js */ 122 | var message = 'Я буду скучать...'; 123 | 124 | onEnd(function () { 125 | alert(message); 126 | }); 127 | /* end: blocks/parting/parting.js */ 128 | ``` 129 | 130 | Чтобы исключить подобное влияние, следует использовать опцию [iife](api.ru.md#iife), которая обернет код каждого исходного файла с помощью [IIFE](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression). 131 | 132 | ### Минимизация кода 133 | 134 | Для минимизации JS-кода используется [UglifyJS](https://github.com/mishoo/UglifyJS2). 135 | 136 | Включить минимизацию можно с помощью опции [compress](api.ru.md#compress). 137 | 138 | Лицензия 139 | -------- 140 | 141 | © 2014 YANDEX LLC. Код лицензирован [Mozilla Public License 2.0](LICENSE.txt). 142 | -------------------------------------------------------------------------------- /api.en.md: -------------------------------------------------------------------------------- 1 | # Technologies API 2 | 3 | The package includes the following technologies: 4 | 5 | * [browser-js](#browser-js) – JavaScript build for working in the browser. 6 | * [node-js](#node-js) – JavaScript build for working in Node.js. 7 | 8 | ## browser-js 9 | 10 | Builds the source JS files for blocks to be used for working in the browser. 11 | 12 | The build includes the files with code that will work in any runtime environment (with the extension `.vanilla.js`), as well as files with code that will only work in the browser (with the extension `.browser.js` or `.js`). 13 | 14 | ### Options 15 | 16 | Options are specified in the `.enb/ make.js` configuration file. 17 | 18 | * [target](#target) 19 | * [filesTarget](#filestarget) 20 | * [sourceSuffixes](#sourcesuffixes) 21 | * [iife](#iife) 22 | * [compress](#compress) 23 | * [sourcemap](#sourcemap) 24 | * [includeYM](#includeym) 25 | 26 | ### target 27 | 28 | Type: `String`. Default: `?.browser.js`. 29 | 30 | The name of the file for saving the result of building the necessary JS files for the project – the compiled `?.browser.js file.` 31 | 32 | #### filesTarget 33 | 34 | Type: `String`. Default: `?.files`. 35 | 36 | The name of the target for accessing the list of source files for the build. The file list is provided by the [files](https://github.com/enb/enb-bem-techs/blob/master/docs/api/api.en.md#files) technology in the [enb-bem-techs](https://github.com/enb/enb-bem-techs/blob/master/README.md) package. 37 | 38 | #### sourceSuffixes 39 | 40 | Type: `String | String[]`. Default: `['vanilla.js', 'js', 'browser.js']`. 41 | 42 | Suffixes of files for filtering out files with JS code. 43 | 44 | #### iife 45 | 46 | Type: `Boolean`. Default: `false`. 47 | 48 | Isolates the code of the blocks by wrapping the code of each file in [IIFE](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression). 49 | 50 | #### compress 51 | 52 | Type: `Boolean`. Default: `false`. 53 | 54 | Minimizing the resulting JS code using [UglifyJS](https://github.com/mishoo/UglifyJS2). 55 | 56 | #### sourcemap 57 | 58 | Type: `Boolean`. Default: `false`. 59 | 60 | Creates source maps with information about the source files. 61 | 62 | The maps are included in a compiled file called `? .files` – they are not stored in separate files with a `.map` extension. 63 | 64 | #### includeYM 65 | 66 | Type: `Boolean`. Default: `false`. 67 | 68 | Adds the [ YModules](https://github.com/ymaps/modules/blob/master/README.md) code at the beginning of the file. 69 | 70 | **Example** 71 | 72 | ```js 73 | /* Code for blocks in the file system before the build 74 | * blocks/ 75 | * ├── block.vanilla.js 76 | * └── block.browser.js 77 | * └── block.js 78 | * 79 | * After the build 80 | * bundle/ 81 | * └── bundle.browser.js 82 | */ 83 | 84 | var BrowserJsTech = require('enb-js/techs/browser-js'), 85 | FileProvideTech = require('enb/techs/file-provider'), 86 | bemTechs = require('enb-bem-techs'); 87 | 88 | module.exports = function(config) { 89 | config.node('bundle', function(node) { 90 | // Getting the list of files for the build 91 | node.addTechs([ 92 | [FileProvideTech, { target: '?.bemdecl.js' }], 93 | [bemTechs.levels, { levels: ['blocks'] }], 94 | [bemTechs.deps], 95 | [bemTechs.files] 96 | ]); 97 | 98 | // Compiling the JS file for working in the browser 99 | node.addTech(BrowserJsTech); 100 | node.addTarget('?.browser.js'); 101 | }); 102 | }; 103 | ``` 104 | 105 | ## node-js 106 | 107 | Builds the source JS files for blocks to be used for working in Node.js. 108 | 109 | The build includes the files with code that will work in any runtime environment (with the extension `.vanilla.js`), as well as files with code that will only work in Node.js (with the extension `.node.js`). 110 | 111 | By default, all source files are connected using `require`. The paths are processed relative to the file that specifies `require`. 112 | 113 | For the build to work correctly, all the source files must exist. To assemble the code into a single independent file (which does not require the source files to be present), use the [bundled](#bundled) option. 114 | 115 | ### Options 116 | 117 | Options are specified in the `.enb/make.js` configuration file. 118 | 119 | * [target](#target-1) 120 | * [filesTarget](#filestarget-1) 121 | * [sourceSuffixes](#sourcesuffixes-1) 122 | * [devMode](#devmode) 123 | * [bundled](#bundled) 124 | * [includeYM](#includeym-1) 125 | 126 | #### target 127 | 128 | Type: `String`. Default: `?.node.js`. 129 | 130 | The name of the file for saving the build result of the necessary project JS files – the compiled `?.node.js` file. 131 | 132 | #### filesTarget 133 | 134 | Type: `String`. Default: `?.files`. 135 | 136 | The name of the target for accessing the list of source files for the build. The file list is provided by the [files](https://github.com/enb/enb-bem-techs/blob/master/docs/api/api.en.md#files) technology in the [enb-bem-techs](https://github.com/enb/enb-bem-techs/blob/master/README.md) package. 137 | 138 | #### sourceSuffixes 139 | 140 | Type: `String | String[]`. Default: `['vanilla.js', 'node.js']`. 141 | 142 | Suffixes of files for filtering out files with JS code. 143 | 144 | #### devMode 145 | 146 | Type: `Boolean`. Default: `true`. 147 | 148 | Build mode for development. The Node.js cache is not taken into account for each build. This allows you to see changes without restarting Node.js. 149 | 150 | #### bundled 151 | 152 | Type: `Boolean`. Default: `false`. 153 | 154 | Compiles all source files into one file. 155 | 156 | When using this option, there is no need to store the source JS files for executing the compiled file. 157 | 158 | #### includeYM 159 | 160 | Type: `Boolean`. Default: `false`. 161 | 162 | Provides [YModules](https://github.com/ymaps/modules/blob/master/README.md) to the `modules` global variable. 163 | 164 | **Example** 165 | 166 | ```js 167 | /* Код блоков в файловой системе до сборки 168 | * blocks/ 169 | * ├── block.vanilla.js 170 | * └── block.node.js 171 | * 172 | * После сбоки 173 | * bundle/ 174 | * └── bundle.node.js 175 | */ 176 | 177 | var NodeJsTech = require('enb-js/techs/node-js'), 178 | FileProvideTech = require('enb/techs/file-provider'), 179 | bemTechs = require('enb-bem-techs'); 180 | 181 | module.exports = function(config) { 182 | config.node('bundle', function(node) { 183 | // Getting the list of files for the build 184 | node.addTechs([ 185 | [FileProvideTech, { target: '?.bemdecl.js' }], 186 | [bemTechs.levels, { levels: ['blocks'] }], 187 | [bemTechs.deps], 188 | [bemTechs.files] 189 | ]); 190 | 191 | // Compiling the JS file for working in Node.js 192 | node.addTech(NodeJsTech); 193 | node.addTarget('?.node.js'); 194 | }); 195 | }; 196 | ``` 197 | -------------------------------------------------------------------------------- /api.ru.md: -------------------------------------------------------------------------------- 1 | # API технологий 2 | 3 | Пакет предоставляет следующие технологии: 4 | 5 | * [browser-js](#browser-js) — сборка JS-кода для работы в браузере. 6 | * [node-js](#node-js) — сборка JS-кода для работы в Node.js. 7 | 8 | ## browser-js 9 | 10 | Собирает исходные JS-файлы блоков, предназначенные для работы в браузере. 11 | 12 | В сборку попадают как файлы, код которых будет работать в любой среде исполнения (с расширением `.vanilla.js`), так и файлы, код которых может работать только в браузере (с расширением `.browser.js` или `.js`). 13 | 14 | ### Опции 15 | 16 | Опции указываются в конфигурационном файле `.enb/make.js`. 17 | 18 | * [target](#target) 19 | * [filesTarget](#filestarget) 20 | * [sourceSuffixes](#sourcesuffixes) 21 | * [iife](#iife) 22 | * [compress](#compress) 23 | * [sourcemap](#sourcemap) 24 | * [includeYM](#includeym) 25 | 26 | ### target 27 | 28 | Тип: `String`. По умолчанию: `?.browser.js`. 29 | 30 | Имя файла, куда будет записан результат сборки необходимых JS-файлов проекта — 31 | скомпилированный файл `?.browser.js` 32 | 33 | #### filesTarget 34 | 35 | Тип: `String`. По умолчанию: `?.files`. 36 | 37 | Имя таргета, откуда будет доступен список исходных файлов для сборки. Список файлов предоставляет технология [files](https://github.com/enb/enb-bem-techs/blob/master/docs/api/api.ru.md#files) пакета [enb-bem-techs](https://github.com/enb/enb-bem-techs/blob/master/README.md). 38 | 39 | #### sourceSuffixes 40 | 41 | Тип: `String | String[]`. По умолчанию: `['vanilla.js', 'js', 'browser.js']`. 42 | 43 | Суффиксы файлов, по которым отбираются файлы с JS-кодом. 44 | 45 | #### iife 46 | 47 | Тип: `Boolean`. По умолчанию: `false`. 48 | 49 | Изолирует код блоков друг от друга, оборачивая код каждого файла в [IIFE](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression). 50 | 51 | #### compress 52 | 53 | Тип: `Boolean`. По умолчанию: `false`. 54 | 55 | Минимизация итогового JS-кода с помощью [UglifyJS](https://github.com/mishoo/UglifyJS2). 56 | 57 | #### sourcemap 58 | 59 | Тип: `Boolean`. По умолчанию: `false`. 60 | 61 | Построение карт кода (source maps) с информацией об исходных файлах. 62 | 63 | Карты встраиваются в скомпилированный файл `?.files`, а не хранятся в отдельном файле с расширением `.map`. 64 | 65 | #### includeYM 66 | 67 | Тип: `Boolean`. По умолчанию: `false`. 68 | 69 | Добавляет код [YModules](https://github.com/ymaps/modules/blob/master/README.ru.md) в начало файла. 70 | 71 | **Пример** 72 | 73 | ```js 74 | /* Код блоков в файловой системе до сборки 75 | * blocks/ 76 | * ├── block.vanilla.js 77 | * └── block.browser.js 78 | * └── block.js 79 | * 80 | * После сбоки 81 | * bundle/ 82 | * └── bundle.browser.js 83 | */ 84 | 85 | var BrowserJsTech = require('enb-js/techs/browser-js'), 86 | FileProvideTech = require('enb/techs/file-provider'), 87 | bemTechs = require('enb-bem-techs'); 88 | 89 | module.exports = function(config) { 90 | config.node('bundle', function(node) { 91 | // Получение списка файлов для сборки 92 | node.addTechs([ 93 | [FileProvideTech, { target: '?.bemdecl.js' }], 94 | [bemTechs.levels, { levels: ['blocks'] }], 95 | [bemTechs.deps], 96 | [bemTechs.files] 97 | ]); 98 | 99 | // Сборка JS-файл для работы в браузере 100 | node.addTech(BrowserJsTech); 101 | node.addTarget('?.browser.js'); 102 | }); 103 | }; 104 | ``` 105 | 106 | ## node-js 107 | 108 | Собирает исходные JS-файлы блоков, предназначенные для работы в Node.js. 109 | 110 | В сборку попадают как файлы, код которых будет работать в любой среде исполнения (с расширением `.vanilla.js`), так и файлы, код которых может работать только в Node.js (с расширением `.node.js`). 111 | 112 | По умолчанию все исходные файлы подключаются с помощью `require`. Пути обрабатываются относительно того файла, в котором написан `require`. 113 | 114 | Для корректной работы требуется наличие всех исходных файлов. Чтобы собрать код в один независимый файл (не требующий наличия исходных файлов), необходимо использовать опцию [bundled](#bundled). 115 | 116 | ### Опции 117 | 118 | Опции указываются в конфигурационном файле `.enb/make.js`. 119 | 120 | * [target](#target-1) 121 | * [filesTarget](#filestarget-1) 122 | * [sourceSuffixes](#sourcesuffixes-1) 123 | * [devMode](#devmode) 124 | * [bundled](#bundled) 125 | * [includeYM](#includeym-1) 126 | 127 | #### target 128 | 129 | Тип: `String`. По умолчанию: `?.node.js`. 130 | 131 | Имя файла, куда будет записан результат сборки необходимых JS-файлов проекта — 132 | скомпилированный файл `?.node.js`. 133 | 134 | #### filesTarget 135 | 136 | Тип: `String`. По умолчанию: `?.files`. 137 | 138 | Имя таргета, откуда будет доступен список исходных файлов для сборки. Список файлов предоставляет технология [files](https://github.com/enb/enb-bem-techs/blob/master/docs/api/api.ru.md#files) пакета [enb-bem-techs](https://github.com/enb/enb-bem-techs/blob/master/README.md). 139 | 140 | #### sourceSuffixes 141 | 142 | Тип: `String | String[]`. По умолчанию: `['vanilla.js', 'node.js']`. 143 | 144 | Суффиксы файлов, по которым отбираются файлы с JS-кодом. 145 | 146 | #### devMode 147 | 148 | Тип: `Boolean`. По умолчанию: `true`. 149 | 150 | Режим сборки, предназначенный для разработки. При каждом выполнении кэш Node.js не будет учитываться. Это позволяет видеть изменения без перезапуска Node.js. 151 | 152 | #### bundled 153 | 154 | Тип: `Boolean`. По умолчанию: `false`. 155 | 156 | Собирает все исходные файлы в один файл. 157 | 158 | При использовании этой опции отпадает необходимость в хранении исходных JS-файлов для выполнения собранного файла. 159 | 160 | #### includeYM 161 | 162 | Тип: `Boolean`. По умолчанию: `false`. 163 | 164 | Предоставляет [YModules](https://github.com/ymaps/modules/blob/master/README.ru.md) в глобальную переменную `modules`. 165 | 166 | **Пример** 167 | 168 | ```js 169 | /* Код блоков в файловой системе до сборки 170 | * blocks/ 171 | * ├── block.vanilla.js 172 | * └── block.node.js 173 | * 174 | * После сбоки 175 | * bundle/ 176 | * └── bundle.node.js 177 | */ 178 | 179 | var NodeJsTech = require('enb-js/techs/node-js'), 180 | FileProvideTech = require('enb/techs/file-provider'), 181 | bemTechs = require('enb-bem-techs'); 182 | 183 | module.exports = function(config) { 184 | config.node('bundle', function(node) { 185 | // Получение списка файлов для сборки 186 | node.addTechs([ 187 | [FileProvideTech, { target: '?.bemdecl.js' }], 188 | [bemTechs.levels, { levels: ['blocks'] }], 189 | [bemTechs.deps], 190 | [bemTechs.files] 191 | ]); 192 | 193 | // Сборка JS-файл для работы в Node.js 194 | node.addTech(NodeJsTech); 195 | node.addTarget('?.node.js'); 196 | }); 197 | }; 198 | ``` 199 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Andrew Abramov ", 3 | "description": "JS techs for ENB", 4 | "name": "enb-js", 5 | "version": "1.1.1", 6 | "repository": "git://github.com/enb/enb-js", 7 | "homepage": "https://github.com/enb/enb-js", 8 | "bugs": "https://github.com/enb/enb-js/issues", 9 | "licenses": [ 10 | { 11 | "type": "MPL-2.0", 12 | "url": "https://github.com/enb-bem/enb-js/blob/master/LICENSE.txt" 13 | } 14 | ], 15 | "contributors": [ 16 | { 17 | "name": "Andrew Abramov", 18 | "email": "andrewblond@yandex.com" 19 | }, 20 | { 21 | "name": "Andrew Kuznetsov", 22 | "email": "andrey.kuznetsov48@yandex.ru" 23 | } 24 | ], 25 | "engines": { 26 | "node": ">= 0.8.0" 27 | }, 28 | "peerDependencies": { 29 | "enb": ">= 0.15.0 <2.0.0" 30 | }, 31 | "dependencies": { 32 | "browserify": "14.3.0", 33 | "enb-source-map": "1.10.0", 34 | "uglify-js": "2.8.22", 35 | "vow": "0.4.15", 36 | "vow-node": "0.3.0", 37 | "ym": "0.1.2" 38 | }, 39 | "devDependencies": { 40 | "chai": "3.2.0", 41 | "chai-as-promised": "5.1.0", 42 | "chai-string": "1.1.2", 43 | "enb": ">= 0.15.0 <2.0.0", 44 | "istanbul": "0.3.20", 45 | "jscs": "1.13.1", 46 | "jshint": "2.8.0", 47 | "mocha": "2.3.2", 48 | "mock-enb": "0.3.0", 49 | "mock-fs": "3.2.0", 50 | "rimraf": "2.4.3" 51 | }, 52 | "scripts": { 53 | "test": "npm run lint && npm run unit", 54 | "lint": "jshint . && jscs -c .jscs.js .", 55 | "unit": "mocha -R spec", 56 | "cover": "istanbul cover _mocha", 57 | "coveralls": "npm i coveralls && npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls" 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /techs/browser-js.js: -------------------------------------------------------------------------------- 1 | var vow = require('vow'), 2 | enb = require('enb'), 3 | vfs = enb.asyncFS || require('enb/lib/fs/async-fs'), 4 | buildFlow = enb.buildFlow || require('enb/lib/build-flow'), 5 | utils = require('enb-source-map/lib/utils'), 6 | File = require('enb-source-map/lib/file'), 7 | minify = require('uglify-js').minify; 8 | 9 | /** 10 | * @class BrowserJsTech 11 | * @augments {BaseTech} 12 | * @classdesc 13 | * 14 | * Builds JavaScript files for browsers.

15 | * 16 | * Compiles any files which contains JavaScript code for browsers: 17 | * `vanilla.js` – isomorphic code, which can be used on client side and server side; 18 | * `js` and `browser.js` – code, which could be used only on client side. 19 | * 20 | * @param {Object} [options] Options 21 | * @param {String} [options.target='?.browser.js'] Path to compiled file. 22 | * @param {String} [options.filesTarget='?.files'] Path to a target with FileList
23 | * {@link http://bit.ly/1GTUOj0} 24 | * @param {String[]} [options.sourceSuffixes=['vanilla.js', 'js', 'browser.js']] Files with specified suffixes
25 | * involved in the assembly. 26 | * @param {Boolean} [options.iife=false] Adds an option to wrap merged
27 | * files to IIFE. 28 | * @param {Boolean} [options.compress=false] Minifies and compresses JS code. 29 | * @param {Boolean} [options.sourcemap=false] Includes inline source maps. 30 | * @param {Boolean} [options.includeYM=false] Prepends code of YModules. 31 | * 32 | * @example 33 | * // Code in a file system before build: 34 | * // blocks/ 35 | * // ├── block.vanilla.js 36 | * // └── block.browser.js 37 | * // └── block.js 38 | * // 39 | * // After build: 40 | * // bundle/ 41 | * // └── bundle.browser.js 42 | * 43 | * var BrowserJsTech = require('enb-diverse-js/techs/browser-js'), 44 | * FileProvideTech = require('enb/techs/file-provider'), 45 | * bemTechs = require('enb-bem-techs'); 46 | * 47 | * module.exports = function(config) { 48 | * config.node('bundle', function(node) { 49 | * // get FileList 50 | * node.addTechs([ 51 | * [FileProvideTech, { target: '?.bemdecl.js' }], 52 | * [bemTechs.levels, levels: ['blocks']], 53 | * [bemTechs.deps], 54 | * [bemTechs.files] 55 | * ]); 56 | * 57 | * // build browser.js file 58 | * node.addTech(BrowserJsTech); 59 | * node.addTarget('?.browser.js'); 60 | * }); 61 | * }; 62 | */ 63 | module.exports = buildFlow.create() 64 | .name('browser-js') 65 | .target('target', '?.browser.js') 66 | .useFileList(['vanilla.js', 'js', 'browser.js']) 67 | .defineOption('includeYM', false) 68 | .defineOption('iife', false) 69 | .defineOption('compress', false) 70 | .defineOption('sourcemap', false) 71 | .builder(function (sourceFiles) { 72 | var promises = [this._readSourceFiles(sourceFiles)]; 73 | 74 | if (this._includeYM) { 75 | promises.push(this._readYM()); 76 | } 77 | 78 | return vow.all(promises) 79 | .spread(function (sources, ymSource) { 80 | var node = this.node, 81 | file = new File(node.resolvePath(this._target), { sourceMap: this._sourcemap }), 82 | needWrapIIFE = this._iife, 83 | needToAddComments = !this._compress, 84 | compressOptions = { fromString: true }, 85 | compressed; 86 | 87 | if (ymSource) { 88 | file.writeFileContent(node.relativePath(ymSource.path), ymSource.contents); 89 | } 90 | 91 | sources.forEach(function (source) { 92 | needToAddComments && file.writeLine('/* begin: ' + source.relPath + ' */'); 93 | needWrapIIFE && file.writeLine('(function(){'); 94 | file.writeFileContent(source.relPath, source.contents); 95 | needWrapIIFE && file.writeLine('}());'); 96 | needToAddComments && file.writeLine('/* end: ' + source.relPath + ' */'); 97 | }); 98 | 99 | if (!this._compress) { 100 | return file.render(); 101 | } 102 | 103 | if (!this._sourcemap) { 104 | return minify(file.render(), compressOptions).code; 105 | } 106 | 107 | compressOptions.inSourceMap = file.getSourceMap(); 108 | compressOptions.outSourceMap = this._target + '.map'; 109 | 110 | compressed = minify(file.getContent(), compressOptions); 111 | return utils.joinContentAndSourceMap(compressed.code, compressed.map); 112 | }, this); 113 | }) 114 | .methods({ 115 | /** 116 | * Reads source js files. 117 | * 118 | * @protected 119 | * @param {FileList} files 120 | * @returns {FileData[]} 121 | */ 122 | _readSourceFiles: function (files) { 123 | var node = this.node; 124 | 125 | return vow.all(files.map(function (file) { 126 | return vfs.read(file.fullname, 'utf8') 127 | .then(function (contents) { 128 | return { 129 | path: file.fullname, 130 | relPath: node.relativePath(file.fullname), 131 | contents: contents 132 | }; 133 | }); 134 | })); 135 | }, 136 | /** 137 | * Reads source code of YModules. 138 | * 139 | * @protected 140 | * @returns {Promise} 141 | */ 142 | _readYM: function () { 143 | var filename = require.resolve('ym'); 144 | 145 | return vfs.read(filename, 'utf-8') 146 | .then(function (contents) { 147 | return { 148 | path: filename, 149 | contents: contents 150 | }; 151 | }); 152 | } 153 | }) 154 | .createTech(); 155 | -------------------------------------------------------------------------------- /techs/node-js.js: -------------------------------------------------------------------------------- 1 | var EOL = require('os').EOL, 2 | vow = require('vow'), 3 | enb = require('enb'), 4 | vfs = enb.asyncFS || require('enb/lib/fs/async-fs'), 5 | buildFlow = enb.buildFlow || require('enb/lib/build-flow'), 6 | browserify = require('browserify'), 7 | promisify = require('vow-node').promisify; 8 | 9 | /** 10 | * @class NodeJsTech 11 | * @augments {BaseTech} 12 | * @classdesc 13 | * 14 | * Builds JavaScript files for Node.js.

15 | * 16 | * Compiles any files which contains JavaScript code for Node.js: 17 | * `vanilla.js` – isomorphic code, which can be used on client side and server side; 18 | * `node.js` – code, which could be used only on Node.js. 19 | * 20 | * @param {Object} [options] Options 21 | * @param {String} [options.target='?.node.js'] Path to compiled file. 22 | * @param {String} [options.filesTarget='?.files'] Path to a target with FileList
23 | * {@link http://bit.ly/1GTUOj0} 24 | * @param {String[]} [options.sourceSuffixes=['vanilla.js', 'node.js']] Files with specified suffixes involved
25 | * in the assembly. 26 | * @param {Boolean} [options.devMode=true] Drops cache for `require` of source modules. 27 | * @param {Boolean} [options.bundled=false] Builds CommonJS files in one file. 28 | * @param {Boolean} [options.includeYM=false] Provides code of YModules 29 | * to `global.modules` var. 30 | * 31 | * @example 32 | * // Code in a file system before build: 33 | * // blocks/ 34 | * // ├── block.vanilla.js 35 | * // └── block.node.js 36 | * // 37 | * // After build: 38 | * // bundle/ 39 | * // └── bundle.node.js 40 | * 41 | * var NodeJsTech = require('enb-diverse-js/techs/node-js'), 42 | * FileProvideTech = require('enb/techs/file-provider'), 43 | * bemTechs = require('enb-bem-techs'); 44 | * 45 | * module.exports = function(config) { 46 | * config.node('bundle', function(node) { 47 | * // get FileList 48 | * node.addTechs([ 49 | * [FileProvideTech, { target: '?.bemdecl.js' }], 50 | * [bemTechs.levels, levels: ['blocks']], 51 | * [bemTechs.deps], 52 | * [bemTechs.files] 53 | * ]); 54 | * 55 | * // build node.js file 56 | * node.addTech(NodeJsTech); 57 | * node.addTarget('?.node.js'); 58 | * }); 59 | * }; 60 | */ 61 | module.exports = buildFlow.create() 62 | .name('node-js') 63 | .target('target', '?.node.js') 64 | .useFileList(['vanilla.js', 'node.js']) 65 | .defineOption('devMode', true) 66 | .defineOption('bundled', false) 67 | .defineOption('includeYM', false) 68 | .builder(function (sourceFiles) { 69 | var node = this.node, 70 | needIncludeYM = this._includeYM, 71 | dropRequireCacheFunc = [ 72 | 'function dropRequireCache(requireFunc, filename) {', 73 | ' var module = requireFunc.cache[filename];', 74 | ' if (module) {', 75 | ' if (module.parent) {', 76 | ' if (module.parent.children) {', 77 | ' var moduleIndex = module.parent.children.indexOf(module);', 78 | ' if (moduleIndex !== -1) {', 79 | ' module.parent.children.splice(moduleIndex, 1);', 80 | ' }', 81 | ' }', 82 | ' delete module.parent;', 83 | ' }', 84 | ' delete module.children;', 85 | ' delete requireFunc.cache[filename];', 86 | ' }', 87 | '};' 88 | ].join(EOL), 89 | devMode = this._devMode || ''; 90 | 91 | if (this._bundled) { 92 | var files = sourceFiles.map(function (file) { 93 | return node.relativePath(file.fullname); 94 | }), 95 | browserifyOptions = { 96 | basedir: node.getDir(), 97 | builtins: [], 98 | bundleExternal: false 99 | }, 100 | renderer = browserify(files, browserifyOptions), 101 | bundle = promisify(renderer.bundle.bind(renderer)), 102 | promises = [bundle()]; 103 | 104 | if (needIncludeYM) { 105 | promises.push(this._readYM()); 106 | } 107 | 108 | return vow.all(promises) 109 | .spread(function (bundleBuf, ymCode) { 110 | ymCode = ymCode ? [ 111 | 'var ctx = { exports: {} };', 112 | '(function(module, exports){', 113 | ymCode, 114 | '}(ctx, ctx.exports));', 115 | 'global.modules = ctx.exports;', 116 | ].join(EOL) : ''; 117 | 118 | return [ 119 | ymCode, 120 | bundleBuf.toString('utf-8'), 121 | ].join(EOL); 122 | }); 123 | } 124 | 125 | return [ 126 | devMode && dropRequireCacheFunc, 127 | needIncludeYM ? 'global.modules = require("ym");' : '', 128 | sourceFiles.map(function (file) { 129 | var relPath = node.relativePath(file.fullname); 130 | 131 | return [ 132 | devMode && 'dropRequireCache(require, require.resolve("' + relPath + '"));', 133 | 'require("' + relPath + '");' 134 | ].join(EOL); 135 | }).join(EOL) 136 | ].join(EOL); 137 | }) 138 | .methods({ 139 | /** 140 | * Reads source code of YModules. 141 | * 142 | * @protected 143 | * @returns {Promise} 144 | */ 145 | _readYM: function () { 146 | var filename = require.resolve('ym'); 147 | 148 | return vfs.read(filename, 'utf-8'); 149 | } 150 | }) 151 | .createTech(); 152 | -------------------------------------------------------------------------------- /test/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise": true, 3 | "eqeqeq": true, 4 | "forin": true, 5 | "freeze": true, 6 | "immed": true, 7 | "latedef": "nofunc", 8 | "noarg": true, 9 | "noempty": true, 10 | "nonbsp": true, 11 | "nonew": true, 12 | "undef": true, 13 | "unused": true, 14 | 15 | "node": true, 16 | "mocha": true, 17 | 18 | "expr": true, 19 | "sub": true 20 | } 21 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | test/**/*.test.js 2 | --ui bdd 3 | --reporter dot 4 | -------------------------------------------------------------------------------- /test/techs/browser-js.test.js: -------------------------------------------------------------------------------- 1 | require('chai') 2 | .use(require('chai-as-promised')) 3 | .use(require('chai-string')) 4 | .should(); 5 | 6 | var EOL = require('os').EOL, 7 | mock = require('mock-fs'), 8 | FileList = require('enb/lib/file-list'), 9 | MockNode = require('mock-enb/lib/mock-node'), 10 | loadDirSync = require('mock-enb/utils/dir-utils').loadDirSync, 11 | browserJsTech = require('../../techs/browser-js'); 12 | 13 | describe('browser-js', function () { 14 | afterEach(function () { 15 | mock.restore(); 16 | }); 17 | 18 | describe('join files', function () { 19 | it('must join all files', function () { 20 | var blocks = { 21 | 'block0.vanilla.js': 'Hello0', 22 | 'block1.browser.js': 'Hello1' 23 | }, 24 | reference = [ 25 | '/* begin: ../blocks/block0.vanilla.js */', 26 | 'Hello0', 27 | '/* end: ../blocks/block0.vanilla.js */', 28 | '/* begin: ../blocks/block1.browser.js */', 29 | 'Hello1', 30 | '/* end: ../blocks/block1.browser.js */', 31 | '' 32 | ].join(EOL); 33 | 34 | return build(blocks) 35 | .spread(function (content) { 36 | content.should.be.equal(reference); 37 | }); 38 | }); 39 | }); 40 | 41 | describe('compress', function () { 42 | it('must compress files', function () { 43 | var blocks = { 44 | 'block0.vanilla.js': 'var b = function () {};', 45 | 'block1.browser.js': 'if (foo) { bar(); }' 46 | }, 47 | reference = 'var b=function(){};foo&&bar();'; 48 | 49 | return build(blocks, { compress: true }) 50 | .spread(function (content) { 51 | content.should.be.equal(reference); 52 | }); 53 | }); 54 | }); 55 | 56 | describe('code executes', function () { 57 | var globals, 58 | blocks = { 59 | 'block0.vanilla.js': 'var a = 1;', 60 | 'block1.browser.js': 'var a; global.TEST.push(a || 2);' 61 | }; 62 | 63 | beforeEach(function () { 64 | globals = global.TEST = []; 65 | }); 66 | 67 | it('code must executed in isolation', function () { 68 | return build(blocks, { iife: true }, true) 69 | .then(function () { 70 | globals[0].should.be.equal(2); 71 | }); 72 | }); 73 | 74 | it('code must be executed in the same scoupe', function () { 75 | return build(blocks, null, true) 76 | .then(function () { 77 | globals[0].should.be.equal(1); 78 | }); 79 | }); 80 | }); 81 | 82 | describe('ym', function () { 83 | var blocks = { 84 | 'block.browser.js': 'Hello' 85 | }; 86 | 87 | it('must prepend ym code', function () { 88 | return build(blocks, { includeYM: true }) 89 | .spread(function (res) { 90 | res.should.startWith('module.exports = "ym"'); 91 | }); 92 | }); 93 | }); 94 | 95 | describe('sourcemap', function () { 96 | it('must generate sourcemap', function () { 97 | var blocks = { 98 | 'block0.vanilla.js': 'Hello0', 99 | 'block1.browser.js': 'Hello1' 100 | }; 101 | 102 | return build(blocks, { sourcemap: true }) 103 | .spread(function (content) { 104 | content.should.match(/sourceMappingURL/); 105 | }); 106 | }); 107 | 108 | it('must generate sourcemap with minification', function () { 109 | var blocks = { 110 | 'block0.vanilla.js': 'Hello0', 111 | 'block1.browser.js': 'Hello1' 112 | }; 113 | 114 | return build(blocks, { sourcemap: true, compress: true }) 115 | .spread(function (content) { 116 | content.should.match(/Hello0,Hello1;\n\/\/#\ssourceMappingURL/); 117 | }); 118 | }); 119 | }); 120 | }); 121 | 122 | function build(blocks, options, isNeedRequire) { 123 | mock({ 124 | blocks: blocks, 125 | bundle: {}, 126 | // jscs:disable 127 | node_modules: { 128 | 'ym': { 129 | 'index.js': 'module.exports = "ym";' 130 | } 131 | } 132 | // jscs:enable 133 | }); 134 | 135 | var bundle = new MockNode('bundle'), 136 | fileList = new FileList(), 137 | testFunc; 138 | 139 | fileList.addFiles(loadDirSync('blocks')); 140 | 141 | bundle.provideTechData('?.files', fileList); 142 | 143 | testFunc = isNeedRequire ? bundle.runTechAndRequire : bundle.runTechAndGetContent; 144 | 145 | return testFunc.call(bundle, browserJsTech, options); 146 | } 147 | -------------------------------------------------------------------------------- /test/techs/node-js.test.js: -------------------------------------------------------------------------------- 1 | require('chai') 2 | .use(require('chai-as-promised')) 3 | .should(); 4 | 5 | var fs = require('fs'), 6 | path = require('path'), 7 | mock = require('mock-fs'), 8 | FileList = require('enb/lib/file-list'), 9 | dropRequireCache = require('enb/lib/fs/drop-require-cache'), 10 | MockNode = require('mock-enb/lib/mock-node'), 11 | NodeJsTech = require('../../techs/node-js'), 12 | loadDirSync = require('mock-enb/utils/dir-utils').loadDirSync, 13 | rimraf = require('rimraf'), 14 | EOL = require('os').EOL; 15 | 16 | describe('node-js', function () { 17 | var globals; 18 | 19 | beforeEach(function () { 20 | globals = global.REQUIRED_TECHS = []; 21 | }); 22 | 23 | afterEach(function () { 24 | mock.restore(); 25 | }); 26 | 27 | describe('join files', function () { 28 | var scheme = { 29 | blocks: { 30 | 'block.vanilla.js': 'global.REQUIRED_TECHS.push("vanilla-js");', 31 | 'block.node.js': 'global.REQUIRED_TECHS.push("node-js");' 32 | } 33 | }, 34 | targetPath = path.resolve('./bundle/bundle.node.js'); 35 | 36 | afterEach(function () { 37 | dropRequireCache(require, targetPath); 38 | }); 39 | 40 | it('must join all files', function () { 41 | return build(scheme) 42 | .then(function () { 43 | require(targetPath); 44 | 45 | globals.should.include('vanilla-js'); 46 | globals.should.include('node-js'); 47 | }); 48 | }); 49 | 50 | it('must join `vanilla.js` file after `node.js` file', function () { 51 | return build(scheme) 52 | .then(function () { 53 | require(targetPath); 54 | 55 | globals.indexOf('node-js').should.be.above(globals.indexOf('vanilla-js')); 56 | }); 57 | }); 58 | }); 59 | 60 | describe('devMode', function () { 61 | var scheme = { 62 | blocks: { 63 | 'block.node.js': 'global.REQUIRED_TECHS.push("node-js");' 64 | } 65 | }, 66 | blockPath = path.resolve('./blocks/block.node.js'), 67 | targetPath = path.resolve('./bundle/bundle.node.js'); 68 | 69 | afterEach(function () { 70 | dropRequireCache(require, blockPath); 71 | dropRequireCache(require, targetPath); 72 | }); 73 | 74 | it('must drop require cache by default', function () { 75 | return build(scheme) 76 | .then(function () { 77 | require(targetPath); 78 | fs.writeFileSync(blockPath, 'global.REQUIRED_TECHS.push("fake-node-js");'); 79 | dropRequireCache(require, targetPath); 80 | require(targetPath); 81 | 82 | globals.should.include('fake-node-js'); 83 | }); 84 | }); 85 | 86 | it('must not drop require cache in production mode', function () { 87 | return build(scheme, { devMode: false }) 88 | .then(function () { 89 | require(targetPath); 90 | fs.writeFileSync(blockPath, 'global.REQUIRED_TECHS.push("fake-node-js");'); 91 | dropRequireCache(require, targetPath); 92 | require(targetPath); 93 | 94 | globals.should.include('node-js'); 95 | globals.should.have.length(1); 96 | }); 97 | }); 98 | }); 99 | 100 | describe('bundled', function () { 101 | var targetPath = path.resolve('./bundle/bundle.node.js'); 102 | 103 | afterEach(function () { 104 | global.__fake = ''; 105 | dropRequireCache(require, targetPath); 106 | }); 107 | 108 | it('must run code from bundle in isolation of blocks', function () { 109 | var scheme = { 110 | blocks: { 111 | 'block.vanilla.js': 'global.REQUIRED_TECHS.push("vanilla-js");', 112 | 'block.node.js': 'global.REQUIRED_TECHS.push("node-js");' 113 | } 114 | }; 115 | return build(scheme, { bundled: true }) 116 | .then(function () { 117 | rimraf.sync('./blocks/'); 118 | require(targetPath); 119 | 120 | globals.should.include('node-js'); 121 | globals.should.include('vanilla-js'); 122 | }); 123 | }); 124 | 125 | it('must use CommonJS module', function () { 126 | var scheme = { 127 | blocks: { 128 | 'block.node.js': 'global.__fake = require("fake");' 129 | }, 130 | // jscs:disable 131 | node_modules: { 132 | fake: { 133 | 'index.js': 'module.exports = "fake";' 134 | } 135 | } 136 | // jscs:enable 137 | }; 138 | 139 | return build(scheme, { bundled: true }) 140 | .then(function () { 141 | dropRequireCache(require, targetPath); 142 | require(targetPath); 143 | 144 | global.__fake.should.be.equal('fake'); 145 | }) 146 | .then(function () { 147 | dropRequireCache(require, require.resolve('fake')); 148 | }) 149 | .fail(function (err) { 150 | dropRequireCache(require, require.resolve('fake')); 151 | throw err; 152 | }); 153 | }); 154 | 155 | it('must not browserify base node modules', function () { 156 | global.__type = 'fake'; 157 | var scheme = { 158 | blocks: { 159 | 'block.node.js': 'global.__type = require("os").type();' 160 | } 161 | }; 162 | 163 | return build(scheme, { bundled: true }) 164 | .then(function () { 165 | dropRequireCache(require, targetPath); 166 | require(targetPath); 167 | 168 | global.__type.should.not.equal('fake'); 169 | global.__type.should.not.equal('Browser'); 170 | }); 171 | }); 172 | 173 | it('must not reveal node_modules', function () { 174 | var scheme = { 175 | blocks: { 176 | 'block.node.js': 'require("fake");' 177 | }, 178 | // jscs:disable 179 | node_modules: { 180 | fake: { 181 | 'index.js': 'module.exports = "fake";' 182 | } 183 | } 184 | // jscs:enable 185 | }, 186 | pathToModule = require.resolve('fake'); 187 | 188 | return build(scheme, { bundled: true }) 189 | .then(function () { 190 | dropRequireCache(require, targetPath); 191 | rimraf.sync('./node_modules'); 192 | 193 | (function () { 194 | require(targetPath); 195 | }).should.throw(/fake/); 196 | }) 197 | .then(function () { 198 | dropRequireCache(require, pathToModule); 199 | }) 200 | .fail(function (err) { 201 | dropRequireCache(require, pathToModule); 202 | throw err; 203 | }); 204 | }); 205 | 206 | describe('ym', function () { 207 | var YMFilename = require.resolve('ym'), 208 | YMContents = fs.readFileSync(YMFilename, 'utf-8'); 209 | 210 | afterEach(function () { 211 | delete global.modules; 212 | }); 213 | 214 | var scheme = { 215 | blocks: { 216 | 'block.node.js': [ 217 | 'modules.define("my-module", function (provide) {', 218 | ' provide("module-value")', 219 | '});' 220 | ].join(EOL) 221 | }, 222 | // jscs:disable 223 | node_modules: { 224 | ym: { 225 | 'index.js': YMContents 226 | } 227 | } 228 | // jscs:enable 229 | }; 230 | 231 | it('must provide ym to `global.modules`', function () { 232 | return build(scheme, { includeYM: true }) 233 | .then(function () { 234 | dropRequireCache(require, targetPath); 235 | require(targetPath); 236 | 237 | global.modules.getState('my-module').should.eql('NOT_RESOLVED'); 238 | }); 239 | }); 240 | 241 | it('must provide ym to `global.modules` in bundled file', function () { 242 | return build(scheme, { includeYM: true, bundled: true }) 243 | .then(function () { 244 | dropRequireCache(require, targetPath); 245 | require(targetPath); 246 | 247 | global.modules.getState('my-module').should.eql('NOT_RESOLVED'); 248 | }); 249 | }); 250 | }); 251 | }); 252 | }); 253 | 254 | function build(scheme, options) { 255 | scheme.bundle = {}; 256 | mock(scheme); 257 | 258 | var bundle = new MockNode('bundle'), 259 | fileList = new FileList(); 260 | 261 | fileList.addFiles(loadDirSync('blocks')); 262 | 263 | bundle.provideTechData('?.files', fileList); 264 | 265 | return bundle.runTech(NodeJsTech, options); 266 | } 267 | --------------------------------------------------------------------------------