Skip to content

Commit

Permalink
use css loader
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
  • Loading branch information
edewit committed Jan 15, 2024
1 parent 562d472 commit f84b647
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions esbuild_scss.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (
"github.com/evanw/esbuild/pkg/cli"
)

func compileSass(inputPath, outputPath string) error {
func compileSass(inputPath, outputPath string) (string, error) {
// Read the input Sass/SCSS file
input, err := os.ReadFile(inputPath)
if err != nil {
return err
return "", err
}

// add sass to the path
current, err := os.Executable()
if err != nil {
return err
return "", err
}
bin := filepath.Dir(current)
pack := filepath.Dir(bin)
Expand All @@ -41,7 +41,7 @@ func compileSass(inputPath, outputPath string) error {
// Create a Dart Sass compiler
compiler, err := godartsass.Start(godartsass.Options{})
if err != nil {
return err
return "", err
}
defer compiler.Close()

Expand All @@ -54,16 +54,10 @@ func compileSass(inputPath, outputPath string) error {
EnableSourceMap: true,
})
if err != nil {
return err
return "", err
}

// Write the compiled CSS to the output file
err = os.WriteFile(outputPath, []byte(output.CSS), 0644)
if err != nil {
return err
}

return nil
return output.CSS, nil
}

var scssPlugin = api.Plugin{
Expand All @@ -83,24 +77,15 @@ var scssPlugin = api.Plugin{
extension := filepath.Ext(args.Path)
filenameWithoutExtension := strings.TrimSuffix(args.Path, extension)
outputPath := filenameWithoutExtension + ".css"
err := compileSass(args.Path, outputPath)
result, err := compileSass(args.Path, outputPath)
if err != nil {
return api.OnLoadResult{}, err
}

// Modify the import path to the generated CSS file
args.Path = outputPath

// Record the generated CSS as a dependency
build.OnResolve(api.OnResolveOptions{
Namespace: "file",
Filter: outputPath,
}, func(args api.OnResolveArgs) (api.OnResolveResult, error) {
return api.OnResolveResult{Path: outputPath, External: true}, nil
})

result := ""
return api.OnLoadResult{Contents: &result}, nil
return api.OnLoadResult{Contents: &result, Loader: api.LoaderCSS}, nil
})
},
}
Expand Down

0 comments on commit f84b647

Please sign in to comment.