Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option inject #174

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ look like:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta charset="UTF-8" />
<title>Webpack App</title>
</head>
<body>
Expand Down Expand Up @@ -149,6 +149,19 @@ An example of this is included in the repository.

Currently only supports script tags.

### `inject`

Type: `boolean | function`, default: `true`

Inject conditionally.

This option accepts arguments of different types:

- boolean `false`: disables injection `true`: enables injection (defaults)
- function any predicate that takes an instance of html-webpack-plugin and
returns either `true` or `false` to control the injection of html metadata for
the html files generated by this instance.

## Examples

When adding assets, it's added to the start of the array, so when
Expand Down
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default class AddAssetHtmlPlugin {
publicPath,
outputPath,
files = [],
inject = true,
},
) {
if (!filepath) {
Expand All @@ -95,6 +96,15 @@ export default class AddAssetHtmlPlugin {

const fileFilters = Array.isArray(files) ? files : [files];

const shouldInject =
typeof inject === 'function'
? inject(htmlPluginData.plugin)
: Boolean(inject);

if (!shouldInject) {
return;
}

if (fileFilters.length > 0) {
const shouldSkip = !fileFilters.some(file =>
micromatch.isMatch(htmlPluginData.outputName, file),
Expand Down
76 changes: 76 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,79 @@ test('filepath without globbyMagic should just return', async () => {
const ret = await handleUrl(assets);
expect(ret).toHaveLength(1);
});

describe('inject assets conditionally', () => {
test('does not inject if false', async () => {
const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = Object.assign(
{ assets: { js: [], css: [] } },
pluginMock,
);
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
inject: false,
});

await plugin.addAllAssetsToCompilation(compilation, pluginData);

expect(pluginData.assets.js).toHaveLength(0);
});

test('if inject is a callback it gets called with the HtmlPlugin data', async () => {
const mock = {
plugin: {
options: {
HTML_PLUGIN_OPTION: 'hello',
},
},
};

const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = Object.assign({ assets: { js: [], css: [] } }, mock);
const injectCallback = jest.fn();
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
inject: injectCallback,
});

await plugin.addAllAssetsToCompilation(compilation, pluginData);

expect(injectCallback).toHaveBeenCalledWith(mock.plugin);
});

test('does not inject if the callback returns false', async () => {
const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = Object.assign(
{ assets: { js: [], css: [] } },
pluginMock,
);
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
inject: () => {
return false;
},
});

await plugin.addAllAssetsToCompilation(compilation, pluginData);

expect(pluginData.assets.js).toHaveLength(0);
});

test('injects if the callback returns true', async () => {
const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = Object.assign(
{ assets: { js: [], css: [] } },
pluginMock,
);
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
inject: () => {
return true;
},
});

await plugin.addAllAssetsToCompilation(compilation, pluginData);

expect(pluginData.assets.js).toHaveLength(1);
});
});