Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Pitching loader support
Browse files Browse the repository at this point in the history
  • Loading branch information
amireh committed Mar 28, 2016
1 parent bb40b78 commit 39a94e2
Show file tree
Hide file tree
Showing 24 changed files with 568 additions and 195 deletions.
1 change: 1 addition & 0 deletions .istanbul.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ instrumentation:
excludes:
- "*.test.js"
- "HappyTestUtils.js"
- "__tests__/fixtures/**/*"
extensions:
- .js
reporting:
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
- [ ] stop serializing options and instead accept webpack config file path and populate fake loader context with that so that worker loaders get access to external options
- [ ] pitching loader applier
- [x] pitching loader applier
7 changes: 7 additions & 0 deletions examples/dependency/loader-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(s) {
return s;
};

module.exports.pitch = function(x, o) {
console.log('[a] in pitch!!\n [remaining] => %s\n [preceding] => %s', x, o)
};
7 changes: 7 additions & 0 deletions examples/dependency/loader-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(s) {
return s;
};

module.exports.pitch = function(x, o) {
console.log('[b] in pitch!!\n [remaining] => %s\n [preceding] => %s', x, o)
};
7 changes: 7 additions & 0 deletions examples/dependency/loader-c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(s) {
return s;
};

module.exports.pitch = function(x, o) {
console.log('[c] in pitch!!\n [remaining] => %s\n [preceding] => %s', x, o)
};
11 changes: 0 additions & 11 deletions examples/dependency/loader.js

This file was deleted.

6 changes: 5 additions & 1 deletion examples/dependency/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ module.exports = {
{
test: /\.js$/,
include: [ path.resolve(__dirname, 'lib') ],
loaders: [ path.resolve(__dirname, 'loader.js') ],
loaders: [
path.resolve(__dirname, 'loader-c.js'),
path.resolve(__dirname, 'loader-b.js'),
path.resolve(__dirname, 'loader-a.js'),
],
},
]
},
Expand Down
1 change: 1 addition & 0 deletions lib/HappyFakeLoaderContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function HappyFakeLoaderContext() {
// for loader RPCs, like this.emitWarning()
this._remoteLoaderId = null;

this.version = 1; // -.-' https://webpack.github.io/docs/loaders.html#version
this.request = null;
this.query = null;

Expand Down
16 changes: 8 additions & 8 deletions lib/HappyPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,16 @@ HappyPlugin.prototype.start = function(compiler, done) {
], done);
};

HappyPlugin.prototype.compile = function(source, map, request, done) {
HappyPlugin.prototype.compile = function(code, map, request, done) {
if (this.state.initialBuildCompleted) {
return this.compileInForeground(source, map, request, done);
return this.compileInForeground(code, map, request, done);
}
else {
return this.compileInBackground(source, map, request, done);
return this.compileInBackground(code, map, request, done);
}
};

HappyPlugin.prototype.compileInBackground = function(source, map, loaderContext, done) {
HappyPlugin.prototype.compileInBackground = function(code, map, loaderContext, done) {
var cache = this.cache;
var filePath = loaderContext.resourcePath;

Expand Down Expand Up @@ -248,7 +248,7 @@ HappyPlugin.prototype.compileInBackground = function(source, map, loaderContext,
});
};

HappyPlugin.prototype.compileInForeground = function(source, map, loaderContext, done) {
HappyPlugin.prototype.compileInForeground = function(code, map, loaderContext, done) {
var filePath = loaderContext.resourcePath;
var runContext = {
loaders: this.config.loaders,
Expand All @@ -260,7 +260,7 @@ HappyPlugin.prototype.compileInForeground = function(source, map, loaderContext,
// successful version (if any)
this.cache.invalidateEntryFor(filePath);

WebpackUtils.applyLoaders(runContext, source, map, function(err, result) {
WebpackUtils.applyLoaders(runContext, code, map, function(err, compiledCode, compiledMap) {
if (err) {
return done(err);
}
Expand All @@ -270,12 +270,12 @@ HappyPlugin.prototype.compileInForeground = function(source, map, loaderContext,
HappyUtils.generateCompiledPath(filePath)
);

fs.writeFileSync(compiledPath, result.code, 'utf-8');
fs.writeFileSync(compiledPath, compiledCode, 'utf-8');

// TODO: SourceMaps??
this.cache.updateMTimeFor(filePath, compiledPath);

done(null, result.code, result.map);
done(null, compiledCode, compiledMap);
}.bind(this));
};

Expand Down
65 changes: 65 additions & 0 deletions lib/HappyTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@ var fs = require('fs-extra');
var sinon = require('sinon');
var chai = require('chai');
var HappyPlugin = require('./HappyPlugin');
var HappyRPCHandler = require('./HappyRPCHandler');
var TEMP_DIR = '/tmp/happypack';
var TestUtils = exports;
var sandbox;
var cleanups = [];

sinon.assert.expose(chai.assert, { prefix: "" });

beforeEach(function() {
sandbox = sinon.sandbox.create();

fs.ensureDirSync(TEMP_DIR);
fs.emptyDirSync(TEMP_DIR);
});

afterEach(function() {
cleanups.forEach(function(callback) {
callback();
});

cleanups = [];

fs.removeSync(TEMP_DIR);

sandbox.restore();
});

exports.HAPPY_LOADER_PATH = path.resolve(__dirname, 'HappyLoader.js');
Expand Down Expand Up @@ -90,6 +103,58 @@ exports.getWebpackOutputBundlePath = function(rawStats, name) {
return path.join(rawStats.compilation.outputOptions.path, name || 'bundle.js');
};

// Listen for HappyLoader instances registering themselves to HappyRPCHandler,
// grab that instance, and yield it so that you can install your spies and such.
//
// @return {Function}
// Returns the latest active loader instance, if any.
exports.spyOnActiveLoader = function(fn) {
var registerActiveLoader = HappyRPCHandler.registerActiveLoader;
var happyLoader;

sandbox.stub(HappyRPCHandler, 'registerActiveLoader', function(id, loader) {
happyLoader = loader;

if (fn) {
fn(happyLoader);
}

return registerActiveLoader.apply(HappyRPCHandler, arguments);
});

Object.defineProperty(TestUtils, 'activeLoader', {
configurable: true,
enumerable: false,
get: function() {
return happyLoader;
}
});

cleanups.push(function() {
happyLoader = null;
});

return function() { return happyLoader; };
};

exports.assertNoWebpackErrors = function(err, rawStats, done) {
if (err) return done(err);

var stats = rawStats.toJson();

if (stats.errors.length) {
return done(stats.errors);
}

if (stats.warnings.length) {
return done(stats.warnings);
}
};

exports.getSinonSandbox = function() {
return sandbox;
};

function interpolateGUID(string) {
return string.replace('[guid]', guid());
}
Expand Down
12 changes: 1 addition & 11 deletions lib/HappyThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var assert = require('assert');
var HappyUtils = require('./HappyUtils');
var HappyLogger = require('./HappyLogger');
var HappyRPCHandler = require('./HappyRPCHandler');
var Once = require('./fnOnce');
var events = require('events');
var WORKER_BIN = path.resolve(__dirname, 'HappyWorkerChannel.js');
var EventEmitter = events.EventEmitter || events;
Expand Down Expand Up @@ -105,14 +106,3 @@ module.exports = HappyThread;
function throwError(e) {
throw e;
}

function Once(fn) {
var called = false;

return function() {
if (!called) {
called = true;
return fn.apply(null, arguments);
}
}
}
4 changes: 2 additions & 2 deletions lib/HappyWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ HappyWorker.prototype.compile = function(params, done) {
loaders: this.options.loaders,
loaderContext: params.loaderContext,
compilerOptions: this.options.compilerOptions
}, source, null, function(err, result) {
}, source, null, function(err, code/*, map*/) {
if (err) {
console.error(err);
fs.writeFileSync(compiledPath, serializeError(err), 'utf-8');
}
else {
fs.writeFileSync(compiledPath, result.code /* TODO sourcemap */);
fs.writeFileSync(compiledPath, code /* TODO sourcemap */);
success = true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/HappyPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('HappyPlugin', function() {
loaders: [createLoader(s => s + '123')]
});

subject.compileInForeground('hello!', null, { resourcePath: 'a.js' }, function(err, source, map) {
subject.compileInForeground('hello!', null, { request: 'a.js', resourcePath: 'a.js' }, function(err, source, map) {
if (err) {
return done(err);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/HappyThread.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("HappyThread", function() {
subject.open(function(openError) {
if (openError) return done(openError);

subject.compile({ resourcePath: inputFile.getPath() }, function(result) {
subject.compile({ request: inputFile.getPath(), resourcePath: inputFile.getPath() }, function(result) {
assert.equal(result.error, undefined);
assert.equal(fs.readFileSync(result.compiledPath, 'utf-8'), 'hehe');

Expand Down
2 changes: 2 additions & 0 deletions lib/__tests__/HappyWorkerChannel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('HappyWorkerChannel', function() {
sourcePath: fixturePath('a.js'),
compiledPath: tempPath('a.out'),
loaderContext: {
request: fixturePath('a.js'),
resourcePath: fixturePath('a.js')
}
});
Expand All @@ -56,6 +57,7 @@ describe('HappyWorkerChannel', function() {
sourcePath: fixturePath('a.js'),
compiledPath: tempPath('a.out'),
loaderContext: {
request: fixturePath('a.js'),
resourcePath: fixturePath('a.js')
}
});
Expand Down
Loading

0 comments on commit 39a94e2

Please sign in to comment.