-
-
Notifications
You must be signed in to change notification settings - Fork 19
refactor(beasties-webpack-plugin): rewrite to typescript #213
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { defineBuildConfig } from 'unbuild' | ||
|
|
||
| export default defineBuildConfig({ | ||
| declaration: 'compatible', | ||
| externals: ['webpack'], | ||
| rollup: { | ||
| dts: { | ||
| respectExternal: false, | ||
| }, | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { Options } from 'beasties' | ||
| import type { Compiler } from 'webpack' | ||
| import Beasties from 'beasties' | ||
|
|
||
| /** | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
|
|
||
| /** | ||
| * Create a Beasties plugin instance with the given options. | ||
| * @public | ||
| * @param {Options} options Options to control how Beasties inlines CSS. See https://github.com/danielroe/beasties#usage | ||
| * @example | ||
| * // webpack.config.js | ||
| * module.exports = { | ||
| * plugins: [ | ||
| * new Beasties({ | ||
| * // Outputs: <link rel="preload" onload="this.rel='stylesheet'"> | ||
| * preload: 'swap', | ||
| * | ||
| * // Don't inline critical font-face rules, but preload the font URLs: | ||
| * preloadFonts: true | ||
| * }) | ||
| * ] | ||
| * } | ||
| */ | ||
| declare class BeastiesWebpackPlugin extends Beasties { | ||
| constructor(options: Options) | ||
| /** | ||
| * Invoked by Webpack during plugin initialization | ||
| */ | ||
| apply(compiler: Compiler): void | ||
| /** | ||
| * Given href, find the corresponding CSS asset | ||
| */ | ||
| getCssAsset(href: string, style: Node): Promise<string | undefined> | ||
| /** | ||
| * Check if the stylesheet should be inlined | ||
| */ | ||
| override checkInlineThreshold(link: Node, style: Node, sheet: string): boolean | ||
| /** | ||
| * Inline the stylesheets from options.additionalStylesheets (assuming it passes `options.filter`) | ||
| */ | ||
| embedAdditionalStylesheet(document: Document): Promise<void> | ||
| /** | ||
| * Prune the source CSS files | ||
| */ | ||
| override pruneSource(style: Node, before: string, sheetInverse: string): boolean | ||
| } | ||
|
|
||
| export = BeastiesWebpackPlugin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import type { Compilation, Compiler } from 'webpack' | ||
|
|
||
| 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()) | ||
| // @ts-expect-error - hooks call | ||
| inst.hooks[camel][async ? 'tapAsync' : 'tap'](pluginName, callback) | ||
| } | ||
| else { | ||
| // @ts-expect-error - webpack3 | ||
| inst.plugin(hook, callback) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Verify the publicPath logic for function types.
The condition on line 79 checks
typeof compiler.options.output.publicPath === 'function'after the||operator, which means ifpublicPathis falsy, the function check won't prevent the assignment on line 82. This could result inundefinedbeing assigned tothis.options.publicPath.🏁 Script executed:
Length of output: 2153
Fix the publicPath assignment logic to handle undefined and function types correctly.
The review comment is correct. The condition
compiler.options.output.publicPath || typeof compiler.options.output.publicPath === 'function'evaluates tofalsewhenpublicPathis falsy (undefined/null), causing the else branch to assigncompiler.options.output.publicPath!, which results inundefinedbeing assigned tothis.options.publicPath.The logic should check for function type first, or restructure to prevent undefined assignment:
This ensures functions are handled and undefined defaults to an empty string, matching the defensive pattern used downstream at line 171.
🤖 Prompt for AI Agents