Skip to content

Commit

Permalink
Merge pull request #94 from DataDog/yoann/remove-webpack-from-vite
Browse files Browse the repository at this point in the history
[vite] Remove leaking dependencies
  • Loading branch information
yoannmoinet authored Sep 5, 2024
2 parents 6888e44 + cd927dc commit 7420768
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 104 deletions.
12 changes: 3 additions & 9 deletions packages/esbuild-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
// Anything between #types-export-injection-marker
// will be updated using the 'yarn cli integrity' command.

import { buildPluginFactory, helpers } from '@dd/factory';
import * as factory from '@dd/factory';

import pkg from '../package.json';

export const datadogEsbuildPlugin = buildPluginFactory({
export const datadogEsbuildPlugin = factory.buildPluginFactory({
version: pkg.version,
}).esbuild;

export { helpers } from '@dd/factory';

export type { Options as EsbuildPluginOptions } from '@dd/core/types';

export type {
Expand All @@ -25,8 +23,4 @@ export type {
// #types-export-injection-marker
} from '@dd/factory';

// This is to prevent overrides from other libraries in the final bundle.
module.exports = {
helpers,
datadogEsbuildPlugin,
};
export const helpers = factory.helpers;
13 changes: 12 additions & 1 deletion packages/factory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,27 @@ export const helpers = {
// #helpers-injection-marker
};

const validateOptions = (options: Options = {}): Options => {
return {
auth: {},
disableGit: false,
logLevel: 'warn',
...options,
};
};

export const buildPluginFactory = ({
version,
}: {
version: string;
}): UnpluginInstance<Options, true> => {
return createUnplugin((options: Options, unpluginMetaContext: UnpluginContextMeta) => {
return createUnplugin((opts: Options, unpluginMetaContext: UnpluginContextMeta) => {
// TODO: Implement config overrides with environment variables.
// TODO: Validate API Key and endpoint.
// TODO: Inject a metric logger into the global context.

const options = validateOptions(opts);

// Set the host name for the esbuild plugin.
if ('esbuildHostName' in unpluginMetaContext) {
unpluginMetaContext.esbuildHostName = 'datadog-plugins';
Expand Down
15 changes: 11 additions & 4 deletions packages/plugins/telemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@
"dependencies": {
"@dd/core": "workspace:*",
"chalk": "2.3.1",
"esbuild": "0.21.5",
"fs-extra": "7.0.1",
"pretty-bytes": "5.6.0",
"unplugin": "1.11.0",
"webpack": "5.92.1"
"unplugin": "1.11.0"
},
"devDependencies": {
"@types/chalk": "2.2.0",
"@types/fs-extra": "8.1.0"
"@types/fs-extra": "8.1.0",
"esbuild": "0.21.5"
},
"peerDependencies": {
"esbuild": ">=0.x",
"webpack": ">= 4.x < 6.x"
},
"peerDependenciesMeta": {
"esbuild": {
"optional": true
},
"webpack": {
"optional": true
}
}
}
1 change: 1 addition & 0 deletions packages/plugins/telemetry/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export interface Hook {
tap?: Tap;
tapAsync?: TapAsync;
tapPromise?: TapPromise;
_fakeHook?: boolean;
}

export interface Tapable {
Expand Down
56 changes: 24 additions & 32 deletions packages/plugins/telemetry/src/webpack-plugin/tapables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
// Copyright 2019-Present Datadog, Inc.

import { performance } from 'perf_hooks';
// In order to not overlap with our own Compilation type.
// TODO use native webpack types now that we need to import it.
import webpack from 'webpack';

import { getPluginName, getValueContext } from '../common/helpers';
import type {
Expand All @@ -25,7 +22,7 @@ import type {
} from '../types';

export class Tapables {
constructor(cwd: string, options: TelemetryOptions) {
constructor(cwd: Tapables['cwd'], options: Tapables['options']) {
this.options = options;
this.cwd = cwd;
}
Expand Down Expand Up @@ -194,50 +191,45 @@ export class Tapables {
hook.tapPromise = this.newTap('promise', hookName, hook.tapPromise!, hook);
}

patchHook(tapableName: string, hookName: string, hook: Hook) {
// Webpack 5 specific, these _fakeHook are not writable.
// eslint-disable-next-line no-underscore-dangle
if (hook._fakeHook) {
return;
}

if (!this.hooks[tapableName]) {
this.hooks[tapableName] = [];
}

if (this.hooks[tapableName].includes(hookName)) {
return;
}

this.hooks[tapableName].push(hookName);
this.replaceTaps(hookName, hook);
}

checkHooks() {
// We reparse hooks in case new ones arrived.
for (const tapable of this.tapables) {
const name = tapable.constructor.name;
for (const hookName of Object.keys(tapable.hooks)) {
if (!this.hooks[name].includes(hookName)) {
this.hooks[name].push(hookName);
this.replaceTaps(hookName, tapable.hooks[hookName]);
}
this.patchHook(name, hookName, tapable.hooks[hookName]);
}
}
}

// Let's navigate through all the hooks we can find.
throughHooks(tapable: Tapable) {
const name = tapable.constructor.name;

if (!this.tapables.includes(tapable)) {
this.tapables.push(tapable);
}
if (!this.hooks[name]) {
this.hooks[name] = [];
}

for (const hookName of Object.keys(tapable.hooks)) {
this.hooks[name].push(hookName);
try {
// Webpack 5 deprecation fix for DEP_WEBPACK_COMPILATION_NORMAL_MODULE_LOADER_HOOK.
if (
hookName === 'normalModuleLoader' &&
typeof webpack.NormalModule.getCompilationHooks === 'function'
) {
const NormalModule = webpack.NormalModule;
// Needed to use it "as webpack.Compilation"
const compil = tapable as unknown;
this.replaceTaps(
hookName,
NormalModule.getCompilationHooks(compil as webpack.Compilation).loader,
);
} else {
this.replaceTaps(hookName, tapable.hooks[hookName]);
}
} catch (e) {
// In Webpack 5 hooks are frequently read-only objects.
// TODO Find a way to replace them.
}
this.patchHook(name, hookName, tapable.hooks[hookName]);
}
}
}
12 changes: 3 additions & 9 deletions packages/rollup-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
// Anything between #types-export-injection-marker
// will be updated using the 'yarn cli integrity' command.

import { buildPluginFactory, helpers } from '@dd/factory';
import * as factory from '@dd/factory';

import pkg from '../package.json';

export const datadogRollupPlugin = buildPluginFactory({
export const datadogRollupPlugin = factory.buildPluginFactory({
version: pkg.version,
}).rollup;

export { helpers } from '@dd/factory';

export type { Options as RollupPluginOptions } from '@dd/core/types';

export type {
Expand All @@ -25,8 +23,4 @@ export type {
// #types-export-injection-marker
} from '@dd/factory';

// This is to prevent overrides from other libraries in the final bundle.
module.exports = {
helpers,
datadogRollupPlugin,
};
export const helpers = factory.helpers;
9 changes: 9 additions & 0 deletions packages/tests/src/factory/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ jest.mock('@dd/telemetry-plugins', () => {
const getPluginsMocked = jest.mocked(getPlugins);

describe('Factory', () => {
test('It should not throw with no options', async () => {
const { buildPluginFactory } = await import('@dd/factory');
expect(() => {
const factory = buildPluginFactory({ version: '1.0.0' });
// Vite can call the factory without options.
// @ts-expect-error - We are testing the factory without options.
factory.vite();
}).not.toThrow();
});
test('It should not call a disabled plugin', async () => {
await runBundlers({ telemetry: { disabled: true } });
expect(getPluginsMocked).not.toHaveBeenCalled();
Expand Down

This file was deleted.

11 changes: 11 additions & 0 deletions packages/tools/src/rollupConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ export const bundle = (config) => ({
output: {
exports: 'named',
sourcemap: true,
// This is to prevent overrides from other libraries in the final bundle.
// "outdent" for instance does override the default value of `exports`.
outro: (rendered) => {
return `
if (typeof module !== 'undefined') {
module.exports = {
${rendered.exports.join(',\n ')}
};
}
`;
},
...config.output,
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@
"typescript": "5.4.3"
},
"peerDependencies": {
"rollup": ">= 4.x < 6.x"
"vite": "5.x"
}
}
16 changes: 5 additions & 11 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
// Anything between #types-export-injection-marker
// will be updated using the 'yarn cli integrity' command.

import { buildPluginFactory, helpers } from '@dd/factory';
import * as factory from '@dd/factory';

import pkg from '../package.json';
import { version } from '../package.json';

export const datadogVitePlugin = buildPluginFactory({
version: pkg.version,
export const datadogVitePlugin = factory.buildPluginFactory({
version,
}).vite;

export { helpers } from '@dd/factory';

export type { Options as VitePluginOptions } from '@dd/core/types';

export type {
Expand All @@ -25,8 +23,4 @@ export type {
// #types-export-injection-marker
} from '@dd/factory';

// This is to prevent overrides from other libraries in the final bundle.
module.exports = {
helpers,
datadogVitePlugin,
};
export const helpers = factory.helpers;
16 changes: 5 additions & 11 deletions packages/webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
// Anything between #types-export-injection-marker
// will be updated using the 'yarn cli integrity' command.

import { buildPluginFactory, helpers } from '@dd/factory';
import * as factory from '@dd/factory';

import pkg from '../package.json';
import { version } from '../package.json';

export const datadogWebpackPlugin = buildPluginFactory({
version: pkg.version,
export const datadogWebpackPlugin = factory.buildPluginFactory({
version,
}).webpack;

export { helpers } from '@dd/factory';

export type { Options as WebpackPluginOptions } from '@dd/core/types';

export type {
Expand All @@ -25,8 +23,4 @@ export type {
// #types-export-injection-marker
} from '@dd/factory';

// This is to prevent overrides from other libraries in the final bundle.
module.exports = {
helpers,
datadogWebpackPlugin,
};
export const helpers = factory.helpers;
8 changes: 6 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ __metadata:
rollup-plugin-esbuild: "npm:6.1.1"
typescript: "npm:5.4.3"
peerDependencies:
rollup: ">= 4.x < 6.x"
vite: 5.x
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -1832,10 +1832,14 @@ __metadata:
fs-extra: "npm:7.0.1"
pretty-bytes: "npm:5.6.0"
unplugin: "npm:1.11.0"
webpack: "npm:5.92.1"
peerDependencies:
esbuild: ">=0.x"
webpack: ">= 4.x < 6.x"
peerDependenciesMeta:
esbuild:
optional: true
webpack:
optional: true
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 7420768

Please sign in to comment.