mirror of
https://github.com/SigNoz/signoz.git
synced 2026-11-26 02:00:41 +00:00
Compare commits
2 Commits
main
...
perf/monac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65eb2d3765 | ||
|
|
b018fdf4a8 |
@@ -298,6 +298,8 @@
|
||||
// Steers call sites to the src/lib/router facade (allowlisted in overrides below)
|
||||
"signoz/no-builder-ai-query-access": "error",
|
||||
// Steers AI slot access to lib/query/builderSlot (allowlisted in overrides below)
|
||||
"signoz/no-monaco-stripped-features": "error",
|
||||
// Bans Monaco features the production build strips, and monaco-editor value imports outside src/lib/monaco (allowlisted in overrides below)
|
||||
"no-restricted-globals": [
|
||||
"error",
|
||||
{
|
||||
@@ -634,6 +636,13 @@
|
||||
"rules": {
|
||||
"signoz/no-builder-ai-query-access": "off"
|
||||
}
|
||||
},
|
||||
{
|
||||
// src/lib/monaco owns the monaco-editor imports that pick the languages and workers
|
||||
"files": ["src/lib/monaco/**"],
|
||||
"rules": {
|
||||
"signoz/no-monaco-stripped-features": "off"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
123
frontend/plugins/__tests__/no-monaco-stripped-features.test.mjs
Normal file
123
frontend/plugins/__tests__/no-monaco-stripped-features.test.mjs
Normal file
@@ -0,0 +1,123 @@
|
||||
import { ruleTester } from './rule-tester.mjs';
|
||||
|
||||
const IMPORT = 'Import Monaco from lib/monaco/setup';
|
||||
const STRIPPED = 'which the production build strips';
|
||||
|
||||
await ruleTester({
|
||||
rule: 'no-monaco-stripped-features',
|
||||
valid: [
|
||||
{
|
||||
name: 'setup import',
|
||||
code: "import { monaco } from 'lib/monaco/setup';",
|
||||
},
|
||||
{
|
||||
name: 'type-only import',
|
||||
code: "import type { editor } from 'monaco-editor';",
|
||||
},
|
||||
{
|
||||
name: 'inline type specifiers only',
|
||||
code: "import { type editor, type languages } from 'monaco-editor';",
|
||||
},
|
||||
{
|
||||
name: 'type-only re-export',
|
||||
code: "export type { editor } from 'monaco-editor';",
|
||||
},
|
||||
{
|
||||
name: 'react wrapper',
|
||||
code: "import Editor from '@monaco-editor/react';",
|
||||
},
|
||||
{
|
||||
name: 'kept provider',
|
||||
code: "monaco.languages.registerCompletionItemProvider('json', {});",
|
||||
},
|
||||
{
|
||||
name: 'stripped option turned off',
|
||||
code:
|
||||
"const o = { experimentalGpuAcceleration: 'off', minimap: { showMarkSectionHeaders: false } };",
|
||||
},
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
name: 'package entry import',
|
||||
code: "import * as monaco from 'monaco-editor';",
|
||||
errors: [{ message: IMPORT, line: 1, column: 25 }],
|
||||
},
|
||||
{
|
||||
name: 'deep import',
|
||||
code:
|
||||
"import 'monaco-editor/esm/vs/basic-languages/python/python.contribution.js';",
|
||||
errors: [{ message: IMPORT }],
|
||||
},
|
||||
{
|
||||
name: 'mixed value and type specifiers',
|
||||
code: "import { editor, type languages } from 'monaco-editor';",
|
||||
errors: [{ message: IMPORT }],
|
||||
},
|
||||
{
|
||||
name: 'dynamic import',
|
||||
code: "const m = await import('monaco-editor');",
|
||||
errors: [{ message: IMPORT }],
|
||||
},
|
||||
{
|
||||
name: 'require',
|
||||
code: "const m = require('monaco-editor');",
|
||||
errors: [{ message: IMPORT }],
|
||||
},
|
||||
{
|
||||
name: 're-export',
|
||||
code: "export { editor } from 'monaco-editor';",
|
||||
errors: [{ message: IMPORT }],
|
||||
},
|
||||
{
|
||||
name: 'export all',
|
||||
code: "export * from 'monaco-editor';",
|
||||
errors: [{ message: IMPORT }],
|
||||
},
|
||||
{
|
||||
name: 'stripped provider on monaco.languages',
|
||||
code: "monaco.languages.registerInlayHintsProvider('sql', {});",
|
||||
errors: [
|
||||
{
|
||||
message: 'needs the Monaco `inlayHints` contribution',
|
||||
line: 1,
|
||||
column: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'stripped provider on a destructured languages',
|
||||
code: "languages.registerSignatureHelpProvider('sql', {});",
|
||||
errors: [{ message: '`parameterHints`' }],
|
||||
},
|
||||
{
|
||||
name: 'stripped provider called bare',
|
||||
code: "registerRenameProvider('json', {});",
|
||||
errors: [{ message: '`rename`' }],
|
||||
},
|
||||
{
|
||||
name: 'stripped provider through a string key',
|
||||
code: "monaco.languages['registerRenameProvider']('json', {});",
|
||||
errors: [{ message: '`rename`' }],
|
||||
},
|
||||
{
|
||||
name: 'semantic tokens provider',
|
||||
code: "monaco.languages.registerDocumentSemanticTokensProvider('json', {});",
|
||||
errors: [{ message: '`semanticTokens`' }],
|
||||
},
|
||||
{
|
||||
name: 'stripped option turned on',
|
||||
code: "const o = { experimentalGpuAcceleration: 'on' };",
|
||||
errors: [{ message: '`gpu`', line: 1, column: 13 }],
|
||||
},
|
||||
{
|
||||
name: 'stripped option with a variable value',
|
||||
code: 'const o = { minimap: { showRegionSectionHeaders: enabled } };',
|
||||
errors: [{ message: STRIPPED }],
|
||||
},
|
||||
{
|
||||
name: 'quoted option key',
|
||||
code: "const o = { 'showMarkSectionHeaders': true };",
|
||||
errors: [{ message: '`sectionHeaders`' }],
|
||||
},
|
||||
],
|
||||
});
|
||||
138
frontend/plugins/rules/no-monaco-stripped-features.mjs
Normal file
138
frontend/plugins/rules/no-monaco-stripped-features.mjs
Normal file
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* Rule: no-monaco-stripped-features
|
||||
*
|
||||
* The production build drops the Monaco contributions listed in
|
||||
* plugins/rules/shared/monaco-stripped-features.mjs. Dev and Storybook still carry them,
|
||||
* so a provider or option for one of them works there and silently does nothing
|
||||
* in production. This rule reports them, plus value imports of `monaco-editor`
|
||||
* outside src/lib/monaco: the package entry registers every language and worker.
|
||||
*
|
||||
* src/lib/monaco is allowlisted via overrides in .oxlintrc.json.
|
||||
*/
|
||||
|
||||
import { MONACO_STRIPPED_FEATURES } from './shared/monaco-stripped-features.mjs';
|
||||
|
||||
const featureByApi = new Map();
|
||||
const featureByOption = new Map();
|
||||
for (const [feature, { providers = [], options = [] }] of Object.entries(
|
||||
MONACO_STRIPPED_FEATURES,
|
||||
)) {
|
||||
providers.forEach((name) => featureByApi.set(name, feature));
|
||||
options.forEach((name) => featureByOption.set(name, feature));
|
||||
}
|
||||
|
||||
function isMonacoSource(value) {
|
||||
return value === 'monaco-editor' || value.startsWith('monaco-editor/');
|
||||
}
|
||||
|
||||
function isTypeOnlyImport(node) {
|
||||
return (
|
||||
node.importKind === 'type' ||
|
||||
(node.specifiers.length > 0 &&
|
||||
node.specifiers.every((s) => s.importKind === 'type'))
|
||||
);
|
||||
}
|
||||
|
||||
function propertyName(node) {
|
||||
if (node.type === 'Identifier') {
|
||||
return node.name;
|
||||
}
|
||||
if (node.type === 'Literal' && typeof node.value === 'string') {
|
||||
return node.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function isDisabledValue(node) {
|
||||
return (
|
||||
node.type === 'Literal' && (node.value === false || node.value === 'off')
|
||||
);
|
||||
}
|
||||
|
||||
export default {
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description:
|
||||
'Disallow Monaco features the production build strips, and value imports of monaco-editor outside src/lib/monaco',
|
||||
category: 'Monaco',
|
||||
},
|
||||
schema: [],
|
||||
messages: {
|
||||
provider:
|
||||
'{{name}} needs the Monaco `{{feature}}` contribution, which the production build strips, so it works in dev and does nothing in production. Remove `{{feature}}` from plugins/rules/shared/monaco-stripped-features.mjs to use it.',
|
||||
option:
|
||||
'`{{name}}` needs the Monaco `{{feature}}` contribution, which the production build strips, so it works in dev and does nothing in production. Remove `{{feature}}` from plugins/rules/shared/monaco-stripped-features.mjs to use it.',
|
||||
import:
|
||||
'Import Monaco from lib/monaco/setup, not monaco-editor: the package entry registers every language and worker. Type-only imports are fine.',
|
||||
},
|
||||
},
|
||||
|
||||
create(context) {
|
||||
const reportSource = (source) => {
|
||||
if (
|
||||
source?.type === 'Literal' &&
|
||||
typeof source.value === 'string' &&
|
||||
isMonacoSource(source.value)
|
||||
) {
|
||||
context.report({ node: source, messageId: 'import' });
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
ImportDeclaration(node) {
|
||||
if (!isTypeOnlyImport(node)) {
|
||||
reportSource(node.source);
|
||||
}
|
||||
},
|
||||
ImportExpression: (node) => reportSource(node.source),
|
||||
ExportAllDeclaration(node) {
|
||||
if (node.exportKind !== 'type') {
|
||||
reportSource(node.source);
|
||||
}
|
||||
},
|
||||
ExportNamedDeclaration(node) {
|
||||
if (node.exportKind !== 'type') {
|
||||
reportSource(node.source);
|
||||
}
|
||||
},
|
||||
CallExpression(node) {
|
||||
const { callee } = node;
|
||||
if (callee.type === 'Identifier' && callee.name === 'require') {
|
||||
reportSource(node.arguments[0]);
|
||||
return;
|
||||
}
|
||||
const name =
|
||||
callee.type === 'MemberExpression'
|
||||
? propertyName(callee.property)
|
||||
: callee.type === 'Identifier'
|
||||
? callee.name
|
||||
: null;
|
||||
if (name !== null && featureByApi.has(name)) {
|
||||
context.report({
|
||||
node: callee,
|
||||
messageId: 'provider',
|
||||
data: { name, feature: featureByApi.get(name) },
|
||||
});
|
||||
}
|
||||
},
|
||||
Property(node) {
|
||||
if (node.computed) {
|
||||
return;
|
||||
}
|
||||
const name = propertyName(node.key);
|
||||
if (
|
||||
name !== null &&
|
||||
featureByOption.has(name) &&
|
||||
!isDisabledValue(node.value)
|
||||
) {
|
||||
context.report({
|
||||
node: node.key,
|
||||
messageId: 'option',
|
||||
data: { name, feature: featureByOption.get(name) },
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
29
frontend/plugins/rules/shared/monaco-stripped-features.mjs
Normal file
29
frontend/plugins/rules/shared/monaco-stripped-features.mjs
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Monaco editor contributions the production build drops (vite.config.ts marks
|
||||
* them side-effect free), with the API that would need them at runtime.
|
||||
* `signoz/no-monaco-stripped-features` reports those APIs, since the build
|
||||
* leaves them without an error and without an effect.
|
||||
*
|
||||
* To use one, delete its entry here.
|
||||
*/
|
||||
export const MONACO_STRIPPED_FEATURES = {
|
||||
codelens: { providers: ['registerCodeLensProvider'] },
|
||||
dropOrPasteInto: {},
|
||||
gpu: { options: ['experimentalGpuAcceleration'] },
|
||||
inlayHints: { providers: ['registerInlayHintsProvider'] },
|
||||
inlineCompletions: { providers: ['registerInlineCompletionsProvider'] },
|
||||
linkedEditing: { providers: ['registerLinkedEditingRangeProvider'] },
|
||||
parameterHints: { providers: ['registerSignatureHelpProvider'] },
|
||||
rename: {
|
||||
providers: ['registerRenameProvider', 'registerNewSymbolNameProvider'],
|
||||
},
|
||||
sectionHeaders: {
|
||||
options: ['showRegionSectionHeaders', 'showMarkSectionHeaders'],
|
||||
},
|
||||
semanticTokens: {
|
||||
providers: [
|
||||
'registerDocumentSemanticTokensProvider',
|
||||
'registerDocumentRangeSemanticTokensProvider',
|
||||
],
|
||||
},
|
||||
};
|
||||
@@ -21,6 +21,7 @@ import noTextCss from './rules/no-text-css.mjs';
|
||||
import requireDisableReason from './rules/require-disable-reason.mjs';
|
||||
import noDomNodeAccess from './rules/no-dom-node-access.mjs';
|
||||
import noBuilderAiQueryAccess from './rules/no-builder-ai-query-access.mjs';
|
||||
import noMonacoStrippedFeatures from './rules/no-monaco-stripped-features.mjs';
|
||||
|
||||
export default {
|
||||
meta: {
|
||||
@@ -43,5 +44,6 @@ export default {
|
||||
'require-disable-reason': requireDisableReason,
|
||||
'no-dom-node-access': noDomNodeAccess,
|
||||
'no-builder-ai-query-access': noBuilderAiQueryAccess,
|
||||
'no-monaco-stripped-features': noMonacoStrippedFeatures,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import * as monaco from 'monaco-editor';
|
||||
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
|
||||
import * as jsonContribution from 'monaco-editor/esm/vs/language/json/monaco.contribution.js';
|
||||
import 'monaco-editor/esm/vs/basic-languages/sql/sql.contribution.js';
|
||||
import 'monaco-editor/esm/vs/basic-languages/yaml/yaml.contribution.js';
|
||||
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
|
||||
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
|
||||
|
||||
@@ -11,10 +14,13 @@ self.MonacoEnvironment = {
|
||||
if (label === 'json') {
|
||||
return new JsonWorker(); // JSON language service
|
||||
}
|
||||
// SigNoz editors use JSON + a hand-registered ClickHouse tokenizer (no worker),
|
||||
// so the base editor worker covers everything else.
|
||||
return new EditorWorker(); // base worker — sql, yaml, plaintext etc.
|
||||
// SigNoz editors use JSON, SQL and YAML. Their tokenizers run on the main thread,
|
||||
// so the base editor worker covers the rest (word suggestions, links).
|
||||
return new EditorWorker(); // base worker: sql, yaml, plaintext etc.
|
||||
},
|
||||
};
|
||||
|
||||
// The `monaco-editor` entry exposes JSON this way; the role editor reads `languages.json`.
|
||||
Object.assign(monaco.languages, { json: jsonContribution });
|
||||
|
||||
export { monaco };
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ComponentType } from 'react';
|
||||
import type { Meta, StoryObj } from '@storybook/react-vite';
|
||||
import * as monaco from 'monaco-editor';
|
||||
import { monaco } from 'lib/monaco/setup';
|
||||
import ROUTES from 'constants/routes';
|
||||
import { renderAtRoute } from '@/storybook/renderAtRoute';
|
||||
import { expect, screen, userEvent, waitFor, within } from 'storybook/test';
|
||||
|
||||
@@ -8,6 +8,7 @@ import vitePluginChecker from 'vite-plugin-checker';
|
||||
import viteCompression from 'vite-plugin-compression';
|
||||
import { ViteImageOptimizer } from 'vite-plugin-image-optimizer';
|
||||
import tsconfigPaths from 'vite-tsconfig-paths';
|
||||
import { MONACO_STRIPPED_FEATURES } from './plugins/rules/shared/monaco-stripped-features.mjs';
|
||||
import type {
|
||||
Appcues,
|
||||
Posthog,
|
||||
@@ -76,6 +77,10 @@ function rawMarkdownPlugin(): Plugin {
|
||||
};
|
||||
}
|
||||
|
||||
// Monaco contributions that need a provider SigNoz never registers. monaco-editor
|
||||
// declares no `sideEffects`, so its bare imports keep all of them.
|
||||
const MONACO_UNUSED_CONTRIBS = Object.keys(MONACO_STRIPPED_FEATURES);
|
||||
|
||||
export default defineConfig(({ mode }): UserConfig => {
|
||||
const env = loadEnv(mode, process.cwd(), '') as ImportMetaEnv;
|
||||
// Base path for serving the app (e.g., '/signoz/'). Defaults to '/'.
|
||||
@@ -188,6 +193,18 @@ export default defineConfig(({ mode }): UserConfig => {
|
||||
sourcemap: true,
|
||||
outDir: 'build',
|
||||
cssMinify: 'esbuild',
|
||||
rolldownOptions: {
|
||||
treeshake: {
|
||||
moduleSideEffects: [
|
||||
{
|
||||
test: new RegExp(
|
||||
`monaco-editor/esm/vs/editor/contrib/(${MONACO_UNUSED_CONTRIBS.join('|')})/`,
|
||||
),
|
||||
sideEffects: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
server: {
|
||||
open: true,
|
||||
|
||||
Reference in New Issue
Block a user