@@ -11,78 +11,61 @@ import (
1111 "strings"
1212
1313 "github.com/claceio/clace/internal/app/appfs"
14+ "github.com/claceio/clace/internal/types"
1415 "github.com/evanw/esbuild/pkg/api"
1516 "github.com/evanw/esbuild/pkg/cli"
1617)
1718
18- type LibraryType string
19-
20- const (
21- ESModule LibraryType = "ecmascript_module"
22- Library LibraryType = "library"
23- )
24-
25- const (
26- LIB_PATH = "static/gen/lib"
27- ESM_PATH = "static/gen/esm"
28- )
29-
30- // JSLibrary handles the downloading for JS libraries and esbuild based bundling for ESM libraries
31- type JSLibrary struct {
32- libType LibraryType
33- directUrl string
34- packageName string
35- version string
36- esbuildArgs [10 ]string // use an array so that the struct can be used as key in the jsCache map
37- sanitizedFileName string
38- }
39-
40- func NewLibrary (url string ) * JSLibrary {
41- j := JSLibrary {
42- libType : Library ,
43- directUrl : url ,
44- sanitizedFileName : sanitizeFileName (url ),
19+ func NewLibrary (url string ) * types.JSLibrary {
20+ j := types.JSLibrary {
21+ LibType : types .Library ,
22+ DirectUrl : url ,
23+ SanitizedFileName : sanitizeFileName (url ),
4524 }
4625 return & j
4726}
4827
49- func NewLibraryESM (packageName string , version string , esbuildArgs []string ) * JSLibrary {
28+ func NewLibraryESM (packageName string , version string , esbuildArgs []string ) * types. JSLibrary {
5029 args := [10 ]string {}
5130 if esbuildArgs != nil {
5231 copy (args [:], esbuildArgs )
5332 }
5433
55- j := JSLibrary {
56- libType : ESModule ,
57- packageName : packageName ,
58- version : version ,
59- esbuildArgs : args ,
60- sanitizedFileName : sanitizeFileName (packageName ) + "-" + version + ".js" ,
34+ j := types. JSLibrary {
35+ LibType : types . ESModule ,
36+ PackageName : packageName ,
37+ Version : version ,
38+ EsbuildArgs : args ,
39+ SanitizedFileName : sanitizeFileName (packageName ) + "-" + version + ".js" ,
6140 }
6241 return & j
6342}
6443
65- func (j * JSLibrary ) Setup (dev * AppDev , sourceFS * appfs.WritableSourceFs , workFS * appfs.WorkFs ) (string , error ) {
66- if j .libType == Library {
67- targetFile := path .Join (LIB_PATH , j .sanitizedFileName )
44+ type JsLibManager struct {
45+ types.JSLibrary
46+ }
47+
48+ func (j * JsLibManager ) Setup (dev * AppDev , sourceFS * appfs.WritableSourceFs , workFS * appfs.WorkFs ) (string , error ) {
49+ if j .LibType == types .Library {
50+ targetFile := path .Join (types .LIB_PATH , j .SanitizedFileName )
6851 targetDir := path .Dir (targetFile )
6952 if err := os .MkdirAll (targetDir , 0755 ); err != nil {
70- return "" , fmt .Errorf ("error creating directory %s : %s" , LIB_PATH , err )
53+ return "" , fmt .Errorf ("error creating directory %s : %s" , types . LIB_PATH , err )
7154 }
72- if err := dev .downloadFile (j .directUrl , sourceFS , targetFile ); err != nil {
73- return "" , fmt .Errorf ("error downloading %s : %s" , j .directUrl , err )
55+ if err := dev .downloadFile (j .DirectUrl , sourceFS , targetFile ); err != nil {
56+ return "" , fmt .Errorf ("error downloading %s : %s" , j .DirectUrl , err )
7457 }
7558 return targetFile , nil
76- } else if j .libType == ESModule {
59+ } else if j .LibType == types . ESModule {
7760 return j .setupEsbuild (dev , sourceFS , workFS )
7861 } else {
79- return "" , fmt .Errorf ("invalid library type : %s" , j .libType )
62+ return "" , fmt .Errorf ("invalid library type : %s" , j .LibType )
8063 }
8164}
8265
83- func (j * JSLibrary ) setupEsbuild (dev * AppDev , sourceFS * appfs.WritableSourceFs , workFS * appfs.WorkFs ) (string , error ) {
84- targetDir := path .Join (sourceFS .Root , ESM_PATH )
85- targetFile := path .Join (targetDir , j .sanitizedFileName )
66+ func (j * JsLibManager ) setupEsbuild (dev * AppDev , sourceFS * appfs.WritableSourceFs , workFS * appfs.WorkFs ) (string , error ) {
67+ targetDir := path .Join (sourceFS .Root , types . ESM_PATH )
68+ targetFile := path .Join (targetDir , j .SanitizedFileName )
8669
8770 sourceFile , err := j .generateSourceFile (workFS )
8871 if err != nil {
@@ -92,7 +75,7 @@ func (j *JSLibrary) setupEsbuild(dev *AppDev, sourceFS *appfs.WritableSourceFs,
9275 esbuildArgs := []string {sourceFile , "--bundle" , "--format=esm" }
9376
9477 args := []string {}
95- for _ , arg := range j .esbuildArgs {
78+ for _ , arg := range j .EsbuildArgs {
9679 if arg == "" {
9780 break
9881 }
@@ -135,11 +118,11 @@ func (j *JSLibrary) setupEsbuild(dev *AppDev, sourceFS *appfs.WritableSourceFs,
135118 if len (result .Errors ) > 0 {
136119 // Return the target file name. The caller can check if the file exists to determine if the
137120 // setup was successful even though this step failed
138- dev .Error ().Msgf ("error building %s : %v" , j .packageName , result .Warnings )
139- return targetFile , fmt .Errorf ("error building %s : %v" , j .packageName , result .Errors )
121+ dev .Error ().Msgf ("error building %s : %v" , j .PackageName , result .Warnings )
122+ return targetFile , fmt .Errorf ("error building %s : %v" , j .PackageName , result .Errors )
140123 }
141124 if len (result .Warnings ) > 0 {
142- dev .Warn ().Msgf ("warning building %s : %v" , j .packageName , result .Warnings )
125+ dev .Warn ().Msgf ("warning building %s : %v" , j .PackageName , result .Warnings )
143126 }
144127
145128 for _ , file := range result .OutputFiles {
@@ -153,9 +136,9 @@ func (j *JSLibrary) setupEsbuild(dev *AppDev, sourceFS *appfs.WritableSourceFs,
153136 return options .Outfile , nil
154137}
155138
156- func (j * JSLibrary ) generateSourceFile (workFS * appfs.WorkFs ) (string , error ) {
157- sourceFileName := j .sanitizedFileName
158- sourceContent := fmt .Sprintf (`export * from "%s"` , j .packageName )
139+ func (j * JsLibManager ) generateSourceFile (workFS * appfs.WorkFs ) (string , error ) {
140+ sourceFileName := j .SanitizedFileName
141+ sourceContent := fmt .Sprintf (`export * from "%s"` , j .PackageName )
159142 if err := workFS .Write (sourceFileName , []byte (sourceContent )); err != nil {
160143 return "" , fmt .Errorf ("error writing source file %s : %s" , sourceFileName , err )
161144 }
0 commit comments