From e51955f9d4fa4009a611ba7e88bd85d0fd4c53a2 Mon Sep 17 00:00:00 2001 From: Shinobu Hayashi Date: Mon, 9 May 2022 11:43:49 +0900 Subject: [PATCH] Feature(transformer): re-implement postProcess API (#8026) --- packages/core/core/src/Transformation.js | 67 +++++++++++++++++++++++- packages/core/types/index.js | 21 +++++--- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/packages/core/core/src/Transformation.js b/packages/core/core/src/Transformation.js index d8e90198d8f..4880b509e65 100644 --- a/packages/core/core/src/Transformation.js +++ b/packages/core/core/src/Transformation.js @@ -74,9 +74,14 @@ import { toProjectPath, } from './projectPath'; import {invalidateOnFileCreateToInternal} from './utils'; +import invariant from 'assert'; type GenerateFunc = (input: UncommittedAsset) => Promise; +type PostProcessFunc = ( + Array, +) => Promise | null>; + export type TransformationOpts = {| options: ParcelOptions, config: ParcelConfig, @@ -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 = + 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 { @@ -426,6 +458,8 @@ export default class Transformation { transformer.plugin, transformer.name, transformer.config, + transformer.configKeyPath, + this.parcelConfig, ); for (let result of transformerResults) { @@ -695,6 +729,8 @@ export default class Transformation { transformer: Transformer, transformerName: string, preloadedConfig: ?Config, + configKeyPath?: string, + parcelConfig: ParcelConfig, ): Promise<$ReadOnlyArray> { const logger = new PluginLogger({origin: transformerName}); @@ -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 => { let publicAsset = new Asset(asset); if (transformer.generate && asset.ast) { @@ -805,6 +841,32 @@ export default class Transformation { ); }; + let postProcess = transformer.postProcess; + if (postProcess) { + pipeline.postProcess = async ( + assets: Array, + ): Promise | 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; } } @@ -816,6 +878,7 @@ type Pipeline = {| pluginOptions: PluginOptions, resolverRunner: ResolverRunner, workerApi: WorkerApi, + postProcess?: PostProcessFunc, generate?: GenerateFunc, |}; diff --git a/packages/core/types/index.js b/packages/core/types/index.js index d4def0daf83..57c29b0c5ee 100644 --- a/packages/core/types/index.js +++ b/packages/core/types/index.js @@ -186,7 +186,7 @@ export type EnvironmentOptions = {| */ export type VersionMap = { [string]: string, - ..., + ... }; export type EnvironmentFeature = @@ -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, @@ -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, @@ -1045,6 +1041,17 @@ export type Transformer = {| options: PluginOptions, logger: PluginLogger, |}): Async>, + /** + * Do some processing after the transformation + * @experimental + */ + postProcess?: ({| + assets: Array, + config: ConfigType, + resolve: ResolveFn, + options: PluginOptions, + logger: PluginLogger, + |}) => Async>, /** Stringify the AST */ generate?: ({| asset: Asset,