diff --git a/lib/compiler/plugins/es.js b/lib/compiler/plugins/es.js new file mode 100644 index 0000000..a009d7b --- /dev/null +++ b/lib/compiler/plugins/es.js @@ -0,0 +1,54 @@ +// Generated by CoffeeScript 1.4.0 +(function() { + var Package, babel, es2015, getBabelConfig, react, syspath, utils; + + utils = require('../../util'); + + Package = require('../../package'); + + syspath = require('path'); + + babel = require('babel-core'); + + es2015 = require('babel-preset-es2015'); + + react = require('babel-preset-react'); + + getBabelConfig = function(configPath) { + var config; + if (!utils.path.exists(configPath)) { + return null; + } + config = utils.file.io.readJSON(configPath); + return config; + }; + + exports.contentType = "javascript"; + + exports.process = function(txt, path, module, cb) { + var babelConfig, babel_config_path, basepath, cwd, name; + try { + name = syspath.basename(path, '.es'); + cwd = process.cwd(); + basepath = utils.path.closest(cwd, Package.FEKIT_MODULE_DIR, true); + if (!basepath) { + basepath = utils.path.closest(cwd, 'fekit.config'); + } + if (!basepath) { + basepath = cwd; + } + babel_config_path = utils.path.join(basepath, '.babelrc'); + babelConfig = getBabelConfig(babel_config_path); + if (!babelConfig) { + babelConfig = { + presets: [es2015, react] + }; + } + txt = (babel.transform(txt, babelConfig)).code; + return cb(null, txt); + } catch (err) { + return cb(err); + } + }; + +}).call(this); diff --git a/lib/compiler/plugins/jsx.js b/lib/compiler/plugins/jsx.js new file mode 100644 index 0000000..e3b9e2e --- /dev/null +++ b/lib/compiler/plugins/jsx.js @@ -0,0 +1,6 @@ +// Generated by CoffeeScript 1.4.0 +(function() { + + module.exports = require('./es'); + +}).call(this); diff --git a/package.json b/package.json index a353a1a..e48d955 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,10 @@ "velocityjs": "0.4.x" }, "devDependencies": { - "chai": "1.3.x" + "chai": "1.3.x", + "babel-cli": "^6.7.5", + "babel-preset-es2015": "^6.6.0", + "babel-preset-react": "^6.5.0" }, "optionalDependencies": {}, "engines": { @@ -56,4 +59,4 @@ "scripts": { "prepublish": "cake build" } -} \ No newline at end of file +} diff --git a/src/compiler/plugins/es.coffee b/src/compiler/plugins/es.coffee new file mode 100644 index 0000000..c679844 --- /dev/null +++ b/src/compiler/plugins/es.coffee @@ -0,0 +1,37 @@ +utils = require '../../util' +Package = require '../../package' +syspath = require 'path' +babel = require 'babel-core' +es2015 = require('babel-preset-es2015') +react = require('babel-preset-react') + +getBabelConfig = ( configPath ) -> + return null unless utils.path.exists configPath + config = utils.file.io.readJSON configPath + return config + +exports.contentType = "javascript" + +exports.process = (txt, path, module, cb) -> + try + name = syspath.basename path, '.es' + + cwd = process.cwd() + # 先寻找最近的fekit_modules目录 + basepath = utils.path.closest cwd , Package.FEKIT_MODULE_DIR , true + # 如果没有,再寻找最近的fekit.config + basepath = utils.path.closest cwd , 'fekit.config' unless basepath + # 再没有,则使用当前目录 + basepath = cwd unless basepath + + babel_config_path = utils.path.join( basepath , '.babelrc' ) + + babelConfig = getBabelConfig babel_config_path + + babelConfig = {presets: [es2015, react]} unless babelConfig + + txt = (babel.transform txt, babelConfig).code + + cb null, txt + catch err + cb err \ No newline at end of file diff --git a/src/compiler/plugins/jsx.coffee b/src/compiler/plugins/jsx.coffee new file mode 100644 index 0000000..55e6ed5 --- /dev/null +++ b/src/compiler/plugins/jsx.coffee @@ -0,0 +1 @@ +module.exports = require './es' \ No newline at end of file