Skip to content

Commit

Permalink
Merge pull request #71 from kuashe/dev
Browse files Browse the repository at this point in the history
Added support for es6 "import"
  • Loading branch information
GianlucaGuarini committed Jun 11, 2016
2 parents 3d0987f + 520d0f5 commit 7e6c56d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ var PRE_TAGS = /<pre(?:\s+(?:[^">]*|"[^"]*")*)?>([\S\s]+?)<\/pre\s*>/gi
*/
var SPEC_TYPES = /^"(?:number|date(?:time)?|time|month|email|color)\b/i

/**
* Matches the 'import' statement
* @const {RegExp}
*/
var IMPORT_STATEMENT = /^(?: )*(?:import)(?:(?:.*))*$/gm

/**
* Matches trailing spaces and tabs by line.
* @const {RegExp}
Expand Down Expand Up @@ -277,6 +283,30 @@ function restoreExpr (html, pcex) {
return html
}

/**
* Return imports statement of the code as a string
* @param {string} js - The js code containing the imports statement
* @returns {string} Js code containing only the imports statement
*/
function compileImports (js) {
var imp = []
var imports = ''
while (imp = IMPORT_STATEMENT.exec(js)) {
imports += imp[0].trim() + '\n'
}
return imports
}

/**
* Remove 'import' statement from JSCode
* @param {string} js - The Js code
* @returns {string} jsCode The js code without 'import' statement
*/
function rmImports (js) {
var jsCode = js.replace(IMPORT_STATEMENT, '')
return jsCode
}

/*
HTML Compiler
-----------------------------------------------------------------------------
Expand Down Expand Up @@ -712,10 +742,11 @@ function _q (s, r) {
* @param {string} css - Styles
* @param {string} attr - Root attributes
* @param {string} js - JavaScript "constructor"
* @param {string} imports - Code containing 'import' statements
* @param {object} opts - Compiler options
* @returns {string} Code to call `riot.tag2`
*/
function mktag (name, html, css, attr, js, opts) {
function mktag (name, html, css, attr, js, imports, opts) {
var
c = opts.debug ? ',\n ' : ', ',
s = '});'
Expand All @@ -724,7 +755,7 @@ function mktag (name, html, css, attr, js, opts) {
if (js && js.slice(-1) !== '\n') s = '\n' + s

// 2016-01-18: html can contain eols if opts.whitespace=1, fix with q(s,1)
return 'riot.tag2(\'' + name + SQ +
return imports + 'riot.tag2(\'' + name + SQ +
c + _q(html, 1) +
c + _q(css) +
c + _q(attr) + ', function(opts) {\n' + js + s
Expand Down Expand Up @@ -1002,6 +1033,7 @@ function compile (src, opts, url) {
jscode = '',
styles = '',
html = '',
imports = '',
pcex = []

pcex._bp = _bp
Expand Down Expand Up @@ -1059,6 +1091,8 @@ function compile (src, opts, url) {
// and the untagged js block
if (included('js')) {
body = _compileJS(blocks[1], opts, null, null, url)
imports = compileImports(jscode)
jscode = rmImports(jscode)
if (body) jscode += (jscode ? '\n' : '') + body
}
}
Expand All @@ -1080,7 +1114,7 @@ function compile (src, opts, url) {
}

// replace the tag with a call to the riot.tag2 function and we are done
return mktag(tagName, html, styles, attribs, jscode, opts)
return mktag(tagName, html, styles, attribs, jscode, imports, opts)
})

// if "entities" return the array of objects of the extraced parts
Expand Down
11 changes: 11 additions & 0 deletions test/specs/parsers/test.import.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<timer>
<h1>Hello</h1>

<script>
import john from 'doe'
time(){
return Date()
}

</script>
</timer>

0 comments on commit 7e6c56d

Please sign in to comment.