Skip to content

Commit

Permalink
WIP Organise sourcemaps upload
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Jun 10, 2024
1 parent 05ea89d commit b477374
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/plugins/rum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"@dd/core": "workspace:*",
"chalk": "2.3.1",
"glob": "7.1.6",
"unplugin": "1.10.1"
}
}
13 changes: 5 additions & 8 deletions packages/plugins/rum/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { getLogger } from '@dd/core/log';
import type { Context } from '@dd/core/plugins';
import type { GetPlugins } from '@dd/core/types';

import { PLUGIN_NAME } from './constants';
import { uploadSourcemaps } from './sourcemaps';
import type { OptionsWithRum, RumOptions } from './types';
import { validateOptions } from './validate';

Expand All @@ -23,21 +23,18 @@ export type types = {
};

export const getPlugins: GetPlugins<OptionsWithRum> = (opts: OptionsWithRum, context: Context) => {
// Verify configuration.
const rumOptions = validateOptions(opts);
return [
{
name: PLUGIN_NAME,
async writeBundle() {
if (rumOptions.disabled || !rumOptions.sourcemaps) {
if (rumOptions.disabled) {
return;
}
const log = getLogger(opts.logLevel, PLUGIN_NAME);
log(`Uploading sourcemaps.`);

// Verify configuration.
// Gather the sourcemaps files using glob.
// Build payloads (with git info from context plugin).
// Upload the sourcemaps.
const log = getLogger(opts.logLevel, PLUGIN_NAME);
await uploadSourcemaps(rumOptions, log);
},
},
];
Expand Down
50 changes: 50 additions & 0 deletions packages/plugins/rum/src/sourcemaps/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import chalk from 'chalk';
import glob from 'glob';
import path from 'path';

import type { RumSourcemapsOptionsWithDefaults, Sourcemap } from '../types';

type PartialSourcemap = Pick<Sourcemap, 'minifiedFilePath' | 'minifiedUrl' | 'relativePath'>;

const getGlobPattern = (basePath: string) => {
// Normalizing the basePath to resolve .. and .
// Always using the posix version to avoid \ on Windows.
const newPath = path.posix.normalize(basePath);
// TODO Test for .mjs files
return path.join(newPath, '**/*.@(js|mjs).map');
};

const decomposePath = (
options: RumSourcemapsOptionsWithDefaults,
sourcemapFilePath: string,
): PartialSourcemap => {
if (path.extname(sourcemapFilePath) !== '.map') {
throw new Error(`The file ${chalk.green.bold(sourcemapFilePath)} is not a sourcemap.`);
}

const minifiedFilePath = sourcemapFilePath.replace(/\.map$/, '');
const relativePath = minifiedFilePath.replace(options.basePath, '');
const minifiedUrl = options.minifiedPathPrefix
? path.join(options.minifiedPathPrefix, relativePath)
: relativePath;

return {
minifiedFilePath,
minifiedUrl,
relativePath,
};
};

export const getSourcemapsFiles = (options: RumSourcemapsOptionsWithDefaults): Sourcemap[] => {
const globPattern = getGlobPattern(options.basePath);
const sourcemapFilesList = glob.sync(globPattern);
const sourcemapFiles = sourcemapFilesList.map((sourcemapFilePath) => {
return {
...decomposePath(options, sourcemapFilePath),
sourcemapFilePath,
minifiedPathPrefix: options.minifiedPathPrefix,
};
});

return sourcemapFiles;
};
23 changes: 23 additions & 0 deletions packages/plugins/rum/src/sourcemaps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Logger } from '@dd/core/log';

import type { RumOptionsWithDefaults } from '../types';

import { getSourcemapsFiles } from './files';
import { getPayloads } from './sender';

export const uploadSourcemaps = async (options: RumOptionsWithDefaults, log: Logger) => {
if (!options.sourcemaps) {
log(`Sourcemaps are not configured.`, 'warn');
return;
}

log(`Uploading sourcemaps.`);

// Gather the sourcemaps files.
const sourcemaps = getSourcemapsFiles(options.sourcemaps);

// Build payloads (with git info from context plugin).
const payloads = getPayloads(sourcemaps, options);

Check failure on line 20 in packages/plugins/rum/src/sourcemaps/index.ts

View workflow job for this annotation

GitHub Actions / Linting

'payloads' is assigned a value but never used

// Upload the sourcemaps.
};
3 changes: 3 additions & 0 deletions packages/plugins/rum/src/sourcemaps/sender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { RumOptionsWithDefaults, Sourcemap } from '../types';

export const getPayloads = (sourcemaps: Sourcemap[], options: RumOptionsWithDefaults) => {};
31 changes: 22 additions & 9 deletions packages/plugins/rum/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,36 @@ import type { GetPluginsOptions } from '@dd/core/types';

import type { CONFIG_KEY } from './constants';

export type RumSourcemapsOptions = {
// TODO: Compute this basePath directly from the bundler's configuration, using the CrossHelper Plugin.
basePath: string;
dryRun?: boolean;
maxConcurrency?: number;
minifiedPathPrefix?: string;
releaseVersion: string;
service: string;
};

export type RumSourcemapsOptionsWithDefaults = Required<RumSourcemapsOptions>;

export type RumOptions = {
disabled?: boolean;
sourcemaps?: {
basePath: string;
dryRun?: boolean;
maxConcurrency?: number;
minifiedPathPrefix?: string;
releaseVersion: string;
service: string;
};
sourcemaps?: RumSourcemapsOptions;
};

export type RumOptionsWithDefaults = RumOptions & {
disabled?: boolean;
sourcemaps?: Required<RumOptions['sourcemaps']>;
sourcemaps?: RumSourcemapsOptionsWithDefaults;
};

export interface OptionsWithRum extends GetPluginsOptions {
[CONFIG_KEY]: RumOptions;
}

export type Sourcemap = {
minifiedFilePath: string;
minifiedUrl: string;
relativePath: string;
sourcemapFilePath: string;
minifiedPathPrefix: string;
};

0 comments on commit b477374

Please sign in to comment.