1
1
import { type Plugin } from 'vite' ;
2
- import { join } from 'path' ;
2
+ import { join , relative } from 'path' ;
3
3
import { readWorkspaceConfig } from 'nx/src/project-graph/file-utils' ;
4
- import { ProjectConfiguration , workspaceRoot } from '@nrwl/devkit' ;
4
+ import {
5
+ createProjectRootMappingsFromProjectConfigurations ,
6
+ findProjectForPath ,
7
+ } from 'nx/src/project-graph/utils/find-project-for-path' ;
8
+ import {
9
+ ProjectConfiguration ,
10
+ ProjectsConfigurations ,
11
+ workspaceRoot ,
12
+ } from '@nrwl/devkit' ;
5
13
import { readFileSync } from 'fs' ;
6
14
7
15
interface QwikVitePluginStub {
8
16
api : {
9
- getOptions : ( ) => { vendorRoots : string [ ] } ;
17
+ getOptions : ( ) => QwikVitePluginOptionsStub ;
10
18
} ;
11
19
}
12
20
21
+ interface QwikVitePluginOptionsStub {
22
+ vendorRoots : string [ ] ;
23
+ rootDir : string ;
24
+ debug : boolean ;
25
+ }
26
+
13
27
export interface ProjectFilter {
14
28
name ?: string [ ] | RegExp ;
15
29
path ?: RegExp ;
@@ -18,6 +32,7 @@ export interface ProjectFilter {
18
32
}
19
33
20
34
export interface QwikNxVitePluginOptions {
35
+ currentProjectName ?: string ;
21
36
includeProjects ?: ProjectFilter ;
22
37
excludeProjects ?: ProjectFilter ;
23
38
debug ?: boolean ;
@@ -30,7 +45,7 @@ export interface QwikNxVitePluginOptions {
30
45
*
31
46
* By default `qwikNxVite` plugin will provide Qwik with paths of all Nx projects, that are specified in the tsconfig.base.json.
32
47
* However, this behavior might not be always suitable, especially in cases when you have code that you don't want the optimizer to go through.
33
- * It is possible to use specifically exclude or include certain projects using plugin options.
48
+ * It is possible to specifically exclude or include certain projects using plugin options.
34
49
*/
35
50
export function qwikNxVite ( options ?: QwikNxVitePluginOptions ) : Plugin {
36
51
const vitePlugin : Plugin = {
@@ -45,17 +60,49 @@ export function qwikNxVite(options?: QwikNxVitePluginOptions): Plugin {
45
60
throw new Error ( 'Missing vite-plugin-qwik' ) ;
46
61
}
47
62
48
- const vendorRoots = getVendorRoots ( options ) ;
63
+ const pluginOptions = qwikPlugin . api . getOptions ( ) ;
49
64
50
- qwikPlugin . api . getOptions ( ) . vendorRoots . push ( ...vendorRoots ) ;
65
+ const vendorRoots = getVendorRoots ( options , pluginOptions ) ;
66
+
67
+ pluginOptions . vendorRoots . push ( ...vendorRoots ) ;
51
68
} ,
52
69
} ;
53
70
54
71
return vitePlugin ;
55
72
}
56
73
74
+ function getCurrentProjectName (
75
+ projectsConfigurations : ProjectsConfigurations ,
76
+ projectRootDir : string
77
+ ) : string {
78
+ const projectRootMappings =
79
+ createProjectRootMappingsFromProjectConfigurations (
80
+ projectsConfigurations . projects
81
+ ) ;
82
+ const relativeDirname = relative ( workspaceRoot , projectRootDir ) ;
83
+ const currentProjectName = findProjectForPath (
84
+ relativeDirname ,
85
+ projectRootMappings
86
+ ) ;
87
+
88
+ if ( ! currentProjectName ) {
89
+ throw new Error (
90
+ `Could not resolve the project name for path "${ projectRootDir } "`
91
+ ) ;
92
+ }
93
+ return currentProjectName ;
94
+ }
95
+
57
96
/** Retrieves vendor roots and applies necessary filtering */
58
- function getVendorRoots ( options ?: QwikNxVitePluginOptions ) : string [ ] {
97
+ function getVendorRoots (
98
+ options : QwikNxVitePluginOptions | undefined ,
99
+ qwikOptions : QwikVitePluginOptionsStub
100
+ ) : string [ ] {
101
+ const log = ( ...str : unknown [ ] ) => {
102
+ ( options ?. debug || qwikOptions . debug ) &&
103
+ console . debug ( `[QWIK-NX-VITE PLUGIN:]` , ...str ) ;
104
+ } ;
105
+
59
106
const workspaceConfig = readWorkspaceConfig ( { format : 'nx' } ) ;
60
107
61
108
const baseTsConfig = JSON . parse (
@@ -67,13 +114,19 @@ function getVendorRoots(options?: QwikNxVitePluginOptions): string[] {
67
114
68
115
let projects = Object . values ( workspaceConfig . projects ) ;
69
116
117
+ const currentProjectName =
118
+ options ?. currentProjectName ??
119
+ getCurrentProjectName ( workspaceConfig , qwikOptions . rootDir ) ;
120
+
121
+ projects = projects . filter ( ( p ) => p . name !== currentProjectName ) ;
122
+
70
123
projects . forEach ( ( p ) => ( p . sourceRoot ??= p . root ) ) ;
71
124
72
125
projects = filterProjects ( projects , options ?. excludeProjects , true ) ;
73
126
projects = filterProjects ( projects , options ?. includeProjects , false ) ;
74
127
75
128
if ( options ?. debug ) {
76
- console . log (
129
+ log (
77
130
'Projects after applying include\\exclude filters:' ,
78
131
projects . map ( ( p ) => p . name )
79
132
) ;
@@ -84,7 +137,7 @@ function getVendorRoots(options?: QwikNxVitePluginOptions): string[] {
84
137
) ;
85
138
86
139
if ( options ?. debug ) {
87
- console . log (
140
+ log (
88
141
'Projects after excluding those not in tsconfig.base.json:' ,
89
142
projects . map ( ( p ) => p . name )
90
143
) ;
0 commit comments