Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit e621cea

Browse files
authored
feat: Add support for multiple apps using module federation (#307)
1 parent 8e2e904 commit e621cea

File tree

4 files changed

+109
-6
lines changed

4 files changed

+109
-6
lines changed

example/test.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,41 @@ console.log();
88

99
const content = fs.readFileSync('./dist/main.bundle.js');
1010
if (
11-
content.toString()
12-
.indexOf(`(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}).SENTRY_RELEASE = {
11+
content
12+
.toString()
13+
.indexOf(
14+
`var _global = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};`
15+
) !== -1
16+
) {
17+
console.log('Saul Goodman, found "var _global" assignment in bundle');
18+
} else {
19+
console.error('Boom, did not find "var _global" assignment in bundle');
20+
process.exit(1);
21+
}
22+
23+
if (
24+
content.toString().indexOf(
25+
`_global.SENTRY_RELEASE = {
1326
id: "foo"
14-
}`) !== -1
27+
};`
28+
) !== -1
1529
) {
1630
console.log('Saul Goodman, found SENTRY_RELEASE in bundle');
17-
process.exit(0);
1831
} else {
1932
console.error('Boom, did not find SENTRY_RELEASE in bundle');
2033
process.exit(1);
2134
}
35+
36+
if (
37+
content.toString().indexOf(
38+
`_global.SENTRY_RELEASES = _global.SENTRY_RELEASES || {};
39+
_global.SENTRY_RELEASES["my-project@my-org"] = {
40+
id: "foo"
41+
};`
42+
) !== -1
43+
) {
44+
console.log('Saul Goodman, found SENTRY_RELEASES assignment in bundle');
45+
} else {
46+
console.error('Boom, did not find SENTRY_RELEASES assignment in bundle');
47+
process.exit(1);
48+
}

example/webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ module.exports = {
2929
configFile: 'sentry.properties',
3030
dryRun: true,
3131
release: 'foo',
32+
project: 'my-project',
33+
org: 'my-org',
3234
dist: '123',
3335
}),
3436
],

src/index.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,63 @@ function attachAfterEmitHook(compiler, callback) {
7474
}
7575
}
7676

77+
function attachAfterCodeGenerationHook(compiler, options) {
78+
if (!compiler.hooks || !compiler.hooks.make) {
79+
return;
80+
}
81+
82+
let webpackSources;
83+
try {
84+
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
85+
webpackSources = require('webpack-sources');
86+
} catch (_e) {
87+
console.warn(
88+
'Coud not resolve package: webpack-sources. Skipping injection for the remote entry file.'
89+
);
90+
return;
91+
}
92+
93+
const { RawSource } = webpackSources;
94+
const moduleFederationPlugin =
95+
compiler.options &&
96+
compiler.options.plugins &&
97+
compiler.options.plugins.find(
98+
x => x.constructor.name === 'ModuleFederationPlugin'
99+
);
100+
101+
if (!moduleFederationPlugin) {
102+
return;
103+
}
104+
105+
compiler.hooks.make.tapAsync('SentryCliPlugin', (compilation, cb) => {
106+
options.releasePromise.then(version => {
107+
compilation.hooks.afterCodeGeneration.tap('SentryCliPlugin', () => {
108+
compilation.modules.forEach(module => {
109+
// eslint-disable-next-line no-underscore-dangle
110+
if (module._name !== moduleFederationPlugin._options.name) return;
111+
const sourceMap = compilation.codeGenerationResults.get(module)
112+
.sources;
113+
const rawSource = sourceMap.get('javascript');
114+
sourceMap.set(
115+
'javascript',
116+
new RawSource(
117+
`${rawSource.source()}
118+
(function (){
119+
var globalThis = (typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {});
120+
globalThis.SENTRY_RELEASES = globalThis.SENTRY_RELEASES || {};
121+
globalThis.SENTRY_RELEASES["${options.project}@${
122+
options.org
123+
}"] = {"id":"${version}"};
124+
})();`
125+
)
126+
);
127+
});
128+
});
129+
cb();
130+
});
131+
});
132+
}
133+
77134
class SentryCliPlugin {
78135
constructor(options = {}) {
79136
const defaults = {
@@ -318,6 +375,8 @@ class SentryCliPlugin {
318375
loader: SENTRY_LOADER,
319376
options: {
320377
releasePromise: this.release,
378+
org: this.options.org || process.env.SENTRY_ORG,
379+
project: this.options.project || process.env.SENTRY_PROJECT,
321380
},
322381
};
323382

@@ -333,6 +392,8 @@ class SentryCliPlugin {
333392
loader: SENTRY_LOADER,
334393
options: {
335394
releasePromise: this.release,
395+
org: this.options.org || process.env.SENTRY_ORG,
396+
project: this.options.project || process.env.SENTRY_PROJECT,
336397
},
337398
},
338399
],
@@ -501,6 +562,12 @@ class SentryCliPlugin {
501562
this.injectRelease(compilerOptions);
502563
}
503564

565+
attachAfterCodeGenerationHook(compiler, {
566+
releasePromise: this.release,
567+
org: this.options.org || process.env.SENTRY_ORG,
568+
project: this.options.project || process.env.SENTRY_PROJECT,
569+
});
570+
504571
attachAfterEmitHook(compiler, (compilation, cb) => {
505572
if (!this.options.include || !this.options.include.length) {
506573
ensure(compilerOptions, 'output', Object);

src/sentry.loader.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
module.exports = function sentryLoader(content, map, meta) {
2-
const { releasePromise } = this.query;
2+
const { releasePromise, org, project } = this.query;
33
const callback = this.async();
44
releasePromise.then(version => {
5-
const sentryRelease = `(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}).SENTRY_RELEASE={id:"${version}"};`;
5+
let sentryRelease = `const _global = (typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}); _global.SENTRY_RELEASE={id:"${version}"};`;
6+
if (project) {
7+
const key = org ? `${project}@${org}` : project;
8+
sentryRelease += `
9+
_global.SENTRY_RELEASES=_global.SENTRY_RELEASES || {};
10+
_global.SENTRY_RELEASES["${key}"]={id:"${version}"};
11+
`;
12+
}
613
callback(null, sentryRelease, map, meta);
714
});
815
};

0 commit comments

Comments
 (0)