diff --git a/lib/module.js b/lib/module.js index 5c31996c..07d2bd77 100644 --- a/lib/module.js +++ b/lib/module.js @@ -1,5 +1,6 @@ import consola from 'consola' import merge from 'lodash.mergewith' +import { resolvePath } from 'mlly' import { captureException, withScope } from '@sentry/node' import { buildHook, initializeServerSentry, shutdownServerSentry, webpackConfigHook } from './core/hooks' import { boolToText, callOnce, canInitialize, clientSentryEnabled, envToBool, serverSentryEnabled } from './core/utils' @@ -14,7 +15,7 @@ function mergeWithCustomizer (objValue, srcValue) { } /** @type {import('@nuxt/types').Module} */ -export default function SentryModule (moduleOptions) { +export default async function SentryModule (moduleOptions) { /** @type {Required} */ const defaults = { lazy: false, @@ -88,6 +89,14 @@ export default function SentryModule (moduleOptions) { logger.info(`Sentry reporting is disabled (${why})`) } + if (clientSentryEnabled(options)) { + // Work-around issues with Nuxt not being able to resolve unhoisted "@sentry/*" dependencies on the client-side. + const clientDependencies = ['lodash.mergewith', '@sentry/integrations', '@sentry/vue', ...(options.tracing ? ['@sentry/tracing'] : [])] + for (const dep of clientDependencies) { + this.options.alias[`~${dep}`] = (await resolvePath(dep)).replace(/\/cjs\//, '/esm/') + } + } + this.nuxt.hook('build:before', callOnce(() => buildHook(this, options, logger))) if (serverSentryEnabled(options)) { diff --git a/lib/plugin.client.js b/lib/plugin.client.js index e7bdac66..e751a35b 100644 --- a/lib/plugin.client.js +++ b/lib/plugin.client.js @@ -1,11 +1,11 @@ import Vue from 'vue' -import merge from 'lodash.mergewith' -import * as Sentry from '@sentry/vue' -<% if (options.tracing) { %>import { BrowserTracing } from '@sentry/tracing'<% } %> +import merge from '~lodash.mergewith' +import * as Sentry from '~@sentry/vue' +<% if (options.tracing) { %>import { BrowserTracing } from '~@sentry/tracing'<% } %> <% if (options.initialize) { let integrations = options.PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations) - if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '@sentry/integrations' + if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '~@sentry/integrations' <%} if (options.clientConfigPath) {%>import getClientConfig from '<%= options.clientConfigPath %>' <%} diff --git a/lib/plugin.lazy.js b/lib/plugin.lazy.js index 36af61c5..3535de15 100644 --- a/lib/plugin.lazy.js +++ b/lib/plugin.lazy.js @@ -109,12 +109,12 @@ async function loadSentry (ctx, inject) { magicComments.push('webpackPreload: true') } %> - const Sentry = await import(/* <%= magicComments.join(', ') %> */ '@sentry/vue') - <% if (options.tracing) { %>const { BrowserTracing } = await import(/* <%= magicComments.join(', ') %> */ '@sentry/tracing')<% } %> + const Sentry = await import(/* <%= magicComments.join(', ') %> */ '~@sentry/vue') + <% if (options.tracing) { %>const { BrowserTracing } = await import(/* <%= magicComments.join(', ') %> */ '~@sentry/tracing')<% } %> <% if (options.initialize) { let integrations = options.PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations) - if (integrations.length) {%>const { <%= integrations.join(', ') %> } = await import(/* <%= magicComments.join(', ') %> */ '@sentry/integrations') + if (integrations.length) {%>const { <%= integrations.join(', ') %> } = await import(/* <%= magicComments.join(', ') %> */ '~@sentry/integrations') <% } integrations = options.BROWSER_INTEGRATIONS.filter(key => key in options.integrations) if (integrations.length) {%> const { <%= integrations.join(', ') %> } = Sentry.Integrations @@ -139,7 +139,7 @@ async function loadSentry (ctx, inject) { const runtimeConfigKey = <%= serialize(options.runtimeConfigKey) %> if (ctx.$config && runtimeConfigKey && ctx.$config[runtimeConfigKey]) { - const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ 'lodash.mergewith') + const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ '~lodash.mergewith') merge(config, ctx.$config[runtimeConfigKey].config, ctx.$config[runtimeConfigKey].clientConfig) } @@ -176,7 +176,7 @@ async function loadSentry (ctx, inject) { <% if (options.clientConfigPath) { %> const clientConfig = (await import(/* <%= magicComments.join(', ') %> */ '<%= options.clientConfigPath %>').then(m => m.default || m))(ctx) - const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ 'lodash.mergewith') + const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ '~lodash.mergewith') clientConfig ? merge(config, clientConfig) : console.error(`[@nuxtjs/sentry] Invalid value returned from the clientConfig plugin.`) <% } %> diff --git a/package.json b/package.json index 58de40d7..6d25bc27 100755 --- a/package.json +++ b/package.json @@ -56,7 +56,8 @@ "@sentry/node": "^7.28.0", "@sentry/vue": "^7.28.0", "consola": "^2.15.3", - "lodash.mergewith": "^4.6.2" + "lodash.mergewith": "^4.6.2", + "mlly": "^1.0.0" }, "devDependencies": { "@babel/core": "^7.20.5", diff --git a/yarn.lock b/yarn.lock index fc87c165..cea9c8dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2814,7 +2814,7 @@ acorn@^6.4.1: resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.4.2.tgz#35866fd710528e92de10cf06016498e47e39e1e6" integrity sha512-XtGIhXwF8YM8bJhGxG5kXgjkEuNGLTkoYqVE+KMR+aspr4KGYmKYg7yUe3KghyQ9yheNwLnjmzh/7+gfDBmHCQ== -acorn@^8.0.4, acorn@^8.5.0, acorn@^8.7.0, acorn@^8.8.0: +acorn@^8.0.4, acorn@^8.5.0, acorn@^8.7.0, acorn@^8.8.0, acorn@^8.8.1: version "8.8.1" resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.1.tgz#0a3f9cbecc4ec3bea6f0a80b66ae8dd2da250b73" integrity sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA== @@ -7935,6 +7935,11 @@ json5@^2.1.2, json5@^2.2.1: resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.2.tgz#64471c5bdcc564c18f7c1d4df2e2297f2457c5ab" integrity sha512-46Tk9JiOL2z7ytNQWFLpj99RZkVgeHf87yGQKsIkaPz1qSH9UczKH1rO7K3wgRselo0tYMUNfecYpm/p1vC7tQ== +jsonc-parser@^3.2.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-3.2.0.tgz#31ff3f4c2b9793f89c67212627c51c6394f88e76" + integrity sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w== + jsonfile@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb" @@ -8621,6 +8626,16 @@ mkdirp@^1.0.3, mkdirp@^1.0.4: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== +mlly@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/mlly/-/mlly-1.0.0.tgz#d38ca6e33ab89b60654f71ef08931d51e83d3569" + integrity sha512-QL108Hwt+u9bXdWgOI0dhzZfACovn5Aen4Xvc8Jasd9ouRH4NjnrXEiyP3nVvJo91zPlYjVRckta0Nt2zfoR6g== + dependencies: + acorn "^8.8.1" + pathe "^1.0.0" + pkg-types "^1.0.0" + ufo "^1.0.0" + modify-values@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022" @@ -9415,6 +9430,11 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== +pathe@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/pathe/-/pathe-1.0.0.tgz#135fc11464fc57c84ef93d5c5ed21247e24571df" + integrity sha512-nPdMG0Pd09HuSsr7QOKUXO2Jr9eqaDiZvDwdyIhNG5SHYujkQHYKDfGQkulBxvbDHz8oHLsTgKN86LSwYzSHAg== + pbkdf2@^3.0.3: version "3.1.2" 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: dependencies: find-up "^4.0.0" +pkg-types@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/pkg-types/-/pkg-types-1.0.1.tgz#25234407f9dc63409af45ced9407625ff446a761" + integrity sha512-jHv9HB+Ho7dj6ItwppRDDl0iZRYBD0jsakHXtFgoLr+cHSF6xC+QL54sJmWxyGxOLYSHm0afhXhXcQDQqH9z8g== + dependencies: + jsonc-parser "^3.2.0" + mlly "^1.0.0" + pathe "^1.0.0" + playwright-chromium@^1.28.1: version "1.29.0" resolved "https://registry.yarnpkg.com/playwright-chromium/-/playwright-chromium-1.29.0.tgz#15935da960dde3b16ed0cf29174d1d4e64f553e4" @@ -12128,6 +12157,11 @@ ufo@^0.7.4: resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.11.tgz#17defad497981290383c5d26357773431fdbadcb" integrity sha512-IT3q0lPvtkqQ8toHQN/BkOi4VIqoqheqM1FnkNWT9y0G8B3xJhwnoKBu5OHx8zHDOvveQzfKuFowJ0VSARiIDg== +ufo@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.0.1.tgz#64ed43b530706bda2e4892f911f568cf4cf67d29" + integrity sha512-boAm74ubXHY7KJQZLlXrtMz52qFvpsbOxDcZOnw/Wf+LS4Mmyu7JxmzD4tDLtUQtmZECypJ0FrCz4QIe6dvKRA== + uglify-js@^3.1.4, uglify-js@^3.5.1: version "3.17.4" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.17.4.tgz#61678cf5fa3f5b7eb789bb345df29afb8257c22c"