mirror of
https://github.com/beijaflor-io/ghcjs-commonjs.git
synced 2024-10-03 20:57:48 +03:00
Workspace update
This commit is contained in:
parent
819bcf20b9
commit
b0f64fa7e0
12
README.md
12
README.md
@ -22,7 +22,7 @@ Utilities for loading GHCJS `.jsexe`s from CommonJS land.
|
||||
This is `main.js`:
|
||||
```javascript
|
||||
const ghcjsRequire = require('ghcjs-require');
|
||||
const Main = ghcjsRequire('./Main.jsexe');
|
||||
const Main = ghcjsRequire(module, './Main.jsexe');
|
||||
// ^^ This is a function that boots the Haskell RTS
|
||||
Main(({wrapped}) => { // <- This callback is executed after the RTS is loaded
|
||||
wrapped.someFunction().then(() => console.log('someFunction is over'));
|
||||
@ -32,16 +32,16 @@ Main(({wrapped}) => { // <- This callback is executed after the RTS is loaded
|
||||
});
|
||||
```
|
||||
|
||||
This is `Main.jsexe`:
|
||||
This is `Main.hs`:
|
||||
```haskell
|
||||
import Control.Concurrency (threadDelay)
|
||||
import GHCJS.CommonJS (defaultMain, export)
|
||||
import Control.Concurrent (threadDelay)
|
||||
import qualified GHCJS.CommonJS as CJS (exportMain, pack)
|
||||
someFunction = do
|
||||
putStrLn "Waiting for a second"
|
||||
threadDelay (1000 * 1000)
|
||||
putStrLn "Done!"
|
||||
main =
|
||||
exportMain [("someFunction", someFunction)]
|
||||
main = CJS.exportMain [ CJS.pack ("someFunction", someFunction)
|
||||
]
|
||||
```
|
||||
|
||||
## `ghcjs-register`
|
||||
|
20
README/LICENSE
Normal file
20
README/LICENSE
Normal 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.
|
25
README/README.cabal
Normal file
25
README/README.cabal
Normal file
@ -0,0 +1,25 @@
|
||||
-- Initial README.cabal generated by cabal init. For further
|
||||
-- documentation, see http://haskell.org/cabal/users-guide/
|
||||
|
||||
name: README
|
||||
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 README
|
||||
main-is: Main.hs
|
||||
-- other-modules:
|
||||
-- other-extensions:
|
||||
build-depends: base >=4.8 && <4.9
|
||||
, ghcjs-commonjs
|
||||
hs-source-dirs: src
|
||||
default-language: Haskell2010
|
2
README/Setup.hs
Normal file
2
README/Setup.hs
Normal file
@ -0,0 +1,2 @@
|
||||
import Distribution.Simple
|
||||
main = defaultMain
|
6
README/js/index.js
Normal file
6
README/js/index.js
Normal file
@ -0,0 +1,6 @@
|
||||
const ghcjsRequire = require('ghcjs-require');
|
||||
const Main = ghcjsRequire(module, 'README');
|
||||
|
||||
Main(({wrapped}) => {
|
||||
wrapped.someFunction().then(() => console.log('someFunction is over'));
|
||||
});
|
17
README/package.json
Normal file
17
README/package.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "README",
|
||||
"version": "1.0.0",
|
||||
"description": "-- Initial README.cabal generated by cabal init. For further -- documentation, see http://haskell.org/cabal/users-guide/",
|
||||
"main": "js/index.js",
|
||||
"scripts": {
|
||||
"build": "stack build",
|
||||
"run": "npm run build && node js/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ghcjs-require": "file:///Users/yamadapc/program/github.com/beijaflor-io/ghcjs-commonjs/ghcjs-require"
|
||||
}
|
||||
}
|
9
README/src/Main.hs
Normal file
9
README/src/Main.hs
Normal file
@ -0,0 +1,9 @@
|
||||
import Control.Concurrent (threadDelay)
|
||||
import qualified GHCJS.CommonJS as CommonJS (exportMain, pack)
|
||||
someFunction = do
|
||||
putStrLn "Waiting for a second"
|
||||
threadDelay (1000 * 1000)
|
||||
putStrLn "Done!"
|
||||
main =
|
||||
CommonJS.exportMain [ CommonJS.pack ("someFunction", someFunction)
|
||||
]
|
13
README/stack.yaml
Normal file
13
README/stack.yaml
Normal 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
|
@ -99,18 +99,45 @@ function find(root) {
|
||||
}
|
||||
}
|
||||
|
||||
const TYPE_ERROR =
|
||||
'`ghcjsRequire(module, fp)` takes 2 arguments:\n' +
|
||||
' ^^^^^^\n' +
|
||||
'The first is the `module` "global"\n' +
|
||||
'`ghcjsRequire(module, fp)`\n' +
|
||||
' ^^\n' +
|
||||
'The second is the path to the *.jsexe bundle or\n' +
|
||||
'an executable name in a stack project in the cwd';
|
||||
|
||||
function ghcjsRequire(module, fp) {
|
||||
if (!(module.require && typeof module.require === 'function')) {
|
||||
throw new TypeError(
|
||||
'Invalid `module` suplied to ghcjsRequire\n' +
|
||||
TYPE_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
if (!(fp && typeof fp === 'string')) {
|
||||
throw new TypeError(
|
||||
'Invalid `fp` suplied to ghcjsRequire\n' +
|
||||
TYPE_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
if (path.extname(fp) !== '.jsexe') {
|
||||
const jsexe = find(fp);
|
||||
if (jsexe) {
|
||||
return ghcjsRequire(module, jsexe)
|
||||
const root = childProcess
|
||||
.execSync('stack path --project-root')
|
||||
.toString()
|
||||
.split('\n')[0];
|
||||
return ghcjsRequire(module, path.join(root, jsexe))
|
||||
}
|
||||
|
||||
throw new Error('Could not resolve Haskell source for ' + fp);
|
||||
}
|
||||
|
||||
addWrapper(fp);
|
||||
return module.require('./' + path.join(fp, 'index.js'));
|
||||
return module.require(path.join(fp, 'index.js'));
|
||||
}
|
||||
|
||||
exports = module.exports = ghcjsRequire;
|
||||
|
Loading…
Reference in New Issue
Block a user