From 55e979d67edf98a2bdc73b72d1fd8c1f6b4bfd30 Mon Sep 17 00:00:00 2001 From: pldg Date: Sun, 7 Jul 2019 19:10:03 +0200 Subject: [PATCH 1/2] Feat: Add `cssAttributes` `jsAttributes` options (resolve #5) - Add `generateAttributes` utility method - Add test for `jsAttributes` - Refactoring `generateCSSReferences` `generateJSReferences` - Update readme to reflect the changes --- README.md | 75 ++++++++++++++++++++++++++++---------- __snapshots__/test.js.snap | 18 +++++++-- index.js | 73 ++++++++++++++++++++++++++++++------- package-lock.json | 41 +++++++++++++++------ test.js | 9 +++++ 5 files changed, 168 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 4ca7136..862bc09 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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' } + } }) ] }; @@ -56,6 +64,7 @@ Or define a template function to generate your own code: ```js const MiniHtmlWebpackPlugin = require('mini-html-webpack-plugin'); const { + generateAttributes, generateCSSReferences, generateJSReferences } = MiniHtmlWebpackPlugin; @@ -63,24 +72,50 @@ const { 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 }) => - ` - `${attribute[0]}="${attribute[1]}"`) - .join(' ')}> - - - ${title} - ${generateCSSReferences(css, publicPath)} - - -
- ${generateJSReferences(js, publicPath)} - - ` + template: ({ + css, + js, + publicPath, + title, + htmlAttributes, + cssAttributes, + jsAttributes + }) => { + const htmlAttrs = generateAttributes(htmlAttributes); + + const cssTags = generateCSSReferences({ + files: css, + publicPath, + cssAttributes: generateAttributes(cssAttributes) + }); + + const jsTags = generateJSReferences({ + files: js, + publicPath, + jsAttributes: generateAttributes(jsAttributes) + }); + + return ` + + + + ${title} + ${cssTags} + + + ${jsTags} + + `; + } }) ] }; diff --git a/__snapshots__/test.js.snap b/__snapshots__/test.js.snap index e9e0e2e..c05777e 100644 --- a/__snapshots__/test.js.snap +++ b/__snapshots__/test.js.snap @@ -6,7 +6,6 @@ exports[`custom filename 1`] = ` - @@ -15,13 +14,26 @@ exports[`custom filename 1`] = ` " `; +exports[`custom js attribute 1`] = ` +" + + + + + + + + + + " +`; + exports[`custom lang 1`] = ` " - @@ -38,7 +50,6 @@ exports[`custom title 1`] = ` Pizza - @@ -53,7 +64,6 @@ exports[`default options 1`] = ` - diff --git a/index.js b/index.js index bc322be..d7706d8 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,14 @@ class MiniHtmlWebpackPlugin { compilation.assets[filename] = new RawSource( (template || defaultTemplate)( - Object.assign({}, { publicPath }, context, files) + Object.assign( + {}, + { + publicPath, + }, + context, + files + ) ) ); @@ -64,39 +71,79 @@ 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, + publicPath, + cssAttributes: generateAttributes(cssAttributes), + }); + + const jsTags = generateJSReferences({ + files: js, + publicPath, + jsAttributes: generateAttributes(jsAttributes), + }); + return ` - `${attribute[0]}="${attribute[1]}"`) - .join(' ')}> + ${title} - - ${generateCSSReferences(css, publicPath)} + ${cssTags} - ${generateJSReferences(js, publicPath)} + ${jsTags} `; } -function generateCSSReferences(files = [], publicPath = '') { +function generateCSSReferences({ + files = [], + publicPath = '', + cssAttributes = '', +}) { return files - .map(file => ``) + .map( + file => + `` + ) .join(''); } -function generateJSReferences(files = [], publicPath = '') { +function generateJSReferences({ + files = [], + publicPath = '', + jsAttributes = '', +}) { return files - .map(file => ``) + .map(file => ``) .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; diff --git a/package-lock.json b/package-lock.json index d7d6bd5..e470cc5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2732,7 +2732,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2753,12 +2754,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2773,17 +2776,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2900,7 +2906,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2912,6 +2919,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2926,6 +2934,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2933,12 +2942,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2957,6 +2968,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -3037,7 +3049,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -3049,6 +3062,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -3134,7 +3148,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -3170,6 +3185,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3189,6 +3205,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3232,12 +3249,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/test.js b/test.js index cdd442f..9f984e9 100644 --- a/test.js +++ b/test.js @@ -33,6 +33,15 @@ test('custom lang', () => { }); }); +test('custom js attribute', () => { + return compiler( + {}, + getConfig({ context: { jsAttributes: { defer: 'defer' } } }) + ).then(result => { + expect(result.compilation.assets['index.html']._value).toMatchSnapshot(); + }); +}); + test('custom template', () => { return compiler( {}, From ff05aa4826deb306a00863d0c4bcd86109eef09f Mon Sep 17 00:00:00 2001 From: pldg Date: Fri, 12 Jul 2019 15:22:11 +0200 Subject: [PATCH 2/2] Fix: #11, publicPath - `generateCSSReferences` and `generateJSReferences` now accept an object without the need to use `generateAttributes` utility - Fix `publicPath` option (didn't work) - Add test for `publicPath` - Update readme to reflect the changes above --- README.md | 8 ++++---- __snapshots__/test.js.snap | 14 ++++++++++++++ index.js | 38 +++++++++++++++++++------------------- test.js | 8 +++----- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 862bc09..bb518e0 100644 --- a/README.md +++ b/README.md @@ -94,14 +94,14 @@ const config = { const cssTags = generateCSSReferences({ files: css, - publicPath, - cssAttributes: generateAttributes(cssAttributes) + attributes: cssAttributes, + publicPath }); const jsTags = generateJSReferences({ files: js, - publicPath, - jsAttributes: generateAttributes(jsAttributes) + attributes: jsAttributes, + publicPath }); return ` diff --git a/__snapshots__/test.js.snap b/__snapshots__/test.js.snap index c05777e..5b19448 100644 --- a/__snapshots__/test.js.snap +++ b/__snapshots__/test.js.snap @@ -42,6 +42,20 @@ exports[`custom lang 1`] = ` " `; +exports[`custom publicPath 1`] = ` +" + + + + + + + + + + " +`; + exports[`custom template 1`] = `"
Pizza
"`; exports[`custom title 1`] = ` diff --git a/index.js b/index.js index d7706d8..0236109 100644 --- a/index.js +++ b/index.js @@ -8,22 +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(); @@ -83,14 +80,14 @@ function defaultTemplate({ const cssTags = generateCSSReferences({ files: css, + attributes: cssAttributes, publicPath, - cssAttributes: generateAttributes(cssAttributes), }); const jsTags = generateJSReferences({ files: js, + attributes: jsAttributes, publicPath, - jsAttributes: generateAttributes(jsAttributes), }); return ` @@ -109,12 +106,13 @@ function defaultTemplate({ function generateCSSReferences({ files = [], publicPath = '', - cssAttributes = '', + attributes = {}, }) { + attributes = generateAttributes(attributes); + return files .map( - file => - `` + file => `` ) .join(''); } @@ -122,10 +120,12 @@ function generateCSSReferences({ function generateJSReferences({ files = [], publicPath = '', - jsAttributes = '', + attributes = {}, }) { + attributes = generateAttributes(attributes); + return files - .map(file => ``) + .map(file => ``) .join(''); } diff --git a/test.js b/test.js index 9f984e9..414db4a 100644 --- a/test.js +++ b/test.js @@ -61,10 +61,8 @@ test('custom filename', () => { }); }); -// TODO: https://github.com/webpack-contrib/test-utils/issues/2 -/* test('custom publicPath', () => { - const publicPath = '/pizza/'; - return compiler({}, getConfig({}, { output: { publicPath } })).then(result => { +test('custom publicPath', () => { + return compiler({}, getConfig({ publicPath: 'pizza/' })).then(result => { expect(result.compilation.assets['index.html']._value).toMatchSnapshot(); }); -}); */ +});