Skip to content

Commit

Permalink
feat: add rule to get generated html tags from js
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Dec 27, 2019
1 parent 7aad73d commit 9ac68f6
Show file tree
Hide file tree
Showing 8 changed files with 567 additions and 1 deletion.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,34 @@ plugins: [
],
```

### Rule

A Webpack loader rule is also provided to make the generated HTML tags available to your JS app.

```js
const plugin = new WebappWebpackPlugin({ logo: '/path/to/logo.png' });

...

plugins: [
plugin
]
module: {
rules: [
plugin.rule()
],
}
```

```js
// now inside Webpack bundle
// you can require logo path
// and get an array of strings with html tags
const favicons = require( '/path/to/logo.png' );
```

> **Note**: `logo` must be an absolute path for `rule` to work.
### Compilation Modes

Modes allow you to choose a very fast simplified favicon compilation or a production ready favicon compilation
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const child = require('./compiler');
const Oracle = require('./oracle');
const { tap, tapHtml, getAssetPath } = require('./compat');
const rule = require('./rule');
const path = require('path');
const crypto = require('crypto');

Expand All @@ -17,6 +18,12 @@ module.exports = class FaviconsWebpackPlugin {
favicons: {},
prefix: 'assets/',
}, options);

this.tags = rule.tags();
}

rule() {
return rule.rule(this);
}

apply(compiler) {
Expand Down Expand Up @@ -70,6 +77,8 @@ module.exports = class FaviconsWebpackPlugin {
faviconCompilation = this.generateFaviconsWebapp(compiler, compilation);
}

faviconCompilation.then(this.tags.resolve, this.tags.reject);

// Hook into the html-webpack-plugin processing and add the html
tapHtml(compilation, 'FaviconsWebpackPlugin', (htmlPluginData, htmlWebpackPluginCallback) => {
faviconCompilation.then((tags) => {
Expand Down
36 changes: 36 additions & 0 deletions src/rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const assert = require('assert');
const path = require('path');

module.exports = function loader() {
const callback = this.async();
const { plugin } = this.query;

plugin.tags.promise.then((tags) => {
callback(null, `module.exports = ['${tags.join("', '")}'];`);
}, callback);
};

module.exports.tags = () => {
const tags = {};

tags.promise = new Promise((resolve, reject) => {
tags.resolve = resolve;
tags.reject = reject;
});

tags.promise.catch(() => {});

return tags;
};

module.exports.rule = (plugin) => {
assert(path.isAbsolute(plugin.options.logo), '`logo` must be an absolute path');

const rule = {
include: plugin.options.logo,
loader: __filename,
options: { plugin },
};

return rule;
};
1 change: 1 addition & 0 deletions test/fixtures/rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./logo.png');
58 changes: 58 additions & 0 deletions test/rule.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const test = require('ava');
const path = require('path');
const fs = require('fs-extra');
const FaviconsWebpackPlugin = require('../');

const { logo, mkdir, generate, snapshotCompilationAssets } = require('./util');

test.beforeEach(async t => t.context.root = await mkdir());

test('should fail to generate rule with relative logo', t => {
const plugin = new FaviconsWebpackPlugin({ logo: './path' });
try {
plugin.rule();
} catch (err) {
t.is(err.message, '`logo` must be an absolute path');
}
});

test('should fail to generate rule with module logo', t => {
const plugin = new FaviconsWebpackPlugin({ logo: 'path' });
try {
plugin.rule();
} catch (err) {
t.is(err.message, '`logo` must be an absolute path');
}
});

test('should succeed to generate rule with absolute logo', t => {
const plugin = new FaviconsWebpackPlugin({ logo: '/path' });
const rule = plugin.rule();
t.pass();
});

test('should generate working rule for getting favicon tags', async t => {
const dist = path.join(t.context.root, 'dist');
const plugin = new FaviconsWebpackPlugin({ logo });
const rule = plugin.rule();

const compilationStats = await generate({
entry: path.resolve(__dirname, 'fixtures/rule.js'),
context: t.context.root,
output: {
path: dist,
filename: 'main.jsx',
libraryTarget: 'commonjs2',
},
plugins: [
plugin,
],
module: {
rules: [rule],
},
});

snapshotCompilationAssets(t, compilationStats);
});

test.afterEach(t => fs.remove(t.context.root));
Loading

0 comments on commit 9ac68f6

Please sign in to comment.