-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support define and context env variables for apps
- Loading branch information
Showing
7 changed files
with
98 additions
and
27 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
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
48 changes: 48 additions & 0 deletions
48
angular/app-types/angular-app-type/plugins/define.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,48 @@ | ||
import { Plugin, PluginBuild } from 'esbuild'; | ||
|
||
export const stringifyDefine = (define: any) => { | ||
return Object.entries(define).reduce((acc: any, [key, value]) => { | ||
acc[key] = JSON.stringify(value); | ||
return acc; | ||
}, {}); | ||
}; | ||
|
||
/** | ||
* Pass environment variables to esbuild. | ||
* @returns An esbuild plugin. | ||
*/ | ||
export default function(defineValues = {}) { | ||
// set variables on global so that they also work during ssr | ||
const keys = Object.keys(defineValues); | ||
keys.forEach((key: any) => { | ||
// @ts-ignore | ||
if (global[key]) { | ||
throw new Error(`Define plugin: key ${ key } already exists on global`); | ||
} else { | ||
// @ts-ignore | ||
global[key] = defineValues[key]; | ||
} | ||
}); | ||
|
||
const plugin: Plugin = { | ||
name: 'env', | ||
|
||
setup(build: PluginBuild) { | ||
const { platform, define = {} } = build.initialOptions; | ||
if (platform === 'node') { | ||
return; | ||
} | ||
|
||
if (typeof defineValues !== 'string') { | ||
defineValues = stringifyDefine(defineValues); | ||
} | ||
|
||
build.initialOptions.define = { | ||
...defineValues, | ||
...define | ||
}; | ||
} | ||
}; | ||
|
||
return plugin; | ||
} |
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