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(rollup-plugin-html): resolves assets in styles #2664

Merged
merged 6 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 51 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/rollup-plugin-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
],
"dependencies": {
"@web/parse5-utils": "^2.1.0",
"clean-css": "^5.3.3",
"glob": "^10.0.0",
"html-minifier-terser": "^7.1.0",
"parse5": "^6.0.1"
},
"devDependencies": {
"@types/clean-css": "^4.2.11",
"@types/html-minifier-terser": "^7.0.0",
"rollup": "^4.4.0"
}
Expand Down
13 changes: 13 additions & 0 deletions packages/rollup-plugin-html/src/output/emitAssets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PluginContext } from 'rollup';
import path from 'path';
import CleanCSS from 'clean-css';

import { InputAsset, InputData } from '../input/InputData';
import { RollupPluginHTMLOptions, TransformAssetFunction } from '../RollupPluginHTMLOptions';
Expand Down Expand Up @@ -43,6 +44,18 @@ export async function emitAssets(
const allAssets = [...hashedAssets, ...staticAssets];

for (const asset of allAssets) {
if (asset.filePath.endsWith('.css')) {
// TODO: try css bundlers like https://lightningcss.dev/bundling.html or esbuild
// TODO: question: do we need to recursively analyze all assets that are referenced inside the first css file?
// TODO: make a flag to enable this behavior, "false" by default
const { styles } = await new CleanCSS({
rebaseTo: path.dirname(asset.filePath),
returnPromise: true,
}).minify([asset.filePath]);
console.log('emitEsset.ts styles', styles);
asset.content = Buffer.from(styles, 'utf-8');
}

const map = asset.hashed ? emittedHashedAssets : emittedStaticAssets;
if (!map.has(asset.filePath)) {
let source: Buffer = asset.content;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@font-face {
font-family: 'Font';
src: url('fonts/font-normal.woff2') format('woff2');
font-weight: normal;
font-style: normal;
font-display: swap;
}

@font-face {
font-family: 'Font';
src: url('fonts/font-bold.woff2') format('woff2');
font-weight: bold;
font-style: normal;
font-display: swap;
}
25 changes: 25 additions & 0 deletions packages/rollup-plugin-html/test/rollup-plugin-html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,4 +1041,29 @@ describe('rollup-plugin-html', () => {
].join(''),
);
});

it.only('includes referenced assets in the bundle', async () => {
const config = {
plugins: [
rollupPluginHTML({
input: {
html: `<html>
<head>
<link rel="stylesheet" href="./styles-with-fonts.css" />
</head>
<body>
</body>
</html>`,
},
rootDir: path.join(__dirname, 'fixtures', 'resolves-assets-in-styles'),
}),
],
};

const bundle = await rollup(config);
const { output } = await bundle.generate(outputConfig);

// TODO: how to test it, e.g. how to check if "output" contains "font-normal.woff2" and "font-bold.woff2"?
console.log('output', output);
});
});
1 change: 1 addition & 0 deletions packages/storybook-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import { DevServerConfig, mergeConfigs, startDevServer } from '@web/dev-server';
import type { DevServer } from '@web/dev-server-core';
import { fromRollup } from '@web/dev-server-rollup';
import { rollupPluginHTML } from '@web/rollup-plugin-html';

Check failure on line 11 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Windows

Duplicate identifier 'rollupPluginHTML'.

Check failure on line 11 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Linux

Duplicate identifier 'rollupPluginHTML'.

Check failure on line 11 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Linux (20)

Duplicate identifier 'rollupPluginHTML'.
import { rollupPluginHTML } from '@web/rollup-plugin-import-meta-assets';

Check failure on line 12 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Windows

Duplicate identifier 'rollupPluginHTML'.

Check failure on line 12 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Windows

Could not find a declaration file for module '@web/rollup-plugin-import-meta-assets'. 'D:/a/web/web/packages/rollup-plugin-import-meta-assets/src/index.js' implicitly has an 'any' type.

Check failure on line 12 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Linux

Duplicate identifier 'rollupPluginHTML'.

Check failure on line 12 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Linux

Could not find a declaration file for module '@web/rollup-plugin-import-meta-assets'. '/home/runner/work/web/web/packages/rollup-plugin-import-meta-assets/src/index.js' implicitly has an 'any' type.

Check failure on line 12 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Linux (20)

Duplicate identifier 'rollupPluginHTML'.

Check failure on line 12 in packages/storybook-builder/src/index.ts

View workflow job for this annotation

GitHub Actions / Linux (20)

Could not find a declaration file for module '@web/rollup-plugin-import-meta-assets'. '/home/runner/work/web/web/packages/rollup-plugin-import-meta-assets/src/index.js' implicitly has an 'any' type.
import express from 'express';
import * as fs from 'fs-extra';
import { join, parse, resolve } from 'path';
Expand Down
Loading