Skip to content

Commit 66c9ab9

Browse files
author
Luca Forstner
authored
ref(webpack): Use webpack peer dependency (#273)
1 parent 2ce1aae commit 66c9ab9

File tree

11 files changed

+235
-84
lines changed

11 files changed

+235
-84
lines changed

packages/e2e-tests/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
"@types/axios": "^0.14.0",
2929
"@types/glob": "8.0.0",
3030
"@types/jest": "^28.1.3",
31-
"@types/webpack4": "npm:@types/webpack@4.41.32",
31+
"@types/webpack4": "npm:@types/webpack@^4",
3232
"esbuild": "0.14.49",
3333
"eslint": "^8.18.0",
3434
"glob": "8.0.3",
3535
"jest": "^28.1.3",
3636
"rollup": "2.77.0",
3737
"ts-node": "^10.9.1",
3838
"vite": "3.0.0",
39-
"webpack": "5.74.0",
40-
"webpack4": "npm:webpack@4.46.0"
39+
"webpack5": "npm:webpack@5.74.0",
40+
"webpack4": "npm:webpack@^4"
4141
},
4242
"volta": {
4343
"extends": "../../package.json"

packages/e2e-tests/utils/create-cjs-bundles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vite from "vite";
22
import * as path from "path";
33
import * as rollup from "rollup";
44
import { default as webpack4 } from "webpack4";
5-
import { webpack as webpack5 } from "webpack";
5+
import { webpack as webpack5 } from "webpack5";
66
import * as esbuild from "esbuild";
77

88
import type { Options } from "@sentry/bundler-plugin-core";

packages/integration-tests/fixtures/build-information-injection/build-information-injection.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ function checkBundle(bundlePath: string): void {
1919
"esbuild",
2020
"rollup",
2121
"vite",
22-
"webpack",
2322
"webpack4",
23+
"webpack5",
2424
]) as string[],
25-
depsVersions: { rollup: 3, vite: 3, webpack: 5 },
25+
depsVersions: { rollup: 3, vite: 3 },
2626
// This will differ based on what env this is run on
2727
nodeVersion: expectedNodeVersion,
2828
})

packages/integration-tests/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
"@sentry/webpack-plugin": "2.1.0",
2525
"@swc/jest": "^0.2.21",
2626
"@types/jest": "^28.1.3",
27-
"@types/webpack4": "npm:@types/webpack@4.41.32",
27+
"@types/webpack4": "npm:@types/webpack@^4",
2828
"esbuild": "0.14.49",
2929
"eslint": "^8.18.0",
3030
"jest": "^28.1.3",
3131
"rollup": "3.2.0",
3232
"ts-node": "^10.9.1",
3333
"vite": "3.0.0",
34-
"webpack": "5.74.0",
35-
"webpack4": "npm:webpack@4.46.0"
34+
"webpack5": "npm:webpack@5.74.0",
35+
"webpack4": "npm:webpack@^4"
3636
},
3737
"volta": {
3838
"extends": "../../package.json"

packages/integration-tests/utils/create-cjs-bundles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vite from "vite";
22
import * as path from "path";
33
import * as rollup from "rollup";
44
import { default as webpack4 } from "webpack4";
5-
import { webpack as webpack5 } from "webpack";
5+
import { webpack as webpack5 } from "webpack5";
66
import * as esbuild from "esbuild";
77
import { Options } from "@sentry/bundler-plugin-core";
88
import { sentryVitePlugin } from "@sentry/vite-plugin";

packages/playground/build-webpack5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
const path = require("path");
3-
const webpack5 = require("webpack");
3+
const webpack5 = require("webpack5");
44
const { sentryWebpackPlugin } = require("@sentry/webpack-plugin");
55

66
webpack5(

packages/playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"http-proxy": "^1.18.1",
2929
"rollup": "3.2.0",
3030
"vite": "3.0.0",
31-
"webpack": "5.74.0",
31+
"webpack5": "npm:webpack@5",
3232
"webpack4": "npm:webpack@4.46.0"
3333
},
3434
"volta": {

packages/webpack-plugin/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
"dependencies": {
5151
"@sentry/bundler-plugin-core": "2.1.0",
5252
"unplugin": "1.0.1",
53-
"uuid": "^9.0.0",
54-
"webpack-4": "npm:webpack@^4"
53+
"uuid": "^9.0.0"
5554
},
5655
"devDependencies": {
5756
"@babel/core": "7.18.5",
@@ -66,12 +65,17 @@
6665
"@types/jest": "^28.1.3",
6766
"@types/node": "^18.6.3",
6867
"@types/uuid": "^9.0.1",
68+
"@types/webpack": "npm:@types/webpack@^4",
6969
"eslint": "^8.18.0",
7070
"jest": "^28.1.1",
7171
"rimraf": "^3.0.2",
7272
"rollup": "2.75.7",
7373
"ts-node": "^10.9.1",
74-
"typescript": "^4.7.4"
74+
"typescript": "^4.7.4",
75+
"webpack": "npm:webpack@^4"
76+
},
77+
"peerDependencies": {
78+
"webpack": ">=4"
7579
},
7680
"volta": {
7781
"extends": "../../package.json"

packages/webpack-plugin/rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const extensions = [".ts"];
99

1010
export default {
1111
input,
12-
external: [...Object.keys(packageJson.dependencies), ...modulePackage.builtinModules],
12+
external: [...Object.keys(packageJson.dependencies), ...modulePackage.builtinModules, "webpack"],
1313
onwarn: (warning) => {
1414
throw new Error(warning.message); // Warnings are usually high-consequence for us so let's throw to catch them
1515
},

packages/webpack-plugin/src/index.ts

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,26 @@ import { UnpluginOptions } from "unplugin";
44
import { v4 as uuidv4 } from "uuid";
55

66
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
7-
// @ts-ignore No typedefs for webpack 4
8-
import { BannerPlugin as Webpack4BannerPlugin } from "webpack-4";
7+
// @ts-ignore webpack is a peer dep
8+
import * as webback4or5 from "webpack";
99

1010
function webpackReleaseInjectionPlugin(injectionCode: string): UnpluginOptions {
1111
return {
1212
name: "sentry-webpack-release-injection-plugin",
1313
webpack(compiler) {
14-
if (compiler?.webpack?.BannerPlugin) {
15-
compiler.options.plugins.push(
16-
new compiler.webpack.BannerPlugin({
17-
raw: true,
18-
include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
19-
banner: injectionCode,
20-
})
21-
);
22-
} else {
23-
compiler.options.plugins.push(
24-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
25-
new Webpack4BannerPlugin({
26-
raw: true,
27-
include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
28-
banner: injectionCode,
29-
})
30-
);
31-
}
14+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
15+
// @ts-ignore webpack version compatibility shenanigans
16+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
17+
const BannerPlugin = compiler?.webpack?.BannerPlugin || webback4or5?.BannerPlugin;
18+
compiler.options.plugins = compiler.options.plugins || [];
19+
compiler.options.plugins.push(
20+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
21+
new BannerPlugin({
22+
raw: true,
23+
include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
24+
banner: injectionCode,
25+
})
26+
);
3227
},
3328
};
3429
}
@@ -37,24 +32,19 @@ function webpackDebugIdInjectionPlugin(): UnpluginOptions {
3732
return {
3833
name: "sentry-webpack-debug-id-injection-plugin",
3934
webpack(compiler) {
40-
if (compiler?.webpack?.BannerPlugin) {
41-
compiler.options.plugins.push(
42-
new compiler.webpack.BannerPlugin({
43-
raw: true,
44-
include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
45-
banner: () => getDebugIdSnippet(uuidv4()),
46-
})
47-
);
48-
} else {
49-
compiler.options.plugins.push(
50-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
51-
new Webpack4BannerPlugin({
52-
raw: true,
53-
include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
54-
banner: () => getDebugIdSnippet(uuidv4()),
55-
})
56-
);
57-
}
35+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
36+
// @ts-ignore webpack version compatibility shenanigans
37+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
38+
const BannerPlugin = compiler?.webpack?.BannerPlugin || webback4or5?.BannerPlugin;
39+
compiler.options.plugins = compiler.options.plugins || [];
40+
compiler.options.plugins.push(
41+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
42+
new BannerPlugin({
43+
raw: true,
44+
include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
45+
banner: () => getDebugIdSnippet(uuidv4()),
46+
})
47+
);
5848
},
5949
};
6050
}
@@ -66,10 +56,11 @@ function webpackDebugIdUploadPlugin(
6656
return {
6757
name: pluginName,
6858
webpack(compiler) {
69-
compiler.hooks.afterEmit.tapAsync(pluginName, (compilation, callback) => {
70-
const outputPath = compilation.outputOptions.path ?? path.resolve();
71-
const buildArtifacts = Object.keys(compilation.assets).map((asset) =>
72-
path.join(outputPath, asset)
59+
compiler.hooks.afterEmit.tapAsync(pluginName, (compilation, callback: () => void) => {
60+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
61+
const outputPath = (compilation.outputOptions.path as string | undefined) ?? path.resolve();
62+
const buildArtifacts = Object.keys(compilation.assets as Record<string, unknown>).map(
63+
(asset) => path.join(outputPath, asset)
7364
);
7465
void upload(buildArtifacts).then(() => {
7566
callback();

0 commit comments

Comments
 (0)