Skip to content

Commit

Permalink
First pass
Browse files Browse the repository at this point in the history
  • Loading branch information
petehunt committed Mar 19, 2014
0 parents commit 42132d8
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
test/bundle.js
*~
37 changes: 37 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var async = require('async');
var loaderUtils = require('loader-utils');
var sweet = require('sweet.js');

module.exports = function(source) {
this.async();

var config = loaderUtils.parseQuery(this.query);

async.map(
config.modules || [],
function(module, callback) {
this.resolve(this.context, module, function(err, result) {
if (err) {
callback(err);
return;
}
try {
callback(null, sweet.loadNodeModule(process.cwd(), result));
} catch (e) {
callback(e);
}
});
}.bind(this),
function(err, results) {
if (err) {
this.callback(err);
return;
}
config.modules = results;
var result = sweet.compile(source, config);

this.cacheable && this.cacheable();
this.callback(null, result.code, result.sourceMap);
}.bind(this)
);
};
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "sweetjs-loader",
"version": "0.0.0",
"description": "Loader for sweetjs macros",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"sweetjs",
"webpack"
],
"author": "Pete Hunt",
"license": "Apache 2",
"dependencies": {
"sweet.js": "^0.5.0",
"loader-utils": "^0.2.1",
"async": "^0.2.10"
},
"devDependencies": {
"jasmine-node": "^1.14.2",
"webpack": "^1.1.0-beta9"
}
}
1 change: 1 addition & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id(42)
14 changes: 14 additions & 0 deletions test/macros.sjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
macro id {
rule {
// after the macro name, match:
// (1) a open paren
// (2) a single token and bind it to `$x`
// (3) a close paren
($x)
} => {
// just return the token that is bound to `$x`
$x
}
}

export id
31 changes: 31 additions & 0 deletions test/sweet.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var fs = require('fs');
var path = require('path');
var webpack = require('webpack');

describe('sweet-loader', function() {
it('should pretty much work', function() {
var BUNDLE_PATH = path.join(__dirname, 'bundle.js');

if (fs.existsSync(BUNDLE_PATH)) {
fs.unlinkSync(BUNDLE_PATH);
}

webpack({
cache: true,
entry: path.join(__dirname, 'basic.js'),
output: {
filename: BUNDLE_PATH
},
module: {
loaders: [
{
test: /\.js$/,
loader: path.join(__dirname, '../index') + '?modules[]=' + path.join(__dirname, './macros.sjs')
}
]
}
}, function(err, stats) {
console.log(err);
})
});
});

0 comments on commit 42132d8

Please sign in to comment.