angular-server-side-configuration
Version:
Configure an angular application on the server
57 lines • 2.93 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ngsscServeWebpackBrowser = ngsscServeWebpackBrowser;
const architect_1 = require("@angular-devkit/architect");
const build_angular_1 = require("@angular-devkit/build-angular");
const index_1 = require("../ngsscbuild/index");
/**
* Ngssc wrapper for the Angular Webpack development server builder.
* @param options Dev Server options.
* @param context The build context.
* @param transforms A map of transforms that can be used to hook into some logic (such as
* transforming webpack configuration before passing it to webpack).
*
* @experimental Direct usage of this function is considered experimental.
*/
function ngsscServeWebpackBrowser(options, context, transforms = {}) {
context.logger.warn(`
The angular-server-side-configuration:dev-server builder is deprecated with no replacement.
Please create an issue at https://github.com/kyubisation/angular-server-side-configuration
if you need a angular-server-side-configuration:application builder.`);
transforms.indexHtml = async (content) => {
const ngsscContext = await (0, index_1.detectVariables)(context, options.searchPattern);
const ngssc = (0, index_1.buildNgssc)(ngsscContext, options);
const populatedVariables = populateVariables(ngssc.environmentVariables);
context.logger.info(`Populated environment variables (Variant: ${ngssc.variant})\n${Object.entries(populatedVariables).map(([key, value]) => ` ${key}: ${value}`)}`);
const iife = generateIife(ngssc.variant, populatedVariables);
return insertIife(content, iife);
};
return (0, build_angular_1.executeDevServerBuilder)(options, context, transforms);
}
function populateVariables(variables) {
const populatedVariables = {};
variables.forEach((v) => (populatedVariables[v] = v in process.env ? process.env[v] || '' : null));
return populatedVariables;
}
function generateIife(variant, populatedVariables) {
const iife = variant === 'NG_ENV'
? `(function(self){self.NG_ENV=${JSON.stringify(populatedVariables)};})(window)`
: `(function(self){self.process=${JSON.stringify({ env: populatedVariables })};})(window)`;
return `<!--ngssc--><script>${iife}</script><!--/ngssc-->`;
}
function insertIife(fileContent, iife) {
if (/<!--ngssc-->[\w\W]*<!--\/ngssc-->/.test(fileContent)) {
return fileContent.replace(/<!--ngssc-->[\w\W]*<!--\/ngssc-->/, iife);
}
else if (/<!--\s*CONFIG\s*-->/.test(fileContent)) {
return fileContent.replace(/<!--\s*CONFIG\s*-->/, iife);
}
else if (fileContent.includes('</title>')) {
return fileContent.replace('</title>', `</title>${iife}`);
}
else {
return fileContent.replace('</head>', `${iife}</head>`);
}
}
exports.default = (0, architect_1.createBuilder)(ngsscServeWebpackBrowser);
//# sourceMappingURL=index.js.map
;