Skip to content

Commit 4aedf2b

Browse files
author
Spencer
authored
add monaco to kbn/ui-shared-deps and load required features for all uses (#58075)
* add monaco to kbn/ui-shared-deps and load required features for all uses * forgot to save a change * remove unused imports * include a cache buster to counteract issue #58077 * include monaco.ts in ts project
1 parent 3bd639d commit 4aedf2b

File tree

15 files changed

+128
-90
lines changed

15 files changed

+128
-90
lines changed

packages/kbn-optimizer/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* under the License.
1818
*/
1919

20+
// cache buster - https://github.com/elastic/kibana/issues/58077 - 1
2021
export { OptimizerConfig } from './optimizer';
2122
export * from './run_optimizer';
2223
export * from './log_optimizer_state';

packages/kbn-ui-shared-deps/entry.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export const ReactDom = require('react-dom');
4040
export const ReactIntl = require('react-intl');
4141
export const ReactRouter = require('react-router'); // eslint-disable-line
4242
export const ReactRouterDom = require('react-router-dom');
43+
export const Monaco = require('./monaco.ts');
44+
export const MonacoBare = require('monaco-editor/esm/vs/editor/editor.api');
4345

4446
// load timezone data into moment-timezone
4547
Moment.tz.load(require('moment-timezone/data/packed/latest.json'));

packages/kbn-ui-shared-deps/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ exports.externals = {
4141
'react-intl': '__kbnSharedDeps__.ReactIntl',
4242
'react-router': '__kbnSharedDeps__.ReactRouter',
4343
'react-router-dom': '__kbnSharedDeps__.ReactRouterDom',
44+
'@kbn/ui-shared-deps/monaco': '__kbnSharedDeps__.Monaco',
45+
// this is how plugins/consumers from npm load monaco
46+
'monaco-editor/esm/vs/editor/editor.api': '__kbnSharedDeps__.MonacoBare',
4447
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
21+
22+
import 'monaco-editor/esm/vs/base/common/worker/simpleWorker';
23+
import 'monaco-editor/esm/vs/base/worker/defaultWorkerFactory';
24+
25+
import 'monaco-editor/esm/vs/editor/browser/controller/coreCommands.js';
26+
import 'monaco-editor/esm/vs/editor/browser/widget/codeEditorWidget.js';
27+
28+
import 'monaco-editor/esm/vs/editor/contrib/suggest/suggestController.js'; // Needed for suggestions
29+
import 'monaco-editor/esm/vs/editor/contrib/hover/hover.js'; // Needed for hover
30+
import 'monaco-editor/esm/vs/editor/contrib/parameterHints/parameterHints.js'; // Needed for signature
31+
32+
export { monaco };

packages/kbn-ui-shared-deps/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@elastic/charts": "^17.0.2",
1313
"abortcontroller-polyfill": "^1.4.0",
1414
"@elastic/eui": "19.0.0",
15+
"@kbn/babel-preset": "1.0.0",
1516
"@kbn/dev-utils": "1.0.0",
1617
"@kbn/i18n": "1.0.0",
1718
"@yarnpkg/lockfile": "^1.1.0",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "../../tsconfig.json",
33
"include": [
4-
"index.d.ts"
4+
"index.d.ts",
5+
"monaco.ts"
56
]
67
}

packages/kbn-ui-shared-deps/webpack.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ exports.getWebpackConfig = ({ dev = false } = {}) => ({
5959
test: /\.css$/,
6060
use: [MiniCssExtractPlugin.loader, 'css-loader'],
6161
},
62+
{
63+
include: [require.resolve('./monaco.ts')],
64+
use: [
65+
{
66+
loader: 'babel-loader',
67+
options: {
68+
presets: [require.resolve('@kbn/babel-preset/webpack_preset')],
69+
},
70+
},
71+
],
72+
},
6273
],
6374
},
6475

src/legacy/core_plugins/vis_type_timelion/public/components/timelion_expression_input.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import React, { useEffect, useCallback, useRef, useMemo } from 'react';
2121
import { EuiFormLabel } from '@elastic/eui';
2222
import { FormattedMessage } from '@kbn/i18n/react';
23-
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
23+
import { monaco } from '@kbn/ui-shared-deps/monaco';
2424

2525
import { CodeEditor, useKibana } from '../../../../../plugins/kibana_react/public';
2626
import { suggest, getSuggestion } from './timelion_expression_input_helpers';
@@ -31,7 +31,7 @@ import {
3131
} from '../../../../../plugins/timelion/common/types';
3232

3333
const LANGUAGE_ID = 'timelion_expression';
34-
monacoEditor.languages.register({ id: LANGUAGE_ID });
34+
monaco.languages.register({ id: LANGUAGE_ID });
3535

3636
interface TimelionExpressionInputProps {
3737
value: string;
@@ -44,10 +44,10 @@ function TimelionExpressionInput({ value, setValue }: TimelionExpressionInputPro
4444
const argValueSuggestions = useMemo(getArgValueSuggestions, []);
4545

4646
const provideCompletionItems = useCallback(
47-
async (model: monacoEditor.editor.ITextModel, position: monacoEditor.Position) => {
47+
async (model: monaco.editor.ITextModel, position: monaco.Position) => {
4848
const text = model.getValue();
4949
const wordUntil = model.getWordUntilPosition(position);
50-
const wordRange = new monacoEditor.Range(
50+
const wordRange = new monaco.Range(
5151
position.lineNumber,
5252
wordUntil.startColumn,
5353
position.lineNumber,
@@ -75,7 +75,7 @@ function TimelionExpressionInput({ value, setValue }: TimelionExpressionInputPro
7575
);
7676

7777
const provideHover = useCallback(
78-
async (model: monacoEditor.editor.ITextModel, position: monacoEditor.Position) => {
78+
async (model: monaco.editor.ITextModel, position: monaco.Position) => {
7979
const suggestions = await suggest(
8080
model.getValue(),
8181
functionList.current,

src/legacy/core_plugins/vis_type_timelion/public/components/timelion_expression_input_helpers.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import { get, startsWith } from 'lodash';
2121
import { i18n } from '@kbn/i18n';
22-
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
22+
import { monaco } from '@kbn/ui-shared-deps/monaco';
2323

2424
import { Parser } from 'pegjs';
2525

@@ -215,22 +215,21 @@ export async function suggest(
215215
export function getSuggestion(
216216
suggestion: ITimelionFunction | TimelionFunctionArgs,
217217
type: SUGGESTION_TYPE,
218-
range: monacoEditor.Range
219-
): monacoEditor.languages.CompletionItem {
220-
let kind: monacoEditor.languages.CompletionItemKind =
221-
monacoEditor.languages.CompletionItemKind.Method;
218+
range: monaco.Range
219+
): monaco.languages.CompletionItem {
220+
let kind: monaco.languages.CompletionItemKind = monaco.languages.CompletionItemKind.Method;
222221
let insertText: string = suggestion.name;
223-
let insertTextRules: monacoEditor.languages.CompletionItem['insertTextRules'];
222+
let insertTextRules: monaco.languages.CompletionItem['insertTextRules'];
224223
let detail: string = '';
225-
let command: monacoEditor.languages.CompletionItem['command'];
224+
let command: monaco.languages.CompletionItem['command'];
226225

227226
switch (type) {
228227
case SUGGESTION_TYPE.ARGUMENTS:
229228
command = {
230229
title: 'Trigger Suggestion Dialog',
231230
id: 'editor.action.triggerSuggest',
232231
};
233-
kind = monacoEditor.languages.CompletionItemKind.Property;
232+
kind = monaco.languages.CompletionItemKind.Property;
234233
insertText = `${insertText}=`;
235234
detail = `${i18n.translate(
236235
'timelion.expressionSuggestions.argument.description.acceptsText',
@@ -245,9 +244,9 @@ export function getSuggestion(
245244
title: 'Trigger Suggestion Dialog',
246245
id: 'editor.action.triggerSuggest',
247246
};
248-
kind = monacoEditor.languages.CompletionItemKind.Function;
247+
kind = monaco.languages.CompletionItemKind.Function;
249248
insertText = `${insertText}($0)`;
250-
insertTextRules = monacoEditor.languages.CompletionItemInsertTextRule.InsertAsSnippet;
249+
insertTextRules = monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet;
251250
detail = `(${
252251
(suggestion as ITimelionFunction).chainable
253252
? i18n.translate('timelion.expressionSuggestions.func.description.chainableHelpText', {
@@ -270,7 +269,7 @@ export function getSuggestion(
270269
title: 'Trigger Suggestion Dialog',
271270
id: 'editor.action.triggerSuggest',
272271
};
273-
kind = monacoEditor.languages.CompletionItemKind.Property;
272+
kind = monaco.languages.CompletionItemKind.Property;
274273
detail = suggestion.help || '';
275274

276275
break;

src/plugins/kibana_react/public/code_editor/code_editor.test.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
import React from 'react';
2121
import { CodeEditor } from './code_editor';
22-
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
22+
import { monaco } from '@kbn/ui-shared-deps/monaco';
2323
import { shallow } from 'enzyme';
2424

2525
import 'monaco-editor/esm/vs/basic-languages/html/html.contribution.js';
2626

2727
// A sample language definition with a few example tokens
28-
const simpleLogLang: monacoEditor.languages.IMonarchLanguage = {
28+
const simpleLogLang: monaco.languages.IMonarchLanguage = {
2929
tokenizer: {
3030
root: [
3131
[/\[error.*/, 'constant'],
@@ -36,8 +36,8 @@ const simpleLogLang: monacoEditor.languages.IMonarchLanguage = {
3636
},
3737
};
3838

39-
monacoEditor.languages.register({ id: 'loglang' });
40-
monacoEditor.languages.setMonarchTokensProvider('loglang', simpleLogLang);
39+
monaco.languages.register({ id: 'loglang' });
40+
monaco.languages.setMonarchTokensProvider('loglang', simpleLogLang);
4141

4242
const logs = `
4343
[Sun Mar 7 20:54:27 2004] [notice] [client xx.xx.xx.xx] This is a notice!
@@ -55,35 +55,34 @@ test('is rendered', () => {
5555

5656
test('editor mount setup', () => {
5757
const suggestionProvider = {
58-
provideCompletionItems: (
59-
model: monacoEditor.editor.ITextModel,
60-
position: monacoEditor.Position
61-
) => ({ suggestions: [] }),
58+
provideCompletionItems: (model: monaco.editor.ITextModel, position: monaco.Position) => ({
59+
suggestions: [],
60+
}),
6261
};
6362
const signatureProvider = {
6463
provideSignatureHelp: () => ({ signatures: [], activeParameter: 0, activeSignature: 0 }),
6564
};
6665
const hoverProvider = {
67-
provideHover: (model: monacoEditor.editor.ITextModel, position: monacoEditor.Position) => ({
66+
provideHover: (model: monaco.editor.ITextModel, position: monaco.Position) => ({
6867
contents: [],
6968
}),
7069
};
7170

7271
const editorWillMount = jest.fn();
7372

74-
monacoEditor.languages.onLanguage = jest.fn((languageId, func) => {
73+
monaco.languages.onLanguage = jest.fn((languageId, func) => {
7574
expect(languageId).toBe('loglang');
7675

7776
// Call the function immediately so we can see our providers
7877
// get setup without a monaco editor setting up completely
7978
func();
8079
}) as any;
8180

82-
monacoEditor.languages.registerCompletionItemProvider = jest.fn();
83-
monacoEditor.languages.registerSignatureHelpProvider = jest.fn();
84-
monacoEditor.languages.registerHoverProvider = jest.fn();
81+
monaco.languages.registerCompletionItemProvider = jest.fn();
82+
monaco.languages.registerSignatureHelpProvider = jest.fn();
83+
monaco.languages.registerHoverProvider = jest.fn();
8584

86-
monacoEditor.editor.defineTheme = jest.fn();
85+
monaco.editor.defineTheme = jest.fn();
8786

8887
const wrapper = shallow(
8988
<CodeEditor
@@ -98,21 +97,17 @@ test('editor mount setup', () => {
9897
);
9998

10099
const instance = wrapper.instance() as CodeEditor;
101-
instance._editorWillMount(monacoEditor);
100+
instance._editorWillMount(monaco);
102101

103102
// Verify our mount callback will be called
104103
expect(editorWillMount.mock.calls.length).toBe(1);
105104

106105
// Verify our theme will be setup
107-
expect((monacoEditor.editor.defineTheme as jest.Mock).mock.calls.length).toBe(1);
106+
expect((monaco.editor.defineTheme as jest.Mock).mock.calls.length).toBe(1);
108107

109108
// Verify our language features have been registered
110-
expect((monacoEditor.languages.onLanguage as jest.Mock).mock.calls.length).toBe(1);
111-
expect(
112-
(monacoEditor.languages.registerCompletionItemProvider as jest.Mock).mock.calls.length
113-
).toBe(1);
114-
expect(
115-
(monacoEditor.languages.registerSignatureHelpProvider as jest.Mock).mock.calls.length
116-
).toBe(1);
117-
expect((monacoEditor.languages.registerHoverProvider as jest.Mock).mock.calls.length).toBe(1);
109+
expect((monaco.languages.onLanguage as jest.Mock).mock.calls.length).toBe(1);
110+
expect((monaco.languages.registerCompletionItemProvider as jest.Mock).mock.calls.length).toBe(1);
111+
expect((monaco.languages.registerSignatureHelpProvider as jest.Mock).mock.calls.length).toBe(1);
112+
expect((monaco.languages.registerHoverProvider as jest.Mock).mock.calls.length).toBe(1);
118113
});

0 commit comments

Comments
 (0)