manfredsteyer / ngx-build-plus

Extend the Angular CLI's default build behavior without ejecting, e. g. for Angular Elements
1.19k stars 136 forks source link

How does environment variable injection work in ssr? Please provide docs #395

Closed zetti-caletti closed 11 months ago

zetti-caletti commented 11 months ago

environment.ts

declare const WEBAPP_PUBLIC_HOSTNAME: string;
declare const HTTPS_ENABLED: string;

export const environment = {
    webappUrl: (HTTPS_ENABLED == 'true' ? 'https://' : 'http://') + WEBAPP_PUBLIC_HOSTNAME,
};
const webpack = require('webpack');

console.log('webpack.partial.js');
console.log({
    "WEBAPP_PUBLIC_HOSTNAME": JSON.stringify(process.env.WEBAPP_PUBLIC_HOSTNAME),
    "HTTPS_ENABLED": JSON.stringify(process.env.HTTPS_ENABLED),
});

module.exports = {
    plugins: [
        new webpack.DefinePlugin({
            "WEBAPP_PUBLIC_HOSTNAME": JSON.stringify(process.env.WEBAPP_PUBLIC_HOSTNAME),
            "HTTPS_ENABLED": JSON.stringify(process.env.HTTPS_ENABLED),
        })
    ]
}
"serve:ssr": "node dist/myproject/server/main.js",
"build:ssr": "ng build --extra-webpack-config webpack.partial.js && ng run myproject:server",
WEBAPP_PUBLIC_HOSTNAME=app.myproject.com HTTPS_ENABLED=true npm run build:ssr
WEBAPP_PUBLIC_HOSTNAME=app.myproject.com HTTPS_ENABLED=true npm run serve:ssr
ReferenceError: HTTPS_ENABLED is not defined
zetti-caletti commented 11 months ago

Got it to work. Had to put "extraWebpackConfig": "webpack.partial.js" under server.options in angular.json. Its poor documented.