@@ -32,8 +32,22 @@ type WidgetPackageJson = {
3232
3333type WidgetViteConfigOptions = {
3434 widgetName : string ;
35+ runtimeDirectoryName ?: string ;
3536} ;
3637
38+ function getResolveAlias ( ) : { find : RegExp ; replacement : string } [ ] {
39+ return [
40+ {
41+ find : / ^ ~ ( .+ ) / ,
42+ replacement : "$1"
43+ } ,
44+ {
45+ find : / ^ s r c \/ / ,
46+ replacement : `${ resolve ( process . cwd ( ) , "src" ) } /`
47+ }
48+ ] ;
49+ }
50+
3751async function copyDir ( src : string , dest : string ) : Promise < void > {
3852 mkdirSync ( dest , { recursive : true } ) ;
3953 if ( existsSync ( src ) ) {
@@ -43,10 +57,14 @@ async function copyDir(src: string, dest: string): Promise<void> {
4357
4458async function buildEditorArtifacts ( editorBuilds : EditorBuild [ ] ) : Promise < void > {
4559 const editorOutDir = "dist/tmp/widgets" ;
60+ const alias = getResolveAlias ( ) ;
4661
4762 for ( const editorBuild of editorBuilds ) {
4863 await viteBuild ( {
4964 configFile : false ,
65+ resolve : {
66+ alias
67+ } ,
5068 build : {
5169 target : "es2019" ,
5270 minify : "esbuild" ,
@@ -100,36 +118,51 @@ function inferMetadataFiles(widgetName: string): FileCopy[] {
100118 ] ;
101119}
102120
103- function inferRequiredArtifacts ( widgetName : string , packagePath : string ) : string [ ] {
121+ function inferRequiredArtifacts (
122+ widgetName : string ,
123+ packagePath : string ,
124+ runtimeDirectoryName : string ,
125+ editorBuilds : EditorBuild [ ]
126+ ) : string [ ] {
104127 const packagePathDir = toPackagePathDir ( packagePath ) ;
105- const widgetDir = widgetName . toLowerCase ( ) ;
128+ const widgetDir = runtimeDirectoryName ;
129+
130+ const editorArtifacts = editorBuilds . map ( editorBuild => editorBuild . outputFile ) ;
106131
107132 return [
108- `${ widgetName } .editorConfig.js` ,
109- `${ widgetName } .editorPreview.js` ,
133+ ...editorArtifacts ,
110134 `${ packagePathDir } /${ widgetDir } /${ widgetName } .js` ,
111135 `${ packagePathDir } /${ widgetDir } /${ widgetName } .mjs`
112136 ] ;
113137}
114138
115- function inferRuntimeOutDir ( widgetName : string , packagePath : string ) : string {
139+ function inferRuntimeOutDir ( packagePath : string , runtimeDirectoryName : string ) : string {
116140 const packagePathDir = toPackagePathDir ( packagePath ) ;
117- return `dist/tmp/widgets/${ packagePathDir } /${ widgetName . toLowerCase ( ) } ` ;
141+ return `dist/tmp/widgets/${ packagePathDir } /${ runtimeDirectoryName } ` ;
118142}
119143
120144function inferEditorBuilds ( widgetName : string ) : EditorBuild [ ] {
121- return [
122- {
123- entry : `src/${ widgetName } .editorPreview.tsx` ,
145+ const editorBuilds : EditorBuild [ ] = [ ] ;
146+
147+ const editorPreviewEntry = `src/${ widgetName } .editorPreview.tsx` ;
148+ if ( existsSync ( editorPreviewEntry ) ) {
149+ editorBuilds . push ( {
150+ entry : editorPreviewEntry ,
124151 outputFile : `${ widgetName } .editorPreview.js` ,
125152 externals : [ / ^ m e n d i x ( $ | \/ ) / , / ^ r e a c t $ / , / ^ r e a c t - d o m $ / ]
126- } ,
127- {
128- entry : `src/${ widgetName } .editorConfig.ts` ,
153+ } ) ;
154+ }
155+
156+ const editorConfigEntry = `src/${ widgetName } .editorConfig.ts` ;
157+ if ( existsSync ( editorConfigEntry ) ) {
158+ editorBuilds . push ( {
159+ entry : editorConfigEntry ,
129160 outputFile : `${ widgetName } .editorConfig.js` ,
130161 externals : [ / ^ m e n d i x ( $ | \/ ) / , / ^ r e a c t $ / , / ^ r e a c t - d o m $ / ]
131- }
132- ] ;
162+ } ) ;
163+ }
164+
165+ return editorBuilds ;
133166}
134167
135168function inferRemoveBeforeCopy ( packageName : string ) : string [ ] {
@@ -155,13 +188,15 @@ type ResolvedConfig = {
155188function resolveConfig ( options : WidgetViteConfigOptions ) : ResolvedConfig {
156189 const widgetPackageJson = readWidgetPackageJson ( ) ;
157190 const primaryRuntimeFormat = inferPrimaryRuntimeFormat ( ) ;
191+ const editorBuilds = inferEditorBuilds ( options . widgetName ) ;
192+ const runtimeDirectoryName = options . runtimeDirectoryName ?? options . widgetName . toLowerCase ( ) ;
158193
159194 return {
160195 widgetName : options . widgetName ,
161196 widgetVersion : widgetPackageJson . version ,
162197 mpkName : widgetPackageJson . mxpackage ?. mpkName ?? `${ options . widgetName } .mpk` ,
163198 runtimeEntry : `src/${ options . widgetName } .tsx` ,
164- runtimeOutDir : inferRuntimeOutDir ( options . widgetName , widgetPackageJson . packagePath ) ,
199+ runtimeOutDir : inferRuntimeOutDir ( widgetPackageJson . packagePath , runtimeDirectoryName ) ,
165200 runtimeOutputs : [
166201 {
167202 format : primaryRuntimeFormat ,
@@ -174,8 +209,13 @@ function resolveConfig(options: WidgetViteConfigOptions): ResolvedConfig {
174209 ] ,
175210 runtimeExternals : [ "react" , "react-dom" , "@mendix/widget-plugin-component-kit" , "big.js" , / ^ m e n d i x ( $ | \/ ) / ] ,
176211 metadataFiles : inferMetadataFiles ( options . widgetName ) ,
177- editorBuilds : inferEditorBuilds ( options . widgetName ) ,
178- requiredArtifacts : inferRequiredArtifacts ( options . widgetName , widgetPackageJson . packagePath ) ,
212+ editorBuilds,
213+ requiredArtifacts : inferRequiredArtifacts (
214+ options . widgetName ,
215+ widgetPackageJson . packagePath ,
216+ runtimeDirectoryName ,
217+ editorBuilds
218+ ) ,
179219 removeBeforeCopy : inferRemoveBeforeCopy ( widgetPackageJson . name ) ,
180220 define : {
181221 "process.env.NODE_ENV" : JSON . stringify ( "production" )
@@ -243,9 +283,13 @@ async function createMPK(options: ResolvedConfig): Promise<void> {
243283
244284export default function createWidgetViteConfig ( options : WidgetViteConfigOptions ) : UserConfig {
245285 const resolvedConfig = resolveConfig ( options ) ;
286+ const alias = getResolveAlias ( ) ;
246287
247288 return defineConfig ( {
248289 define : resolvedConfig . define ,
290+ resolve : {
291+ alias
292+ } ,
249293 build : {
250294 target : "es2019" ,
251295 minify : "esbuild" ,
0 commit comments