Skip to content

Instantly share code, notes, and snippets.

@arturovt
Created February 28, 2021 02:08
Show Gist options
  • Save arturovt/8312d6125fa8ab0560be8bd0035420a6 to your computer and use it in GitHub Desktop.
Save arturovt/8312d6125fa8ab0560be8bd0035420a6 to your computer and use it in GitHub Desktop.
import * as ts from 'typescript';
const replaceIsPlatformCallsTransformerFactory: ts.TransformerFactory<ts.SourceFile> = (
context: ts.TransformationContext,
) => (sourceFile: ts.SourceFile) => {
const visitCallExpression: ts.Visitor = (node: ts.Node) => {
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
// If it's a call expression like `isPlatformServer(platformId)`.
if (node.expression.escapedText === 'isPlatformServer') {
return ts.factory.createFalse();
} else if (node.expression.escapedText === 'isPlatformBrowser') {
return ts.factory.createTrue();
}
}
return ts.visitEachChild(node, visitCallExpression, context);
};
return ts.visitEachChild(sourceFile, visitCallExpression, context);
};
const transformation = require('@ngtools/webpack/src/ivy/transformation');
const createAotTransformers = transformation.createAotTransformers;
transformation.createAotTransformers = function () {
const transformers: ts.CustomTransformers = createAotTransformers(...arguments);
transformers.before!.push(replaceIsPlatformCallsTransformerFactory);
return transformers;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment