From 56f049f6483366a66889e5d9a43bfcca502f9744 Mon Sep 17 00:00:00 2001 From: apisim <63506558+apisim@users.noreply.github.com> Date: Thu, 10 Sep 2020 01:22:20 -0500 Subject: [PATCH] Build as ESM and UMD libs (#305) * Build as ESM and UMD libs * backout dependencies updates in yarn.lock * refactored tsconfig.esm and tsconfig.umd to extend tsconfig * Add info in README about the ESM and UMD modules now produced by builds * clean up the lib directory before a build * update CHANGELOG.md with info about the ESM and UMD libs * update CHANGELOG.md with info about the ESM and UMD libs Co-authored-by: apimastery --- .gitignore | 1 + CHANGELOG.md | 4 +++ README.md | 4 +++ package.json | 10 ++++--- .../services/schemaRequestHandler.ts | 2 +- src/languageservice/services/yamlFormatter.ts | 26 +++++++++++++++---- tsconfig.esm.json | 15 +++++++++++ tsconfig.umd.json | 14 ++++++++++ yarn.lock | 8 +++--- 9 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 tsconfig.esm.json create mode 100644 tsconfig.umd.json diff --git a/.gitignore b/.gitignore index 5e1b99a6..7ae14b0c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ out/server .DS_Store yarn-error.log coverage +lib/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 1852619d..eb62d7f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +#### 0.11.0 + +- Make yaml-language-server available as ESM and UMD modules in the `/lib` directory [#305](https://github.com/redhat-developer/yaml-language-server/pull/305) + #### 0.10.1 - Fix for cannot read property 'lineComments' of undefined Code: -32603 [redhat-developer/vscode-yaml#312](https://github.com/redhat-developer/vscode-yaml/issues/358) diff --git a/README.md b/README.md index 38db7333..1e229412 100755 --- a/README.md +++ b/README.md @@ -199,3 +199,7 @@ This repository only contains the server implementation. Here are some known cli ### Connecting to the language server via stdio We have included the option to connect to the language server via [stdio](https://github.com/redhat-developer/yaml-language-server/blob/681985b5a059c2cb55c8171235b07e1651b6c546/src/server.ts#L46-L51) to help with intergrating the language server into different clients. + +### ESM and UMD Modules + +Building the YAML Language Server produces [CommonJS](http://www.commonjs.org/) modules in the `/out/server/src` directory. In addition, a build also produces [UMD](https://github.com/umdjs/umd) (Universal Module Definition) modules and [ES Modules](https://tc39.es/ecma262/#sec-modules) (ESM) in the `/lib` directory. That gives you choices in using the YAML Language Server with different module loaders on the server side and in the browser with bundlers like webpack. diff --git a/package.json b/package.json index 401168a8..943c0bed 100755 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "devDependencies": { "@types/mocha": "2.2.48", "@types/node": "^12.11.7", - "@types/prettier": "^1.18.0", + "@types/prettier": "2.0.2", "@typescript-eslint/eslint-plugin": "^3.2.0", "@typescript-eslint/parser": "^3.2.0", "coveralls": "^3.0.5", @@ -61,7 +61,7 @@ "typescript": "^3.8.3" }, "scripts": { - "clean": "rimraf out/server", + "clean": "rimraf out/server && rimraf lib", "compile": "tsc -p .", "watch": "tsc --watch -p .", "test": "mocha --require ts-node/register --ui tdd ./test/*.test.ts", @@ -69,7 +69,10 @@ "coveralls": "nyc --reporter=lcov --reporter=text mocha --require ts-node/register --require source-map-support/register --recursive --ui tdd ./test/*.test.ts", "lint": "eslint -c .eslintrc.js --ext .ts src test", "prettier-fix": "yarn prettier --write .", - "build": "yarn clean && yarn lint && yarn compile" + "build": "yarn clean && yarn lint && yarn compile && yarn build:libs", + "build:libs": "yarn compile:umd && yarn compile:esm", + "compile:umd": "tsc -p ./tsconfig.umd.json", + "compile:esm": "tsc -p ./tsconfig.esm.json" }, "nyc": { "extension": [ @@ -80,6 +83,7 @@ "**/*.d.ts", "test/", "out", + "lib", "coverage/", ".eslintrc.js" ], diff --git a/src/languageservice/services/schemaRequestHandler.ts b/src/languageservice/services/schemaRequestHandler.ts index 62a58f3e..1dba49fd 100644 --- a/src/languageservice/services/schemaRequestHandler.ts +++ b/src/languageservice/services/schemaRequestHandler.ts @@ -1,7 +1,7 @@ import { URI } from 'vscode-uri'; import { IConnection } from 'vscode-languageserver'; import { xhr, XHRResponse, getErrorStatusDescription } from 'request-light'; -import fs = require('fs'); +import * as fs from 'fs'; import { VSCodeContentRequest, CustomSchemaContentRequest } from '../../requestTypes'; import { isRelativePath, relativeToAbsolutePath } from '../utils/paths'; diff --git a/src/languageservice/services/yamlFormatter.ts b/src/languageservice/services/yamlFormatter.ts index 97ffaf9e..5a6ca9f1 100644 --- a/src/languageservice/services/yamlFormatter.ts +++ b/src/languageservice/services/yamlFormatter.ts @@ -5,8 +5,11 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import { TextDocument, Range, Position, TextEdit } from 'vscode-languageserver-types'; +import { TextDocument, Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types'; import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService'; +import * as prettier from 'prettier'; +import { Options } from 'prettier'; +import * as parser from 'prettier/parser-yaml'; export class YAMLFormatter { private formatterEnabled = true; @@ -17,17 +20,30 @@ export class YAMLFormatter { } } - public format(document: TextDocument, options: CustomFormatterOptions): TextEdit[] { + public format(document: TextDocument, options: FormattingOptions & CustomFormatterOptions): TextEdit[] { if (!this.formatterEnabled) { return []; } try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const prettier = require('prettier'); const text = document.getText(); - const formatted = prettier.format(text, Object.assign(options, { parser: 'yaml' })); + const prettierOptions: Options = { + parser: 'yaml', + plugins: [parser], + + // --- FormattingOptions --- + tabWidth: options.tabSize, + + // --- CustomFormatterOptions --- + singleQuote: options.singleQuote, + bracketSpacing: options.bracketSpacing, + // 'preserve' is the default for Options.proseWrap. See also server.ts + proseWrap: 'always' === options.proseWrap ? 'always' : 'never' === options.proseWrap ? 'never' : 'preserve', + printWidth: options.printWidth, + }; + + const formatted = prettier.format(text, prettierOptions); return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)]; } catch (error) { diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 00000000..a352c286 --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "downlevelIteration": true, + "lib": ["dom"], + "module": "esnext", + "outDir": "./lib/esm", + }, + "exclude": [ + "node_modules", + "out", + "lib", + "test" + ] +} diff --git a/tsconfig.umd.json b/tsconfig.umd.json new file mode 100644 index 00000000..09cf5fcd --- /dev/null +++ b/tsconfig.umd.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "lib": ["dom"], + "module": "umd", + "outDir": "./lib/umd", + }, + "exclude": [ + "node_modules", + "out", + "lib", + "test" + ] +} diff --git a/yarn.lock b/yarn.lock index b4bd0f4a..5543cc0a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -119,10 +119,10 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.47.tgz#5007b8866a2f9150de82335ca7e24dd1d59bdfb5" integrity sha512-yzBInQFhdY8kaZmqoL2+3U5dSTMrKaYcb561VU+lDzAYvqt+2lojvBEy+hmpSNuXnPTx7m9+04CzWYOUqWME2A== -"@types/prettier@^1.18.0": - version "1.19.1" - resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-1.19.1.tgz#33509849f8e679e4add158959fdb086440e9553f" - integrity sha512-5qOlnZscTn4xxM5MeGXAMOsIOIKIbh9e85zJWfBRVPlRMEVawzoPhINYbRGkBZCI8LxvBe7tJCdWiarA99OZfQ== +"@types/prettier@2.0.2": + version "2.0.2" + resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.0.2.tgz#5bb52ee68d0f8efa9cc0099920e56be6cc4e37f3" + integrity sha512-IkVfat549ggtkZUthUzEX49562eGikhSYeVGX97SkMFn+sTZrgRewXjQ4tPKFPCykZHkX1Zfd9OoELGqKU2jJA== "@typescript-eslint/eslint-plugin@^3.2.0": version "3.2.0"