Skip to content

Commit

Permalink
✨ feat: Switch dev server to babel and start to use styleguidist
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKuehl committed Oct 3, 2018
1 parent 5893cd5 commit 0d21eb5
Show file tree
Hide file tree
Showing 14 changed files with 1,367 additions and 26,410 deletions.
1 change: 0 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"lerna": "2.5.1",
"packages": ["packages/*"],
"version": "independent",
"useWorkspaces": true
}
36 changes: 16 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
{
"workspaces": [
"packages/*"
],
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**/webpack",
"**/webpack/**",
"**/webpack-cli",
"**/webpack-cli/**",
"**/babel-core",
"**/babel-core/**"
]
},
"private": true,
"scripts": {
"precommit": "lint-staged",
"build:libs": "lerna run build:lib",
"develop": "lerna exec --scope @jmm/* -- yarn run build:watch"
"develop": "lerna run build:watch --parallel"
},
"prettier": {
"semi": false,
Expand All @@ -24,28 +34,14 @@
]
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"babel-plugin-eslint-disable": "^0.1.2",
"file-loader": "^2.0.0",
"flow-bin": "^0.79.1",
"husky": "^0.14.3",
"lerna": "^2.5.1",
"lint-staged": "^7.2.2",
"node-sass": "^4.9.3",
"nwb": "0.23.x",
"nwb-sass": "^0.9.0",
"prettier": "^1.14.2",
"react": "^16.5.2",
"react-dom": "^16.5.2"
},
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**/webpack",
"**/webpack/**"
]
"react-dom": "^16.5.2",
"library-utils": "*"
}
}
9,801 changes: 0 additions & 9,801 deletions packages/cra-app/yarn.lock

This file was deleted.

3 changes: 3 additions & 0 deletions packages/library-utils/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["react-app"]
}
46 changes: 46 additions & 0 deletions packages/library-utils/bin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var app = spawnNodemon()
// Generate a random port
const port = '600' + Math.floor(Math.random() * 9 + 1)
if (type === 'withStorybook') {
startStorybook(port)
}
app.on('message', function(event) {
if (event.type === 'boot') {
clearConsole()
type === 'withStorybook'
? console.log(
chalk.cyan('Starting storybook and nodemon watcher...\u001b[39m')
)
: console.log(chalk.cyan('Starting nodemon watcher...\u001b[39m'))
} else if (event.type === 'config:update') {
console.log('Watching: ' + event.data.dirs[0])
} else if (event.type === 'start') {
console.log('⏳ Compiling...')
} else if (event.type === 'crash') {
console.log(chalk.red('Failed to compile. Waiting for changes...'))
} else if (event.type === 'restart') {
clearConsole()
console.log(chalk.yellow('Changes detected:'))
event.data.forEach(d => console.log('--> ' + d))
} else if (event.type === 'exit') {
clearConsole()
type === 'withStorybook'
? console.log(
chalk.green('Compiled successfully! Waiting for changes...') +
'\n\n' +
'📕 Storybook running on ' +
'http://localhost:' +
chalk.bold(port) +
'/' +
'\n\n' +
'Please note that the output of the storyboard dev server is not logged. \nTo start a standalone storyboard server, use ' +
chalk.cyan('yarn run storybook') +
'.'
)
: console.log(
chalk.green('Compiled successfully! Waiting for changes...')
)
} else {
// console.log(event)
}
})
116 changes: 43 additions & 73 deletions packages/library-utils/bin/start.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,62 @@
'use strict'
const {spawn} = require('child_process')
const {clearConsole} = require('./utils.js')
const {execFile} = require('child_process')
const {clearConsole, printDevServerInfo} = require('./utils.js')
const chalk = require('chalk')
const dirCommand = require('@babel/cli/lib/babel/dir.js').default
const parseArgv = require('@babel/cli/lib/babel/options.js').default
const path = require('path')

function spawnStorybook(port) {
const cp = spawn('start-storybook', ['-p', port, '-c', '.storybook'])
function spawnStyleguide(port) {
const cp = execFile(
require.resolve('react-styleguidist/bin/styleguidist.js'),
['server']
)
cp.stdout.on('data', function(data) {
console.log(data.toString())
})
return cp
}

/**
* Starts the storybook dev server
* Starts the styleguide dev server
*/
function startStorybook(port) {
var app = spawnStorybook(port)
}

function spawnNodemon() {
const cp = spawn(
'nodemon',
// $ nodemon -e '*' -w src -x 'rm .babelrc &> /dev/null; nwb build-react-component --no-demo --copy-files'
[
'--watch',
'src',
'-x',
'rm .babelrc &> /dev/null; nwb build-react-component --no-demo --copy-files'
],
{
// the important part is the 4th option 'ipc'
// this way `process.send` will be available in the child process (nodemon)
// so it can communicate back with parent process (through `.on()`, `.send()`)
// https://nodejs.org/api/child_process.html#child_process_options_stdio
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
}
)

return cp
async function startStyleguide(port) {
var app = spawnStyleguide()
}

/**
* Starts a dev server
* @param {string} type The servers type
*/
function startDev(type = 'default') {
var app = spawnNodemon()
// Generate a random port
const port = '600' + Math.floor(Math.random() * 9 + 1)
type === 'withStorybook'
? console.log(
chalk.cyan('Starting styleguide and babel watcher...\u001b[39m')
)
: console.log(chalk.cyan('Starting babel watcher...\u001b[39m'))

// Spawn Babel server
process.env.NODE_ENV = 'development'
const opts = parseArgv([
process.argv[0],
process.argv[1],
'-w',
'--presets=react-app',
'src',
'--out-dir',
'es',
'--copy-files',
'--delete-dir-on-start',
'--ignore',
'__tests__,spec.js,test.js,__snapshots__'
])
dirCommand(opts).catch(err => {
console.error(err)
process.exit(1)
})
if (type === 'withStorybook') {
startStorybook(port)
startStyleguide()
}
app.on('message', function(event) {
if (event.type === 'boot') {
clearConsole()
type === 'withStorybook'
? console.log(
chalk.cyan('Starting storybook and nodemon watcher...\u001b[39m')
)
: console.log(chalk.cyan('Starting nodemon watcher...\u001b[39m'))
} else if (event.type === 'config:update') {
console.log('Watching: ' + event.data.dirs[0])
} else if (event.type === 'start') {
console.log('⏳ Compiling...')
} else if (event.type === 'crash') {
console.log(chalk.red('Failed to compile. Waiting for changes...'))
} else if (event.type === 'restart') {
clearConsole()
console.log(chalk.yellow('Changes detected:'))
event.data.forEach(d => console.log('--> ' + d))
} else if (event.type === 'exit') {
clearConsole()
type === 'withStorybook'
? console.log(
chalk.green('Compiled successfully! Waiting for changes...') +
'\n\n' +
'📕 Storybook running on ' +
'http://localhost:' +
chalk.bold(port) +
'/' +
'\n\n' +
'Please note that the output of the storyboard dev server is not logged. \nTo start a standalone storyboard server, use ' +
chalk.cyan('yarn run storybook') +
'.'
)
: console.log(
chalk.green('Compiled successfully! Waiting for changes...')
)
} else {
// console.log(event)
}
})
printDevServerInfo()
}
module.exports = {startDev}
25 changes: 20 additions & 5 deletions packages/library-utils/bin/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
const chalk = require('chalk')

function clearConsole() {
process.stdout.write(
process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'
)
}
function printDevServerInfo() {
clearConsole()
console.log(chalk.green('Started watcher! Waiting for changes...'))

console.log(
'\nThe ' +
chalk.bold('babel') +
' watcher does not provide proper console output after first compilation.\nPlease refer to the consuming applications dev server for error logging.'
)
console.log('\n')
}
module.exports = {
clearConsole: function() {
process.stdout.write(
process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'
)
}
clearConsole: clearConsole,
printDevServerInfo: printDevServerInfo
}
2 changes: 0 additions & 2 deletions packages/library-utils/nwb.config.js

This file was deleted.

19 changes: 11 additions & 8 deletions packages/library-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@
},
"scripts": {},
"dependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"babel-plugin-eslint-disable": "^0.1.2",
"babel-preset-react-app": "^5.0.2",
"chalk": "^2.4.1",
"nodemon": "^1.18.4"
"react-scripts": "^2.0.3",
"react-styleguidist": "^7.3.8",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2"
},
"peerDependencies": {
"css-loader": "1.x",
"nwb": "0.x",
"react": "16.x",
"sass-loader": "7.x",
"style-loader": "0.x"
"react": "16.x"
},
"author": "",
"homepage": "",
"author": "Felix Kühl",
"homepage": "www.jobmatch.me",
"license": "MIT",
"repository": ""
}
2 changes: 0 additions & 2 deletions packages/library/nwb.config.js

This file was deleted.

Loading

0 comments on commit 0d21eb5

Please sign in to comment.