From 78a92f511754084d162d472ba5d7fa69c2e0c173 Mon Sep 17 00:00:00 2001 From: TrueXPixells Date: Fri, 22 Nov 2024 20:35:43 +0000 Subject: [PATCH 1/2] impl feat --- packages/html/README.md | 7 +++ packages/html/recipes/external-files/index.js | 9 +++- packages/html/src/index.ts | 20 +++++--- packages/html/test/snapshots/test.js.md | 46 ++++++++++++++++++ packages/html/test/snapshots/test.js.snap | Bin 1452 -> 1529 bytes packages/html/test/test.js | 17 +++++++ packages/html/types/index.d.ts | 2 + 7 files changed, 93 insertions(+), 8 deletions(-) diff --git a/packages/html/README.md b/packages/html/README.md index 56e5d68b0..b3a047543 100644 --- a/packages/html/README.md +++ b/packages/html/README.md @@ -121,6 +121,13 @@ Default: `'Rollup Bundle'` Specifies the HTML document title. +### `scriptsOnHead` + +Type: `Boolean`
+Default: `false` + +Place scripts in head instead body. + ## Exports ### `makeHtmlAttributes(attributes)` diff --git a/packages/html/recipes/external-files/index.js b/packages/html/recipes/external-files/index.js index ea6697fe2..2c0553b20 100644 --- a/packages/html/recipes/external-files/index.js +++ b/packages/html/recipes/external-files/index.js @@ -4,10 +4,10 @@ * @param {Array} externals List of external files. * The format is: [{ type: 'js', file: '//xxxx1.js', pos: 'before' }, { type: 'css', file: '//xxxx1.css' }] * - * @return {Function} The templae method required by plugin-html + * @return {Function} The template method required by plugin-html */ export default function htmlTemplate(externals) { - return ({ attributes, files, meta, publicPath, title }) => { + return ({ attributes, files, meta, publicPath, title, scriptsOnHead }) => { let scripts = [...(files.js || [])]; let links = [...(files.css || [])]; @@ -43,6 +43,11 @@ export default function htmlTemplate(externals) { }) .join('\n'); + if (scriptsOnHead === true) { + links += scripts; + scripts = ''; + } + const metas = meta .map((input) => { const attrs = makeHtmlAttributes(input); diff --git a/packages/html/src/index.ts b/packages/html/src/index.ts index c22650057..aec6aeef0 100644 --- a/packages/html/src/index.ts +++ b/packages/html/src/index.ts @@ -31,22 +31,28 @@ const defaultTemplate = async ({ files, meta, publicPath, - title + title, + scriptsOnHead }: RollupHtmlTemplateOptions) => { - const scripts = (files.js || []) + let scripts = (files.js || []) .map(({ fileName }) => { const attrs = makeHtmlAttributes(attributes.script); return ``; }) .join('\n'); - const links = (files.css || []) + let links = (files.css || []) .map(({ fileName }) => { const attrs = makeHtmlAttributes(attributes.link); return ``; }) .join('\n'); + if (scriptsOnHead === true) { + links += scripts; + scripts = ''; + } + const metas = meta .map((input) => { const attrs = makeHtmlAttributes(input); @@ -80,11 +86,12 @@ const defaults = { meta: [{ charset: 'utf-8' }], publicPath: '', template: defaultTemplate, - title: 'Rollup Bundle' + title: 'Rollup Bundle', + scriptsOnHead: false }; export default function html(opts: RollupHtmlOptions = {}): Plugin { - const { attributes, fileName, meta, publicPath, template, title } = Object.assign( + const { attributes, fileName, meta, publicPath, template, title, scriptsOnHead } = Object.assign( {}, defaults, opts @@ -117,7 +124,8 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { files, meta, publicPath, - title + title, + scriptsOnHead }); const htmlFile: EmittedAsset = { diff --git a/packages/html/test/snapshots/test.js.md b/packages/html/test/snapshots/test.js.md index 39485f833..60a6d7206 100644 --- a/packages/html/test/snapshots/test.js.md +++ b/packages/html/test/snapshots/test.js.md @@ -353,3 +353,49 @@ Generated by [AVA](https://avajs.dev). source: '
', }, ] + +## scripts on head + +> Snapshot 1 + + [ + { + code: `(function (factory) {␊ + typeof define === 'function' && define.amd ? define(factory) :␊ + factory();␊ + })((function () { 'use strict';␊ + ␊ + ␊ + ␊ + }));␊ + `, + fileName: 'joker.js', + map: null, + source: undefined, + }, + { + code: undefined, + fileName: 'joker.css', + map: undefined, + source: Buffer @Uint8Array [ + 2a207b20 77696474 683a2031 3030253b 207d0a + ], + }, + { + code: undefined, + fileName: 'index.html', + map: undefined, + source: `␊ + ␊ + ␊ + ␊ + ␊ + Rollup Bundle␊ + ␊ + ␊ + ␊ + ␊ + ␊ + `, + }, + ] diff --git a/packages/html/test/snapshots/test.js.snap b/packages/html/test/snapshots/test.js.snap index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;@c~pN8mV<;K{$eWiQ$SX73j!=yrF{d zE(+BI-qni$Tf0J45^gksYqf{oy_$RIZvj`^m!+>^@;&e~um$yH&qK6_-i_(D*x-xX zZ`a-8ww~0L@6VL$i=h^Kh|W6u-n98ScH@bKWNXqW#LUgPOn6{ra)__ReEb(41#k2( GAOHYrHP#aV diff --git a/packages/html/test/test.js b/packages/html/test/test.js index a6a361eff..676664863 100644 --- a/packages/html/test/test.js +++ b/packages/html/test/test.js @@ -120,3 +120,20 @@ test.serial('template', async (t) => { const code = await getCode(bundle, output, true); t.snapshot(code); }); + +test.serial('scripts on head', async (t) => { + const bundle = await rollup({ + input: 'joker.js', + plugins: [ + css({ extract: true }), + html({ + attributes: { + script: { defer: true } + }, + scriptsOnHead: true + }) + ] + }); + const code = await getCode(bundle, output, true); + t.snapshot(code); +}); diff --git a/packages/html/types/index.d.ts b/packages/html/types/index.d.ts index 715e9a03c..4fb36d744 100644 --- a/packages/html/types/index.d.ts +++ b/packages/html/types/index.d.ts @@ -7,6 +7,7 @@ export interface RollupHtmlTemplateOptions { meta: Record[]; bundle: OutputBundle; files: Record; + scriptsOnHead?: boolean; } export interface RollupHtmlOptions { @@ -16,6 +17,7 @@ export interface RollupHtmlOptions { meta?: Record[]; publicPath?: string; template?: (templateoptions: RollupHtmlTemplateOptions) => string; + scriptsOnHead?: boolean; } export function makeHtmlAttributes(attributes: Record): string; From 5426ba997761e63b3fe8dfe36639a6cb8e00c8c4 Mon Sep 17 00:00:00 2001 From: shellscape Date: Sun, 15 Dec 2024 10:54:13 -0500 Subject: [PATCH 2/2] chore: refactor option name --- packages/html/src/index.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/html/src/index.ts b/packages/html/src/index.ts index bd10b6496..73cd7f1a1 100644 --- a/packages/html/src/index.ts +++ b/packages/html/src/index.ts @@ -91,13 +91,15 @@ const defaults: Required = { }; export default function html(opts: RollupHtmlOptions = {}): Plugin { - // const { attributes, fileName, meta, publicPath, template, title, addScriptsToHead } = Object.assign( - // {}, - // defaults, - // opts - // ); - const { attributes, fileName, meta, publicPath, template, title }: Required = - Object.assign({}, defaults, opts); + const { + addScriptsToHead, + attributes, + fileName, + meta, + publicPath, + template, + title + }: Required = Object.assign({}, defaults, opts); return { name: 'html', @@ -122,12 +124,12 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { const files = getFiles(bundle); const source = await template({ attributes, + addScriptsToHead, bundle, files, meta, publicPath, - title, - addScriptsToHead + title }); const htmlFile: EmittedAsset = {