riot_js-rails 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,205 +2,165 @@
2
2
  * The compiler.parsers object holds the compiler's predefined parsers
3
3
  * @module
4
4
  */
5
+ 'use strict'
5
6
 
6
- var path = require('path') // for sass
7
+ var REQPATH = './parsers/'
8
+ var TRUE = true
9
+ var NULL = null
7
10
 
8
- // dummy function for the none and javascript parsers
9
- function _none (src) { return src }
11
+ // Passtrough for the internal `none` and `javascript` parsers
12
+ function _none (src) {
13
+ return src
14
+ }
10
15
 
11
- /** Cache of required modules */
12
- var _mods = {
13
- none: _none,
14
- javascript: _none
16
+ // This is the main parsers object holding the html, js, and css keys
17
+ // initialized with the parsers that cannot be required.
18
+ //
19
+ var _parsers = {
20
+ html: {},
21
+ css: {},
22
+ js: { none: _none, javascript: _none }
15
23
  }
16
24
 
17
- /**
18
- * Returns the module name for the given parser's name.
19
- *
20
- * @param {string} name - one of the `html`, `css`, or `js` parsers.
21
- * @returns {string} The module name for using with `require()`
22
- * @static
23
- */
24
- function _modname (name) {
25
- switch (name) {
26
- case 'es6':
27
- return 'babel'
28
- case 'babel':
29
- return 'babel-core'
30
- case 'javascript':
31
- return 'none'
32
- case 'coffee':
33
- case 'coffeescript':
34
- return 'coffee-script'
35
- case 'scss':
36
- case 'sass':
37
- return 'node-sass'
38
- case 'typescript':
39
- return 'typescript-simple'
40
- default:
41
- return name
42
- }
25
+ // Native riot parsers go here, having false if already required.
26
+ var _loaders = {
27
+ html: { jade: TRUE, pug: TRUE },
28
+ css: { sass: TRUE, scss: TRUE, less: TRUE, stylus: TRUE },
29
+ js: { es6: TRUE, buble: TRUE, coffee: TRUE, livescript: TRUE, typescript: TRUE }
43
30
  }
44
31
 
32
+ _loaders.js.coffeescript = TRUE // 4 the nostalgics
33
+
45
34
  /**
46
- * Loads a parser instance via `require`, without generating error.
35
+ * Loads a "native" riot parser.
36
+ *
37
+ * It set the flag in the _loaders object to false for the required parser.
38
+ * Try to load the parser and save the module to the _parsers object.
39
+ * On error, throws a custom exception (adds 'riot' notice to the original).
40
+ * On success returns the loaded module.
47
41
  *
48
- * @param {string} name - one of the `html`, `css`, or `js` parsers.
49
- * @param {string} [req=name] - name for `require()`
50
- * @returns {function} parser function, or null if error
42
+ * @param {string} branch - The branch name inside _parsers/loaders
43
+ * @param {string} parser - The parser's name
44
+ * @returns {Function} Loaded module.
51
45
  */
52
- function _try (name, req) {
46
+ function _load (branch, parser) {
47
+ var req = REQPATH + (parser === 'coffeescript' ? 'coffee' : parser)
48
+ var mod
53
49
 
54
- function fn (r) {
55
- try { return require(r) } catch (_) {/**/}
56
- return null
50
+ _loaders[branch][parser] = false // try once
51
+ _parsers[branch][parser] = null
52
+ try {
53
+ mod = _parsers[branch][parser] = require(req)
54
+ } catch (e) {
55
+ // istanbul ignore next
56
+ var err = 'Can\'t load the ' + branch + '.' + parser +
57
+ ' riot parser: ' + ('' + e).replace(/^Error:\s/, '')
58
+ // istanbul ignore next
59
+ throw new Error(err)
57
60
  }
58
-
59
- var p = _mods[name] = fn(req || _modname(name))
60
-
61
- // istanbul ignore next: babel-core v5.8.x is not loaded by CI
62
- if (!p && name === 'es6') {
63
- p = _mods[name] = fn('babel-core')
64
- }
65
- return p
61
+ return mod
66
62
  }
67
63
 
68
64
  /**
69
- * Returns a parser instance by its name, require the module if necessary.
70
- * Public through the `parsers._req` function.
65
+ * Returns the branch where the parser resides, or NULL if the parser not found.
66
+ * If the parameter 'branch' is empty, the precedence order is js, css, html.
71
67
  *
72
- * @param {string} name - The parser's name, as registered in the parsers object
73
- * @param {string} [req] - To be used by `_try` with `require`
74
- * @returns {function} The parser instance, null if the parser is not found
75
- * @static
68
+ * @param {string} branch - The name of the branch to search, can be empty
69
+ * @param {string} name - The parser's name
70
+ * @returns {string} Name of the parser branch.
76
71
  */
77
- function _req (name, req) {
78
- return name in _mods ? _mods[name] : _try(name, req)
72
+ function _find (branch, name) {
73
+ return branch ? _parsers[branch][name] && branch
74
+ : _parsers.js[name] ? 'js'
75
+ : _parsers.css[name] ? 'css'
76
+ : _parsers.html[name] ? 'html' : NULL
79
77
  }
80
78
 
81
79
  /**
82
- * Merge the properties of the first object with the properties of the second.
80
+ * Returns a parser instance by its name, requiring the module without generating error.
81
+ * Parsers name can include the branch (ej. 'js.es6').
82
+ * If branch is not included, the precedence order for searching is 'js', 'css', 'html'
83
83
  *
84
- * @param {object} target - Target object
85
- * @param {object} source - Source of the extra properties
86
- * @returns {object} Target object containing the new properties
84
+ * Public through the `parsers._req` function.
85
+ *
86
+ * @param {string} name - The parser's name, as registered in the parsers object
87
+ * @param {boolean} [req] - true if required (throws on error)
88
+ * @returns {Function} The parser instance, null if the parser is not found.
87
89
  */
88
- function extend (target, source) {
89
- if (source) {
90
- for (var prop in source) {
91
- /* istanbul ignore next */
92
- if (source.hasOwnProperty(prop)) {
93
- target[prop] = source[prop]
90
+ function _req (name, req) {
91
+ var
92
+ err,
93
+ mod,
94
+ branch,
95
+ parser = name.split('.')
96
+
97
+ if (parser.length > 1) {
98
+ branch = parser[0]
99
+ parser = parser[1]
100
+ } else {
101
+ branch = NULL
102
+ parser = name
103
+ }
104
+
105
+ // is the parser registered?
106
+ branch = _find(branch, parser)
107
+ if (!branch) {
108
+ if (req) {
109
+ err = 'Riot parser "' + name + '" is not registered.'
110
+ throw new Error(err)
111
+ }
112
+ return NULL
113
+ }
114
+
115
+ // parser registered, needs load?
116
+ if (_loaders[branch][parser]) {
117
+ if (req) {
118
+ mod = _load(branch, parser)
119
+ } else {
120
+ try {
121
+ mod = _load(branch, parser)
122
+ } catch (_) {
123
+ // istanbul ignore next
124
+ mod = NULL
94
125
  }
95
126
  }
127
+ } else {
128
+ mod = _parsers[branch][parser]
96
129
  }
97
- return target
130
+
131
+ return mod
98
132
  }
99
133
 
100
- module.exports = {
101
- /**
102
- * The HTML parsers.
103
- * @prop {function} jade - http://jade-lang.com
104
- */
105
- html: {
106
- jade: function (html, opts, url) {
107
- opts = extend({
108
- pretty: true,
109
- filename: url,
110
- doctype: 'html'
111
- }, opts)
112
- return _req('jade').render(html, opts)
134
+ /**
135
+ * Fill the parsers object with loaders for each parser.
136
+ *
137
+ * @param {Object} _p - The `parsers` object
138
+ * @returns {Object} The received object.
139
+ * @private
140
+ */
141
+ function _setLoaders (_p) {
142
+
143
+ // loads the module at first use and returns the parsed result
144
+ function mkloader (branch, parser) {
145
+ return function _loadParser (p1, p2, p3, p4) {
146
+ var fn = _load(branch, parser)
147
+ return fn(p1, p2, p3, p4)
113
148
  }
114
- },
115
- /**
116
- * Style parsers. In browsers, only less is supported.
117
- * @prop {function} sass - http://sass-lang.com
118
- * @prop {function} scss - http://sass-lang.com
119
- * @prop {function} less - http://lesscss.org
120
- * @prop {function} stylus - http://stylus-lang.com
121
- */
122
- css: {
123
- sass: function (tag, css, opts, url) {
124
- opts = extend({
125
- data: css,
126
- includePaths: [path.dirname(url)],
127
- indentedSyntax: true,
128
- omitSourceMapUrl: true,
129
- outputStyle: 'compact'
130
- }, opts)
131
- return _req('sass').renderSync(opts).css + ''
132
- },
133
- scss: function (tag, css, opts, url) {
134
- opts = extend({
135
- data: css,
136
- includePaths: [path.dirname(url)],
137
- indentedSyntax: false,
138
- omitSourceMapUrl: true,
139
- outputStyle: 'compact'
140
- }, opts)
141
- return _req('scss').renderSync(opts).css + ''
142
- },
143
- less: function (tag, css, opts, url) {
144
- var ret
145
-
146
- opts = extend({
147
- sync: true,
148
- syncImport: true,
149
- filename: url
150
- }, opts)
151
- _req('less').render(css, opts, function (err, result) {
152
- /* istanbul ignore next */
153
- if (err) throw err
154
- ret = result.css
149
+ }
150
+
151
+ for (var branch in _loaders) {
152
+ // istanbul ignore else
153
+ if (_loaders.hasOwnProperty(branch)) {
154
+ var names = Object.keys(_loaders[branch])
155
+
156
+ names.forEach(function (name) {
157
+ _p[branch][name] = mkloader(branch, name)
155
158
  })
156
- return ret
157
- },
158
- stylus: function (tag, css, opts, url) {
159
- var
160
- stylus = _req('stylus'),
161
- nib = _req('nib') // optional nib support
162
-
163
- opts = extend({ filename: url }, opts)
164
- /* istanbul ignore next: can't run both */
165
- return nib
166
- ? stylus(css, opts).use(nib()).import('nib').render() : stylus.render(css, opts)
167
159
  }
168
- },
169
- /**
170
- * The JavaScript parsers.
171
- * @prop {function} es6 - https://babeljs.io - babel or babel-core up to v5.8
172
- * @prop {function} babel - https://babeljs.io - for v6.x or later
173
- * @prop {function} coffee - http://coffeescript.org
174
- * @prop {function} livescript - http://livescript.net
175
- * @prop {function} typescript - http://www.typescriptlang.org
176
- */
177
- js: {
178
- es6: function (js, opts) {
179
- opts = extend({
180
- blacklist: ['useStrict', 'strict', 'react'],
181
- sourceMaps: false,
182
- comments: false
183
- }, opts)
184
- return _req('es6').transform(js, opts).code
185
- },
186
- babel: function (js, opts, url) {
187
- return _req('babel').transform(js, extend({ filename: url }, opts)).code
188
- },
189
- coffee: function (js, opts) {
190
- return _req('coffee').compile(js, extend({ bare: true }, opts))
191
- },
192
- livescript: function (js, opts) {
193
- return _req('livescript').compile(js, extend({ bare: true, header: false }, opts))
194
- },
195
- typescript: function (js, opts) {
196
- return _req('typescript')(js, opts)
197
- },
198
- none: _none, javascript: _none
199
- },
200
- _modname: _modname,
201
- _req: _req
160
+ }
161
+ return _p
202
162
  }
203
163
 
204
- exports = module.exports
205
- exports.js.coffeescript = exports.js.coffee // 4 the nostalgics
164
+ _setLoaders(_parsers)._req = _req
206
165
 
166
+ module.exports = _parsers
@@ -0,0 +1,47 @@
1
+ /*
2
+ Utility functions
3
+ */
4
+ var hasOwnProp = Object.prototype.hasOwnProperty
5
+
6
+ /**
7
+ * Returns an new object with all of the properties from each received object.
8
+ * When there are identical properties, the right-most property takes precedence.
9
+ *
10
+ * @returns {object} A new object containing all the properties.
11
+ */
12
+ function _mixobj () {
13
+ var target = {}
14
+
15
+ for (var i = 0; i < arguments.length; i++) {
16
+ var source = arguments[i]
17
+
18
+ if (source) {
19
+ for (var key in source) {
20
+ // istanbul ignore else
21
+ if (hasOwnProp.call(source, key)) {
22
+ target[key] = source[key]
23
+ }
24
+ }
25
+ }
26
+ }
27
+ return target
28
+ }
29
+
30
+ /**
31
+ * Loads and returns a module instance without generating error.
32
+ *
33
+ * @param {string} name - The name of the module to require
34
+ * @returns {Function} Module instance, or null if error.
35
+ */
36
+ function _tryreq (name) {
37
+ var mod = null
38
+
39
+ try { mod = require(name) } catch (e) {/**/}
40
+
41
+ return mod
42
+ }
43
+
44
+ module.exports = {
45
+ mixobj: _mixobj,
46
+ tryreq: _tryreq
47
+ }
@@ -0,0 +1,18 @@
1
+ /*
2
+ bublé 0.13.x JS plugin.
3
+ Part of the riot-compiler, license MIT
4
+
5
+ History
6
+ -------
7
+ 2016-08-26: Initital release
8
+ */
9
+ var
10
+ mixobj = require('./_utils').mixobj,
11
+ parser = require('buble')
12
+
13
+ module.exports = function _buble (js, opts, url) {
14
+
15
+ opts = mixobj({ source: url, modules: false }, opts)
16
+
17
+ return parser.transform(js, opts).code
18
+ }
@@ -0,0 +1,19 @@
1
+ /*
2
+ CoffeeScript JS plugin.
3
+ Part of the riot-compiler, license MIT
4
+
5
+ History
6
+ -------
7
+ 2016-03-09: Initital release
8
+ */
9
+ var
10
+ mixobj = require('./_utils').mixobj,
11
+ parser = require('coffee-script')
12
+
13
+ var defopts = {
14
+ bare: true
15
+ }
16
+
17
+ module.exports = function _coffee (js, opts) {
18
+ return parser.compile(js, mixobj(defopts, opts))
19
+ }
@@ -0,0 +1,18 @@
1
+ /*
2
+ babel-core 6.x JS plugin.
3
+ Part of the riot-compiler, license MIT
4
+
5
+ History
6
+ -------
7
+ 2016-03-09: Initital release
8
+ */
9
+ var
10
+ mixobj = require('./_utils').mixobj,
11
+ parser = require('babel-core')
12
+
13
+ module.exports = function _babel (js, opts, url) {
14
+
15
+ opts = mixobj({ filename: url }, opts)
16
+
17
+ return parser.transform(js, opts).code
18
+ }
@@ -0,0 +1,27 @@
1
+ /*
2
+ Jade HTML plugin.
3
+ Part of the riot-compiler, license MIT
4
+
5
+ History
6
+ -------
7
+ 2016-03-09: Initital release
8
+ */
9
+ var
10
+ mixobj = require('./_utils').mixobj,
11
+ parser = require('jade')
12
+
13
+ var defopts = {
14
+ pretty: true,
15
+ doctype: 'html'
16
+ }
17
+
18
+ /* eslint-disable */
19
+ console.log('DEPRECATION WARNING: jade was renamed "pug" - the jade parser will be removed in riot@3.0.0!')
20
+ /* eslint-enable */
21
+
22
+ module.exports = function _jade (html, opts, url) {
23
+
24
+ opts = mixobj(defopts, { filename: url }, opts)
25
+
26
+ return parser.render(html, opts)
27
+ }