Skip to content

Commit 82071ce

Browse files
authored
fix: not able to resolve un-hoisted client-side dependencies (#486)
1 parent c9894da commit 82071ce

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

lib/module.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import consola from 'consola'
22
import merge from 'lodash.mergewith'
3+
import { resolvePath } from 'mlly'
34
import { captureException, withScope } from '@sentry/node'
45
import { buildHook, initializeServerSentry, shutdownServerSentry, webpackConfigHook } from './core/hooks'
56
import { boolToText, callOnce, canInitialize, clientSentryEnabled, envToBool, serverSentryEnabled } from './core/utils'
@@ -14,7 +15,7 @@ function mergeWithCustomizer (objValue, srcValue) {
1415
}
1516

1617
/** @type {import('@nuxt/types').Module<import('../types').ModuleConfiguration>} */
17-
export default function SentryModule (moduleOptions) {
18+
export default async function SentryModule (moduleOptions) {
1819
/** @type {Required<import('../types/sentry').ModuleConfiguration>} */
1920
const defaults = {
2021
lazy: false,
@@ -88,6 +89,14 @@ export default function SentryModule (moduleOptions) {
8889
logger.info(`Sentry reporting is disabled (${why})`)
8990
}
9091

92+
if (clientSentryEnabled(options)) {
93+
// Work-around issues with Nuxt not being able to resolve unhoisted "@sentry/*" dependencies on the client-side.
94+
const clientDependencies = ['lodash.mergewith', '@sentry/integrations', '@sentry/vue', ...(options.tracing ? ['@sentry/tracing'] : [])]
95+
for (const dep of clientDependencies) {
96+
this.options.alias[`~${dep}`] = (await resolvePath(dep)).replace(/\/cjs\//, '/esm/')
97+
}
98+
}
99+
91100
this.nuxt.hook('build:before', callOnce(() => buildHook(this, options, logger)))
92101

93102
if (serverSentryEnabled(options)) {

lib/plugin.client.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Vue from 'vue'
2-
import merge from 'lodash.mergewith'
3-
import * as Sentry from '@sentry/vue'
4-
<% if (options.tracing) { %>import { BrowserTracing } from '@sentry/tracing'<% } %>
2+
import merge from '~lodash.mergewith'
3+
import * as Sentry from '~@sentry/vue'
4+
<% if (options.tracing) { %>import { BrowserTracing } from '~@sentry/tracing'<% } %>
55
<%
66
if (options.initialize) {
77
let integrations = options.PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations)
8-
if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '@sentry/integrations'
8+
if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '~@sentry/integrations'
99
<%}
1010
if (options.clientConfigPath) {%>import getClientConfig from '<%= options.clientConfigPath %>'
1111
<%}

lib/plugin.lazy.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ async function loadSentry (ctx, inject) {
109109
magicComments.push('webpackPreload: true')
110110
}
111111
%>
112-
const Sentry = await import(/* <%= magicComments.join(', ') %> */ '@sentry/vue')
113-
<% if (options.tracing) { %>const { BrowserTracing } = await import(/* <%= magicComments.join(', ') %> */ '@sentry/tracing')<% } %>
112+
const Sentry = await import(/* <%= magicComments.join(', ') %> */ '~@sentry/vue')
113+
<% if (options.tracing) { %>const { BrowserTracing } = await import(/* <%= magicComments.join(', ') %> */ '~@sentry/tracing')<% } %>
114114
<%
115115
if (options.initialize) {
116116
let integrations = options.PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations)
117-
if (integrations.length) {%>const { <%= integrations.join(', ') %> } = await import(/* <%= magicComments.join(', ') %> */ '@sentry/integrations')
117+
if (integrations.length) {%>const { <%= integrations.join(', ') %> } = await import(/* <%= magicComments.join(', ') %> */ '~@sentry/integrations')
118118
<% }
119119
integrations = options.BROWSER_INTEGRATIONS.filter(key => key in options.integrations)
120120
if (integrations.length) {%> const { <%= integrations.join(', ') %> } = Sentry.Integrations
@@ -139,7 +139,7 @@ async function loadSentry (ctx, inject) {
139139

140140
const runtimeConfigKey = <%= serialize(options.runtimeConfigKey) %>
141141
if (ctx.$config && runtimeConfigKey && ctx.$config[runtimeConfigKey]) {
142-
const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ 'lodash.mergewith')
142+
const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ '~lodash.mergewith')
143143
merge(config, ctx.$config[runtimeConfigKey].config, ctx.$config[runtimeConfigKey].clientConfig)
144144
}
145145

@@ -176,7 +176,7 @@ async function loadSentry (ctx, inject) {
176176

177177
<% if (options.clientConfigPath) { %>
178178
const clientConfig = (await import(/* <%= magicComments.join(', ') %> */ '<%= options.clientConfigPath %>').then(m => m.default || m))(ctx)
179-
const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ 'lodash.mergewith')
179+
const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ '~lodash.mergewith')
180180
clientConfig ? merge(config, clientConfig) : console.error(`[@nuxtjs/sentry] Invalid value returned from the clientConfig plugin.`)
181181
<% } %>
182182

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"@sentry/node": "^7.28.0",
5757
"@sentry/vue": "^7.28.0",
5858
"consola": "^2.15.3",
59-
"lodash.mergewith": "^4.6.2"
59+
"lodash.mergewith": "^4.6.2",
60+
"mlly": "^1.0.0"
6061
},
6162
"devDependencies": {
6263
"@babel/core": "^7.20.5",

yarn.lock

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,7 @@ acorn@^6.4.1:
28142814
resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.4.2.tgz#35866fd710528e92de10cf06016498e47e39e1e6"
28152815
integrity sha512-XtGIhXwF8YM8bJhGxG5kXgjkEuNGLTkoYqVE+KMR+aspr4KGYmKYg7yUe3KghyQ9yheNwLnjmzh/7+gfDBmHCQ==
28162816

2817-
acorn@^8.0.4, acorn@^8.5.0, acorn@^8.7.0, acorn@^8.8.0:
2817+
acorn@^8.0.4, acorn@^8.5.0, acorn@^8.7.0, acorn@^8.8.0, acorn@^8.8.1:
28182818
version "8.8.1"
28192819
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.1.tgz#0a3f9cbecc4ec3bea6f0a80b66ae8dd2da250b73"
28202820
integrity sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA==
@@ -7935,6 +7935,11 @@ json5@^2.1.2, json5@^2.2.1:
79357935
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.2.tgz#64471c5bdcc564c18f7c1d4df2e2297f2457c5ab"
79367936
integrity sha512-46Tk9JiOL2z7ytNQWFLpj99RZkVgeHf87yGQKsIkaPz1qSH9UczKH1rO7K3wgRselo0tYMUNfecYpm/p1vC7tQ==
79377937

7938+
jsonc-parser@^3.2.0:
7939+
version "3.2.0"
7940+
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-3.2.0.tgz#31ff3f4c2b9793f89c67212627c51c6394f88e76"
7941+
integrity sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==
7942+
79387943
jsonfile@^4.0.0:
79397944
version "4.0.0"
79407945
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
@@ -8621,6 +8626,16 @@ mkdirp@^1.0.3, mkdirp@^1.0.4:
86218626
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
86228627
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==
86238628

8629+
mlly@^1.0.0:
8630+
version "1.0.0"
8631+
resolved "https://registry.yarnpkg.com/mlly/-/mlly-1.0.0.tgz#d38ca6e33ab89b60654f71ef08931d51e83d3569"
8632+
integrity sha512-QL108Hwt+u9bXdWgOI0dhzZfACovn5Aen4Xvc8Jasd9ouRH4NjnrXEiyP3nVvJo91zPlYjVRckta0Nt2zfoR6g==
8633+
dependencies:
8634+
acorn "^8.8.1"
8635+
pathe "^1.0.0"
8636+
pkg-types "^1.0.0"
8637+
ufo "^1.0.0"
8638+
86248639
modify-values@^1.0.0:
86258640
version "1.0.1"
86268641
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
@@ -9415,6 +9430,11 @@ path-type@^4.0.0:
94159430
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
94169431
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==
94179432

9433+
pathe@^1.0.0:
9434+
version "1.0.0"
9435+
resolved "https://registry.yarnpkg.com/pathe/-/pathe-1.0.0.tgz#135fc11464fc57c84ef93d5c5ed21247e24571df"
9436+
integrity sha512-nPdMG0Pd09HuSsr7QOKUXO2Jr9eqaDiZvDwdyIhNG5SHYujkQHYKDfGQkulBxvbDHz8oHLsTgKN86LSwYzSHAg==
9437+
94189438
pbkdf2@^3.0.3:
94199439
version "3.1.2"
94209440
resolved "https://registry.yarnpkg.com/pbkdf2/-/pbkdf2-3.1.2.tgz#dd822aa0887580e52f1a039dc3eda108efae3075"
@@ -9485,6 +9505,15 @@ pkg-dir@^4.1.0, pkg-dir@^4.2.0:
94859505
dependencies:
94869506
find-up "^4.0.0"
94879507

9508+
pkg-types@^1.0.0:
9509+
version "1.0.1"
9510+
resolved "https://registry.yarnpkg.com/pkg-types/-/pkg-types-1.0.1.tgz#25234407f9dc63409af45ced9407625ff446a761"
9511+
integrity sha512-jHv9HB+Ho7dj6ItwppRDDl0iZRYBD0jsakHXtFgoLr+cHSF6xC+QL54sJmWxyGxOLYSHm0afhXhXcQDQqH9z8g==
9512+
dependencies:
9513+
jsonc-parser "^3.2.0"
9514+
mlly "^1.0.0"
9515+
pathe "^1.0.0"
9516+
94889517
playwright-chromium@^1.28.1:
94899518
version "1.29.0"
94909519
resolved "https://registry.yarnpkg.com/playwright-chromium/-/playwright-chromium-1.29.0.tgz#15935da960dde3b16ed0cf29174d1d4e64f553e4"
@@ -12128,6 +12157,11 @@ ufo@^0.7.4:
1212812157
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.11.tgz#17defad497981290383c5d26357773431fdbadcb"
1212912158
integrity sha512-IT3q0lPvtkqQ8toHQN/BkOi4VIqoqheqM1FnkNWT9y0G8B3xJhwnoKBu5OHx8zHDOvveQzfKuFowJ0VSARiIDg==
1213012159

12160+
ufo@^1.0.0:
12161+
version "1.0.1"
12162+
resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.0.1.tgz#64ed43b530706bda2e4892f911f568cf4cf67d29"
12163+
integrity sha512-boAm74ubXHY7KJQZLlXrtMz52qFvpsbOxDcZOnw/Wf+LS4Mmyu7JxmzD4tDLtUQtmZECypJ0FrCz4QIe6dvKRA==
12164+
1213112165
uglify-js@^3.1.4, uglify-js@^3.5.1:
1213212166
version "3.17.4"
1213312167
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.17.4.tgz#61678cf5fa3f5b7eb789bb345df29afb8257c22c"

0 commit comments

Comments
 (0)