Skip to content

Commit

Permalink
Feature(transformer): re-implement postProcess API (#8026)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinyaigeek committed May 9, 2022
1 parent 1568064 commit e51955f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
67 changes: 65 additions & 2 deletions packages/core/core/src/Transformation.js
Expand Up @@ -74,9 +74,14 @@ import {
toProjectPath,
} from './projectPath';
import {invalidateOnFileCreateToInternal} from './utils';
import invariant from 'assert';

type GenerateFunc = (input: UncommittedAsset) => Promise<GenerateOutput>;

type PostProcessFunc = (
Array<UncommittedAsset>,
) => Promise<Array<UncommittedAsset> | null>;

export type TransformationOpts = {|
options: ParcelOptions,
config: ParcelConfig,
Expand Down Expand Up @@ -336,7 +341,34 @@ export default class Transformation {
}
}

return finalAssets;
if (!pipeline.postProcess) {
return finalAssets;
}

let pipelineHash = await this.getPipelineHash(pipeline);
let invalidationHash = await getInvalidationHash(
finalAssets.flatMap(asset => asset.getInvalidations()),
this.options,
);
let processedCacheEntry = await this.readFromCache(
this.getCacheKey(finalAssets, invalidationHash, pipelineHash),
);

invariant(pipeline.postProcess != null);
let processedFinalAssets: Array<UncommittedAsset> =
processedCacheEntry ?? (await pipeline.postProcess(finalAssets)) ?? [];

if (!processedCacheEntry) {
await this.writeToCache(
this.getCacheKey(processedFinalAssets, invalidationHash, pipelineHash),
processedFinalAssets,

invalidationHash,
pipelineHash,
);
}

return processedFinalAssets;
}

async getPipelineHash(pipeline: Pipeline): Promise<string> {
Expand Down Expand Up @@ -426,6 +458,8 @@ export default class Transformation {
transformer.plugin,
transformer.name,
transformer.config,
transformer.configKeyPath,
this.parcelConfig,
);

for (let result of transformerResults) {
Expand Down Expand Up @@ -695,6 +729,8 @@ export default class Transformation {
transformer: Transformer<mixed>,
transformerName: string,
preloadedConfig: ?Config,
configKeyPath?: string,
parcelConfig: ParcelConfig,
): Promise<$ReadOnlyArray<TransformerResult | UncommittedAsset>> {
const logger = new PluginLogger({origin: transformerName});

Expand Down Expand Up @@ -786,7 +822,7 @@ export default class Transformation {
});
let results = await normalizeAssets(this.options, transfomerResult);

// Create generate function that can be called later
// Create generate and postProcess function that can be called later
asset.generate = (): Promise<GenerateOutput> => {
let publicAsset = new Asset(asset);
if (transformer.generate && asset.ast) {
Expand All @@ -805,6 +841,32 @@ export default class Transformation {
);
};

let postProcess = transformer.postProcess;
if (postProcess) {
pipeline.postProcess = async (
assets: Array<UncommittedAsset>,
): Promise<Array<UncommittedAsset> | null> => {
let results = await postProcess.call(transformer, {
assets: assets.map(asset => new MutableAsset(asset)),
config,
options: pipeline.pluginOptions,
resolve,
logger,
});

return Promise.all(
results.map(result =>
asset.createChildAsset(
result,
transformerName,
parcelConfig.filePath,
// configKeyPath,
),
),
);
};
}

return results;
}
}
Expand All @@ -816,6 +878,7 @@ type Pipeline = {|
pluginOptions: PluginOptions,
resolverRunner: ResolverRunner,
workerApi: WorkerApi,
postProcess?: PostProcessFunc,
generate?: GenerateFunc,
|};

Expand Down
21 changes: 14 additions & 7 deletions packages/core/types/index.js
Expand Up @@ -186,7 +186,7 @@ export type EnvironmentOptions = {|
*/
export type VersionMap = {
[string]: string,
...,
...
};

export type EnvironmentFeature =
Expand Down Expand Up @@ -398,9 +398,7 @@ export interface AssetSymbols // eslint-disable-next-line no-undef
* This is the default state.
*/
+isCleared: boolean;
get(
exportSymbol: Symbol,
): ?{|
get(exportSymbol: Symbol): ?{|
local: Symbol,
loc: ?SourceLocation,
meta?: ?Meta,
Expand Down Expand Up @@ -443,9 +441,7 @@ export interface MutableDependencySymbols // eslint-disable-next-line no-undef
* This is the default state.
*/
+isCleared: boolean;
get(
exportSymbol: Symbol,
): ?{|
get(exportSymbol: Symbol): ?{|
local: Symbol,
loc: ?SourceLocation,
isWeak: boolean,
Expand Down Expand Up @@ -1045,6 +1041,17 @@ export type Transformer<ConfigType> = {|
options: PluginOptions,
logger: PluginLogger,
|}): Async<Array<TransformerResult | MutableAsset>>,
/**
* Do some processing after the transformation
* @experimental
*/
postProcess?: ({|
assets: Array<MutableAsset>,
config: ConfigType,
resolve: ResolveFn,
options: PluginOptions,
logger: PluginLogger,
|}) => Async<Array<TransformerResult>>,
/** Stringify the AST */
generate?: ({|
asset: Asset,
Expand Down

0 comments on commit e51955f

Please sign in to comment.