Created
September 11, 2017 04:59
-
-
Save hdon/c696f821ac0ed55ed6f2b80d88f7371d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var util = require('../lib/util'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var opt; | |
var reducer; | |
var reducerFunction; | |
var transpiledModulePath; | |
opt = require('node-getopt').create([ | |
['r', 'root-reducer=ARG', 'specify root reducer module'] | |
, ['b', 'use-babel=ARG', 'enable or disable babel transpilation (default: enable)'] | |
, ['h', 'help', 'display this help'] | |
]) | |
.bindHelp() | |
.parseSystem(); | |
if (!opt.options['root-reducer']) { | |
console.error('no root reducer specified'); | |
process.exit(1) | |
} | |
const reducerModulePath = fs.realpathSync(opt.options['root-reducer']); | |
const useBabel = ('use-babel' in opt.options) && opt.options['use-babel'] == '=enable'; | |
const babel = useBabel ? require('babel-core') : null; | |
transpiledModulePath = util.compileModule(babel, reducerModulePath); | |
// neither gives me the effect that i want | |
//require.paths = [path.dirname(reducerModulePath)]; | |
//process.env.NODE_PATH = path.dirname(reducerModulePath); | |
reducer = require(transpiledModulePath); | |
/* ... */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var mkdirp = require('mkdirp'); | |
var resolve = require('resolve'); | |
function compileModule(babel, modulePath) { | |
var visitedFiles = {}; | |
return _compileModule(modulePath); | |
function _compileModule(modulePath) { | |
var filePath = resolve.sync(modulePath, { basedir: process.cwd() }); | |
var realPath = fs.realpathSync(filePath); | |
if (realPath in visitedFiles) | |
return; | |
console.log('compiling', realPath); | |
visitedFiles[realPath] = true; | |
/* transpile */ | |
var input = fs.readFileSync(filePath, 'utf-8'); | |
var piled = babel.transform(input, { | |
ast: false | |
, code: true | |
, sourceType: 'module' | |
, presets: ['es2016', 'es2015'] | |
, plugins: ['transform-object-rest-spread'] | |
, sourceFileName: filePath | |
, sourceRoot: '/home/hdon/src/git/pitchboard-frontend' | |
}); | |
piled.metadata.modules.imports.forEach(imp => { | |
if (imp.source[0] == '.') | |
_compileModule(path.join(path.dirname(realPath), imp.source)) | |
}) | |
/* cache the result */ | |
var outFilePath = process.env.HOME + '/.redux-shell/transpiled/' + realPath; | |
mkdirp.sync(path.dirname(outFilePath)); | |
fs.writeFileSync(outFilePath, piled.code); | |
return outFilePath.substr(0, outFilePath.length - '.js'.length); | |
} | |
} | |
module.exports.compileModule = compileModule; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment