From 72e7aa1c92c05abf919cfdf9b88352df5be9a615 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Sat, 9 Mar 2024 12:50:34 +0000 Subject: [PATCH] feat: add webpack cjs support, fixes #320 --- extension/asset_platform.go | 33 ++++++++++++++++++-------------- extension/asset_platform_test.go | 30 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/extension/asset_platform.go b/extension/asset_platform.go index 5615219..3fa29de 100644 --- a/extension/asset_platform.go +++ b/extension/asset_platform.go @@ -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 { @@ -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 } @@ -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 } @@ -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 } @@ -281,7 +280,6 @@ func InstallNodeModulesOfConfigs(ctx context.Context, cfgs ExtensionAssetConfig, } npmPackage, err := getNpmPackage(npmPath) - if err != nil { return nil, err } @@ -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) } @@ -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 @@ -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) } @@ -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 } diff --git a/extension/asset_platform_test.go b/extension/asset_platform_test.go index c0c65ec..281061d 100644 --- a/extension/asset_platform_test.go +++ b/extension/asset_platform_test.go @@ -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{})