Skip to content
Merged
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
75 changes: 55 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The plugin writes CSS and JS asset paths for you automatically. Works with webpa

## Usage

```
```sh
npm install mini-html-webpack-plugin
```

Expand All @@ -18,11 +18,19 @@ const MiniHtmlWebpackPlugin = require('mini-html-webpack-plugin');
const config = {
plugins: [
new MiniHtmlWebpackPlugin({
// Optional, defaults to `index.html`
filename: 'demo.html',
// Optional
publicPath: 'demo/',
context: {
title: 'Webpack demo',
htmlAttributes: { lang: 'en' } // Optional, defaults to { lang: 'en' }
},
filename: 'demo.html' // Optional, defaults to `index.html`
// Optional, defaults to `{ lang: 'en' }`
htmlAttributes: { lang: 'en' },
// Optional
cssAttributes: { rel: 'preload' },
// Optional
jsAttributes: { defer: 'defer' }
}
})
]
};
Expand Down Expand Up @@ -56,31 +64,58 @@ Or define a template function to generate your own code:
```js
const MiniHtmlWebpackPlugin = require('mini-html-webpack-plugin');
const {
generateAttributes,
generateCSSReferences,
generateJSReferences
} = MiniHtmlWebpackPlugin;

const config = {
plugins: [
new MiniHtmlWebpackPlugin({
filename: 'demo.html',
publicPath: 'demo/',
// `context` is available in `template` below
context: {
title: 'Custom template' // Available in the context below
title: 'Webpack demo',
htmlAttributes: { lang: 'en' },
cssAttributes: { rel: 'preload' },
jsAttributes: { defer: 'defer' }
},
template: ({ css, js, title, htmlAttributes, publicPath }) =>
`<!DOCTYPE html>
<html ${Object.entries(htmlAttributes)
.map(attribute => `${attribute[0]}="${attribute[1]}"`)
.join(' ')}>
<head>
<meta charset="UTF-8">
<title>${title}</title>
${generateCSSReferences(css, publicPath)}
</head>
<body>
<div id="app"></div>
${generateJSReferences(js, publicPath)}
</body>
</html>`
template: ({
css,
js,
publicPath,
title,
htmlAttributes,
cssAttributes,
jsAttributes
}) => {
const htmlAttrs = generateAttributes(htmlAttributes);

const cssTags = generateCSSReferences({
files: css,
attributes: cssAttributes,
publicPath
});

const jsTags = generateJSReferences({
files: js,
attributes: jsAttributes,
publicPath
});

return `<!DOCTYPE html>
<html${htmlAttrs}>
<head>
<meta charset="UTF-8">
<title>${title}</title>
${cssTags}
</head>
<body>
${jsTags}
</body>
</html>`;
}
})
]
};
Expand Down
32 changes: 28 additions & 4 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ exports[`custom filename 1`] = `
<head>
<meta charset=\\"UTF-8\\">
<title></title>


</head>
<body>
Expand All @@ -15,13 +14,26 @@ exports[`custom filename 1`] = `
</html>"
`;

exports[`custom js attribute 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"UTF-8\\">
<title></title>

</head>
<body>
<script src=\\"runtime~main.js\\" defer=\\"defer\\"></script><script src=\\"main.js\\" defer=\\"defer\\"></script>
</body>
</html>"
`;

exports[`custom lang 1`] = `
"<!DOCTYPE html>
<html lang=\\"ru\\">
<head>
<meta charset=\\"UTF-8\\">
<title></title>


</head>
<body>
Expand All @@ -30,6 +42,20 @@ exports[`custom lang 1`] = `
</html>"
`;

exports[`custom publicPath 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"UTF-8\\">
<title></title>

</head>
<body>
<script src=\\"pizza/runtime~main.js\\"></script><script src=\\"pizza/main.js\\"></script>
</body>
</html>"
`;

exports[`custom template 1`] = `"<div>Pizza</div>"`;

exports[`custom title 1`] = `
Expand All @@ -38,7 +64,6 @@ exports[`custom title 1`] = `
<head>
<meta charset=\\"UTF-8\\">
<title>Pizza</title>


</head>
<body>
Expand All @@ -53,7 +78,6 @@ exports[`default options 1`] = `
<head>
<meta charset=\\"UTF-8\\">
<title></title>


</head>
<body>
Expand Down
81 changes: 64 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ class MiniHtmlWebpackPlugin {
}

plugin(compilation, callback) {
const { publicPath } = compilation.options.output;
const { filename = 'index.html', template, context } = this.options;
const {
filename = 'index.html',
publicPath = '',
template,
context,
} = this.options;

const files = getFiles(normalizeEntrypoints(compilation.entrypoints));

const options = Object.assign({}, { publicPath }, context, files);

compilation.assets[filename] = new RawSource(
(template || defaultTemplate)(
Object.assign({}, { publicPath }, context, files)
)
(template || defaultTemplate)(options)
);

callback();
Expand Down Expand Up @@ -64,39 +68,82 @@ function normalizeEntrypoints(entrypoints) {
function defaultTemplate({
css,
js,
publicPath = '',
title = '',
htmlAttributes = { lang: 'en' },
publicPath,
htmlAttributes = {
lang: 'en',
},
cssAttributes = {},
jsAttributes = {},
}) {
const htmlAttrs = generateAttributes(htmlAttributes);

const cssTags = generateCSSReferences({
files: css,
attributes: cssAttributes,
publicPath,
});

const jsTags = generateJSReferences({
files: js,
attributes: jsAttributes,
publicPath,
});

return `<!DOCTYPE html>
<html ${Object.entries(htmlAttributes)
.map(attribute => `${attribute[0]}="${attribute[1]}"`)
.join(' ')}>
<html${htmlAttrs}>
<head>
<meta charset="UTF-8">
<title>${title}</title>

${generateCSSReferences(css, publicPath)}
${cssTags}
</head>
<body>
${generateJSReferences(js, publicPath)}
${jsTags}
</body>
</html>`;
}

function generateCSSReferences(files = [], publicPath = '') {
function generateCSSReferences({
files = [],
publicPath = '',
attributes = {},
}) {
attributes = generateAttributes(attributes);

return files
.map(file => `<link href="${publicPath}${file}" rel="stylesheet">`)
.map(
file => `<link href="${publicPath}${file}" rel="stylesheet"${attributes}>`
)
.join('');
}

function generateJSReferences(files = [], publicPath = '') {
function generateJSReferences({
files = [],
publicPath = '',
attributes = {},
}) {
attributes = generateAttributes(attributes);
Copy link
Member

@sapegin sapegin Jul 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not shadow the function argument to make code easier to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sapegin! In the current release v1.0.0, generateCSSReferences accept two arguments (css, publicPath), in this pull request I changed it to accept an object ({files: css, attributes: cssAttributes, publicPath}). Same situation for generateJSReferences. This is a breaking change for me. Do you think it should be released under v1.1.0 or 2.0.0?


return files
.map(file => `<script src="${publicPath}${file}"></script>`)
.map(file => `<script src="${publicPath}${file}"${attributes}></script>`)
.join('');
}

function generateAttributes(attributes = {}) {
attributes = Object.entries(attributes);

if (attributes.length === 0) {
return '';
}

return (
' ' +
attributes.map(attribute => `${attribute[0]}="${attribute[1]}"`).join(' ')
);
}

module.exports = MiniHtmlWebpackPlugin;
module.exports.defaultTemplate = defaultTemplate;
module.exports.generateAttributes = generateAttributes;
module.exports.generateCSSReferences = generateCSSReferences;
module.exports.generateJSReferences = generateJSReferences;
Loading