Created
February 28, 2021 02:07
-
-
Save arturovt/e70d2dcf00dffe3deb2c60262c82c56b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as ts from 'typescript'; | |
import * as webpack from 'webpack'; | |
import { AngularCompilerPlugin, PLATFORM } from '@ngtools/webpack'; | |
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); | |
}; | |
export default (config: webpack.Configuration) => { | |
const angularCompilerPlugin = config.plugins!.find( | |
(plugin): plugin is AngularCompilerPlugin => plugin instanceof AngularCompilerPlugin, | |
); | |
const platform: PLATFORM = angularCompilerPlugin!['_platform']; | |
if (platform === PLATFORM.Browser) { | |
angularCompilerPlugin!['_transformers'].unshift(replaceIsPlatformCallsTransformerFactory); | |
} | |
return config; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment