Create a gist now

Instantly share code, notes, and snippets.

@bkonkle /Client.purs
Last active Sep 14, 2016

What would you like to do?
Ygor build config for PureScript
#!/usr/bin/env babel-node
import {exec} from 'child-process-promise'
import path from 'path'
import ygor from 'ygor'
const PROJECT_DIR = path.dirname(__dirname)
const SRC = `${PROJECT_DIR}/src`
const nodePath = extend => `NODE_PATH=${extend}:${process.env.NODE_PATH || ''}`
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
function babel (options = '') {
return `${nodePath(SRC)} babel ${SRC} --source-maps --out-dir ${SRC} ${options}`
}
function pulp (cmd) {
return `${nodePath(SRC)} pulp ${cmd}`
}
/*
* Tasks
*/
function clean () {
ygor.shell('rm -r build output 2> /dev/null || true')
}
function lint () {
return exec('standard src').then(result => console.log(result.stdout))
}
async function client () {
await javascript()
ygor.shell(`${nodePath(SRC)} webpack --config=webpack.${process.env.NODE_ENV}.js --color=always --progress`)
}
async function dev () {
javascriptWatch()
await sleep(1000) // Give the watch command time to start up
ygor.shell(pulp('run --main SelectFrom.Server.Development'))
}
async function javascript () {
await lint()
ygor.shell(babel())
}
async function javascriptWatch () {
await lint()
return exec(babel('-w')).then(result => console.log(result.stdout))
}
async function purescript () {
await javascript()
ygor.shell(pulp('build -- --censor-codes=UnusedFFIImplementations'))
}
function test () {
ygor.shell(pulp('test -- --censor-codes=UnusedFFIImplementations'))
}
// Intended to be run with "dev" in another terminal window
function watch () {
ygor.shell(`${nodePath(SRC)} pscid --test --censor-codes=UnusedFFIImplementations`)
}
/*
* Interface
*/
ygor
.task('default', client)
.task('clean', clean)
.task('client', client)
.task('dev', dev)
.task('javascript', () => ygor()
.task('default', javascript)
.task('watch', javascriptWatch)
)
.task('purescript', purescript)
.task('test', test)
.task('watch', watch)
module SelectFrom.Client (init) where
import Prelude
import Control.Monad.Eff (Eff)
import DOM (DOM)
import DOM.HTML (window)
import DOM.HTML.Document (body)
import DOM.HTML.Types (htmlElementToNode, htmlDocumentToDocument)
import DOM.HTML.Window (document)
import DOM.Node.Document (createElement) as Document
import DOM.Node.Element (setAttribute)
import DOM.Node.Node (appendChild)
import DOM.Node.Types (elementToNode)
import Data.Maybe (Maybe, fromJust)
import Data.Nullable (toMaybe)
import Partial.Unsafe (unsafePartial)
import React (ReactComponent)
import React (createElement) as React
import ReactDOM (render)
import SelectFrom.Components.App (app)
init :: forall eff. Eff ( dom :: DOM | eff ) (Maybe ReactComponent)
init = do
htmlDocument <- window >>= document
htmlBody <- map (unsafePartial (fromJust <<< toMaybe)) $ body htmlDocument
-- Create an element to house the application
container <- Document.createElement "div" $ htmlDocumentToDocument htmlDocument
setAttribute "id" "app" container
-- Append it to the body
appendChild (elementToNode container) (htmlElementToNode htmlBody)
-- Render the app to the container
render (React.createElement app {} []) container
import chalk from 'chalk'
import devMiddleware from 'webpack-dev-middleware'
import express from 'express'
import findup from 'findup-sync'
import morgan from 'morgan'
import path from 'path'
import webpack from 'webpack'
export function main () {
const PORT = process.env.PORT || 3000
const DOMAIN = 'localhost'
const URL = `http://${DOMAIN}:${PORT}`
const configPath = findup(`webpack.${process.env.NODE_ENV}.js`)
const config = require(configPath)
const index = path.join(config.output.path, 'index.html')
const publicPath = config.output.publicPath
const compiler = webpack(config)
const app = express()
app.use(morgan('dev'))
app.use(devMiddleware(compiler, {
publicPath,
stats: {colors: true, chunks: false},
watchOptions: {aggregateTimeout: 300}
}))
app.get('*', (req, res) => {
const memoryFs = compiler.outputFileSystem
const html = memoryFs.readFileSync(index)
res.end(html)
})
return app.listen(PORT, 'localhost', err => {
if (err) throw new Error(err)
console.log(`[dev-server] Webpack dev server listening at: ${chalk.green(URL)}`)
})
}
const HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')
const webpack = require('webpack')
const nodeModules = path.join(__dirname, '/node_modules')
module.exports = {
devtool: 'eval',
entry: './src/SelectFrom/Init.js',
module: {
loaders: [{
test: /\.purs$/,
loader: 'purs',
exclude: /node_modules/,
query: {
psc: 'psa',
src: ['bower_components/purescript-*/src/**/*.purs', 'src/**/*.purs'],
warnings: false
}
}, {
test: /\.json$/,
loaders: ['json']
}, {
test: /\.css$/,
loaders: [
'style?singleton',
'css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
'resolve-url?sourceMap'
]
}, {
test: /\.scss$/,
loaders: [
'style?singleton',
'css?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
'resolve-url?sourceMap',
'sass?sourceMap'
]
}, {
test: /\.sass$/,
loaders: [
'style?singleton',
'css?sourceMap&modules&importLoaders=2&localIdentName=[name]__[local]___[hash:base64:5]',
'resolve-url?sourceMap',
'sass?sourceMap&indentedSyntax=true'
]
}, {
test: /\.(png|gif|jpe?g)$/i,
loaders: [
'file?hash=sha512&digest=hex&name=[hash].[ext]',
'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
]
}, {
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url?limit=10000&mimetype=application/font-woff&hash=sha512&digest=hex&name=[hash].[ext]'
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url?limit=10000&hash=sha512&digest=hex&name=[hash].[ext]'
}
]},
output: {
path: path.join(__dirname, 'build'),
publicPath: '/',
filename: 'js/SelectFrom.js'
},
plugins: [
new webpack.DefinePlugin({'process.env.NODE_ENV': '"development"'}),
new HtmlWebpackPlugin({title: 'SELECT FROM'}),
new webpack.NoErrorsPlugin()
],
resolve: {
root: [nodeModules].concat(process.env.NODE_PATH.split(':')),
extensions: ['', '.js', '.json', '.purs']
},
resolveLoader: {
root: [nodeModules].concat(process.env.NODE_PATH.split(':'))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment