Skip to content

Commit

Permalink
feat: add webpack cjs support, fixes #320
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Mar 9, 2024
1 parent 27af6d9 commit 72e7aa1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
33 changes: 19 additions & 14 deletions extension/asset_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (
)

const (
StorefrontWebpackConfig = "Resources/app/storefront/build/webpack.config.js"
StorefrontEntrypointJS = "Resources/app/storefront/src/main.js"
StorefrontEntrypointTS = "Resources/app/storefront/src/main.ts"
StorefrontBaseCSS = "Resources/app/storefront/src/scss/base.scss"
AdministrationWebpackConfig = "Resources/app/administration/build/webpack.config.js"
AdministrationEntrypointJS = "Resources/app/administration/src/main.js"
AdministrationEntrypointTS = "Resources/app/administration/src/main.ts"
StorefrontWebpackConfig = "Resources/app/storefront/build/webpack.config.js"
StorefrontWebpackCJSConfig = "Resources/app/storefront/build/webpack.config.cjs"
StorefrontEntrypointJS = "Resources/app/storefront/src/main.js"
StorefrontEntrypointTS = "Resources/app/storefront/src/main.ts"
StorefrontBaseCSS = "Resources/app/storefront/src/scss/base.scss"
AdministrationWebpackConfig = "Resources/app/administration/build/webpack.config.js"
AdministrationWebpackCJSConfig = "Resources/app/administration/build/webpack.config.cjs"
AdministrationEntrypointJS = "Resources/app/administration/src/main.js"
AdministrationEntrypointTS = "Resources/app/administration/src/main.ts"
)

type AssetBuildConfig struct {
Expand Down Expand Up @@ -60,7 +62,6 @@ func BuildAssetsForExtensions(ctx context.Context, sources []asset.Source, asset
shopwareRoot := assetConfig.ShopwareRoot
if shopwareRoot == "" && requiresShopwareSources {
shopwareRoot, err = setupShopwareInTemp(ctx, minVersion)

if err != nil {
return err
}
Expand Down Expand Up @@ -101,7 +102,6 @@ func BuildAssetsForExtensions(ctx context.Context, sources []asset.Source, asset
var additionalNpmParameters []string

npmPackage, err := getNpmPackage(administrationRoot)

if err != nil {
return err
}
Expand Down Expand Up @@ -197,7 +197,6 @@ func BuildAssetsForExtensions(ctx context.Context, sources []asset.Source, asset
additionalNpmParameters := []string{"caniuse-lite"}

npmPackage, err := getNpmPackage(storefrontRoot)

if err != nil {
return err
}
Expand Down Expand Up @@ -281,7 +280,6 @@ func InstallNodeModulesOfConfigs(ctx context.Context, cfgs ExtensionAssetConfig,
}

npmPackage, err := getNpmPackage(npmPath)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -409,13 +407,11 @@ func prepareShopwareForAsset(shopwareRoot string, cfgs map[string]ExtensionAsset
}

err = os.WriteFile(fmt.Sprintf("%s/var/plugins.json", shopwareRoot), pluginJson, os.ModePerm)

if err != nil {
return fmt.Errorf("prepareShopwareForAsset: %w", err)
}

err = os.WriteFile(fmt.Sprintf("%s/var/features.json", shopwareRoot), []byte("{}"), os.ModePerm)

if err != nil {
return fmt.Errorf("prepareShopwareForAsset: %w", err)
}
Expand Down Expand Up @@ -480,6 +476,11 @@ func createConfigFromPath(entryPointName string, extensionRoot string) Extension
webpackFileAdmin = &val
}

if _, err := os.Stat(path.Join(extensionRoot, AdministrationWebpackCJSConfig)); err == nil {
val := AdministrationWebpackCJSConfig
webpackFileAdmin = &val
}

if _, err := os.Stat(path.Join(extensionRoot, StorefrontEntrypointJS)); err == nil {
val := StorefrontEntrypointJS
entryFilePathStorefront = &val
Expand All @@ -495,6 +496,11 @@ func createConfigFromPath(entryPointName string, extensionRoot string) Extension
webpackFileStorefront = &val
}

if _, err := os.Stat(path.Join(extensionRoot, StorefrontWebpackCJSConfig)); err == nil {
val := StorefrontWebpackCJSConfig
webpackFileStorefront = &val
}

if _, err := os.Stat(path.Join(extensionRoot, StorefrontBaseCSS)); err == nil {
storefrontStyles = append(storefrontStyles, StorefrontBaseCSS)
}
Expand Down Expand Up @@ -540,7 +546,6 @@ func setupShopwareInTemp(ctx context.Context, minVersion string) (string, error)
gitCheckoutCmd.Stdout = os.Stdout
gitCheckoutCmd.Stderr = os.Stderr
err = gitCheckoutCmd.Run()

if err != nil {
return "", err
}
Expand Down
30 changes: 30 additions & 0 deletions extension/asset_platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ func TestGenerateConfigWithTypeScript(t *testing.T) {
assert.Equal(t, "Resources/app/storefront/src", config["FroshTools"].Storefront.Path)
}

func TestGenerateWebpackCJS(t *testing.T) {
dir := t.TempDir()

assert.NoError(t, os.MkdirAll(path.Join(dir, "Resources", "app", "administration", "src"), os.ModePerm))
assert.NoError(t, os.MkdirAll(path.Join(dir, "Resources", "app", "administration", "build"), os.ModePerm))

assert.NoError(t, os.MkdirAll(path.Join(dir, "Resources", "app", "storefront", "src"), os.ModePerm))
assert.NoError(t, os.MkdirAll(path.Join(dir, "Resources", "app", "storefront", "build"), os.ModePerm))

assert.NoError(t, os.WriteFile(path.Join(dir, "Resources", "app", "administration", "src", "main.ts"), []byte("test"), os.ModePerm))

assert.NoError(t, os.WriteFile(path.Join(dir, "Resources", "app", "administration", "build", "webpack.config.cjs"), []byte("test"), os.ModePerm))

assert.NoError(t, os.WriteFile(path.Join(dir, "Resources", "app", "storefront", "src", "main.ts"), []byte("test"), os.ModePerm))
assert.NoError(t, os.WriteFile(path.Join(dir, "Resources", "app", "storefront", "build", "webpack.config.cjs"), []byte("test"), os.ModePerm))

config := BuildAssetConfigFromExtensions(getTestContext(), []asset.Source{{Name: "FroshTools", Path: dir}}, AssetBuildConfig{})

assert.True(t, config.Has("FroshTools"))
assert.True(t, config.RequiresAdminBuild())
assert.True(t, config.RequiresStorefrontBuild())
assert.Equal(t, "frosh-tools", config["FroshTools"].TechnicalName)
assert.Equal(t, "Resources/app/administration/src/main.ts", *config["FroshTools"].Administration.EntryFilePath)
assert.Equal(t, "Resources/app/storefront/src/main.ts", *config["FroshTools"].Storefront.EntryFilePath)
assert.Equal(t, "Resources/app/administration/build/webpack.config.cjs", *config["FroshTools"].Administration.Webpack)
assert.Equal(t, "Resources/app/storefront/build/webpack.config.cjs", *config["FroshTools"].Storefront.Webpack)
assert.Equal(t, "Resources/app/administration/src", config["FroshTools"].Administration.Path)
assert.Equal(t, "Resources/app/storefront/src", config["FroshTools"].Storefront.Path)
}

func TestGenerateConfigAddsStorefrontAlwaysAsEntrypoint(t *testing.T) {
config := BuildAssetConfigFromExtensions(getTestContext(), []asset.Source{}, AssetBuildConfig{})

Expand Down

0 comments on commit 72e7aa1

Please sign in to comment.