Skip to content

Commit

Permalink
Put babel config in context
Browse files Browse the repository at this point in the history
  • Loading branch information
geowarin committed May 4, 2016
1 parent e17f3e0 commit 4de05c0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 53 deletions.
54 changes: 54 additions & 0 deletions lib/babel/createBabelConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const resolve = require('resolve');

function findReact () {
try {
return resolve.sync('react', {basedir: process.cwd()})
} catch (e) {
throw new UserError('React must be installed')
}
}

function getAliases (context) {
var aliases = context.userConfig.aliases;
if (!aliases) {
return [];
}
return aliases.map(alias => {
var aliasName = Object.keys(alias)[0];
return {
src: alias[aliasName],
expose: aliasName
}
});
}

module.exports = function createBabelConfig (context) {
const reactPath = findReact();
return {
presets: [
require.resolve('babel-preset-react'),
require.resolve('babel-preset-es2015-webpack'),
require.resolve('babel-preset-stage-0')
],
cacheDirectory: true,
plugins: [
[require.resolve("babel-plugin-module-alias"), getAliases(context)]
],
env: {
development: {
plugins: [
[require.resolve('babel-plugin-react-transform'), {
transforms: [{
transform: require.resolve('react-transform-hmr'),
imports: [reactPath],
locals: ['module']
}, {
transform: require.resolve('react-transform-catch-errors'),
imports: [reactPath, require.resolve('redbox-noreact')]
}]
}]
]
}
}
}
}
3 changes: 3 additions & 0 deletions lib/bin/tarec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');
const yaml = require('js-yaml');
const fs = require('fs');
const createBabelConfig = require('../babel/createBabelConfig');

const rootDir = path.join(__dirname, '../..');
const projectDir = process.cwd();
Expand All @@ -29,6 +30,8 @@ const context = {
indexPath: indexPath
};

context.babelConfig = createBabelConfig(context);

if (process.argv.length == 2) {
require('../tasks/start')(context);
} else if (process.argv[2] == 'build') {
Expand Down
55 changes: 2 additions & 53 deletions lib/loaders/babel.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,9 @@
const resolve = require('resolve');

function findReact () {
try {
return resolve.sync('react', {basedir: process.cwd()})
} catch (e) {
throw new UserError('React must be installed')
}
}

function getAliases (options) {
var aliases = options.userConfig.aliases;
if (!aliases) {
return [];
}
return aliases.map(alias => {
var aliasName = Object.keys(alias)[0];
return {
src: alias[aliasName],
expose: aliasName
}
});
}

const reactPath = findReact();

module.exports = [
(options) => ({
(context) => ({
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/,
query: {
presets: [
require.resolve('babel-preset-react'),
require.resolve('babel-preset-es2015-webpack'),
require.resolve('babel-preset-stage-0')
],
cacheDirectory: true,
plugins: [
[require.resolve("babel-plugin-module-alias"), getAliases(options)]
],
env: {
development: {
plugins: [
[require.resolve('babel-plugin-react-transform'), {
transforms: [{
transform: require.resolve('react-transform-hmr'),
imports: [reactPath],
locals: ['module']
}, {
transform: require.resolve('react-transform-catch-errors'),
imports: [reactPath, require.resolve('redbox-noreact')]
}]
}]
]
}
}
}
query: context.babelConfig
})
];

0 comments on commit 4de05c0

Please sign in to comment.