Skip to content

Commit

Permalink
global module resolution off by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Dec 19, 2019
1 parent 6c008c5 commit bcd5c47
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi

<!-- Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. -->

## [3.17.0]

- Due to performance issues, global module resolution is now off by default. Enable by setting `prettier.resolveGlobalModules` to `true`.

## [3.16.0]

- Show error when prettier configuration file is invalid.
Expand Down
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -76,7 +76,7 @@ To ensure that this extension is used over other extensions you may have install

### Prettier Resolution

This extension will use prettier from your project's local dependencies (recommended). If no local module is found, the extension will attempt to resolve prettier globally. Should prettier not be installed locally with your project's dependencies or globally on the machine, the version of prettier that is bundled with the extension will be used.
This extension will use prettier from your project's local dependencies (recommended). When the `prettier.resolveGlobalModules` is set to `true` the extension can also attempt to resolve global modules. Should prettier not be installed locally with your project's dependencies or globally on the machine, the version of prettier that is bundled with the extension will be used.

To install prettier in your project run:

Expand Down Expand Up @@ -243,7 +243,13 @@ Supply a custom path to the prettier module.

#### prettier.packageManager

Controls the package manager to be used to resolve the ESLint library. This has only an influence if the ESLint library is resolved globally. Valid values are `"npm"` or `"yarn"` or `"pnpm"`.
Controls the package manager to be used to resolve modules. This has only an influence if the `prettier.resolveGlobalModules` setting is `true` and modules are resolved globally. Valid values are `"npm"` or `"yarn"` or `"pnpm"`.

#### prettier.resolveGlobalModules (default: false)

When enabled, this extension will attempt to use global npm or yarn modules if local modules cannot be resolved.

> NOTE: This setting can have a negative performance impact, particularly on Windows when you have attached network drives. Only enable this if you must use global modules. It is recommended that you always use local modules when possible.
#### prettier.disableLanguages

Expand Down
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "prettier-vscode",
"displayName": "Prettier - Code formatter",
"description": "Code formatter using prettier",
"version": "3.16.1",
"version": "3.17.0",
"publisher": "esbenp",
"author": "Prettier <@prettiercode>",
"galleryBanner": {
Expand Down Expand Up @@ -145,6 +145,12 @@
"description": "%ext.config.requireConfig%",
"scope": "resource"
},
"prettier.resolveGlobalModules": {
"type": "boolean",
"default": false,
"description": "%ext.config.resolveGlobalModules%",
"scope": "resource"
},
"prettier.packageManager": {
"scope": "resource",
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Expand Up @@ -20,6 +20,7 @@
"ext.config.quoteProps": "Change when properties in objects are quoted",
"ext.config.requireConfig": "Require a prettier configuration file to format. See [documentation for valid configuration files](https://prettier.io/docs/en/configuration.html).\n Note, untitled files will still be formatted using the VS Code prettier settings even when this setting is set.",
"ext.config.requirePragma": "Prettier can restrict itself to only format files that contain a special comment, called a pragma, at the top of the file. This is very useful when gradually transitioning large, unformatted codebases to prettier.",
"ext.config.resolveGlobalModules": "When enabled, this extension will attempt to use global npm or yarn modules if local modules cannot be resolved.\n NOTE: This setting can have a negative performance impact, particularly on Windows when you have attached network drives. Only enable this if you must use global modules.",
"ext.config.semi": "Whether to add a semicolon at the end of every line",
"ext.config.singleQuote": "If true, will use single instead of double quotes",
"ext.config.stylelintIntegration": "Use 'prettier-stylelint' instead of 'prettier'. Other settings will only be fallbacks in case they could not be inferred from stylelint rules.",
Expand Down
14 changes: 6 additions & 8 deletions src/ModuleResolver.ts
Expand Up @@ -86,7 +86,7 @@ export class ModuleResolver implements Disposable {
return prettier;
}

const { prettierPath, packageManager } = getConfig();
const { prettierPath, packageManager, resolveGlobalModules } = getConfig();

// tslint:disable-next-line: prefer-const
let { moduleInstance, modulePath } = this.requireLocalPkg<PrettierModule>(
Expand All @@ -95,7 +95,7 @@ export class ModuleResolver implements Disposable {
prettierPath
);

if (!moduleInstance) {
if (resolveGlobalModules && !moduleInstance) {
const globalModuleResult = this.requireGlobalPkg<PrettierModule>(
packageManager,
"prettier"
Expand Down Expand Up @@ -138,13 +138,11 @@ export class ModuleResolver implements Disposable {
return moduleInstance || prettier;
}

public getModuleInstance(
fsPath: string,
packageManager: PackageManagers,
pkgName: string
): any {
public getModuleInstance(fsPath: string, pkgName: string): any {
let { moduleInstance } = this.requireLocalPkg<any>(fsPath, pkgName);
if (!moduleInstance) {

const { packageManager, resolveGlobalModules } = getConfig();
if (resolveGlobalModules && !moduleInstance) {
const globalModuleResult = this.requireGlobalPkg<PrettierModule>(
packageManager,
pkgName
Expand Down
3 changes: 0 additions & 3 deletions src/PrettierEditService.ts
Expand Up @@ -324,7 +324,6 @@ export default class PrettierEditService implements Disposable {
if (parser === "typescript") {
const prettierTslintModule = this.moduleResolver.getModuleInstance(
fileName,
vscodeConfig.packageManager,
"prettier-tslint"
);

Expand All @@ -345,7 +344,6 @@ export default class PrettierEditService implements Disposable {
if (this.languageResolver.doesLanguageSupportESLint(languageId)) {
const prettierEslintModule = this.moduleResolver.getModuleInstance(
fileName,
vscodeConfig.packageManager,
"prettier-eslint"
);
if (prettierEslintModule) {
Expand All @@ -365,7 +363,6 @@ export default class PrettierEditService implements Disposable {
if (this.languageResolver.doesParserSupportStylelint(parser)) {
const prettierStylelintModule = this.moduleResolver.getModuleInstance(
fileName,
vscodeConfig.packageManager,
"prettier-stylelint"
);
if (prettierStylelintModule) {
Expand Down
6 changes: 5 additions & 1 deletion src/types.d.ts
Expand Up @@ -50,9 +50,13 @@ interface IExtensionConfig {
*/
disableLanguages: string[];
/**
* If true, take into account the .editorconfig file when resolving configuration
* If true, take into account the .editorconfig file when resolving configuration.
*/
useEditorConfig: boolean;
/**
* If true, this extension will attempt to use global npm or yarn modules.
*/
resolveGlobalModules: boolean;
}
/**
* Configuration for prettier-vscode
Expand Down

0 comments on commit bcd5c47

Please sign in to comment.