@datadog/esbuild-plugin 2.3.1-dev-8 → 2.3.1-dev-9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2354,14 +2354,6 @@ const truncateString = (str, maxLength = 60, placeholder = "[...]") => {
2354
2354
  return `${str.slice(0, leftStop)}${placeholder}${str.slice(-rightStop)}`;
2355
2355
  };
2356
2356
  const isInjection = (filename) => filename.includes(INJECTED_FILE);
2357
- const isInternalPlugin = (pluginName, context) => {
2358
- for (const internalPluginName of context.pluginNames) {
2359
- if (pluginName.includes(internalPluginName)) {
2360
- return true;
2361
- }
2362
- }
2363
- return false;
2364
- };
2365
2357
 
2366
2358
  var balancedMatch = balanced$1;
2367
2359
  function balanced$1(a, b, str) {
@@ -19061,17 +19053,15 @@ const getInjectionPlugins = (opts, context, toInject) => {
19061
19053
  const log = getLogger(opts.logLevel, PLUGIN_NAME$4);
19062
19054
  const contentToInject = [];
19063
19055
  const getContentToInject = () => {
19064
- contentToInject.unshift(
19065
- // Needs at least one element otherwise ESBuild will throw 'Do not know how to load path'.
19066
- // Most likely because it tries to generate an empty file.
19067
- `
19056
+ const before = `
19068
19057
  /********************************************/
19069
- /* BEGIN INJECTION BY DATADOG BUILD PLUGINS */`
19070
- );
19071
- contentToInject.push(`
19058
+ /* BEGIN INJECTION BY DATADOG BUILD PLUGINS */`;
19059
+ const after = `
19072
19060
  /* END INJECTION BY DATADOG BUILD PLUGINS */
19073
- /********************************************/`);
19074
- return contentToInject.join("\n\n");
19061
+ /********************************************/`;
19062
+ return `${before}
19063
+ ${contentToInject.join("\n\n")}
19064
+ ${after}`;
19075
19065
  };
19076
19066
  const rollupInjectionPlugin = {
19077
19067
  banner(chunk) {
@@ -19082,11 +19072,11 @@ const getInjectionPlugins = (opts, context, toInject) => {
19082
19072
  }
19083
19073
  };
19084
19074
  return [
19085
- // Resolve the injected file.
19075
+ // Resolve the injected file for all bundlers.
19086
19076
  {
19087
19077
  name: RESOLUTION_PLUGIN_NAME,
19088
19078
  enforce: "pre",
19089
- resolveId(id) {
19079
+ async resolveId(id) {
19090
19080
  if (isInjection(id)) {
19091
19081
  return { id, moduleSideEffects: true };
19092
19082
  }
@@ -19102,7 +19092,7 @@ const getInjectionPlugins = (opts, context, toInject) => {
19102
19092
  }
19103
19093
  }
19104
19094
  },
19105
- // Prepare and fetch the content to inject.
19095
+ // Prepare and fetch the content to inject for all bundlers.
19106
19096
  {
19107
19097
  name: PREPARATION_PLUGIN_NAME,
19108
19098
  enforce: "pre",
@@ -19113,25 +19103,53 @@ const getInjectionPlugins = (opts, context, toInject) => {
19113
19103
  }
19114
19104
  },
19115
19105
  // Inject the virtual file that will be home of all injected content.
19106
+ // Each bundler has its own way to inject a file.
19116
19107
  {
19117
19108
  name: PLUGIN_NAME$4,
19118
19109
  esbuild: {
19119
19110
  setup(build) {
19120
19111
  const { initialOptions } = build;
19121
- const initialInject = initialOptions.inject ? [...initialOptions.inject] : [];
19122
- const plugins = initialOptions.plugins || [];
19123
- initialOptions.inject = initialOptions.inject || [];
19124
- initialOptions.inject.push(INJECTED_FILE);
19125
- for (const plugin of plugins) {
19126
- const oldSetup = plugin.setup;
19127
- if (isInternalPlugin(plugin.name, context)) {
19128
- continue;
19112
+ build.onResolve({ filter: /.*/ }, async (args) => {
19113
+ if (args.kind !== "entry-point") {
19114
+ return null;
19115
+ }
19116
+ if (initialOptions.inject?.includes(args.path)) {
19117
+ return null;
19118
+ }
19119
+ return {
19120
+ pluginName: PLUGIN_NAME$4,
19121
+ path: require$$1.isAbsolute(args.path) ? args.path : require$$1.join(args.resolveDir, args.path),
19122
+ pluginData: {
19123
+ isInjectionResolver: true,
19124
+ originalPath: args.path,
19125
+ originalResolveDir: args.resolveDir
19126
+ },
19127
+ // Adding a suffix prevents esbuild from marking the entrypoint as resolved,
19128
+ // avoiding a dependency loop with the proxy module.
19129
+ // This ensures esbuild continues to traverse the module tree
19130
+ // and re-resolves the entrypoint when imported from the proxy module.
19131
+ suffix: "?datadogInjected=true"
19132
+ };
19133
+ });
19134
+ build.onLoad({ filter: /.*/ }, async (args) => {
19135
+ if (!args.pluginData?.isInjectionResolver) {
19136
+ return null;
19129
19137
  }
19130
- plugin.setup = async (esbuild) => {
19131
- esbuild.initialOptions.inject = initialInject;
19132
- await oldSetup(esbuild);
19138
+ const originalPath = args.pluginData.originalPath;
19139
+ const originalResolveDir = args.pluginData.originalResolveDir;
19140
+ const contents = `
19141
+ import ${JSON.stringify(INJECTED_FILE)};
19142
+ import * as OriginalModule from ${JSON.stringify(originalPath)};
19143
+ export default OriginalModule['default'.toString()];
19144
+ export * from ${JSON.stringify(originalPath)};
19145
+ `;
19146
+ return {
19147
+ loader: "js",
19148
+ pluginName: PLUGIN_NAME$4,
19149
+ contents,
19150
+ resolveDir: originalResolveDir
19133
19151
  };
19134
- }
19152
+ });
19135
19153
  }
19136
19154
  },
19137
19155
  webpack: (compiler) => {
@@ -36470,7 +36488,7 @@ const buildPluginFactory = ({
36470
36488
 
36471
36489
  var name = "@datadog/esbuild-plugin";
36472
36490
  var packageManager = "yarn@4.0.2";
36473
- var version$1 = "2.3.1-dev-8";
36491
+ var version$1 = "2.3.1-dev-9";
36474
36492
  var license = "MIT";
36475
36493
  var author = "Datadog";
36476
36494
  var description = "Datadog ESBuild Plugin";