33// Copyright 2019-Present Datadog, Inc.
44
55import { instrument } from '@datadog/js-instrumentation-wasm' ;
6- import { readFileSync } from '@dd/core/helpers/fs' ;
76import type { GlobalContext , PluginOptions } from '@dd/core/types' ;
7+ import { InjectPosition } from '@dd/core/types' ;
88import { createFilter } from '@rollup/pluginutils' ;
99import path from 'node:path' ;
1010
11- import { PLUGIN_NAME , PRIVACY_HELPERS_FILE_NAME } from './constants' ;
11+ import { PLUGIN_NAME } from './constants' ;
1212import { buildTransformOptions } from './transform' ;
1313import type { PrivacyOptionsWithDefaults } from './types' ;
1414
@@ -22,40 +22,20 @@ export const getPrivacyPlugin = (
2222 return ;
2323 }
2424
25+ context . inject ( {
26+ type : 'file' ,
27+ position : InjectPosition . BEFORE ,
28+ value : path . join ( __dirname , './privacy-helpers.js' ) ,
29+ } ) ;
30+
2531 const transformOptions = buildTransformOptions ( pluginOptions ) ;
2632 const transformFilter = createFilter ( pluginOptions . include , pluginOptions . exclude ) ;
27- const { helpersModule } = pluginOptions ;
2833 return {
2934 name : PLUGIN_NAME ,
3035 // Enforce when the plugin will be executed.
3136 // Not supported by Rollup and ESBuild.
3237 // https://vitejs.dev/guide/api-plugin.html#plugin-ordering
3338 enforce : 'post' ,
34- // webpack's id filter is outside of loader logic,
35- // an additional hook is needed for better perf on webpack
36- async resolveId ( source ) {
37- if ( source . includes ( helpersModule ) ) {
38- return { id : source } ;
39- }
40- return null ;
41- } ,
42-
43- loadInclude ( id ) {
44- if ( id . includes ( helpersModule ) ) {
45- return true ;
46- }
47- return false ;
48- } ,
49-
50- async load ( id ) {
51- if ( id . includes ( helpersModule ) ) {
52- const filename = `${ path . join ( __dirname , PRIVACY_HELPERS_FILE_NAME ) } .${ id . endsWith ( '.cjs' ) ? 'js' : 'mjs' } ` ;
53- return { code : readFileSync ( filename ) , map : null } ;
54- }
55- return null ;
56- } ,
57- // webpack's id filter is outside of loader logic,
58- // an additional hook is needed for better perf on webpack
5939 transformInclude ( id ) {
6040 return transformFilter ( id ) ;
6141 } ,
@@ -68,8 +48,7 @@ export const getPrivacyPlugin = (
6848 embedCodeInSourceMap : true ,
6949 } ;
7050 }
71- const result = instrument ( { id, code } , transformOptions ) ;
72- return result ;
51+ return instrument ( { id, code } , transformOptions ) ;
7352 } catch ( e ) {
7453 log . error ( `Instrumentation Error: ${ e } ` ) ;
7554 return {
0 commit comments