Add working webpack example

This commit is contained in:
yamadapc 2016-06-18 16:06:12 -03:00
parent d0483454fa
commit 65bb7c90a9
14 changed files with 176 additions and 2 deletions

4
.gitignore vendored
View File

@ -63,3 +63,7 @@ jspm_packages
# Optional REPL history
.node_repl_history
.tern-port
*.js_hi
*.js_o
*.jsexe

View File

@ -19,3 +19,6 @@ Haskell/JavaScript. It shows that we may wrap pure and unpure computations
## `failure` Handling errors from Haskell in JavaScript
**failure** shows that any errors thrown from the Haskell world will result in
the Promise being rejected.
## `webpack` Integrating Haskell code onto an existing webpack build
**webpack**

View File

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

20
examples/webpack/LICENSE Normal file
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,25 @@
-- 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
, 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');

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,25 @@
{
"name": "ghcjs-loader-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"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",
"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

@ -4,7 +4,7 @@ const chalk = require('chalk');
const ghcjsRequire = require('ghcjs-require');
const path = require('path');
const GHCJS_COMMAND = 'stack ghc --compiler ghcjs-0.2.0.20160414_ghc-7.10.3';
const GHCJS_COMMAND = 'stack ghc'; // '--compiler ghcjs-0.2.0.20160414_ghc-7.10.3';
const CLOSURE_COMPILER_COMMAND = 'closure-compiler -O ADVANCED';
function compileSync(loader, content) {
@ -64,13 +64,15 @@ exports = module.exports = function ghcjsLoader(content) {
const cwd = process.cwd();
const relPath = path.relative(cwd, this.resourcePath);
console.log(chalk.blue.bold('[ghcjs] >>>'), 'Generating wrapper...');
console.log(chalk.blue.bold('[ghcjs] >>>'), `Generating wrapper ${jsExePath}/index.js...`);
// const minPath = runClosureCompiler(this, jsExePath);
const out = ghcjsRequire.generateWrapper(
jsExePath
// ,
// fs.readFileSync(minPath).toString()
);
console.log(chalk.blue.bold('[ghcjs] >>>'), 'Finished compiling ' + relPath);
return patchWrapper(out);
};