33// Copyright 2019-Present Datadog, Inc.
44
55import { instrument } from '@datadog/js-instrumentation-wasm' ;
6- import type { PluginOptions } from '@dd/core/types' ;
6+ import type { GlobalContext , PluginOptions } from '@dd/core/types' ;
77import { createFilter } from '@rollup/pluginutils' ;
88import fs from 'node:fs' ;
99import path from 'node:path' ;
@@ -12,38 +12,54 @@ import { PRIVACY_HELPERS_MODULE_ID, PLUGIN_NAME } from './constants';
1212import { buildTransformOptions } from './transform' ;
1313import type { PrivacyOptions } from './types' ;
1414
15- export const getPrivacyPlugin = ( pluginOptions : PrivacyOptions ) : PluginOptions | undefined => {
15+ export const getPrivacyPlugin = (
16+ pluginOptions : PrivacyOptions ,
17+ context : GlobalContext ,
18+ ) : PluginOptions | undefined => {
19+ const log = context . getLogger ( PLUGIN_NAME ) ;
20+
1621 if ( pluginOptions . disabled ) {
1722 return ;
1823 }
1924
2025 const transformOptions = buildTransformOptions ( pluginOptions ) ;
2126 const transformFilter = createFilter ( pluginOptions . include , pluginOptions . exclude ) ;
22-
23- // Read the privacy helpers code
24- const privacyHelpersPath = path . join (
25- __dirname ,
26- pluginOptions . module === 'cjs' ? './privacy-helpers.js' : './privacy-helpers.mjs' ,
27- ) ;
28-
2927 return {
3028 name : PLUGIN_NAME ,
3129 // Enforce when the plugin will be executed.
3230 // Not supported by Rollup and ESBuild.
3331 // https://vitejs.dev/guide/api-plugin.html#plugin-ordering
34- enforce : 'pre ' ,
32+ enforce : 'post ' ,
3533 // webpack's id filter is outside of loader logic,
3634 // an additional hook is needed for better perf on webpack
3735 async resolveId ( source ) {
38- if ( source === PRIVACY_HELPERS_MODULE_ID ) {
39- return { id : PRIVACY_HELPERS_MODULE_ID } ;
36+ if ( source . includes ( PRIVACY_HELPERS_MODULE_ID ) ) {
37+ return { id : source } ;
4038 }
4139 return null ;
4240 } ,
4341
4442 async load ( id ) {
45- if ( id === PRIVACY_HELPERS_MODULE_ID ) {
46- return { code : fs . readFileSync ( privacyHelpersPath , 'utf8' ) } ;
43+ let privacyHelpersPath : string ;
44+ if ( id . includes ( PRIVACY_HELPERS_MODULE_ID ) ) {
45+ if ( id . endsWith ( '.cjs' ) ) {
46+ privacyHelpersPath = path . join ( __dirname , 'privacy-helpers.js' ) ;
47+ } else {
48+ privacyHelpersPath = path . join ( __dirname , 'privacy-helpers.mjs' ) ;
49+ }
50+ const code = fs . readFileSync ( privacyHelpersPath , 'utf8' ) ;
51+ if ( context . bundler . name === 'rollup' ) {
52+ // prepend AAAA to sourcemap
53+ const sourcemap = {
54+ version : 3 ,
55+ sources : [ privacyHelpersPath ] ,
56+ sourcesContent : [ code ] ,
57+ names : [ ] ,
58+ mappings : Array ( code . split ( '\n' ) . length ) . fill ( 'AAAA' ) . join ( ';' ) ,
59+ } ;
60+ return { code, map : sourcemap } ;
61+ }
62+ return { code, map : null } ;
4763 }
4864 return null ;
4965 } ,
@@ -53,7 +69,25 @@ export const getPrivacyPlugin = (pluginOptions: PrivacyOptions): PluginOptions |
5369 return transformFilter ( id ) ;
5470 } ,
5571 async transform ( code , id ) {
56- return instrument ( { id, code } , transformOptions ) ;
72+ try {
73+ if (
74+ context . bundler . name === 'esbuild' ||
75+ context . bundler . name === 'webpack' ||
76+ context . bundler . name === 'rspack'
77+ ) {
78+ transformOptions . output = {
79+ ...transformOptions . output ,
80+ inlineSourceMap : false ,
81+ embedCodeInSourceMap : true ,
82+ } ;
83+ }
84+ return instrument ( { id, code } , transformOptions ) ;
85+ } catch ( e ) {
86+ log . error ( `Instrumentation Error: ${ e } ` ) ;
87+ return {
88+ code,
89+ } ;
90+ }
5791 } ,
5892 } ;
5993} ;
0 commit comments