Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

Commit

Permalink
convert bin and config to es6, add lint coverage to them
Browse files Browse the repository at this point in the history
  • Loading branch information
bdefore committed Dec 30, 2015
1 parent 3134f67 commit 64dbbb3
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 230 deletions.
47 changes: 2 additions & 45 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,3 @@
#!/usr/bin/env node
var webpack = require('webpack');
var webpackConfig = require('./merge-configs');
var fs = require('fs');
var buildStats = false;

console.log('\nBuilding webpack bundle...');
webpack(webpackConfig, function(err, stats) {
if(err) {
console.log('Webpack build had fatal error:', err);
return;
}

var options = {
hash: true,
version: true,
timings: true,
assets: true,
chunks: false,
colors: true
}

console.log('Webpack compile was successful.');

var jsonStats = stats.toJson();
if(jsonStats.errors.length > 0) {
console.log('Webpack had errors.');
options.errors = true;
}
if(jsonStats.warnings.length > 0) {
console.log('Webpack had warnings.');
options.warnings = true;
}

console.log(stats.toString(options));

if(buildStats) {
fs.writeFile('./webpack-stats.json', JSON.stringify(stats.toJson()), function(err) {
if(err) {
return console.log(err);
}

console.log('Webpack output stats were saved to', outputStatsPath);
});
}
});
require('./transpile'); // babel registration (runtime transpilation for node)
require('./build_es6.js');
47 changes: 47 additions & 0 deletions bin/build_es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node
const webpack = require('webpack');
const webpackConfig = require('./merge-configs');
const fs = require('fs');
const buildStats = false;
const outputStatsPath = './webpack-stats.json';

console.log('\nBuilding webpack bundle...');
webpack(webpackConfig, (err, stats) => {
if (err) {
console.log('Webpack build had fatal error:', err);
return;
}

const options = {
hash: true,
version: true,
timings: true,
assets: true,
chunks: false,
colors: true
};

console.log('Webpack compile was successful.');

const jsonStats = stats.toJson();
if (jsonStats.errors.length > 0) {
console.log('Webpack had errors.');
options.errors = true;
}
if (jsonStats.warnings.length > 0) {
console.log('Webpack had warnings.');
options.warnings = true;
}

console.log(stats.toString(options));

if (buildStats) {
fs.writeFile(outputStatsPath, JSON.stringify(stats.toJson()), (writeError) => {
if (writeError) {
return console.log(writeError);
}

console.log('Webpack output stats were saved to', outputStatsPath);
});
}
});
4 changes: 2 additions & 2 deletions bin/local-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ compile() {
echo Source: $ROOT_DIR
echo Destination: $PROJECT_PATH/node_modules/universal-redux
echo
cp $ROOT_DIR/bin/* $PROJECT_PATH/node_modules/universal-redux/bin/ > /dev/null
cp $ROOT_DIR/config/* $PROJECT_PATH/node_modules/universal-redux/config/ > /dev/null
cp -a $ROOT_DIR/bin/* $PROJECT_PATH/node_modules/universal-redux/bin/ > /dev/null
cp -a $ROOT_DIR/config/* $PROJECT_PATH/node_modules/universal-redux/config/ > /dev/null
cp $ROOT_DIR/.babelrc $PROJECT_PATH/node_modules/universal-redux
cp $ROOT_DIR/.eslintrc $PROJECT_PATH/node_modules/universal-redux
babel $ROOT_DIR/src/ --presets es2015,stage-0,react --plugins transform-runtime --out-dir $PROJECT_PATH/node_modules/universal-redux/lib > /dev/null
Expand Down
90 changes: 34 additions & 56 deletions bin/merge-babel-config.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,43 @@
var fs = require('fs');
var strip = require('strip-loader');
var path = require('path');
var util = require('util');

module.exports = function(userBabelConfig, verbose) {

var babelrc = fs.readFileSync(path.resolve(__dirname, '..', './.babelrc'));
var babelConfig = {};
var jsLoaders;

try {
babelConfig = JSON.parse(babelrc);
} catch (err) {
console.error('==> ERROR: Error parsing your .babelrc.');
console.error(err);
}

if(userBabelConfig) {
console.log('Merging universal-redux Babel defaults with project Babel configuration');

var userBabelConfigFile = fs.readFileSync(path.resolve(userBabelConfig));
var userBabelConfig = {};

try {
userBabelConfig = JSON.parse(userBabelConfigFile);
} catch (err) {
console.error('==> ERROR: Error parsing your project-level Babel configuration.');
console.error(err);
}

babelConfig = Object.assign(babelConfig, userBabelConfig);
}

if (process.env.NODE_ENV !== 'production') {
const fs = require('fs');
const strip = require('strip-loader');
const path = require('path');
const util = require('util');
const isProduction = process.env.NODE_ENV !== 'production';

function loadAndParse(filePath) {
const file = fs.readFileSync(filePath);
return JSON.parse(file);
}

var hmrConfig = [
"react-transform", {
"transforms": [
{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"]
},
{
"transform": "react-transform-catch-errors",
"imports": ["react", "redbox-react"]
}
]
module.exports = (userBabelConfig, verbose) => {

const baseBabelConfig = loadAndParse(path.resolve(__dirname, '..', './.babelrc'));
const babelConfig = userBabelConfig ? Object.assign(baseBabelConfig, loadAndParse(path.resolve(userBabelConfig))) : baseBabelConfig;

const hmrConfig = [
'react-transform', {
'transforms': [
{
'transform': 'react-transform-hmr',
'imports': ['react'],
'locals': ['module']
},
{
'transform': 'react-transform-catch-errors',
'imports': ['react', 'redbox-react']
}
]
}
];

babelConfig.env.development.plugins.unshift(hmrConfig);
babelConfig.env.development.plugins.unshift(hmrConfig);

jsLoaders = ['babel-loader?' + JSON.stringify(babelConfig)];
} else {
jsLoaders = [strip.loader('debug'), 'babel-loader?' + JSON.stringify(babelConfig)];
}
const babelLoader = 'babel-loader?' + JSON.stringify(babelConfig);
const jsLoaders = isProduction ? [strip.loader('debug'), babelLoader] : [babelLoader];

// output configuration files if user wants verbosity
if(verbose) {
var utilOptions = {
if (verbose) {
const utilOptions = {
depth: 10,
colors: true
};
Expand All @@ -69,4 +47,4 @@ module.exports = function(userBabelConfig, verbose) {
}

return jsLoaders;
}
};
77 changes: 37 additions & 40 deletions bin/merge-configs.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,93 @@
#!/usr/bin/env node
var _ = require('lodash');
var path = require('path');
var util = require('util');
var webpack = require('webpack');
var mergeWebpack = require('webpack-config-merger');
var mergeBabel = require('./merge-babel-config');
var baseWebpackConfig = require('../config/webpack.config.js');
var baseDevConfig = mergeWebpack(baseWebpackConfig.common, baseWebpackConfig.development);
var baseProdConfig = mergeWebpack(baseWebpackConfig.common, baseWebpackConfig.production);
var baseToolsConfig = require('../config/webpack-isomorphic-tools-config');
var WebpackErrorNotificationPlugin = require('webpack-error-notification');

var isProduction = process.env.NODE_ENV === 'production';
const path = require('path');
const util = require('util');

const lodash = require('lodash');
const webpack = require('webpack');
const mergeWebpack = require('webpack-config-merger');
const mergeBabel = require('./merge-babel-config');
const baseWebpackConfig = require('../config/webpack.config.js');
const baseDevConfig = mergeWebpack(baseWebpackConfig.common, baseWebpackConfig.development);
const baseProdConfig = mergeWebpack(baseWebpackConfig.common, baseWebpackConfig.production);
const baseToolsConfig = require('../config/webpack-isomorphic-tools-config');
const WebpackErrorNotificationPlugin = require('webpack-error-notification');

const isProduction = process.env.NODE_ENV === 'production';

// gather webpack config
var userConfigPath = 'config/universal-redux.config.js';
var userConfig = require(path.resolve(userConfigPath));
const userConfigPath = 'config/universal-redux.config.js';
const userConfig = require(path.resolve(userConfigPath));

// merge with base config if directed to
if(userConfig.webpack.merge) {
var baseConfig = isProduction ? baseProdConfig : baseDevConfig;
combinedWebpackConfig = mergeWebpack(baseConfig, userConfig.webpack.config);
} else {
combinedWebpackConfig = userConfig.webpack.config;
}
const baseConfig = isProduction ? baseProdConfig : baseDevConfig;
const combinedWebpackConfig = userConfig.webpack.merge ? mergeWebpack(baseConfig, userConfig.webpack.config) : userConfig.webpack.config;

// add babel for js transpiling
var babelConfig = mergeBabel(userConfig.babelConfig, userConfig.verbose);
const babelConfig = mergeBabel(userConfig.babelConfig, userConfig.verbose);
combinedWebpackConfig.module.loaders.unshift({ test: /\.jsx?$/, exclude: /node_modules/, loaders: babelConfig });

// gather tools config
var combinedToolsConfig = baseToolsConfig;
if(userConfig.toolsConfigPath) {
var userToolsConfig = require(path.resolve(userConfig.toolsConfigPath));
combinedToolsConfig = _.merge(baseToolsConfig, userToolsConfig);
const combinedToolsConfig = baseToolsConfig;
if (userConfig.toolsConfigPath) {
const userToolsConfig = require(path.resolve(userConfig.toolsConfigPath));
combinedToolsConfig = lodash.merge(baseToolsConfig, userToolsConfig);
}
combinedToolsConfig.webpack_assets_file_path = 'node_modules/universal-redux/webpack-assets.json';

// add tools settings to combined weback config
var WebpackIsomorphicToolsPlugin = require('webpack-isomorphic-tools/plugin');
var toolsPlugin = new WebpackIsomorphicToolsPlugin(combinedToolsConfig);
const WebpackIsomorphicToolsPlugin = require('webpack-isomorphic-tools/plugin');
const toolsPlugin = new WebpackIsomorphicToolsPlugin(combinedToolsConfig);

combinedWebpackConfig.module.loaders.push({ test: toolsPlugin.regular_expression('images'), loader: 'url-loader?limit=10240' });
combinedWebpackConfig.plugins.push(isProduction ? toolsPlugin : toolsPlugin.development());

// turn on linting per webpack build, unless directed not to
if(userConfig.lint && userConfig.lint.enabled !== false && !isProduction) {
if (userConfig.lint && userConfig.lint.enabled !== false && !isProduction) {
combinedWebpackConfig.module.loaders[0].loaders.push('eslint-loader');
const lintConfigPath = userConfig.lint.config || path.resolve(__dirname, '../.eslintrc');
combinedWebpackConfig.eslint = {
configFile: lintConfigPath
}
}
};
}

// turn on desktop notifications if user elects to
if(userConfig.notifications === true && !isProduction) {
if (userConfig.notifications === true && !isProduction) {
combinedWebpackConfig.plugins.push(new WebpackErrorNotificationPlugin());
}

// add default settings that are used by server via process.env
var definitions = {
const definitions = {
__LOGGER__: false,
__DEVTOOLS__: !isProduction,
__DEVELOPMENT__: !isProduction,
__REDUCER_INDEX__: userConfig.redux.reducers // only used for hot reloader in src/redux/create.js. may be able to remove?
};

// override with user settings
_.each(userConfig.globals, function(value, key) { definitions[key] = JSON.stringify(value); });
lodash.each(userConfig.globals, (value, key) => { definitions[key] = JSON.stringify(value); });
combinedWebpackConfig.plugins.push(new webpack.DefinePlugin(definitions));

// add routes and reducer aliases so that client has access to them
combinedWebpackConfig.resolve.alias = combinedWebpackConfig.resolve.alias || {};
combinedWebpackConfig.resolve.alias.routes = userConfig.routes;
combinedWebpackConfig.resolve.alias.reducers = userConfig.redux.reducers;
combinedWebpackConfig.resolve.alias.config = combinedWebpackConfig.context + '/' + userConfigPath;
if(userConfig.redux.middleware) {
if (userConfig.redux.middleware) {
combinedWebpackConfig.resolve.alias.middleware = userConfig.redux.middleware;
} else {
combinedWebpackConfig.resolve.alias.middleware = path.resolve(__dirname, '../lib/redux/middleware/index.js');
combinedWebpackConfig.resolve.alias.middleware = path.resolve(__dirname, '../src/redux/middleware/index.js');
}

// add project level vendor libs
if(userConfig.webpack && userConfig.webpack.vendorLibraries && isProduction) {
_.each(userConfig.webpack.vendorLibraries, (lib) => {
if (userConfig.webpack && userConfig.webpack.vendorLibraries && isProduction) {
lodash.each(userConfig.webpack.vendorLibraries, (lib) => {
combinedWebpackConfig.entry.vendor.push(lib);
});
}

// output configuration files if user wants verbosity
if(userConfig.verbose) {
var utilOptions = {
if (userConfig.verbose) {
const utilOptions = {
depth: 6,
colors: true
};
Expand Down
2 changes: 2 additions & 0 deletions bin/transpile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// enable runtime transpilation to use ES6/7 in node

/* eslint-disable */
var fs = require('fs');
var path = require('path');

Expand All @@ -12,5 +13,6 @@ try {
console.error('==> ERROR: Error parsing your babelrc');
console.error(err);
}
/* eslint-enable */

require('babel-core/register')(config);
36 changes: 2 additions & 34 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
#!/usr/bin/env node
var path = require('path');
var Express = require('express');
var webpack = require('webpack');
var webpackConfig = require('./merge-configs');
var userConfig = require(path.resolve('config/universal-redux.config.js'));

var compiler = webpack(webpackConfig);

var host = userConfig.server.host || 'localhost';
var port = parseInt(userConfig.server.port) + 1 || 3001;
var serverOptions = {
contentBase: 'http://' + host + ':' + port,
quiet: true,
noInfo: true,
hot: true,
inline: true,
lazy: false,
publicPath: webpackConfig.output.publicPath,
headers: {'Access-Control-Allow-Origin': '*'},
stats: {colors: true}
};

var app = new Express();

app.use(require('webpack-dev-middleware')(compiler, serverOptions));
app.use(require('webpack-hot-middleware')(compiler));

app.listen(port, function onAppListening(err) {
if (err) {
console.error(err);
} else {
console.info('==> 🚧 Webpack development server listening on port %s', port);
}
});
require('./transpile'); // babel registration (runtime transpilation for node)
require('./webpack-dev-server_es6.js');
Loading

0 comments on commit 64dbbb3

Please sign in to comment.