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

fix(html): Template files should only include entry chunks #688

Open
wants to merge 5 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 8 additions & 14 deletions packages/html/src/index.ts
Expand Up @@ -2,22 +2,14 @@ import { extname } from 'path';

import { Plugin, NormalizedOutputOptions, OutputBundle, EmittedAsset } from 'rollup';

import { RollupHtmlOptions, RollupHtmlTemplateOptions } from '../types';

const getFiles = (bundle: OutputBundle): RollupHtmlTemplateOptions['files'] => {
const files = Object.values(bundle).filter(
(file) =>
file.type === 'chunk' ||
(typeof file.type === 'string' ? file.type === 'asset' : file.isAsset)
);
const result = {} as ReturnType<typeof getFiles>;
for (const file of files) {
const { fileName } = file;
const extension = extname(fileName).substring(1);
import { RollupHtmlFileMap, RollupHtmlOptions, RollupHtmlTemplateOptions } from '../types';

const getFilesByExtension = (bundle: OutputBundle): RollupHtmlFileMap => {
const result: RollupHtmlFileMap = {};
for (const file of Object.values(bundle)) {
const extension = extname(file.fileName).substring(1);
result[extension] = (result[extension] || []).concat(file);
}

return result;
};

Expand All @@ -39,6 +31,7 @@ const defaultTemplate = async ({
title
}: RollupHtmlTemplateOptions) => {
const scripts = (files.js || [])
.filter((file: any) => file.isEntry)
.map(({ fileName }) => {
const attrs = makeHtmlAttributes(attributes.script);
return `<script src="${publicPath}${fileName}"${attrs}></script>`;
Expand Down Expand Up @@ -115,7 +108,8 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin {
});
}

const files = getFiles(bundle);
const files = getFilesByExtension(bundle);

const source = await template({
attributes,
bundle,
Expand Down
86 changes: 85 additions & 1 deletion packages/html/test/snapshots/test.js.md
Expand Up @@ -2,7 +2,7 @@

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## attributes

Expand Down Expand Up @@ -134,6 +134,90 @@ Generated by [AVA](https://ava.li).
},
]

## default template scripts should not include js assets

> Snapshot 1

[
{
code: `(function (factory) {␊
typeof define === 'function' && define.amd ? define(factory) :␊
factory();␊
}((function () { 'use strict';␊
})));␊
`,
fileName: 'robin.js',
map: null,
source: undefined,
},
{
code: undefined,
fileName: 'assets/asset-37a63f1f.js',
map: undefined,
source: '',
},
{
code: undefined,
fileName: 'index.html',
map: undefined,
source: `␊
<!doctype html>␊
<html lang="en">␊
<head>␊
<meta charset="utf-8">␊
<title>Rollup Bundle</title>␊
</head>␊
<body>␊
<script src="robin.js" type="module"></script>␊
</body>␊
</html>`,
},
]

## default template scripts should only include entry chunks

> Snapshot 1

[
{
code: `import './batman-dce5b48d.js';␊
// eslint-disable-next-line␊
`,
fileName: 'robin.js',
map: null,
source: undefined,
},
{
code: `␊
`,
fileName: 'batman-dce5b48d.js',
map: null,
source: undefined,
},
{
code: undefined,
fileName: 'index.html',
map: undefined,
source: `␊
<!doctype html>␊
<html lang="en">␊
<head>␊
<meta charset="utf-8">␊
<title>Rollup Bundle</title>␊
</head>␊
<body>␊
<script src="robin.js" type="module"></script>␊
</body>␊
</html>`,
},
]

## esm

> Snapshot 1
Expand Down
Binary file modified packages/html/test/snapshots/test.js.snap
Binary file not shown.
30 changes: 30 additions & 0 deletions packages/html/test/test.js
Expand Up @@ -120,3 +120,33 @@ test.serial('template', async (t) => {
const code = await getCode(bundle, output, true);
t.snapshot(code);
});

test.serial('default template scripts should only include entry chunks', async (t) => {
const bundle = await rollup({
input: 'robin.js',
manualChunks: {
batman: ['batman.js']
},
plugins: [html()],
treeshake: false
});
const outputOptions = { ...output, format: 'es' };
const code = await getCode(bundle, outputOptions, true);
t.snapshot(code);
});

test.serial('default template scripts should not include js assets', async (t) => {
const bundle = await rollup({
input: 'robin.js',
plugins: [
{
generateBundle() {
this.emitFile({ name: 'asset.js', source: '', type: 'asset' });
}
},
html()
]
});
const code = await getCode(bundle, output, true);
t.snapshot(code);
});
4 changes: 3 additions & 1 deletion packages/html/types/index.d.ts
Expand Up @@ -15,9 +15,11 @@ export interface RollupHtmlTemplateOptions {
publicPath: string;
meta: Record<string, any>[];
bundle: OutputBundle;
files: Record<string, (OutputChunk | OutputAsset)[]>;
files: RollupHtmlFileMap;
}

export type RollupHtmlFileMap = Record<string, (OutputChunk | OutputAsset)[]>;

export function makeHtmlAttributes(attributes: Record<string, string>): string;

/**
Expand Down