From a42f24b85a304e128c45003b806cc5c4d551b3af Mon Sep 17 00:00:00 2001 From: eric thul Date: Sun, 21 Sep 2014 12:50:12 -0400 Subject: Implementation of the loader --- .gitignore | 1 + LICENSE | 20 ++++++++++++++++++++ README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 14 ++++++++++---- 5 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..aaed7de --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2014 Eric Thul + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..17fe735 --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# purs-loader + +> [PureScript](http://www.purescript.org) loader for [webpack](https://github.com/webpack/webpack) + +## Install + +Install with [npm](https://npmjs.org/package/purs-loader) + +``` +npm install purs-loader --save-dev +``` + +## Options + +#### options + + - **no-prelude**: Boolean value that toggles `--no-prelude` + - Do not include the Prelude in the generated Javascript. + - **no-opts**: Boolean value that toggles `--no-opts` + - Disable all optimizations. + - **no-magic-do**: Boolean value that toggles `--no-magic-do` + - Turn off optimizations which inline calls to >>= for the Eff monad. + - **no-tco**: Boolean value that toggles `--no-tco` + - Turn off tail-call elimination. + - **runtime-type-checks**: Boolean value that toggles `--runtime-type-checks` + - Generate simple runtime type checks for function arguments with simple types. + - **verbose-errors**: Boolean value that toggles `--verbose-errors` + - Generate verbose error messages. + - **output**: String value that sets `--output=` + - Write the generated Javascript to the specified file. + +## Example + +```js +var path = require('path'); + +module.exports = { + entry: './src/test', + output: { + path: path.join(__dirname, 'dist'), + filename: 'app.js' + }, + module: { + loaders: [{ + test: /\.purs$/, + loader: 'purs-loader?no-prelude&output=output' + }] + }, + resolve: { + modulesDirectories: [ + 'node_modules', + 'web_modules', + 'output' + ] + } +}; +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..a7665f3 --- /dev/null +++ b/index.js @@ -0,0 +1,55 @@ +var cp = require('child_process') + , path = require('path') + , fs = require('fs') + , glob = require('glob') + , lodash = require('lodash') + , chalk = require('chalk') + , lu = require('loader-utils') + , cwd = process.cwd() + , PSC_MAKE = 'psc-make' + , OUTPUT = 'output' + , OPTIONS = { + 'no-prelude': '--no-prelude', + 'no-opts': '--no-opts', + 'no-magic-do': '--no-magic-do', + 'no-tco': '--no-tco', + 'runtime-type-checks': '--runtime-type-checks', + 'verbose-errors': '--verbose-errors', + 'output': '--output' + } +; + +module.exports = function(source){ + var callback = this.async() + , request = lu.getRemainingRequest(this) + , root = path.dirname(path.relative(cwd, request)) + , query = lu.parseQuery(this.query) + , opts = lodash.foldl(lodash.keys(query), function(acc, k){ + var h = function(v){return acc.concat(query[k] && OPTIONS[k] ? [v] : []);} + if (k === OUTPUT) return h(OPTIONS[k] + '=' + query[k]); + else return h(OPTIONS[k]); + }, []) + ; + glob(path.join(root, '**', '*.purs'), function(e, files){ + if (e !== null) callback(e); + else { + var cmd = cp.spawn(PSC_MAKE, opts.concat(files)); + cmd.on('close', function(e){ + if (e) callback(e); + else { + var module = path.basename(request, '.purs'); + fs.readFile(path.join(opts[OPTIONS[OUTPUT]] || OUTPUT, module, 'index.js'), 'utf-8', function(e, output){ + if (e) callback(e); + else callback(e, output); + }); + } + }); + cmd.stdout.on('data', function(stdout){ + console.log('Stdout from \'' + chalk.cyan(PSC_MAKE) + '\'\n' + chalk.magenta(stdout)); + }); + cmd.stderr.on('data', function(stderr){ + console.log('Stderr from \'' + chalk.cyan(PSC_MAKE) + '\'\n' + chalk.magenta(stderr)); + }); + } + }); +}; diff --git a/package.json b/package.json index 9a19a8b..eed4fd4 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "purescript-loader", + "name": "purs-loader", "version": "0.0.0", "description": "PureScript loader for webpack", "main": "index.js", @@ -8,12 +8,18 @@ }, "repository": { "type": "git", - "url": "git@github.com:ethul/purescript-loader.git" + "url": "git@github.com:ethul/purs-loader.git" }, "author": "Eric Thul", "license": "MIT", "bugs": { - "url": "https://github.com/ethul/purescript-loader/issues" + "url": "https://github.com/ethul/purs-loader/issues" }, - "homepage": "https://github.com/ethul/purescript-loader" + "homepage": "https://github.com/ethul/purs-loader", + "dependencies": { + "chalk": "^0.5.1", + "glob": "4.0.6", + "loader-utils": "^0.2.3", + "lodash": "^2.4.1" + } } -- cgit v1.2.3