Plugins: Pass build mode to frontend (#114092)
* pass build mode to frontend * set buildMode build time using shared plugin webpack conf
This commit is contained in:
@@ -37,6 +37,7 @@ export type AppPluginConfig = {
|
||||
dependencies: PluginDependencies;
|
||||
extensions: PluginExtensions;
|
||||
moduleHash?: string;
|
||||
buildMode?: string;
|
||||
};
|
||||
|
||||
export type PreinstalledPlugin = {
|
||||
|
||||
@@ -4,7 +4,7 @@ import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
|
||||
import path from 'path';
|
||||
import ReplaceInFileWebpackPlugin from 'replace-in-file-webpack-plugin';
|
||||
import TerserPlugin from 'terser-webpack-plugin';
|
||||
import webpack, { type Configuration } from 'webpack';
|
||||
import webpack, { type Configuration, type Compiler } from 'webpack';
|
||||
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
|
||||
import VirtualModulesPlugin from 'webpack-virtual-modules';
|
||||
|
||||
@@ -31,6 +31,36 @@ function skipFiles(f: string): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
class BuildModeWebpackPlugin {
|
||||
apply(compiler: Compiler) {
|
||||
compiler.hooks.compilation.tap('BuildModeWebpackPlugin', (compilation) => {
|
||||
compilation.hooks.processAssets.tap(
|
||||
{
|
||||
name: 'BuildModeWebpackPlugin',
|
||||
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
|
||||
},
|
||||
async () => {
|
||||
const assets = compilation.getAssets();
|
||||
for (const asset of assets) {
|
||||
if (asset.name.endsWith('plugin.json')) {
|
||||
const pluginJsonString = asset.source.source().toString();
|
||||
const pluginJsonWithBuildMode = JSON.stringify(
|
||||
{
|
||||
...JSON.parse(pluginJsonString),
|
||||
buildMode: compilation.options.mode,
|
||||
},
|
||||
null,
|
||||
4
|
||||
);
|
||||
compilation.updateAsset(asset.name, new webpack.sources.RawSource(pluginJsonWithBuildMode));
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export type Env = {
|
||||
[key: string]: true | string | Env;
|
||||
};
|
||||
@@ -253,6 +283,8 @@ const config = async (env: Env): Promise<Configuration> => {
|
||||
],
|
||||
},
|
||||
]),
|
||||
// Add buildMode to plugin.json
|
||||
new BuildModeWebpackPlugin(),
|
||||
...(env.development
|
||||
? [
|
||||
new ForkTsCheckerWebpackPlugin({
|
||||
|
||||
@@ -637,6 +637,7 @@ func (hs *HTTPServer) newAppDTO(ctx context.Context, plugin pluginstore.Plugin,
|
||||
Dependencies: plugin.Dependencies,
|
||||
ModuleHash: hs.pluginAssets.ModuleHash(ctx, plugin),
|
||||
Translations: plugin.Translations,
|
||||
BuildMode: plugin.BuildMode,
|
||||
}
|
||||
|
||||
if settings.Enabled {
|
||||
|
||||
@@ -339,6 +339,7 @@ type AppDTO struct {
|
||||
Dependencies Dependencies `json:"dependencies"`
|
||||
ModuleHash string `json:"moduleHash,omitempty"`
|
||||
Translations map[string]string `json:"translations,omitempty"`
|
||||
BuildMode string `json:"buildMode,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -133,6 +133,9 @@ type JSONData struct {
|
||||
|
||||
// List of languages supported by the plugin
|
||||
Languages []string `json:"languages,omitempty"`
|
||||
|
||||
// Build mode of the plugin (set automatically at build time)
|
||||
BuildMode string `json:"buildMode,omitempty"`
|
||||
}
|
||||
|
||||
func ReadPluginJSON(reader io.Reader) (JSONData, error) {
|
||||
|
||||
Reference in New Issue
Block a user