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: Dynamically import webpack in webpack plugin #171

Merged

Conversation

nicholas-codecov
Copy link
Collaborator

Description

This PR updates the webpack plugin, to dynamically import webpack so that we shouldn't have any issues with conflicting webpack issues with NextJS

Copy link

codecov bot commented Sep 10, 2024

Codecov Report

Attention: Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 75.40%. Comparing base (518f350) to head (6add9e5).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...ack-bundle-analysis/webpackBundleAnalysisPlugin.ts 40.00% 3 Missing ⚠️
Additional details and impacted files
Components Coverage Δ
Plugin core 96.84% <ø> (ø)
Rollup plugin 10.81% <ø> (ø)
Vite plugin 11.02% <ø> (ø)
Webpack plugin 49.88% <40.00%> (-0.24%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codecov-notifications
Copy link

codecov-notifications bot commented Sep 10, 2024

Codecov Report

Attention: Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files Patch % Lines
...ack-bundle-analysis/webpackBundleAnalysisPlugin.ts 40.00% 3 Missing ⚠️
Components Coverage Δ
Plugin core 96.84% <ø> (ø)
Rollup plugin 10.81% <ø> (ø)
Vite plugin 11.02% <ø> (ø)
Webpack plugin 49.88% <40.00%> (-0.24%) ⬇️

📢 Thoughts on this report? Let us know!

Copy link

@codecov codecov bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff is well-structured and mitigates problems related to importing the top level webpack. However, this change assumes that the webpack package is always available in the same environment where this code runs. If it's not, createRequire could throw an error.

@@ -1,5 +1,6 @@
import { type BundleAnalysisUploadPlugin } from "@codecov/bundler-plugin-core";
import * as webpack from "webpack";
import { createRequire } from "node:module";
import type * as TWebpack from "webpack";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import type declaration could be changed to a named import to make it more explicit. That way, instead of import type * as TWebpack from 'webpack';, it could be import type { Compiler } from 'webpack';.

@@ -25,6 +26,10 @@
await output.write();
},
webpack(compiler) {
Copy link

@codecov codecov bot Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using dynamic imports to load the webpack module at runtime. While this does provide flexibility, it can make the code harder to analyze and may have performance implications compared to static import due to the overhead of dynamic calls.

@@ -25,6 +26,10 @@
await output.write();
},
webpack(compiler) {
const webpack = createRequire(import.meta.url)(
"webpack",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The casting as typeof TWebpack assumes that the import will always be valid. If webpack is not available or the import is invalid, it will throw an error. Consider adding error handling or checks to ensure webpack is available before importing.

@codecov-staging
Copy link

Bundle Report

Bundle size has no change ✅

Bundle name Size Change
@codecov/example-next-app-edge-server-array-push 354 bytes 0 bytes
@codecov/example-next-15-app-edge-server-array-push 356 bytes 0 bytes

Copy link

codecov bot commented Sep 10, 2024

Bundle Report

Changes will decrease total bundle size by 220 bytes (-0.0%) ⬇️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
@codecov/vite-plugin-cjs 2.79kB 8 bytes ⬇️
@codecov/remix-vite-plugin-cjs 1.31kB 8 bytes ⬇️
@codecov/vite-plugin-esm 2.32kB 8 bytes ⬇️
@codecov/rollup-plugin-cjs 2.81kB 8 bytes ⬇️
@codecov/nuxt-plugin-cjs 1.4kB 8 bytes ⬇️
@codecov/sveltekit-plugin-cjs 1.32kB 8 bytes ⬇️
@codecov/sveltekit-plugin-esm 891 bytes 206 bytes ⬇️
@codecov/webpack-plugin-cjs 4.4kB 49 bytes ⬆️
@codecov/solidstart-plugin-cjs 1.33kB 8 bytes ⬇️
@codecov/example-sveltekit-app-client-esm 714.98kB 2 bytes ⬆️
@codecov/example-sveltekit-app-server-esm 974.84kB 1 bytes ⬇️
@codecov/nextjs-webpack-plugin-cjs 2.1kB 8 bytes ⬇️

@@ -25,6 +26,10 @@ export const webpackBundleAnalysisPlugin: BundleAnalysisUploadPlugin = ({
await output.write();
},
webpack(compiler) {
const webpack = createRequire(import.meta.url)(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer if this statement is split up to make it easier to read.

const generatedRequire = createRequire(import.meta.url);
const webpack = generatedRequire('webpack') as typeof TWebpack;

could you also sanity check that this transpiles properly for the cjs build?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what's in the .cjs file:

    const generatedRequire = node_module.createRequire(
      typeof document === "undefined"
        ? require("u" + "rl").pathToFileURL(__filename).href
        : (_documentCurrentScript && _documentCurrentScript.src) ||
            new URL("index.cjs", document.baseURI).href,
    );
    const webpack = generatedRequire("webpack");

And what's in the .mjs:

    const generatedRequire = createRequire(import.meta.url);
    const webpack = generatedRequire("webpack");

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unminified*

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that looks right, since the cjs is using require directly

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup that is correct

Copy link

@codecov codecov bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodecovAI submitted a new review for 6add9e5

@@ -1,5 +1,6 @@
import { type BundleAnalysisUploadPlugin } from "@codecov/bundler-plugin-core";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have removed a static import of the webpack module and replaced it with a dynamic import later in the code. This change isn't inherently problematic, but it does make the dependency on webpack less clear read at first glance. It's generally best to clearly state all dependencies via static imports at the top of your code.

@@ -1,5 +1,6 @@
import { type BundleAnalysisUploadPlugin } from "@codecov/bundler-plugin-core";
import * as webpack from "webpack";
import { createRequire } from "node:module";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have introduced the node:module package, currently it's an experimental feature in Node.js. Using experimental features can introduce instability to your codebase and for dependency software like codecov's plugins, it might be best to stick with stable features to ensure widest compatibility.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is not true codecov bot, you're like 6 node versions late 😭

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this bot has been replaced ... i should really remove it

@nicholas-codecov nicholas-codecov merged commit 3bf96e4 into main Sep 10, 2024
50 of 54 checks passed
@nicholas-codecov nicholas-codecov deleted the fix-dynamically-import-webpack-in-webpack-plugin branch September 10, 2024 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants