Accessing Azure Pipeline variables in Webpack Node.js
Recently, I struggled a bit to figure out a way to access Azure-Pipeline Variables in Node.js JavaScript/TypeScript project. However, it is easy.
.yml Pipeline code - task: Npm@1
displayName: "Run project"
env:
MyVariableName: $(MYVARIABLENAME) // this is pipeline variable name, in Capital letters
inputs:
commands: 'custom'
CustomCommand: 'run test'
npm Package install package string-replace-loader
TypeScript/JavaScript File Create a file with any name “test.ts”
test.ts
const test = () => {
const myVariable = "My-Variable";
};
Setup webpack.config.js module: {
rules: [
{
test: /test\.ts$/,
loader: 'string-replace-loader',
options: {
search: 'My-Variable',
replace: process.env['MyVariableName'] // from .yml env
}
}
]
}
}
Finally
"scripts" :{
"test": "webpack --config webpack.