Skip to content

Commit

Permalink
Tap into compiler's emit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jouni-kantola committed Nov 27, 2019
1 parent 8c53993 commit b604041
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
51 changes: 14 additions & 37 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,30 @@
"use strict";

const RawSource = require("webpack-sources").RawSource;

const log = require("./logger").log;
const RuleSet = require("./rule-set");
const CompiledChunks = require("./compiled-chunks");
const publicPath = require("./public-path");
const TemplatedAssets = require("./templated-assets");
const tap = require("./tap");

class Plugin {
constructor(opts) {
const options = opts || {};
let options = opts || {};
if (!options.rules) {
log("No rules specified. No templated chunks will be outputted.");
log(options);
options.rules = [];
}

this.rules = RuleSet.from(options.rules);
this.pluginName = "TemplatedAssetsWebpackPlugin";
this.pluginOptions = options;
}

apply(compiler) {
compiler.plugin("emit", (compilation, callback) => {
const chunks = new CompiledChunks(
compilation.chunks,
compilation.assets,
publicPath(compilation)
).chunks;

new TemplatedAssets(chunks, this.rules)
.process()
.then(templatedAssets => {
templatedAssets.forEach(asset => {
if (!asset.emitAsset) return;
try {
compilation.assets[asset.filename] = new RawSource(asset.source);
} catch (e) {
throw new Error(
`Failed to include asset ${JSON.stringify(
asset
)} to compilation.
${e.message}`
);
}
});

callback();
})
.catch(log);
});
if (!!compiler.hooks) {
compiler.hooks.emit.tapAsync(this.pluginName, (compilation, callback) =>
tap(this.pluginOptions, compilation, callback)
);
} else {
compiler.plugin("emit", (compilation, callback) =>
tap(this.pluginOptions, compilation, callback)
);
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions lib/tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const RawSource = require("webpack-sources").RawSource;

const RuleSet = require("./rule-set");
const CompiledChunks = require("./compiled-chunks");
const publicPath = require("./public-path");
const TemplatedAssets = require("./templated-assets");
const log = require("./logger").log;

function tap(options, compilation, callback) {
const rules = RuleSet.from(options.rules);
const chunks = new CompiledChunks(
compilation.chunks,
compilation.assets,
publicPath(compilation)
).chunks;

new TemplatedAssets(chunks, rules)
.process()
.then(templatedAssets => {
templatedAssets.forEach(asset => {
if (!asset.emitAsset) return;
try {
compilation.assets[asset.filename] = new RawSource(asset.source);
} catch (e) {
throw new Error(
`Failed to include asset ${JSON.stringify(asset)} to compilation.
${e.message}`
);
}
});

callback();
})
.catch(log);
}

module.exports = tap;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "templated-assets-webpack-plugin",
"version": "1.2.0",
"version": "1.3.0",
"description": "webpack plugin for creating assets to be used with server rendered web frameworks.",
"main": "index.js",
"dependencies": {
Expand Down
9 changes: 3 additions & 6 deletions test/plugin-init-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import test from "ava";
import Plugin from "../lib/plugin";

test("fallback to empty rule set", t => {
const ruleSet = new Plugin().rules;
const rules = ruleSet.rules;

const { rules } = new Plugin().pluginOptions;
t.true(Array.isArray(rules));
t.is(rules.length, 0);
});

test("init rules", t => {
const ruleSet = new Plugin({
const { rules } = new Plugin({
rules: [{ name: "an-entry" }]
}).rules;
const rules = ruleSet.rules;
}).pluginOptions;

t.true(Array.isArray(rules));
t.is(rules.length, 1);
Expand Down

0 comments on commit b604041

Please sign in to comment.