Skip to content

Commit

Permalink
fix(compiler): Allow OutputTargetCustom to be called on devMode (#5541)
Browse files Browse the repository at this point in the history
* fix(compiler): Allow OutputTargetCustom to be called on devMode

* fix(compiler): Enable custom tasks to be called up in watch mode

* fix(compiler): run prettier & lint

---------

Co-authored-by: Sukaato <dev@Sukaato>
Co-authored-by: Sukaato <sukaato@proton.me>
  • Loading branch information
3 people authored May 7, 2024
1 parent 945df46 commit b0a9f7b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/compiler/output-targets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { outputCollection } from './dist-collection';
import { outputCustomElements } from './dist-custom-elements';
import { outputHydrateScript } from './dist-hydrate-script';
import { outputLazy } from './dist-lazy/lazy-output';
import { outputCustom } from './output-custom';
import { outputDocs } from './output-docs';
import { outputLazyLoader } from './output-lazy-loader';
import { outputTypes } from './output-types';
Expand Down Expand Up @@ -45,6 +46,7 @@ export const generateOutputTargets = async (
// since it validates files were created
await outputDocs(config, compilerCtx, buildCtx);
await outputTypes(config, compilerCtx, buildCtx);
await outputCustom(config, compilerCtx, buildCtx);

timeSpan.finish('generate outputs finished');
};
Expand Down
18 changes: 9 additions & 9 deletions src/compiler/output-targets/output-custom.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { catchError, isOutputTargetCustom } from '@utils';

import type * as d from '../../declarations';
import { generateDocData } from '../docs/generate-doc-data';

export const outputCustom = async (config: d.ValidatedConfig, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => {
const task = config.watch ? 'always' : 'onBuildOnly';
const customOutputTargets = config.outputTargets
.filter(isOutputTargetCustom)
.filter((o) => (o.taskShouldRun === undefined ? true : o.taskShouldRun === task));

export const outputCustom = async (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
docs: d.JsonDocs,
outputTargets: d.OutputTarget[],
) => {
const customOutputTargets = outputTargets.filter(isOutputTargetCustom);
if (customOutputTargets.length === 0) {
return;
}
const docsData = await generateDocData(config, compilerCtx, buildCtx);

await Promise.all(
customOutputTargets.map(async (o) => {
const timespan = buildCtx.createTimeSpan(`generating ${o.name} started`);
try {
await o.generator(config, compilerCtx, buildCtx, docs);
await o.generator(config, compilerCtx, buildCtx, docsData);
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/output-targets/output-docs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
isOutputTargetCustom,
isOutputTargetDocsCustom,
isOutputTargetDocsJson,
isOutputTargetDocsReadme,
Expand All @@ -12,7 +11,6 @@ import { generateDocData } from '../docs/generate-doc-data';
import { generateJsonDocs } from '../docs/json';
import { generateReadmeDocs } from '../docs/readme';
import { generateVscodeDocs } from '../docs/vscode';
import { outputCustom } from './output-custom';

/**
* Generate documentation-related output targets
Expand All @@ -30,7 +28,6 @@ export const outputDocs = async (
}
const docsOutputTargets = config.outputTargets.filter(
(o) =>
isOutputTargetCustom(o) ||
isOutputTargetDocsReadme(o) ||
isOutputTargetDocsJson(o) ||
isOutputTargetDocsCustom(o) ||
Expand All @@ -51,6 +48,5 @@ export const outputDocs = async (
generateJsonDocs(config, compilerCtx, docsData, docsOutputTargets),
generateVscodeDocs(compilerCtx, docsData, docsOutputTargets),
generateCustomDocs(config, docsData, docsOutputTargets),
outputCustom(config, compilerCtx, buildCtx, docsData, docsOutputTargets),
]);
};
12 changes: 11 additions & 1 deletion src/declarations/stencil-public-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ConfigFlags } from '../cli/config-flags';
import type { PrerenderUrlResults, PrintLine } from '../internal';
import type { BuildCtx, CompilerCtx } from './stencil-private';
import type { JsonDocs } from './stencil-public-docs';

export * from './stencil-public-docs';
Expand Down Expand Up @@ -2239,8 +2240,17 @@ export interface OutputTargetHydrate extends OutputTargetBase {
export interface OutputTargetCustom extends OutputTargetBase {
type: 'custom';
name: string;
/**
* Indicate when the output target should be executed.
*
* - `"onBuildOnly"`: Executed only when `stencil build` is called without `--watch`.
* - `"always"`: Executed on every build, including in `watch` mode.
*
* Defaults to "always".
*/
taskShouldRun?: 'onBuildOnly' | 'always';
validate?: (config: Config, diagnostics: Diagnostic[]) => void;
generator: (config: Config, compilerCtx: any, buildCtx: any, docs: any) => Promise<void>;
generator: (config: Config, compilerCtx: CompilerCtx, buildCtx: BuildCtx, docs: JsonDocs) => Promise<void>;
copy?: CopyTask[];
}

Expand Down

0 comments on commit b0a9f7b

Please sign in to comment.