Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,41 @@ 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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this process.exit, and why not add it to the previous check? Should we expect to find all the assignments? If we successfully find one but fail the others, should we exit? If this is the case, should we move the process.exit(0) in the last block to run after it?

Copy link
Contributor Author

@hasanayan hasanayan Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there used to be only one step check of existence on a string. Now the test does 3-step check (for existence of 3 different strings). when any of them fails, we call process.exit(1). However, calling process.exit(0) should be done when only the last check passes successfully because; let's say if we put it on success case of the first case, the test will simply finish without continuing on the next steps.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the case, should we move the process.exit(0) in the last block to run after it?

So moving the last process.exit(0) out of the block, so that the logic is the same?

Instead of:

if (!A) exit(1)
if (!B) exit(1)
if (C) exit(0)
if (!C) exit(1)

this instead:

if (!A) exit(1)
if (!B) exit(1)
if (!C) exit(1)
exit(0) <-- and omit this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with Iker. Updated the test. Last process.exit(0) is redundant anyway, as scripts by default exit with 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');
} else {
console.error('Boom, did not find SENTRY_RELEASES assignment in bundle');
process.exit(1);
}
2 changes: 2 additions & 0 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module.exports = {
configFile: 'sentry.properties',
dryRun: true,
release: 'foo',
project: 'my-project',
org: 'my-org',
dist: '123',
}),
],
Expand Down
67 changes: 67 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,63 @@ function attachAfterEmitHook(compiler, callback) {
}
}

function attachAfterCodeGenerationHook(compiler, options) {
if (!compiler.hooks || !compiler.hooks.make) {
return;
}

let webpackSources;
try {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
webpackSources = require('webpack-sources');
} catch (_e) {
console.warn(
'Coud not resolve package: webpack-sources. Skipping injection for the remote entry file.'
);
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 !== moduleFederationPlugin._options.name) 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 = {
Expand Down Expand Up @@ -318,6 +375,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,
},
};

Expand All @@ -333,6 +392,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,
},
},
],
Expand Down Expand Up @@ -501,6 +562,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);
Expand Down
11 changes: 9 additions & 2 deletions src/sentry.loader.js
Original file line number Diff line number Diff line change
@@ -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);
});
};