Skip to content

Commit

Permalink
feat: allow to specify a custom output paths
Browse files Browse the repository at this point in the history
  • Loading branch information
okcoker authored and jantimon committed Aug 14, 2019
1 parent 0494f05 commit 0755161
Show file tree
Hide file tree
Showing 105 changed files with 265 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ plugins: [
// Enable caching and optionally specify the path to store cached data
// Note: disabling caching may increase build times considerably
cache: true,
// Override the publicPath option usually read from webpack configuration
publicPath: '/static',
// The directory to output the assets relative to the webpack output dir.
// Relative string paths are allowed here ie '../public/static'. If this
// option is not set, `prefix` is used.
outputPath: '/public/static',
// Prefix path for generated assets
prefix: 'assets/',
// Inject html links/metadata (requires html-webpack-plugin).
Expand Down
6 changes: 3 additions & 3 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ const findCacheDir = require('find-cache-dir');
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const { getAssetPath } = require('./compat');

module.exports.run = ({ prefix, favicons: options, logo, cache }, context, compilation) => {
module.exports.run = ({ prefix, favicons: options, logo, cache, publicPath: publicPathOption, outputPath }, context, compilation) => {
// The entry file is just an empty helper
const filename = '[hash]';
const { publicPath = '/' } = compilation.outputOptions;
const publicPath = publicPathOption || compilation.outputOptions.publicPath || '/';

// Create an additional child compiler which takes the template
// and turns it into an Node.JS html factory.
// This allows us to use loaders during the compilation
const compiler = compilation.createChildCompiler('favicons-webpack-plugin', { filename, publicPath });
compiler.context = context;

const loader = `!${require.resolve('./loader')}?${JSON.stringify({ prefix, options, path: publicPath })}`;
const loader = `!${require.resolve('./loader')}?${JSON.stringify({ prefix, options, path: publicPath, outputPath })}`;

const cacheDirectory = cache && (
(typeof cache === 'string')
Expand Down
4 changes: 2 additions & 2 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ module.exports = function (content) {
context: getContext(this),
content: msgpack.encode([content, query.options, pkg.version]), // hash must depend on logo + config + version
}));

const outputPath = query.outputPath ? trailingSlash(query.outputPath) : prefix;
// Generate icons
return favicons(content, Object.assign(query.options, { path: url.resolve(path, prefix) }))
.then(({ html: tags, images, files }) => {
const assets = [...images, ...files].map(({ name, contents }) => ({ name: prefix + name, contents }));
const assets = [...images, ...files].map(({ name, contents }) => ({ name: outputPath + name, contents }));
return callback(null, `module.exports = '${msgpack.encode({ tags, assets }).toString('base64')}'`);
})
.catch(callback);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/expected/outputpathoption/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html><head><link rel="shortcut icon" href="/public/path/assets/favicon.ico"><link rel="icon" type="image/png" sizes="16x16" href="/public/path/assets/favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="/public/path/assets/favicon-32x32.png"><link rel="manifest" href="/public/path/assets/manifest.json"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#fff"><meta name="application-name"><link rel="apple-touch-icon" sizes="57x57" href="/public/path/assets/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/public/path/assets/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/public/path/assets/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/public/path/assets/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/public/path/assets/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/public/path/assets/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/public/path/assets/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/public/path/assets/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="167x167" href="/public/path/assets/apple-touch-icon-167x167.png"><link rel="apple-touch-icon" sizes="180x180" href="/public/path/assets/apple-touch-icon-180x180.png"><link rel="apple-touch-icon" sizes="1024x1024" href="/public/path/assets/apple-touch-icon-1024x1024.png"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"><meta name="apple-mobile-web-app-title"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)" href="/public/path/assets/apple-touch-startup-image-320x460.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" href="/public/path/assets/apple-touch-startup-image-640x920.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" href="/public/path/assets/apple-touch-startup-image-640x1096.png"><link rel="apple-touch-startup-image" media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)" href="/public/path/assets/apple-touch-startup-image-750x1294.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 3)" href="/public/path/assets/apple-touch-startup-image-1182x2208.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 3)" href="/public/path/assets/apple-touch-startup-image-1242x2148.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 1)" href="/public/path/assets/apple-touch-startup-image-748x1024.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 1)" href="/public/path/assets/apple-touch-startup-image-768x1004.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)" href="/public/path/assets/apple-touch-startup-image-1496x2048.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)" href="/public/path/assets/apple-touch-startup-image-1536x2008.png"><link rel="icon" type="image/png" sizes="228x228" href="/public/path/assets/coast-228x228.png"><meta name="msapplication-TileColor" content="#fff"><meta name="msapplication-TileImage" content="/public/path/assets/mstile-144x144.png"><meta name="msapplication-config" content="/public/path/assets/browserconfig.xml"><link rel="yandex-tableau-widget" href="/public/path/assets/yandex-browser-manifest.json"></head><body></body></html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square70x70logo src="/public/path/assets/mstile-70x70.png"/>
<square150x150logo src="/public/path/assets/mstile-150x150.png"/>
<wide310x150logo src="/public/path/assets/mstile-310x150.png"/>
<square310x310logo src="/public/path/assets/mstile-310x310.png"/>
<TileColor>#fff</TileColor>

</tile>

</msapplication>

</browserconfig>
Binary file not shown.
59 changes: 59 additions & 0 deletions test/fixtures/expected/outputpathoption/test/path/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": null,
"short_name": null,
"description": null,
"dir": "auto",
"lang": "en-US",
"display": "standalone",
"orientation": "any",
"start_url": "/?homescreen=1",
"background_color": "#fff",
"theme_color": "#fff",
"icons": [
{
"src": "/public/path/assets/android-chrome-36x36.png",
"sizes": "36x36",
"type": "image/png"
},
{
"src": "/public/path/assets/android-chrome-48x48.png",
"sizes": "48x48",
"type": "image/png"
},
{
"src": "/public/path/assets/android-chrome-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "/public/path/assets/android-chrome-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "/public/path/assets/android-chrome-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "/public/path/assets/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/public/path/assets/android-chrome-256x256.png",
"sizes": "256x256",
"type": "image/png"
},
{
"src": "/public/path/assets/android-chrome-384x384.png",
"sizes": "384x384",
"type": "image/png"
},
{
"src": "/public/path/assets/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
14 changes: 14 additions & 0 deletions test/fixtures/expected/outputpathoption/test/path/manifest.webapp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1.0",
"name": null,
"description": null,
"icons": {
"60": "/public/path/assets/firefox_app_60x60.png",
"128": "/public/path/assets/firefox_app_128x128.png",
"512": "/public/path/assets/firefox_app_512x512.png"
},
"developer": {
"name": null,
"url": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "1.0",
"api_version": 1,
"layout": {
"logo": "/public/path/assets/yandex-browser-50x50.png",
"color": "#fff",
"show_title": true
}
}
15 changes: 15 additions & 0 deletions test/fixtures/expected/publicpathoption/assets/browserconfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square70x70logo src="/another/path/assets/mstile-70x70.png"/>
<square150x150logo src="/another/path/assets/mstile-150x150.png"/>
<wide310x150logo src="/another/path/assets/mstile-310x150.png"/>
<square310x310logo src="/another/path/assets/mstile-310x310.png"/>
<TileColor>#fff</TileColor>

</tile>

</msapplication>

</browserconfig>
Binary file not shown.
59 changes: 59 additions & 0 deletions test/fixtures/expected/publicpathoption/assets/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": null,
"short_name": null,
"description": null,
"dir": "auto",
"lang": "en-US",
"display": "standalone",
"orientation": "any",
"start_url": "/?homescreen=1",
"background_color": "#fff",
"theme_color": "#fff",
"icons": [
{
"src": "/another/path/assets/android-chrome-36x36.png",
"sizes": "36x36",
"type": "image/png"
},
{
"src": "/another/path/assets/android-chrome-48x48.png",
"sizes": "48x48",
"type": "image/png"
},
{
"src": "/another/path/assets/android-chrome-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "/another/path/assets/android-chrome-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "/another/path/assets/android-chrome-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "/another/path/assets/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/another/path/assets/android-chrome-256x256.png",
"sizes": "256x256",
"type": "image/png"
},
{
"src": "/another/path/assets/android-chrome-384x384.png",
"sizes": "384x384",
"type": "image/png"
},
{
"src": "/another/path/assets/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
14 changes: 14 additions & 0 deletions test/fixtures/expected/publicpathoption/assets/manifest.webapp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1.0",
"name": null,
"description": null,
"icons": {
"60": "/another/path/assets/firefox_app_60x60.png",
"128": "/another/path/assets/firefox_app_128x128.png",
"512": "/another/path/assets/firefox_app_512x512.png"
},
"developer": {
"name": null,
"url": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "1.0",
"api_version": 1,
"layout": {
"logo": "/another/path/assets/yandex-browser-50x50.png",
"color": "#fff",
"show_title": true
}
}
1 change: 1 addition & 0 deletions test/fixtures/expected/publicpathoption/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html><head><link rel="shortcut icon" href="/another/path/assets/favicon.ico"><link rel="icon" type="image/png" sizes="16x16" href="/another/path/assets/favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="/another/path/assets/favicon-32x32.png"><link rel="manifest" href="/another/path/assets/manifest.json"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#fff"><meta name="application-name"><link rel="apple-touch-icon" sizes="57x57" href="/another/path/assets/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/another/path/assets/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/another/path/assets/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/another/path/assets/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/another/path/assets/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/another/path/assets/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/another/path/assets/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/another/path/assets/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="167x167" href="/another/path/assets/apple-touch-icon-167x167.png"><link rel="apple-touch-icon" sizes="180x180" href="/another/path/assets/apple-touch-icon-180x180.png"><link rel="apple-touch-icon" sizes="1024x1024" href="/another/path/assets/apple-touch-icon-1024x1024.png"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"><meta name="apple-mobile-web-app-title"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)" href="/another/path/assets/apple-touch-startup-image-320x460.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" href="/another/path/assets/apple-touch-startup-image-640x920.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" href="/another/path/assets/apple-touch-startup-image-640x1096.png"><link rel="apple-touch-startup-image" media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)" href="/another/path/assets/apple-touch-startup-image-750x1294.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 3)" href="/another/path/assets/apple-touch-startup-image-1182x2208.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 3)" href="/another/path/assets/apple-touch-startup-image-1242x2148.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 1)" href="/another/path/assets/apple-touch-startup-image-748x1024.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 1)" href="/another/path/assets/apple-touch-startup-image-768x1004.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)" href="/another/path/assets/apple-touch-startup-image-1496x2048.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)" href="/another/path/assets/apple-touch-startup-image-1536x2008.png"><link rel="icon" type="image/png" sizes="228x228" href="/another/path/assets/coast-228x228.png"><meta name="msapplication-TileColor" content="#fff"><meta name="msapplication-TileImage" content="/another/path/assets/mstile-144x144.png"><meta name="msapplication-config" content="/another/path/assets/browserconfig.xml"><link rel="yandex-tableau-widget" href="/another/path/assets/yandex-browser-manifest.json"></head><body></body></html>
29 changes: 29 additions & 0 deletions test/outputpathoption.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const test = require('ava');
const path = require('path');
const fs = require('fs-extra');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const FaviconsWebpackPlugin = require('../');

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

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

test('should allow for overriding the output path of favicons', async t => {
const dist = path.join(t.context.root, 'dist');

await generate({
context: t.context.root,
output: {
path: dist,
publicPath: '/public/path',
},
plugins: [
new HtmlWebpackPlugin(),
new FaviconsWebpackPlugin({ logo, outputPath: 'test/path' }),
],
});

t.deepEqual(await compare(dist, path.resolve(expected, 'outputpathoption')), []);
});

test.afterEach(t => fs.remove(t.context.root));
29 changes: 29 additions & 0 deletions test/publicpathoption.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const test = require('ava');
const path = require('path');
const fs = require('fs-extra');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const FaviconsWebpackPlugin = require('../');

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

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

test('should allow for overriding the publicPath option', async t => {
const dist = path.join(t.context.root, 'dist');

await generate({
context: t.context.root,
output: {
path: dist,
publicPath: '/public/path',
},
plugins: [
new HtmlWebpackPlugin(),
new FaviconsWebpackPlugin({ logo, publicPath: '/another/path' }),
],
});

t.deepEqual(await compare(dist, path.resolve(expected, 'publicpathoption')), []);
});

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

0 comments on commit 0755161

Please sign in to comment.