diff --git a/package.json b/package.json index dabd102..32e7ec5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "beasties-root", "private": true, - "packageManager": "pnpm@10.30.1", + "packageManager": "pnpm@10.33.0", "description": "Inline critical CSS and lazy-load the rest.", "author": "The Chromium Authors", "contributors": [ @@ -34,9 +34,9 @@ "test:versions": "installed-check --no-include-workspace-root --ignore-dev" }, "devDependencies": { - "@antfu/eslint-config": "7.4.3", + "@antfu/eslint-config": "7.7.3", "@codspeed/vitest-plugin": "5.2.0", - "@types/node": "24.10.13", + "@types/node": "24.12.2", "@types/postcss-safe-parser": "5.0.4", "@vitest/coverage-v8": "4.1.2", "bumpp": "11.0.1", @@ -45,8 +45,8 @@ "eslint": "10.1.0", "installed-check": "10.0.1", "jsdom": "29.0.1", - "knip": "6.0.2", - "lint-staged": "16.2.7", + "knip": "6.3.0", + "lint-staged": "16.4.0", "simple-git-hooks": "2.13.1", "typescript": "6.0.2", "vitest": "4.1.2" diff --git a/packages/beasties-webpack-plugin/package.json b/packages/beasties-webpack-plugin/package.json index 38146e4..4739f2d 100644 --- a/packages/beasties-webpack-plugin/package.json +++ b/packages/beasties-webpack-plugin/package.json @@ -68,13 +68,13 @@ "minimatch": "^10.0.1" }, "devDependencies": { - "@types/jsdom": "28.0.0", + "@types/jsdom": "28.0.1", "css-loader": "7.1.4", "documentation": "14.0.3", "file-loader": "6.2.0", "html-webpack-plugin": "5.6.6", - "mini-css-extract-plugin": "2.10.0", + "mini-css-extract-plugin": "2.10.2", "unbuild": "3.6.1", - "webpack": "5.105.2" + "webpack": "5.105.4" } } diff --git a/packages/beasties-webpack-plugin/src/index.ts b/packages/beasties-webpack-plugin/src/index.ts index 3afdacf..3fc1208 100644 --- a/packages/beasties-webpack-plugin/src/index.ts +++ b/packages/beasties-webpack-plugin/src/index.ts @@ -32,6 +32,10 @@ const $require // Used to annotate this plugin's hooks in Tappable invocations const PLUGIN_NAME = 'beasties-webpack-plugin' +const HTML_EXT_RE = /\.html$/ +const LEADING_SLASH_RE = /^\// +const PUBLIC_PATH_RE = /(^\/|\/$)/g +const DOT_SLASH_RE = /^\.\// /** * Create a Beasties plugin instance with the given options. @@ -134,7 +138,7 @@ export default class BeastiesWebpackPlugin extends Beasties { let htmlAssetName: string | undefined for (const name in assets) { - if (name.match(/\.html$/)) { + if (HTML_EXT_RE.test(name)) { htmlAssetName = name break } @@ -167,19 +171,19 @@ export default class BeastiesWebpackPlugin extends Beasties { // CHECK - the output path // path on disk (with output.publicPath removed) - let normalizedPath = href.replace(/^\//, '') - const pathPrefix = `${(publicPath || '').replace(/(^\/|\/$)/g, '')}/` + let normalizedPath = href.replace(LEADING_SLASH_RE, '') + const pathPrefix = `${(publicPath || '').replace(PUBLIC_PATH_RE, '')}/` if (normalizedPath.indexOf(pathPrefix) === 0) { normalizedPath = normalizedPath .substring(pathPrefix.length) - .replace(/^\//, '') + .replace(LEADING_SLASH_RE, '') } const filename = path.resolve(outputPath, normalizedPath) // try to find a matching asset by filename in webpack's output (not yet written to disk) const relativePath = path .relative(outputPath, filename) - .replace(/^\.\//, '') + .replace(DOT_SLASH_RE, '') const asset = this.compilation.assets[relativePath] // compilation.assets[relativePath]; // Attempt to read from assets, falling back to a disk read diff --git a/packages/beasties-webpack-plugin/src/util.ts b/packages/beasties-webpack-plugin/src/util.ts index 9e32e81..867c2fb 100644 --- a/packages/beasties-webpack-plugin/src/util.ts +++ b/packages/beasties-webpack-plugin/src/util.ts @@ -1,8 +1,10 @@ import type { Compilation, Compiler } from 'webpack' +const KEBAB_CASE_RE = /-([a-z])/g + export function tap(inst: Compiler | Compilation, hook: string, pluginName: string, async: boolean, callback: (...rest: any[]) => void): void { if (inst.hooks) { - const camel = hook.replace(/-([a-z])/g, (s, i) => i.toUpperCase()) + const camel = hook.replace(KEBAB_CASE_RE, (_, i) => i.toUpperCase()) // @ts-expect-error - hooks call inst.hooks[camel][async ? 'tapAsync' : 'tap'](pluginName, callback) } diff --git a/packages/beasties/README.md b/packages/beasties/README.md index 325700c..1311a1a 100644 --- a/packages/beasties/README.md +++ b/packages/beasties/README.md @@ -312,4 +312,3 @@ This is not an official Google product. [beasties-webpack-plugin]: https://github.com/danielroe/beasties/tree/main/packages/beasties-webpack-plugin [critical css]: https://www.smashingmagazine.com/2015/08/understanding-critical-css/ -[html-webpack-plugin]: https://github.com/jantimon/html-webpack-plugin diff --git a/packages/beasties/src/dom.ts b/packages/beasties/src/dom.ts index 97b2560..ca14240 100644 --- a/packages/beasties/src/dom.ts +++ b/packages/beasties/src/dom.ts @@ -191,10 +191,8 @@ function extendElement(element: typeof Element.prototype) { setAttribute: { value(name: string, value: string) { - if (this.attribs == null) - this.attribs = {} - if (value == null) - value = '' + this.attribs ??= {} + value ??= '' this.attribs[name] = value }, }, diff --git a/packages/beasties/src/index.ts b/packages/beasties/src/index.ts index 0bcd1c3..bdfaecb 100644 --- a/packages/beasties/src/index.ts +++ b/packages/beasties/src/index.ts @@ -30,6 +30,17 @@ const removePseudoClassesAndElementsPattern = /(?+~])\s*(?!\1)([>+~])/g const emptyCombinatorPattern = /([>+~])\s*(?=\1|$)/g const removeTrailingCommasPattern = /\(\s*,|,\s*\)/g +const LEADING_SLASH_OR_QUERY_RE = /^\/(?!\/)|[?#].*$/g +const PUBLIC_PATH_RE = /(^\/(?!\/)|\/$)/g +const REMOTE_URL_RE = /^https?:\/\// +const BEFORE_AFTER_PSEUDO_RE = /^::?(?:before|after)$/ +const FONT_FAMILY_RE = /\bfont(?:-family)?\b/i +// eslint-disable-next-line regexp/no-useless-assertions +const BEASTIES_COMMENT_RE = /^(? { if (rule.type === 'comment') { // we might want to remove a leading ! on comment blocks - // beasties can be part of "legal comments" which aren't striped on build - // TODO: address regexp - // eslint-disable-next-line regexp/no-useless-assertions - const beastiesComment = rule.text.match(/^(? { if (!asset) return Promise.resolve('') return new Promise((resolve) => { - setTimeout(() => resolve(asset.content), asset.delay) + setTimeout(resolve, asset.delay, asset.content) }) } @@ -150,7 +150,7 @@ describe('beasties', () => { vi.useRealTimers() // Verify style tags are in correct order (first, second, third) - const styleOrder = [...result.matchAll(/