-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable VariableDetector to also detect index access usages
BREAKING CHANGE: This also refactors the ngsscbuild builder extensively. This breaking change will only affect you, if you used the internals of this library.
- Loading branch information
1 parent
86d12df
commit 702dac3
Showing
7 changed files
with
173 additions
and
231 deletions.
There are no files selected for viewing
6 changes: 0 additions & 6 deletions
6
projects/angular-server-side-configuration/builders/ngsscbuild/browser-options.ts
This file was deleted.
Oops, something went wrong.
93 changes: 85 additions & 8 deletions
93
projects/angular-server-side-configuration/builders/ngsscbuild/index.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,90 @@ | ||
import { BuilderContext, createBuilder } from '@angular-devkit/architect'; | ||
import { JsonObject } from '@angular-devkit/core'; | ||
|
||
import { NgsscBuilder } from './ngssc-builder'; | ||
import { promises } from 'fs'; | ||
import { basename, join } from 'path'; | ||
import { BuilderContext, createBuilder, targetFromTargetString } from '@angular-devkit/architect'; | ||
import { json, JsonObject } from '@angular-devkit/core'; | ||
import { Ngssc } from 'angular-server-side-configuration'; | ||
import { BrowserBuilderOptions } from '@angular-devkit/build-angular'; | ||
import { Schema } from './schema'; | ||
import { VariableDetector } from './variable-detector'; | ||
import { NgsscContext } from './ngssc-context'; | ||
|
||
export type NgsscBuildSchema = Schema; | ||
|
||
const readFileAsync = promises.readFile; | ||
const writeFileAsync = promises.writeFile; | ||
|
||
export { Schema as NgsscBuildSchema } from './schema'; | ||
export async function ngsscBuild(options: NgsscBuildSchema, context: BuilderContext) { | ||
const browserTarget = targetFromTargetString(options.browserTarget); | ||
const rawBrowserOptions = await context.getTargetOptions(browserTarget); | ||
const browserName = await context.getBuilderNameForTarget(browserTarget); | ||
const browserOptions = await context.validateOptions<json.JsonObject & BrowserBuilderOptions>( | ||
rawBrowserOptions, | ||
browserName | ||
); | ||
const scheduledTarget = await context.scheduleTarget(browserTarget); | ||
const result = await scheduledTarget.result; | ||
await detectVariablesAndBuildNgsscJson(options, browserOptions, context); | ||
return result; | ||
} | ||
|
||
export async function detectVariablesAndBuildNgsscJson( | ||
options: NgsscBuildSchema, | ||
browserOptions: BrowserBuilderOptions, | ||
context: BuilderContext | ||
) { | ||
const ngsscContext = await detectVariables(options, context); | ||
const outputPath = join(context.workspaceRoot, browserOptions.outputPath); | ||
const ngssc = buildNgssc(ngsscContext, options, browserOptions); | ||
await writeFileAsync(join(outputPath, 'ngssc.json'), JSON.stringify(ngssc, null, 2), 'utf8'); | ||
} | ||
|
||
export async function detectVariables( | ||
options: NgsscBuildSchema, | ||
context: BuilderContext | ||
): Promise<NgsscContext> { | ||
const environmentVariableFile = join(context.workspaceRoot, options.ngsscEnvironmentFile); | ||
const detector = new VariableDetector(context.logger); | ||
const fileContent = await readFileAsync(environmentVariableFile, 'utf8'); | ||
const ngsscContext = detector.detect(fileContent); | ||
context.logger.info( | ||
`ngssc: Detected variant '${ngsscContext.variant}' with variables ` + | ||
`'${ngsscContext.variables.join(', ')}'` | ||
); | ||
if (ngsscContext.variant === 'NG_ENV') { | ||
context.logger.warn( | ||
'Variant NG_ENV is deprecated and will be removed with version 14. ' + | ||
'Please change usage to `process.env`.' | ||
); | ||
} | ||
|
||
return ngsscContext; | ||
} | ||
|
||
export function buildNgssc( | ||
ngsscContext: NgsscContext, | ||
options: NgsscBuildSchema, | ||
browserOptions?: BrowserBuilderOptions | ||
): Ngssc { | ||
return { | ||
environmentVariables: [ | ||
...ngsscContext.variables, | ||
...(options.additionalEnvironmentVariables || []), | ||
], | ||
filePattern: options.filePattern || extractFilePattern(browserOptions?.index), | ||
variant: ngsscContext.variant, | ||
}; | ||
} | ||
|
||
export async function ngsscBuild(options: Schema, context: BuilderContext) { | ||
return await new NgsscBuilder(options, context).run(); | ||
function extractFilePattern(index: BrowserBuilderOptions['index'] | undefined) { | ||
if (!index) { | ||
return '**/index.html'; | ||
} else if (typeof index === 'string') { | ||
return basename(index); | ||
} else if (index.output) { | ||
return basename(index.output); | ||
} else { | ||
return basename(index.input); | ||
} | ||
} | ||
|
||
export default createBuilder<Schema & JsonObject>(ngsscBuild); | ||
export default createBuilder<NgsscBuildSchema & JsonObject>(ngsscBuild); |
90 changes: 0 additions & 90 deletions
90
projects/angular-server-side-configuration/builders/ngsscbuild/ngssc-builder.ts
This file was deleted.
Oops, something went wrong.
8 changes: 2 additions & 6 deletions
8
projects/angular-server-side-configuration/builders/ngsscbuild/ngssc-context.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import { Variant } from 'angular-server-side-configuration'; | ||
|
||
/** | ||
* The context for ngssc. | ||
* @public | ||
*/ | ||
/** The context for ngssc. */ | ||
export interface NgsscContext { | ||
variant: Variant; | ||
variantImport: string | undefined; | ||
variables: { variable: string; expression: string }[]; | ||
variables: string[]; | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.