From 529cf3e622e904b636d8c2fb9d5ed72355e2ab16 Mon Sep 17 00:00:00 2001 From: Hasan Ayan Date: Thu, 19 Aug 2021 23:05:09 +0300 Subject: [PATCH 1/5] add support for multiple apps using module federetion --- src/index.js | 62 ++++++++++++++++++++++++++++++++++++++++++++ src/sentry.loader.js | 11 ++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index bc82e6cd..bd3c62a7 100644 --- a/src/index.js +++ b/src/index.js @@ -74,6 +74,58 @@ function attachAfterEmitHook(compiler, callback) { } } +function attachAfterCodeGenerationHook(compiler, options) { + if (!compiler.hooks || !compiler.hooks.make) { + return; + } + + // eslint-disable-next-line global-require + const webpackSources = require(// eslint-disable-next-line import/no-extraneous-dependencies + 'webpack-sources'); + + if (!webpackSources) return; + + const { RawSource } = webpackSources; + const moduleFederationPlugin = + compiler.options && + compiler.options.plugins && + compiler.options.plugins.find( + x => x.constructor.name === 'ModuleFederationPlugin' + ); + + if (!moduleFederationPlugin) { + return; + } + + compiler.hooks.make.tapAsync('SentryCliPlugin', (compilation, cb) => { + options.releasePromise.then(version => { + compilation.hooks.afterCodeGeneration.tap('SentryCliPlugin', () => { + compilation.modules.forEach(module => { + // eslint-disable-next-line no-underscore-dangle + if (module._name !== options.remoteModuleName) return; + const sourceMap = compilation.codeGenerationResults.get(module) + .sources; + const rawSource = sourceMap.get('javascript'); + sourceMap.set( + 'javascript', + new RawSource( + `${rawSource.source()} +(function (){ +var globalThis = (typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}); +globalThis.SENTRY_RELEASES = globalThis.SENTRY_RELEASES || {}; +globalThis.SENTRY_RELEASES["${options.project}@${ + options.org + }"] = {"id":"${version}"}; +})();` + ) + ); + }); + }); + cb(); + }); + }); +} + class SentryCliPlugin { constructor(options = {}) { const defaults = { @@ -318,6 +370,8 @@ class SentryCliPlugin { loader: SENTRY_LOADER, options: { releasePromise: this.release, + org: this.options.org || process.env.SENTRY_ORG, + project: this.options.project || process.env.SENTRY_PROJECT, }, }; @@ -333,6 +387,8 @@ class SentryCliPlugin { loader: SENTRY_LOADER, options: { releasePromise: this.release, + org: this.options.org || process.env.SENTRY_ORG, + project: this.options.project || process.env.SENTRY_PROJECT, }, }, ], @@ -501,6 +557,12 @@ class SentryCliPlugin { this.injectRelease(compilerOptions); } + attachAfterCodeGenerationHook(compiler, { + releasePromise: this.release, + org: this.options.org || process.env.SENTRY_ORG, + project: this.options.project || process.env.SENTRY_PROJECT, + }); + attachAfterEmitHook(compiler, (compilation, cb) => { if (!this.options.include || !this.options.include.length) { ensure(compilerOptions, 'output', Object); diff --git a/src/sentry.loader.js b/src/sentry.loader.js index 95f2634c..3a153be0 100644 --- a/src/sentry.loader.js +++ b/src/sentry.loader.js @@ -1,8 +1,15 @@ module.exports = function sentryLoader(content, map, meta) { - const { releasePromise } = this.query; + const { releasePromise, org, project } = this.query; const callback = this.async(); releasePromise.then(version => { - const sentryRelease = `(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}).SENTRY_RELEASE={id:"${version}"};`; + let sentryRelease = `const _global = (typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}); _global.SENTRY_RELEASE={id:"${version}"};`; + if (project) { + const key = org ? `${project}@${org}` : project; + sentryRelease += ` + _global.SENTRY_RELEASES=_global.SENTRY_RELEASES||{}; + _global.SENTRY_RELEASES["${key}"]={id:"${version}"}; + `; + } callback(null, sentryRelease, map, meta); }); }; From 6ff7cefbdc14c62d88102eb3cff634e748a7e282 Mon Sep 17 00:00:00 2001 From: Hasan Ayan Date: Mon, 6 Sep 2021 16:15:05 +0300 Subject: [PATCH 2/5] Update src/index.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit reposition eslint comment Co-authored-by: Kamil Ogórek --- src/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index bd3c62a7..89902619 100644 --- a/src/index.js +++ b/src/index.js @@ -79,9 +79,11 @@ function attachAfterCodeGenerationHook(compiler, options) { return; } - // eslint-disable-next-line global-require - const webpackSources = require(// eslint-disable-next-line import/no-extraneous-dependencies - 'webpack-sources'); + let webpackSources; + try { + // eslint-disable-next-line global-require, import/no-extraneous-dependencies + webpackSources = require('webpack-sources'); + } catch(_e) {} if (!webpackSources) return; From 3fb1970ebf9faa0d73b6e61393cb627a3597f20d Mon Sep 17 00:00:00 2001 From: Hasan Ayan Date: Fri, 8 Oct 2021 17:42:03 +0300 Subject: [PATCH 3/5] fixed failing integration test --- example/test.js | 36 ++++++++++++++++++++++++++++++++---- example/webpack.config.js | 2 ++ src/index.js | 11 +++++++---- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/example/test.js b/example/test.js index c72b9854..54b37589 100644 --- a/example/test.js +++ b/example/test.js @@ -8,14 +8,42 @@ console.log(); const content = fs.readFileSync('./dist/main.bundle.js'); if ( - content.toString() - .indexOf(`(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}).SENTRY_RELEASE = { + content + .toString() + .indexOf( + `var _global = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};` + ) !== -1 +) { + console.log('Saul Goodman, found "var _global" assignment in bundle'); +} else { + console.error('Boom, did not find "var _global" assignment in bundle'); + process.exit(1); +} + +if ( + content.toString().indexOf( + `_global.SENTRY_RELEASE = { id: "foo" -}`) !== -1 +};` + ) !== -1 ) { console.log('Saul Goodman, found SENTRY_RELEASE in bundle'); - process.exit(0); } else { console.error('Boom, did not find SENTRY_RELEASE in bundle'); process.exit(1); } + +if ( + content.toString().indexOf( + `_global.SENTRY_RELEASES = _global.SENTRY_RELEASES || {}; +_global.SENTRY_RELEASES["my-project@my-org"] = { + id: "foo" +};` + ) !== -1 +) { + console.log('Saul Goodman, found SENTRY_RELEASES assignment in bundle'); + process.exit(0); +} else { + console.error('Boom, did not find SENTRY_RELEASES assignment in bundle'); + process.exit(1); +} diff --git a/example/webpack.config.js b/example/webpack.config.js index 2b8420b8..6866eabf 100644 --- a/example/webpack.config.js +++ b/example/webpack.config.js @@ -29,6 +29,8 @@ module.exports = { configFile: 'sentry.properties', dryRun: true, release: 'foo', + project: 'my-project', + org: 'my-org', dist: '123', }), ], diff --git a/src/index.js b/src/index.js index 89902619..0ff58fb7 100644 --- a/src/index.js +++ b/src/index.js @@ -83,9 +83,12 @@ function attachAfterCodeGenerationHook(compiler, options) { try { // eslint-disable-next-line global-require, import/no-extraneous-dependencies webpackSources = require('webpack-sources'); - } catch(_e) {} - - if (!webpackSources) return; + } catch (_e) { + console.warn( + 'Coud not resolve package: webpack-sources. Skipping injection for the remote entry file.' + ); + return; + } const { RawSource } = webpackSources; const moduleFederationPlugin = @@ -104,7 +107,7 @@ function attachAfterCodeGenerationHook(compiler, options) { compilation.hooks.afterCodeGeneration.tap('SentryCliPlugin', () => { compilation.modules.forEach(module => { // eslint-disable-next-line no-underscore-dangle - if (module._name !== options.remoteModuleName) return; + if (module._name !== moduleFederationPlugin._options.name) return; const sourceMap = compilation.codeGenerationResults.get(module) .sources; const rawSource = sourceMap.get('javascript'); From 576615f43b6bebcde1a087e8a10ea88ff917accd Mon Sep 17 00:00:00 2001 From: Hasan Ayan Date: Tue, 12 Oct 2021 11:20:29 +0300 Subject: [PATCH 4/5] Update src/sentry.loader.js Co-authored-by: iker barriocanal <32816711+iker-barriocanal@users.noreply.github.com> --- src/sentry.loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry.loader.js b/src/sentry.loader.js index 3a153be0..f18f5e1b 100644 --- a/src/sentry.loader.js +++ b/src/sentry.loader.js @@ -6,7 +6,7 @@ module.exports = function sentryLoader(content, map, meta) { if (project) { const key = org ? `${project}@${org}` : project; sentryRelease += ` - _global.SENTRY_RELEASES=_global.SENTRY_RELEASES||{}; + _global.SENTRY_RELEASES=_global.SENTRY_RELEASES || {}; _global.SENTRY_RELEASES["${key}"]={id:"${version}"}; `; } From fddc3dee6b6db2177a72c1b86b9bf7f5ffedb04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Tue, 12 Oct 2021 12:35:20 +0200 Subject: [PATCH 5/5] Update example/test.js --- example/test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/example/test.js b/example/test.js index 54b37589..101de9b5 100644 --- a/example/test.js +++ b/example/test.js @@ -42,7 +42,6 @@ _global.SENTRY_RELEASES["my-project@my-org"] = { ) !== -1 ) { console.log('Saul Goodman, found SENTRY_RELEASES assignment in bundle'); - process.exit(0); } else { console.error('Boom, did not find SENTRY_RELEASES assignment in bundle'); process.exit(1);