Add way to build & run examples in one command

This commit is contained in:
yamadapc 2016-06-19 22:07:21 -03:00
parent 4b19aa7824
commit 62df5b8c8e
16 changed files with 213 additions and 10 deletions

25
Makefile Normal file
View File

@ -0,0 +1,25 @@
all:
make hello-world-example
make hello-variable-name
make fibonacci
make failure
hello-world-example:
cd ./examples/hello-world && stack build
cd ./examples/hello-world && npm install
cd ./examples/hello-world && node ./index.js
hello-variable-name:
cd ./examples/hello-variable-name && stack build
cd ./examples/hello-variable-name && npm install
cd ./examples/hello-variable-name && node ./index.js
fibonacci:
cd ./examples/fibonacci && stack build
cd ./examples/fibonacci && npm install
cd ./examples/fibonacci && node ./index.js
failure:
cd ./examples/failure && stack build
cd ./examples/failure && npm install
cd ./examples/failure && node ./index.js

View File

@ -7,6 +7,7 @@ HelloWorld(({wrapped}) => {
wrapped.fibsIO(10).then((fibs) => {
console.log('Haskell calculated', fibs, 'after logging stuff to the console');
process.exit(0);
});
});
});

View File

@ -4,5 +4,6 @@ const HelloWorld = ghcjsRequire(module, 'ghcjs-commonjs-hello-world');
HelloWorld(({wrapped}) => {
wrapped.sayHello('John').then(() => {
console.log('I\'m JavaScript');
process.exit(0);
});
});

View File

@ -4,5 +4,6 @@ const HelloWorld = ghcjsRequire(module, 'ghcjs-commonjs-hello-world');
HelloWorld(({wrapped}) => {
wrapped.sayHello().then(() => {
console.log('And I\'m a JavaScript');
process.exit(0);
});
});

View File

@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-1"]
}

View File

@ -0,0 +1,20 @@
Copyright (c) 2016 Pedro Tacla Yamada
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

View File

@ -0,0 +1,26 @@
-- Initial ghcjs-commonjs-webpack.cabal generated by cabal init. For
-- further documentation, see http://haskell.org/cabal/users-guide/
name: ghcjs-commonjs-webpack
version: 0.1.0.0
-- synopsis:
-- description:
license: MIT
license-file: LICENSE
author: Pedro Tacla Yamada
maintainer: tacla.yamada@gmail.com
-- copyright:
-- category:
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable ghcjs-commonjs-webpack
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.8 && <4.9
, pandoc
, ghcjs-commonjs
hs-source-dirs: hs
default-language: Haskell2010

View File

@ -0,0 +1,16 @@
import Control.Concurrent
import qualified GHCJS.CommonJS as CommonJS
helloWorld = putStrLn "[haskell] Hello World"
launchTheMissiles :: IO Int
launchTheMissiles = do
threadDelay (1000 * 1000 * 5)
putStrLn "[haskell] OMG what did I do?!"
return 10
main =
CommonJS.exportMain
[ CommonJS.pack ("helloWorld", helloWorld)
, CommonJS.pack ("launchTheMissiles", launchTheMissiles)
]

View File

@ -0,0 +1,10 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Document</title>
</head>
<body>
<script src="dist/index.bundle.js"></script>
</body>
</html>

View File

@ -0,0 +1,11 @@
// import Main from '../../Main.hs';
// console.log(Main);
// Main(({wrapped}) => {
// wrapped.launchTheMissiles();
// wrapped.launchTheMissiles();
// wrapped.launchTheMissiles();
// wrapped.helloWorld();
// });
// console.log('[javascript] Hello world');

9
examples/react-pandoc/js/index.js vendored Normal file
View File

@ -0,0 +1,9 @@
const Main = require('../hs/Main.hs');
console.log('[javascript] Hello there');
console.log('[javascript] Main =', Main);
Main(({wrapped}) => {
wrapped.launchTheMissiles();
wrapped.launchTheMissiles();
wrapped.launchTheMissiles();
wrapped.helloWorld();
});

View File

@ -0,0 +1,29 @@
{
"name": "ghcjs-loader-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"webpack-build": "webpack",
"webpack-build-production": "NODE_ENV=production webpack",
"build": "npm run webpack-build-production",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.5.2",
"babel-core": "^6.9.1",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-1": "^6.5.0",
"ghcjs-loader": "file:///Users/yamadapc/program/github.com/beijaflor-io/ghcjs-commonjs/ghcjs-loader",
"ghcjs-require": "file:///Users/yamadapc/program/github.com/beijaflor-io/ghcjs-commonjs/ghcjs-require",
"humanize": "0.0.9",
"react": "^15.1.0",
"webpack": "^1.13.1",
"webpack-bundle-tracker": "0.0.93",
"webpack-node-externals": "^1.2.0"
}
}

View File

@ -0,0 +1,13 @@
resolver: lts-6.3
compiler: ghcjs-0.2.0.20160414_ghc-7.10.3
compiler-check: match-exact
setup-info:
ghcjs:
source:
ghcjs-0.2.0.20160414_ghc-7.10.3:
url: https://s3.amazonaws.com/ghcjs/ghcjs-0.2.0.20160414_ghc-7.10.3.tar.gz
sha1: 6d6f307503be9e94e0c96ef1308c7cf224d06be3
packages:
- .
- ../../ghcjs-commonjs

View File

@ -0,0 +1,31 @@
const webpack = require('webpack');
exports = module.exports = {
module: {
loaders: [
{
test: /\.hs$/,
loader: 'ghcjs-loader',
},
{
test: /\.jsx?$/,
loader: 'babel-loader',
},
],
},
entry: './js/index.js',
output: {
path: './dist',
filename: 'index.bundle.js',
},
plugins: [
//new webpack.ContextReplacementPlugin(/.*$/, /NEVER_MATCH^/),
//new webpack.ContextReplacementPlugin(/.*$/, /NEVER_MATCH^/),
].concat(process.env.NODE_ENV === 'production' ? [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
}
}),
] : [])
};

View File

@ -146,13 +146,18 @@ exportMain es = do
fibs :: [Int]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
pack :: ToCommonJSExport (String, e) => (String, e) -> (String, CommonJSExport)
pack e = (exportName e, toCommonJSExport e)
exports :: ToCommonJSExport (String, e) => String -> e -> (String, CommonJSExport)
exports n e = (n, toCommonJSExport (n, e))
pack :: ToCommonJSExport (String, e) => (String, e) -> (String, CommonJSExport)
pack (n, e) = exports n e
test :: IO ()
test =
exportMain [ pack ("hello", print "Hello")
, pack ("fibs", \n -> (take n fibs))
, pack ("yo", print "yo")
exportMain [ "hello" `exports` print "Hello"
, "fibs" `exports` \n -> take n fibs
, "yo" `exports` print "yo"
, pack ("yoyo", print "yo")
]
foreign import javascript unsafe "$r = new Error($1)"
@ -185,17 +190,17 @@ call fn [a1, a2] = js_call2 fn a1 a2
call fn [a1, a2, a3] = js_call3 fn a1 a2 a3
call fn args = js_apply fn (JSArray.fromList args)
exports :: MVar ExportMap
exports = unsafePerformIO $ newMVar Map.empty
{-# NOINLINE exports #-}
exportsMap :: MVar ExportMap
exportsMap = unsafePerformIO $ newMVar Map.empty
{-# NOINLINE exportsMap #-}
getExport :: String -> IO (Maybe CommonJSExport)
getExport name = do
es <- readMVar exports
es <- readMVar exportsMap
return $ Map.lookup name es
registerExport' :: (String, CommonJSExport) -> IO ()
registerExport' e = modifyMVar_ exports $ \es ->
registerExport' e = modifyMVar_ exportsMap $ \es ->
return $ uncurry Map.insert e es
registerExport :: ToCommonJSExport e => e -> IO ()