Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build as ESM and UMD libs #305

Merged
merged 8 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ out/server
.DS_Store
yarn-error.log
coverage
lib/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -61,15 +61,18 @@
"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",
"coverage": "nyc mocha --require ts-node/register --require source-map-support/register --recursive --ui tdd ./test/*.test.ts",
"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": [
Expand All @@ -80,6 +83,7 @@
"**/*.d.ts",
"test/",
"out",
"lib",
"coverage/",
".eslintrc.js"
],
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/schemaRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
26 changes: 21 additions & 5 deletions src/languageservice/services/yamlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"downlevelIteration": true,
"lib": ["dom"],
"module": "esnext",
"outDir": "./lib/esm",
},
"exclude": [
"node_modules",
"out",
"lib",
"test"
]
}
14 changes: 14 additions & 0 deletions tsconfig.umd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": ["dom"],
"module": "umd",
"outDir": "./lib/umd",
},
"exclude": [
"node_modules",
"out",
"lib",
"test"
]
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down