diff --git a/monaco-json/README.md b/monaco-json/README.md index 60fa1e16..ecdead4c 100644 --- a/monaco-json/README.md +++ b/monaco-json/README.md @@ -10,30 +10,14 @@ JSON language plugin for the Monaco Editor. It provides the following features w - Syntax highlighting - Color decorators for all properties matching a schema containing `format: "color-hex"'` (non-standard schema extension) -Schemas can be provided by configuration. See [here](https://github.com/Microsoft/monaco-json/blob/master/monaco.d.ts) +Schemas can be provided by configuration. See [`monaco.d.ts`](./monaco.d.ts) for the API that the JSON plugin offers to configure the JSON language support. -Internally the JSON plugin uses the [vscode-json-languageservice](https://github.com/Microsoft/vscode-json-languageservice) +Internally the JSON plugin uses the [`vscode-json-languageservice`](https://github.com/microsoft/vscode-json-languageservice) node module, providing the implementation of the features listed above. The same module is also used -in [Visual Studio Code](https://github.com/Microsoft/vscode) to power the JSON editing experience. - -## Issues - -Please file issues concerning `monaco-json` in the [`monaco-editor` repository](https://github.com/Microsoft/monaco-editor/issues). - -## Installing - -This npm module is bundled and distributed in the [monaco-editor](https://www.npmjs.com/package/monaco-editor) npm module. +in [Visual Studio Code](https://github.com/microsoft/vscode) to power the JSON editing experience. ## Development -- `git clone https://github.com/Microsoft/monaco-json` -- `npm install .` -- compile with `npm run compile` - watch with `npm run watch` -- `npm run prepublishOnly` -- open `$/monaco-json/test/index.html` in your favorite browser. - -## License - -[MIT](https://github.com/Microsoft/monaco-json/blob/master/LICENSE.md) +- compile with `npm run prepublishOnly` diff --git a/monaco-json/package.json b/monaco-json/package.json index b0a007e9..82a29a5c 100644 --- a/monaco-json/package.json +++ b/monaco-json/package.json @@ -1,7 +1,6 @@ { "scripts": { - "compile": "../node_modules/.bin/mrmdir ./out && ../node_modules/.bin/tsc -p ./src/tsconfig.json && ../node_modules/.bin/tsc -p ./src/tsconfig.esm.json && node ./scripts/dts && ../node_modules/.bin/prettier --write ./monaco.d.ts", "watch": "../node_modules/.bin/tsc -p ./src --watch", - "prepublishOnly": "../node_modules/.bin/mrmdir ./release && npm run compile && node ./scripts/release.js && node ./scripts/bundle && ../node_modules/.bin/mcopy ./out/esm/monaco.contribution.d.ts ./release/esm/monaco.contribution.d.ts && ../node_modules/.bin/mcopy ./out/esm/fillers/monaco-editor-core.d.ts ./release/esm/fillers/monaco-editor-core.d.ts" + "prepublishOnly": "node ./scripts/build" } } diff --git a/monaco-json/scripts/build.js b/monaco-json/scripts/build.js new file mode 100644 index 00000000..eb6a3f1f --- /dev/null +++ b/monaco-json/scripts/build.js @@ -0,0 +1,43 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +const esbuild = require('esbuild'); +const alias = require('esbuild-plugin-alias'); +const path = require('path'); +const cp = require('child_process'); +const { copyFile, removeDir, tsc, dts } = require('../../build/utils'); + +removeDir(`monaco-json/release`); +removeDir(`monaco-json/out`); + +tsc(`monaco-json/src/tsconfig.json`); + +dts(`monaco-json/out/amd/monaco.contribution.d.ts`, `monaco-json/monaco.d.ts`, 'monaco.languages.json'); + +esbuild.build({ + entryPoints: ['src/jsonMode.ts', 'src/json.worker.ts', 'src/monaco.contribution.ts'], + bundle: true, + target: 'esnext', + format: 'esm', + external: ['monaco-editor-core', '*/jsonMode'], + outdir: 'release/esm/', + plugins: [ + alias({ + 'vscode-nls': path.join(__dirname, '../src/fillers/vscode-nls.ts'), + }), + ], +}).then((result) => { + if (result.errors.length > 0) { + console.error(result.errors); + } + if (result.warnings.length > 0) { + console.error(result.warnings); + } +}); + +copyFile('monaco-json/out/amd/monaco.contribution.d.ts', 'monaco-json/release/esm/monaco.contribution.d.ts'); +copyFile('monaco-json/out/amd/fillers/monaco-editor-core.d.ts', 'monaco-json/release/esm/fillers/monaco-editor-core.d.ts'); + +cp.spawnSync(process.execPath, [path.join(__dirname, './bundle.js')], { stdio: 'inherit', stderr: 'inherit' }); diff --git a/monaco-json/scripts/bundle.js b/monaco-json/scripts/bundle.js index cb54926f..cb2b31cc 100644 --- a/monaco-json/scripts/bundle.js +++ b/monaco-json/scripts/bundle.js @@ -1,24 +1,17 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + const requirejs = require('requirejs'); const path = require('path'); const fs = require('fs'); -const Terser = require('terser'); -const helpers = require('monaco-plugin-helpers'); +const terser = require('terser'); +const { getBundledFileHeader } = require('../../build/utils'); const REPO_ROOT = path.resolve(__dirname, '..', '..'); -const sha1 = helpers.getGitVersion(REPO_ROOT); -const semver = require('../../package.json').version; -const headerVersion = semver + '(' + sha1 + ')'; - -const BUNDLED_FILE_HEADER = [ - '/*!-----------------------------------------------------------------------------', - ' * Copyright (c) Microsoft Corporation. All rights reserved.', - ' * monaco-json version: ' + headerVersion, - ' * Released under the MIT license', - ' * https://github.com/Microsoft/monaco-json/blob/master/LICENSE.md', - ' *-----------------------------------------------------------------------------*/', - '' -].join('\n'); +const BUNDLED_FILE_HEADER = getBundledFileHeader(); bundleOne('monaco.contribution'); bundleOne('jsonMode', ['vs/language/json/monaco.contribution']); @@ -74,8 +67,9 @@ function bundleOne(moduleId, exclude) { const devFilePath = path.join(REPO_ROOT, 'monaco-json/release/dev/' + moduleId + '.js'); const minFilePath = path.join(REPO_ROOT, 'monaco-json/release/min/' + moduleId + '.js'); const fileContents = fs.readFileSync(devFilePath).toString(); + console.log(); console.log(`Minifying ${devFilePath}...`); - const result = await Terser.minify(fileContents, { + const result = await terser.minify(fileContents, { output: { comments: 'some' } diff --git a/monaco-json/scripts/dts.js b/monaco-json/scripts/dts.js deleted file mode 100644 index 3e3af1cf..00000000 --- a/monaco-json/scripts/dts.js +++ /dev/null @@ -1,41 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -const path = require('path'); -const fs = require('fs'); - -const REPO_ROOT = path.join(__dirname, '../'); -const SRC_PATH = path.join(REPO_ROOT, 'out/amd/monaco.contribution.d.ts'); -const DST_PATH = path.join(REPO_ROOT, 'monaco.d.ts'); - -const lines = fs - .readFileSync(SRC_PATH) - .toString() - .split(/\r\n|\r|\n/); -let result = [ - `/*---------------------------------------------------------------------------------------------`, - ` * Copyright (c) Microsoft Corporation. All rights reserved.`, - ` * Licensed under the MIT License. See License.txt in the project root for license information.`, - ` *--------------------------------------------------------------------------------------------*/`, - ``, - `/// `, - ``, - `declare namespace monaco.languages.json {` -]; -for (let line of lines) { - if (/^import/.test(line)) { - continue; - } - line = line.replace(/ /g, '\t'); - line = line.replace(/declare /g, ''); - if (line.length > 0) { - line = `\t${line}`; - result.push(line); - } -} -result.push(`}`); -result.push(``); - -fs.writeFileSync(DST_PATH, result.join('\n')); diff --git a/monaco-json/scripts/release.js b/monaco-json/scripts/release.js deleted file mode 100644 index 746f7fc0..00000000 --- a/monaco-json/scripts/release.js +++ /dev/null @@ -1,27 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -const path = require('path'); -const helpers = require('monaco-plugin-helpers'); - -const REPO_ROOT = path.join(__dirname, '../../'); - -helpers.packageESM({ - repoRoot: REPO_ROOT, - esmSource: 'monaco-json/out/esm', - esmDestination: 'monaco-json/release/esm', - entryPoints: ['monaco.contribution.js', 'jsonMode.js', 'json.worker.js'], - resolveAlias: { - 'vscode-nls': path.join(REPO_ROOT, 'monaco-json/out/esm/fillers/vscode-nls.js') - }, - resolveSkip: ['monaco-editor-core'], - destinationFolderSimplification: { - node_modules: '_deps', - 'jsonc-parser/lib/esm': 'jsonc-parser', - 'vscode-languageserver-types/lib/esm': 'vscode-languageserver-types', - 'vscode-uri/lib/esm': 'vscode-uri', - 'vscode-json-languageservice/lib/esm': 'vscode-json-languageservice' - } -}); diff --git a/monaco-json/src/tokenization.ts b/monaco-json/src/tokenization.ts index aa9bd5a4..a4d0ad7e 100644 --- a/monaco-json/src/tokenization.ts +++ b/monaco-json/src/tokenization.ts @@ -69,13 +69,13 @@ class ParentsStack { class JSONState implements languages.IState { private _state: languages.IState; - public scanError: json.ScanError; + public scanError: ScanError; public lastWasColon: boolean; public parents: ParentsStack | null; constructor( state: languages.IState, - scanError: json.ScanError, + scanError: ScanError, lastWasColon: boolean, parents: ParentsStack | null ) { @@ -112,6 +112,36 @@ class JSONState implements languages.IState { } } +const enum ScanError { + None = 0, + UnexpectedEndOfComment = 1, + UnexpectedEndOfString = 2, + UnexpectedEndOfNumber = 3, + InvalidUnicode = 4, + InvalidEscapeCharacter = 5, + InvalidCharacter = 6 +} + +const enum SyntaxKind { + OpenBraceToken = 1, + CloseBraceToken = 2, + OpenBracketToken = 3, + CloseBracketToken = 4, + CommaToken = 5, + ColonToken = 6, + NullKeyword = 7, + TrueKeyword = 8, + FalseKeyword = 9, + StringLiteral = 10, + NumericLiteral = 11, + LineCommentTrivia = 12, + BlockCommentTrivia = 13, + LineBreakTrivia = 14, + Trivia = 15, + Unknown = 16, + EOF = 17 +} + function tokenize( comments: boolean, line: string, @@ -124,11 +154,11 @@ function tokenize( let adjustOffset = false; switch (state.scanError) { - case json.ScanError.UnexpectedEndOfString: + case ScanError.UnexpectedEndOfString: line = '"' + line; numberOfInsertedCharacters = 1; break; - case json.ScanError.UnexpectedEndOfComment: + case ScanError.UnexpectedEndOfComment: line = '/*' + line; numberOfInsertedCharacters = 2; break; @@ -147,8 +177,8 @@ function tokenize( let offset = offsetDelta + scanner.getPosition(); let type = ''; - const kind = scanner.scan(); - if (kind === json.SyntaxKind.EOF) { + const kind = scanner.scan(); + if (kind === SyntaxKind.EOF) { break; } @@ -168,50 +198,50 @@ function tokenize( // brackets and type switch (kind) { - case json.SyntaxKind.OpenBraceToken: + case SyntaxKind.OpenBraceToken: parents = ParentsStack.push(parents, JSONParent.Object); type = TOKEN_DELIM_OBJECT; lastWasColon = false; break; - case json.SyntaxKind.CloseBraceToken: + case SyntaxKind.CloseBraceToken: parents = ParentsStack.pop(parents); type = TOKEN_DELIM_OBJECT; lastWasColon = false; break; - case json.SyntaxKind.OpenBracketToken: + case SyntaxKind.OpenBracketToken: parents = ParentsStack.push(parents, JSONParent.Array); type = TOKEN_DELIM_ARRAY; lastWasColon = false; break; - case json.SyntaxKind.CloseBracketToken: + case SyntaxKind.CloseBracketToken: parents = ParentsStack.pop(parents); type = TOKEN_DELIM_ARRAY; lastWasColon = false; break; - case json.SyntaxKind.ColonToken: + case SyntaxKind.ColonToken: type = TOKEN_DELIM_COLON; lastWasColon = true; break; - case json.SyntaxKind.CommaToken: + case SyntaxKind.CommaToken: type = TOKEN_DELIM_COMMA; lastWasColon = false; break; - case json.SyntaxKind.TrueKeyword: - case json.SyntaxKind.FalseKeyword: + case SyntaxKind.TrueKeyword: + case SyntaxKind.FalseKeyword: type = TOKEN_VALUE_BOOLEAN; lastWasColon = false; break; - case json.SyntaxKind.NullKeyword: + case SyntaxKind.NullKeyword: type = TOKEN_VALUE_NULL; lastWasColon = false; break; - case json.SyntaxKind.StringLiteral: + case SyntaxKind.StringLiteral: const currentParent = parents ? parents.type : JSONParent.Object; const inArray = currentParent === JSONParent.Array; type = lastWasColon || inArray ? TOKEN_VALUE_STRING : TOKEN_PROPERTY_NAME; lastWasColon = false; break; - case json.SyntaxKind.NumericLiteral: + case SyntaxKind.NumericLiteral: type = TOKEN_VALUE_NUMBER; lastWasColon = false; break; @@ -220,10 +250,10 @@ function tokenize( // comments, iff enabled if (comments) { switch (kind) { - case json.SyntaxKind.LineCommentTrivia: + case SyntaxKind.LineCommentTrivia: type = TOKEN_COMMENT_LINE; break; - case json.SyntaxKind.BlockCommentTrivia: + case SyntaxKind.BlockCommentTrivia: type = TOKEN_COMMENT_BLOCK; break; } @@ -231,7 +261,7 @@ function tokenize( ret.endState = new JSONState( state.getStateData(), - scanner.getTokenError(), + scanner.getTokenError(), lastWasColon, parents ); diff --git a/monaco-json/src/tsconfig.esm.json b/monaco-json/src/tsconfig.esm.json deleted file mode 100644 index b97a230f..00000000 --- a/monaco-json/src/tsconfig.esm.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "compilerOptions": { - "declaration": true, - "module": "esnext", - "moduleResolution": "node", - "outDir": "../out/esm", - "target": "es5", - "lib": ["dom", "es5", "es2015.collection", "es2015.promise", "es2015.iterable"] - } -}