2
2
3
3
import { execSync } from 'child_process' ;
4
4
import deepmerge from 'deepmerge' ;
5
- import type { Config } from 'prettier' ;
5
+ import type { Config , Plugin } from 'prettier' ;
6
6
import type { RubyConfig } from '@prettier/plugin-ruby' ;
7
- import * as plugin_ruby from '@prettier/plugin-ruby' ;
7
+ import plugin_ruby from '@prettier/plugin-ruby' ;
8
8
import * as plugin_packagejson from 'prettier-plugin-packagejson' ;
9
9
10
+ // eslint-disable-next-line n/no-process-env -- this is the only config we need.
11
+ const in_vscode_ext = process . env . VSCODE_PID != null ;
12
+
10
13
const is_prettier_gem_installed : boolean = ( ( ) => {
11
14
try {
12
15
return (
@@ -20,6 +23,34 @@ const is_prettier_gem_installed: boolean = (() => {
20
23
}
21
24
} ) ( ) ;
22
25
26
+ function resolve_plugin (
27
+ plugin : 'prettier-plugin-packagejson' | '@prettier/plugin-ruby' ,
28
+ ) : string | Plugin {
29
+ if ( ! in_vscode_ext ) {
30
+ switch ( plugin ) {
31
+ case 'prettier-plugin-packagejson' :
32
+ return plugin_packagejson ;
33
+ case '@prettier/plugin-ruby' :
34
+ return plugin_ruby ;
35
+ }
36
+ } else {
37
+ /**
38
+ * vscode's prettier extension fails to load esm plugins when they include
39
+ * an esm import (https://github.com/prettier/prettier-vscode/issues/3066)
40
+ * so we resolve the package's path here instead.
41
+ */
42
+ return (
43
+ import . meta
44
+ . resolve ( plugin )
45
+ /**
46
+ * vscode's prettier ext can't understand URIs, so we must convert
47
+ * the URI to an absolute path instead.
48
+ */
49
+ . replace ( / ^ f i l e : \/ \/ / u, '' )
50
+ ) ;
51
+ }
52
+ }
53
+
23
54
const option_sets : Record < string , Config | RubyConfig > = {
24
55
general : {
25
56
singleQuote : true ,
@@ -52,12 +83,12 @@ const option_sets: Record<string, Config | RubyConfig> = {
52
83
53
84
package_json : {
54
85
// TODO(0): consider switching to `prettier-plugin-pkg`
55
- plugins : [ plugin_packagejson ] ,
86
+ plugins : [ resolve_plugin ( 'prettier-plugin-packagejson' ) ] ,
56
87
} ,
57
88
58
89
ruby : is_prettier_gem_installed
59
90
? {
60
- plugins : [ plugin_ruby ] ,
91
+ plugins : [ resolve_plugin ( '@prettier/plugin-ruby' ) ] ,
61
92
62
93
rubySingleQuote : true ,
63
94
// rubyPlugins: ['plugin/single_quotes'],
0 commit comments