Skip to content

Commit

Permalink
feat(docz-core): set babel on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 15, 2018
1 parent 66fb277 commit 672be49
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 34 deletions.
4 changes: 0 additions & 4 deletions examples/basic/.babelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"emotion"
]
Expand Down
3 changes: 0 additions & 3 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
"react-router-dom": "^4.2.2"
},
"devDependencies": {
"@babel/core": "7.0.0-beta.42",
"@babel/preset-env": "^7.0.0-beta.42",
"@babel/preset-react": "^7.0.0-beta.42",
"babel-plugin-emotion": "^9.0.1"
}
}
3 changes: 3 additions & 0 deletions packages/playgrodd-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
"babel-types": "^6.26.0",
"babylon": "^6.18.0",
"connect-history-api-fallback": "^1.5.0",
"deepmerge": "^2.1.0",
"del": "^3.0.0",
"express": "^4.16.3",
"fast-glob": "^2.2.0",
"html-webpack-plugin": "^3.1.0",
"mkdirp": "^0.5.1",
"react-hot-loader": "^4.0.0",
"webpack": "^4.2.0",
"webpack-dev-middleware": "^3.0.1",
"webpack-hot-middleware": "^2.21.2"
Expand All @@ -35,6 +37,7 @@
"@types/babel-traverse": "^6.25.3",
"@types/babylon": "^6.16.2",
"@types/connect-history-api-fallback": "^1.3.1",
"@types/deepmerge": "^2.1.0",
"@types/del": "^3.0.0",
"@types/express": "^4.11.1",
"@types/mkdirp": "^0.5.2",
Expand Down
52 changes: 25 additions & 27 deletions packages/playgrodd-core/src/compiler/create-config.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
import * as fs from 'fs'
import * as path from 'path'
import * as findup from 'find-up'
import { Loader, Configuration } from 'webpack'
import * as webpack from 'webpack'
import * as HtmlWebpackPlugin from 'html-webpack-plugin'
import * as merge from 'deepmerge'

import { IEntryObj } from './files-parser'
import { loadConfig } from '../utils/load-config'
import * as paths from '../config/paths'

const babelLoader = (babelrc: string | null): Loader => ({
loader: require.resolve('babel-loader'),
options: babelrc
? JSON.parse(babelrc)
: {
babelrc: false,
cacheDirectory: true,
presets: [
require.resolve('@babel/preset-env'),
require.resolve('@babel/preset-react'),
],
},
})
const babelLoader = (): Loader => {
const babelrc = loadConfig('babel', null)
const options = merge(babelrc, {
babelrc: false,
cacheDirectory: true,
presets: [
require.resolve('@babel/preset-env'),
require.resolve('@babel/preset-react'),
],
plugins: [require.resolve('react-hot-loader/babel')],
})

return {
options,
loader: require.resolve('babel-loader'),
}
}

export const createConfig = async (
entries: IEntryObj[]
): Promise<Configuration> => {
const babelrcPath = findup.sync(['.babelrc', 'babelrc.js'])
const babelrc = babelrcPath ? fs.readFileSync(babelrcPath, 'utf-8') : null

return {
mode: 'development',
context: paths.ROOT,
devtool: '#source-map',
entry: [
require.resolve('babel-polyfill'),
`${require.resolve(
'webpack-hot-middleware/client'
)}?path=/__webpack_hmr&timeout=20000'`,
...entries.map(entry => entry.filepath),
paths.INDEX_JS,
],
entry: [require.resolve('babel-polyfill'), paths.INDEX_JS],
output: {
pathinfo: true,
path: paths.DIST,
Expand All @@ -56,7 +50,7 @@ export const createConfig = async (
test: /\.(js|jsx)$/,
exclude: /node_modules/,
include: [paths.ROOT],
use: babelLoader(babelrc),
use: babelLoader(),
},
],
},
Expand All @@ -67,6 +61,10 @@ export const createConfig = async (
src: path.join(paths.ROOT, 'src'),
},
},
devServer: {
contentBase: paths.DIST,
hot: true,
},
plugins: [
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
Expand Down
35 changes: 35 additions & 0 deletions packages/playgrodd-core/src/utils/load-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as fs from 'fs'
import * as path from 'path'
import * as findup from 'find-up'
import * as merge from 'deepmerge'

const finds = (name: string): string[] => [
`${name}.json`,
`.${name}rc`,
`${name}rc.js`,
`${name}rc.json`,
`${name}rc.yml`,
`${name}rc.yaml`,
`${name}.config.js`,
`${name}.config.json`,
]

export const loadConfig = (name: string, defaultConfig: any = {}) => {
let file
const filepath = findup.sync(finds(name))

if (filepath) {
try {
const isJS = path.extname(filepath) === '.js'

file = isJS
? require(filepath)
: JSON.parse(fs.readFileSync(filepath, 'utf-8'))
} catch (err) {
// console.log(err)
file = defaultConfig
}
}

return defaultConfig !== null ? merge(defaultConfig, file) : file
}

0 comments on commit 672be49

Please sign in to comment.