Skip to content

Commit

Permalink
feat: modify source files before compilation
Browse files Browse the repository at this point in the history
Ability to modify source files before compilation.
E.g.: might be used to compile pug files to html.
Probably related to ng-packagr#1619.
NgPackage config can have resourceReader string - path to exported JS
function responsible for reading sources code files.

closes ng-packagr#1601
  • Loading branch information
lekhmanrus committed Apr 14, 2022
1 parent fc91063 commit 3e696cf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/lib/ng-package/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export class NgPackage {
return path.join(this.basePath, this.primary.$get('dest'));
}

/** Absolute path of the package's resource reader function. */
public get resourceReader(): string {
return path.join(this.basePath, this.primary.$get('resourceReader'));
}

public get keepLifecycleScripts(): boolean {
return this.primary.$get('keepLifecycleScripts');
}
Expand Down
3 changes: 1 addition & 2 deletions src/lib/ngc/compile-source-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export async function compileSourceFiles(
const tsConfigOptions: CompilerOptions = { ...tsConfig.options, ...extraOptions };
const entryPoint: EntryPointNode = graph.find(isEntryPointInProgress());
const ngPackageNode: PackageNode = graph.find(isPackage);
const inlineStyleLanguage = ngPackageNode.data.inlineStyleLanguage;

const tsCompilerHost = ngccTransformCompilerHost(
cacheCompilerHost(
Expand All @@ -32,7 +31,7 @@ export async function compileSourceFiles(
tsConfigOptions,
moduleResolutionCache,
stylesheetProcessor,
inlineStyleLanguage,
ngPackageNode.data,
),
tsConfigOptions,
ngccProcessor,
Expand Down
30 changes: 18 additions & 12 deletions src/lib/ts/cache-compiler-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { CompilerHost, CompilerOptions } from '@angular/compiler-cli';
import { createHash } from 'crypto';
import * as path from 'path';
import ts from 'typescript';
import { NgPackageConfig } from '../../ng-package.schema';
import { FileCache } from '../file-system/file-cache';
import { BuildGraph } from '../graph/build-graph';
import { Node } from '../graph/node';
import { EntryPointNode, fileUrl } from '../ng-package/nodes';
import { NgPackage } from '../ng-package/package';
import { StylesheetProcessor } from '../styles/stylesheet-processor';
import { ensureUnixPath } from '../utils/path';

Expand All @@ -17,7 +17,7 @@ export function cacheCompilerHost(
compilerOptions: CompilerOptions,
moduleResolutionCache: ts.ModuleResolutionCache,
stylesheetProcessor?: StylesheetProcessor,
inlineStyleLanguage?: NgPackageConfig['inlineStyleLanguage'],
ngPackage?: NgPackage,
sourcesFileCache: FileCache = entryPoint.cache.sourcesFileCache,
): CompilerHost {
const compilerHost = ts.createIncrementalCompilerHost(compilerOptions);
Expand Down Expand Up @@ -127,15 +127,21 @@ export function cacheCompilerHost(

const cache = sourcesFileCache.getOrCreate(fileName);
if (cache.content === undefined) {
if (/(?:html?|svg)$/.test(path.extname(fileName))) {
// template
cache.content = compilerHost.readFile.call(this, fileName);
if (ngPackage?.resourceReader) {
const resourceReaderPath = path.relative(__dirname, ngPackage.resourceReader).replace(/\\/g, '/');
const resourceReaderFn = await import(resourceReaderPath);
cache.content = resourceReaderFn.default(fileName, compilerHost, stylesheetProcessor);
} else {
// stylesheet
cache.content = await stylesheetProcessor.process({
filePath: fileName,
content: compilerHost.readFile.call(this, fileName),
});
if (/(?:html?|svg)$/.test(path.extname(fileName))) {
// template
cache.content = compilerHost.readFile.call(this, fileName);
} else {
// stylesheet
cache.content = await stylesheetProcessor.process({
filePath: fileName,
content: compilerHost.readFile.call(this, fileName),
});
}
}

if (cache.content === undefined) {
Expand All @@ -152,9 +158,9 @@ export function cacheCompilerHost(
return null;
}

if (inlineStyleLanguage) {
if (ngPackage?.inlineStyleLanguage) {
const key = createHash('sha1').update(data).digest('hex');
const fileName = `${context.containingFile}-${key}.${inlineStyleLanguage}`;
const fileName = `${context.containingFile}-${key}.${ngPackage.inlineStyleLanguage}`;
const cache = sourcesFileCache.getOrCreate(fileName);
if (cache.content === undefined) {
cache.content = await stylesheetProcessor.process({
Expand Down
4 changes: 4 additions & 0 deletions src/ng-package.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"type": "string",
"default": "dist"
},
"resourceReader": {
"description": "Path to the resource reader function, that could be used to transform source files before compilation.",
"type": "string"
},
"keepLifecycleScripts": {
"description": "Enable this to keep the 'scripts' section in package.json. Read the NPM Blog on 'Package install scripts vulnerability' – http://blog.npmjs.org/post/141702881055/package-install-scripts-vulnerability",
"type": "boolean",
Expand Down

0 comments on commit 3e696cf

Please sign in to comment.