-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client-only): add configuration mechanism
- Loading branch information
Showing
12 changed files
with
198 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'arui-scripts': minor | ||
--- | ||
|
||
Добавлен механизм работы с конфигурацией для client-only режима |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/arui-scripts/src/configs/client-env-config/add-env-to-html-template.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { getEnvConfigContent } from './get-env-config'; | ||
|
||
export function addEnvToHtmlTemplate(html: string) { | ||
return html.replace('<%= envConfig %>', getEnvConfigContent()); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/arui-scripts/src/configs/client-env-config/client-config-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Compiler } from 'webpack'; | ||
|
||
import { ENV_CONFIG_FILENAME } from './constants'; | ||
import { getEnvConfigContent } from './get-env-config'; | ||
|
||
export class ClientConfigPlugin { | ||
protected cachedContent: string | null = null; | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
apply(compiler: Compiler) { | ||
const pluginName = ClientConfigPlugin.name; | ||
|
||
const { webpack } = compiler; | ||
|
||
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { | ||
// Tapping to the assets processing pipeline on a specific stage. | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: pluginName, | ||
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE, | ||
}, | ||
() => { | ||
const content = getEnvConfigContent(); | ||
|
||
compilation.emitAsset( | ||
`../${ENV_CONFIG_FILENAME}`, | ||
new webpack.sources.RawSource(content) | ||
); | ||
} | ||
); | ||
}); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/arui-scripts/src/configs/client-env-config/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ENV_CONFIG_FILENAME = 'env-config.json'; |
32 changes: 32 additions & 0 deletions
32
packages/arui-scripts/src/configs/client-env-config/get-env-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { configs } from '../app-configs'; | ||
|
||
import { ENV_CONFIG_FILENAME } from './constants'; | ||
|
||
export function replaceTemplateVariables(template: string, variables: Record<string, string | undefined>) { | ||
return template.replace(/\$\{(\w+)}/g, (match, varName) => variables[varName] || ''); | ||
} | ||
|
||
let cachedEnvConfig: string | null = null; | ||
|
||
export function getEnvConfigContent() { | ||
if (cachedEnvConfig) { | ||
return cachedEnvConfig; | ||
} | ||
|
||
const configTemplate = path.join(configs.cwd, ENV_CONFIG_FILENAME); | ||
|
||
if (!fs.existsSync(configTemplate)) { | ||
cachedEnvConfig = '{}'; | ||
|
||
return cachedEnvConfig | ||
} | ||
|
||
const templateContent = fs.readFileSync(configTemplate, 'utf8'); | ||
|
||
cachedEnvConfig = replaceTemplateVariables(templateContent, process.env); | ||
|
||
return cachedEnvConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { ENV_CONFIG_FILENAME } from './constants' | ||
export { ClientConfigPlugin } from './client-config-plugin'; | ||
export { addEnvToHtmlTemplate } from './add-env-to-html-template'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters