Skip to content
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
5 changes: 5 additions & 0 deletions packages/bundler-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
"import": "./build/esm/core/index.js",
"require": "./build/cjs/core/index.js"
},
"./loader-utils": {
"types": "./build/types/loader-utils/index.d.ts",
"import": "./build/esm/loader-utils/index.js",
"require": "./build/cjs/loader-utils/index.js"
},
"./babel-plugin": {
"types": "./build/types/babel-plugin/index.d.ts",
"import": "./build/esm/babel-plugin/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-plugins/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default makeNPMConfigVariants(
makeBaseNPMConfig({
entrypoints: [
'src/core/index.ts',
'src/loader-utils/index.ts',
'src/rollup/index.ts',
'src/vite/index.ts',
'src/esbuild/index.ts',
Expand Down
43 changes: 43 additions & 0 deletions packages/bundler-plugins/src/core/component-annotate-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createOxcComponentNameAnnotateHooks, getOxcParseAstAsync } from './component-annotation-oxc';
import type { ComponentAnnotationTransformMeta, ParseAstAsync } from './component-annotation-oxc-ast';
import type { Logger } from './logger';

const PARSER_UNAVAILABLE_MESSAGE =
'Could not load `oxc-parser` for this platform. React components will not be annotated.';

// Module level, because the Turbopack loader creates new hooks for
// every file.
let warnedParserUnavailable = false;

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createComponentNameAnnotateHooks(
ignoredComponents: string[],
injectIntoHtml: boolean,
options: { getParseAstAsync?: () => Promise<ParseAstAsync | null>; logger?: Logger } = {},
) {
const hooks = createOxcComponentNameAnnotateHooks(
ignoredComponents,
async () => {
const parseAstAsync = (await options.getParseAstAsync?.()) ?? (await getOxcParseAstAsync());

if (!parseAstAsync && !warnedParserUnavailable) {
warnedParserUnavailable = true;
if (options.logger) {
options.logger.warn(PARSER_UNAVAILABLE_MESSAGE);
} else {
// eslint-disable-next-line no-console
console.warn(`[@sentry/bundler-plugins] ${PARSER_UNAVAILABLE_MESSAGE}`);
}
}

return parseAstAsync;
},
injectIntoHtml,
);

return {
transform(this: void, code: string, id: string, meta?: ComponentAnnotationTransformMeta) {
return hooks.transform(code, id, meta);
},
};
}
43 changes: 1 addition & 42 deletions packages/bundler-plugins/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { CodeInjection, containsOnlyImports, stripQueryAndHashFromPath } from './utils';
import { createOxcComponentNameAnnotateHooks, getOxcParseAstAsync } from './component-annotation-oxc';
import type { ComponentAnnotationTransformMeta, ParseAstAsync } from './component-annotation-oxc-ast';
import type { Logger } from './logger';

/**
* Checks if a file is a JavaScript file based on its extension.
Expand Down Expand Up @@ -43,45 +40,7 @@ export function shouldSkipCodeInjection(code: string, facadeModuleId: string | n
export { globFiles } from './glob';
export { getCodeInjectionPosition } from './get-code-injection-position';

const PARSER_UNAVAILABLE_MESSAGE =
'Could not load `oxc-parser` for this platform. React components will not be annotated.';

// Module level, because the Turbopack loader creates new hooks for
// every file.
let warnedParserUnavailable = false;

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createComponentNameAnnotateHooks(
ignoredComponents: string[],
injectIntoHtml: boolean,
options: { getParseAstAsync?: () => Promise<ParseAstAsync | null>; logger?: Logger } = {},
) {
const hooks = createOxcComponentNameAnnotateHooks(
ignoredComponents,
async () => {
const parseAstAsync = (await options.getParseAstAsync?.()) ?? (await getOxcParseAstAsync());

if (!parseAstAsync && !warnedParserUnavailable) {
warnedParserUnavailable = true;
if (options.logger) {
options.logger.warn(PARSER_UNAVAILABLE_MESSAGE);
} else {
// eslint-disable-next-line no-console
console.warn(`[@sentry/bundler-plugins] ${PARSER_UNAVAILABLE_MESSAGE}`);
}
}

return parseAstAsync;
},
injectIntoHtml,
);

return {
transform(this: void, code: string, id: string, meta?: ComponentAnnotationTransformMeta) {
return hooks.transform(code, id, meta);
},
};
}
export { createComponentNameAnnotateHooks } from './component-annotate-hooks';

export function getDebugIdSnippet(debugId: string): CodeInjection {
return new CodeInjection(
Expand Down
4 changes: 4 additions & 0 deletions packages/bundler-plugins/src/loader-utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Internal to Sentry SDKs, not public API. Kept separate from `./core` so bundler loaders
// don't pull in the build plugin manager and the `sentry` CLI.
export { getCodeInjectionPosition } from '../core/get-code-injection-position';
export { createComponentNameAnnotateHooks } from '../core/component-annotate-hooks';
14 changes: 14 additions & 0 deletions packages/bundler-plugins/test/loader-utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it, vi } from 'vitest';

vi.mock('sentry', () => {
throw new Error('`loader-utils` must not import the `sentry` CLI');
});

describe('loader-utils', () => {
it('exports the loader helpers without loading the sentry CLI', async () => {
const loaderUtils = await import('../../src/loader-utils');

expect(loaderUtils.getCodeInjectionPosition).toBeInstanceOf(Function);
expect(loaderUtils.createComponentNameAnnotateHooks).toBeInstanceOf(Function);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createComponentNameAnnotateHooks } from '@sentry/bundler-plugins/core';
import { createComponentNameAnnotateHooks } from '@sentry/bundler-plugins/loader-utils';
import type { LoaderThis } from './types';

export type ComponentAnnotationLoaderOptions = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCodeInjectionPosition } from '@sentry/bundler-plugins/core';
import { getCodeInjectionPosition } from '@sentry/bundler-plugins/loader-utils';
import type { LoaderThis } from './types';

export type ModuleMetadataInjectionLoaderOptions = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCodeInjectionPosition } from '@sentry/bundler-plugins/core';
import { getCodeInjectionPosition } from '@sentry/bundler-plugins/loader-utils';
import type { LoaderThis } from './types';

export type ValueInjectionLoaderOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { mockTransform, mockCreateHooks } = vi.hoisted(() => {
return { mockTransform, mockCreateHooks };
});

vi.mock('@sentry/bundler-plugins/core', () => ({
vi.mock('@sentry/bundler-plugins/loader-utils', () => ({
createComponentNameAnnotateHooks: mockCreateHooks,
}));

Expand Down
Loading