-
Notifications
You must be signed in to change notification settings - Fork 48
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
Compiler.generate: support YModules, CommonJS and global dependencies #424
Conversation
lib/compiler.js
Outdated
|
||
'})(typeof window !== "undefined" ? ' + | ||
'window : typeof global !== "undefined" ? global : this);' | ||
|
||
].join('\n'); |
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.
EOL = require('os').EOL
test/api-generate-test.js
Outdated
var vow = require('vow'); | ||
/* jscs:disable */ | ||
// fake commonJS module after browserify | ||
var FAKE_COMMON_MODULE = 'var require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module \'"+o+"\'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"fake":[function(require,module,exports){ module.exports = { getText: function () { return "Hello templates!"; } }; },{}]},{},[]);'; |
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.
test/api-generate-test.js
Outdated
var bundle = bemhtml.generate('', { | ||
requires: { text: { globals: 'text' } } | ||
}); | ||
var sandbox = { global: {}, exports: {} }; |
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.
Сделать метод как в enb-bemxjst…
e2043e2
to
02628c1
Compare
commonJSNames: [], | ||
ym: [], | ||
ymVars: [], | ||
ymLibs: [] |
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.
ym настолько особенный, что аж ну ваще
return deps; | ||
|
||
for (var lib in requires) { | ||
if (requires.hasOwnProperty(lib)) { |
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.
if (!requires.hasOwn...) continue;
?
@zxqfox how is it? |
02628c1
to
c4027be
Compare
@@ -456,6 +424,111 @@ $ cat stderr.txt | |||
BEMXJST ERROR: cannot render block b1, elem undefined, mods {}, elemMods {} [TypeError: Cannot read property 'undef' of undefined] | |||
``` | |||
|
|||
### Using thirdparty libraries |
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 can you check this english documentation?
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.
Looked also at tests, looks fine.
'function buildBemXjst(libs) {', | ||
'var exports;', | ||
|
||
'/* BEM-XJST Runtime Start */', |
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.
Do we really want replace these great beautiful banners with usual boring comment blocks?
) + '}'), | ||
|
||
// Provide to YModules | ||
'if (typeof modules === "object") {', |
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.
Afaik electron
has modules
but not ym
. /cc @awinogradov
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.
ym
has modules
100%
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.
I mean electron
has no ym
but has modules
variable and this code will be broken there. AFAIK.
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.
Ah, the electron context is a product of merging browser and nodejs contexts, and we have there modules
, module
and require
at the same time. I'd add a test for this.
), | ||
|
||
'})(typeof window !== "undefined" ? ' + | ||
'window : typeof global !== "undefined" ? global : this);' |
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.
I feel like this code smell, but I don't atm know how to bake it better.
I'd say it's fine solution since compile should generate code with exports.
docs/en/3-api.md
Outdated
} | ||
``` | ||
|
||
`lib-name` module will accessible in templates body like this: |
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.
will accessible -> will be accessible
docs/en/3-api.md
Outdated
}); | ||
``` | ||
|
||
It’s necessary to specify all the modular systems for connecting the library. |
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.
- It’s necessary to specify all the modular systems for connecting the library.
+ It’s necessary to specify each environment you want to expose library to.
docs/en/3-api.md
Outdated
|
||
It’s necessary to specify all the modular systems for connecting the library. | ||
|
||
For example: you can specify dependencies only from global scope. In this case |
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.
E.g. if you specify just global scope
the library will only be available as global variable even though some module system will be present in runtime.
docs/en/3-api.md
Outdated
|
||
Example of using `moment.js` library: | ||
|
||
You need no set path to module: |
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.
You don't need to to provide path to module
docs/ru/3-api.md
Outdated
|
||
## Подключение сторонних библиотек | ||
|
||
Технологии [bemtree](api.ru.md#bemtree) и [bemhtml](api.ru.md#bemhtml) поддерживают возможность подключения сторонних библиотек как глобально, так и для разных модульных систем с помощью опции [requires](api.ru.md#requires). |
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.
В английском было капсом, что правильно
docs/ru/3-api.md
Outdated
@@ -451,6 +419,110 @@ BEMXJST ERROR: cannot render block b1, elem undefined, mods {}, elemMods {} [Typ | |||
``` | |||
|
|||
|
|||
### Расширение BEMContext |
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.
порядок не совпадает с порядком в TOC-е
) + '}'), | ||
|
||
// Provide to YModules | ||
'if (typeof modules === "object") {', |
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.
ym
has modules
100%
c4027be
to
9ca1480
Compare
9ca1480
to
b20bb52
Compare
@tadatuta updated! |
Я тут запустил, и у меня |
Fixes #375