转换器
转换器插件将单个资源转换为编译、发现依赖或转换为不同格式。许多转换器是其他工具(如编译器和预处理器)的包装器,负责将它们与 Parcel 集成。
转换资源
#必须传递给 Transformer
构造函数的一个必需函数是 transform
。此函数接收一个 MutableAsset
对象,它代表一个文件。可以检索资源的源代码或内容,以及任何关联的源映射(见下文)。然后,转换器可以以某种方式转换此内容,并将编译结果设置回资源。
import { Transformer } from "@parcel/plugin";
export default new Transformer({
async transform({ asset }) {
// 检索资源的源代码和源映射。
let source = await asset.getCode();
let sourceMap = await asset.getMap();
// 通过某些编译器运行,并将结果
// 设置在资源上。
let { code, map } = compile(source, sourceMap);
asset.setCode(code);
asset.setMap(map);
// 返回资源
return [asset];
},
});
加载配置
#应在转换器插件的 loadConfig
方法中从用户的项目加载配置。有关如何执行此操作的详细信息,请参见加载配置。
note:使用 Parcel 的配置加载机制非常重要,以便正确地使缓存失效。避免直接从文件系统加载文件。
更改资源类型
#转换器可以将资源从一种格式转换为另一种格式,例如从 TypeScript 转换为 JavaScript。为此,请将资源的 type
属性设置为新的文件类型(例如 js
)。然后,资源将由匹配新类型的管道处理。有关详细信息,请参见 Parcel 配置文档中的转换器。
import {Transformer} from '@parcel/plugin';
export default new Transformer({
async transform({asset}) {
let code = await asset.getCode();
let result = compile(code);
asset.type = 'js';
asset.setCode(result);
return [asset];
}
});
环境
#资源与 Environment
相关联,描述了应如何编译资源。同一资源可能会使用不同的环境多次处理,例如,在为现代和传统目标构建时。如果可能,转换器插件应考虑环境,以确保编译的代码适用于目标并进行优化。有关详细信息,请参见目标。
import {Transformer} from '@parcel/plugin';
export default new Transformer({
async transform({asset}) {
let code = await asset.getCode();
let result = asset.env.isBrowser()
? compileForBrowser(code, asset.engines.browser)
: compileForNode(code, asset.engines.node);
asset.setCode(result);
return [asset];
}
});
有关可用属性的详细信息,请参见 Environment
API 文档。
添加依赖
#除了转换资源的内容外,转换器插件还负责发现代码中的依赖,以便 Parcel 可以处理它们。某些转换器不需要担心这一点,因为另一个转换器将在之后运行并执行此操作(例如默认的 JavaScript 转换器)。如果您正在为 Parcel 尚不支持的语言添加支持,或者以其他方式在编译后的代码之外引入依赖,则需要将它们添加到资源中。
可以使用 addDependency
方法将依赖添加到资源中,传递一个 DependencyOptions
对象。有两个必需的参数:specifier
,它是描述依赖位置的字符串,以及 specifierType
,它描述应如何解释指定符。有关详细信息,请参见依赖解析。
import {Transformer} from '@parcel/plugin';
export default new Transformer({
async transform({asset}) {
let code = await asset.getCode();
let deps = code.matchAll(/import "(.*?)"/g);
for (let dep of deps) {
asset.addDependency({
specifier: dep,
specifierType: 'esm'
});
}
return [asset];
}
});
影响打包
#指定依赖的方式可以影响其打包方式。默认情况下,依赖会被打包到同一个输出文件中。依赖的 priority
属性可以指定它应该延迟加载或与依赖它的资源并行加载。例如,JavaScript 中的动态 import()
以 lazy
优先级加载依赖。请参见代码分割。
bundleBehavior
属性进一步控制依赖的打包方式。例如,通过将 bundleBehavior
设置为 inline
,依赖可以被分离到新的捆绑包中,但内联到父级。请参见捆绑内联。
有关每个可用选项的更多详细信息,请参见 DependencyOptions
。
URL 依赖
#在某些语言中,依赖通过 URL 引用其他文件。在编译后的代码中,这些 URL 引用需要更新以指向最终的捆绑包名称。但是,在转换资源时,捆绑包名称尚未知。
addDependency
返回一个唯一的依赖 ID。这可以放置在转换后的资源内容中作为最终捆绑包 URL 的占位符,并且稍后将由打包器在 URL 已知时替换。
作为快捷方式,addURLDependency
方法创建一个 specifierType
设置为 url
,priority
设置为 lazy
(以创建单独的捆绑包)的依赖。
import {Transformer} from '@parcel/plugin';
export default new Transformer({
async transform({asset}) {
let code = await asset.getCode();
let result = code.replace(/import "(.*?)"/g, (m, dep) => {
// 将原始指定符替换为依赖 ID 作为占位符。
// 这将稍后被替换为最终的捆绑包 URL。
let depId = asset.addURLDependency(dep);
return `import "${depId}"`;
});
asset.setCode(result);
return [asset];
}
});
重用 AST
#如果多个转换器插件连续在一个资源上运行,如果它们可以重用相同的解析 AST,那么为每个插件解析、转换和生成代码将是浪费的。Parcel 通过将 transform
函数拆分为几个部分来促进 AST 共享:
canReuseAST
– 如果前一个转换器插件有可用的 AST,它将被传递给下一个转换器的此方法(如果可用)。它应检查 AST 的type
和version
,以确定是否可以重用它。如果返回true
,则不调用parse
方法并重用 AST。如果返回false
,则调用前一个转换器的generate
函数,并使用结果调用下一个转换器的parse
函数。parse
– 如果没有可用的 AST,或canReuseAST
返回 false,则调用转换器的parse
函数。它应返回一个描述 AST 的类型、版本和内容的AST
对象。generate
– 如果下一个转换器无法重用 AST,或这是管道中的最后一个转换器,则将调用generate
。它应返回一个包含生成的内容和源映射的结果对象。
import { Transformer } from "@parcel/plugin";
import semver from "semver";
export default new Transformer({
async canReuseAST({ ast }) {
return (
ast.type === "my-compiler" && semver.satisfies(ast.version, "^1.0.0")
);
},
async parse({ asset }) {
return {
type: "my-compiler",
version: "1.0.0",
program: parse(await asset.getCode()),
};
},
async transform({ asset }) {
let ast = await asset.getAST();
let compiledAST = compile(ast.program);
asset.setAST({
type: "my-compiler",
version: "1.0.0",
program: compiledAST,
});
return [asset];
},
async generate({ ast }) {
let { content, map } = generate(ast.program);
return {
content,
map,
};
},
});
源映射
#源映射帮助开发者在浏览器中调试编译和打包的代码,方法是将编译后代码中的位置映射回原始源代码。转换器插件应在转换其内容时尽可能为资源添加源映射。由于资源可能由多个转换器处理,因此除了其内容外,还应转换资源附带的现有源映射。
Parcel 使用 @parcel/source-map
库进行源映射操作。有关如何使用它的更多详细信息,请参见源映射。您可能需要在其他工具之间转换传递的源映射。
import { Transformer } from "@parcel/plugin";
import SourceMap from "@parcel/source-map";
export default new Transformer({
async transform({ asset, options }) {
let source = await asset.getCode();
let sourceMap = await asset.getMap();
// 将输入源映射转换为 JSON。
let result = compile(source, sourceMap.toVLQ());
asset.setCode(result.code);
// 将返回的 JSON 源映射转换为 Parcel SourceMap。
let map = new SourceMap(options.projectRoot);
map.addVLQMap(result.map);
asset.setMap(map);
return [asset];
},
});
二进制数据
#除了文本源代码,转换器还可以处理二进制内容。这可以通过使用 Buffer 或使用流来完成。
import {Transformer} from '@parcel/plugin';
export default new Transformer({
async transform({asset}) {
let buffer = await asset.getBuffer();
let result = transform(buffer);
asset.setBuffer(result);
return [asset];
}
});
查询参数
#资源可能通过带有查询参数的依赖引用。这些指定转换器在编译或转换资源时使用的选项。例如,Parcel 图像转换器使用查询参数允许用户指定转换图像的宽度、高度和格式。同一资源可能会使用不同的查询参数多次编译。
import {Transformer} from '@parcel/plugin';
export default new Transformer({
async transform({asset}) {
let buffer = await asset.getBuffer();
let result = resize(
buffer,
asset.query.width,
asset.query.height
);
asset.setBuffer(result);
return [asset];
}
});
返回多个资源
#到目前为止的所有示例都展示了如何转换单个资源。然而,有时一个文件可能包含多个不同的资源。例如,在 HTML 中,有内联的 <script>
和 <style>
元素,应通过它们自己的单独 Parcel 管道进行处理。为此,Parcel 允许从转换器返回多个资源。
要创建新资源,返回一个 TransformerResult
对象数组。这些对象必须具有 type
和 content
,但也可以有自己的依赖,以及许多与 Asset
对象相同的选项。
通常,应返回原始资源以及它可能具有的任何子资源。父资源可以通过为子资源分配 uniqueKey
属性并将其作为依赖的 specifier
来创建对子资源的依赖。这允许创建"虚拟"资源,这些资源实际上并不存在于文件系统中,但可以像它们存在一样被引用。
import { Transformer } from "@parcel/plugin";
export default new Transformer({
async transform({ asset }) {
let code = await asset.getCode();
// 提取内联资源以与此资源一起返回。
let assets = [asset];
let uniqueKey = `${asset.id}-style`;
assets.push({
type: "css",
content: "...",
uniqueKey,
bundleBehavior: "inline",
});
// 添加依赖,使用 uniqueKey 作为指定符。
asset.addDependency({
specifier: uniqueKey,
specifierType: "esm",
});
return assets;
},
});
相关 API
#DependencyOptions parcel/packages/core/types/index.js:476
Usen when creating a Dependency, see that.
type DependencyOptions = {|
+specifier: DependencySpecifier,
The specifier used to resolve the dependency.
+specifierType: SpecifierType,
How the specifier should be interpreted.
- esm: An ES module specifier. It is parsed as a URL, but bare specifiers are treated as node_modules.
- commonjs: A CommonJS specifier. It is not parsed as a URL.
- url: A URL that works as in a browser. Bare specifiers are treated as relative URLs.
- custom: A custom specifier. Must be handled by a custom resolver plugin.
+priority?: DependencyPriority,
When the dependency should be loaded.
- sync: The dependency should be resolvable synchronously. The resolved asset will be placed in the same bundle as the parent, or another bundle that's already on the page.
- parallel: The dependency should be placed in a separate bundle that's loaded in parallel with the current bundle.
- lazy: The dependency should be placed in a separate bundle that's loaded later.
Default value: 'sync'
+bundleBehavior?: BundleBehavior,
Controls the behavior of the bundle the resolved asset is placed into. Use in combination with priority
to determine when the bundle is loaded.
- inline: The resolved asset will be placed into a new inline bundle. Inline bundles are not written to a separate file, but embedded into the parent bundle.
- isolated: The resolved asset will be isolated from its parents in a separate bundle. Shared assets will be duplicated.
+needsStableName?: boolean,
When the dependency is a bundle entry (priority is "parallel" or "lazy"), this controls the naming
of that bundle. needsStableName
indicates that the name should be stable over time, even when the
content of the bundle changes. This is useful for entries that a user would manually enter the URL
for, as well as for things like service workers or RSS feeds, where the URL must remain consistent
over time.
+isOptional?: boolean,
Whether the dependency is optional. If the dependency cannot be resolved, this will not fail the build.
+loc?: SourceLocation,
The location within the source file where the dependency was found.
+env?: EnvironmentOptions,
The environment of the dependency.
+packageConditions?: Array<string>,
A list of custom conditions to use when resolving package.json "exports" and "imports". This is combined with the conditions from the environment. However, it overrides the default "import" and "require" conditions inferred from the specifierType. To include those in addition to custom conditions, explicitly add them to this list.
+meta?: Meta,
Plugin-specific metadata for the dependency.
+pipeline?: string,
The pipeline defined in .parcelrc that the dependency should be processed with.
+resolveFrom?: FilePath,
The file path where the dependency should be resolved from. By default, this is the path of the source file where the dependency was specified.
+range?: SemverRange,
The semver version range expected for the dependency.
+symbols?: $ReadOnlyMap<Symbol, {|
local: Symbol,
loc: ?SourceLocation,
isWeak: boolean,
meta?: Meta,
|}>,
The symbols within the resolved module that the source file depends on.
|}
Referenced by:
MutableAsset, TransformerResultDependency parcel/packages/core/types/index.js:551
A Dependency denotes a connection between two assets (likely some effect from the importee is expected - be it a side effect or a value is being imported).
interface Dependency {
+id: string,
The id of the dependency.
+specifier: DependencySpecifier,
The specifier used to resolve the dependency.
+specifierType: SpecifierType,
How the specifier should be interpreted.
- esm: An ES module specifier. It is parsed as a URL, but bare specifiers are treated as node_modules.
- commonjs: A CommonJS specifier. It is not parsed as a URL.
- url: A URL that works as in a browser. Bare specifiers are treated as relative URLs.
- custom: A custom specifier. Must be handled by a custom resolver plugin.
+priority: DependencyPriority,
When the dependency should be loaded.
- sync: The dependency should be resolvable synchronously. The resolved asset will be placed in the same bundle as the parent, or another bundle that's already on the page.
- parallel: The dependency should be placed in a separate bundle that's loaded in parallel with the current bundle.
- lazy: The dependency should be placed in a separate bundle that's loaded later.
Default value: 'sync'
+bundleBehavior: ?BundleBehavior,
Controls the behavior of the bundle the resolved asset is placed into. Use in combination with priority
to determine when the bundle is loaded.
- inline: The resolved asset will be placed into a new inline bundle. Inline bundles are not written to a separate file, but embedded into the parent bundle.
- isolated: The resolved asset will be isolated from its parents in a separate bundle. Shared assets will be duplicated.
+needsStableName: boolean,
When the dependency is a bundle entry (priority is "parallel" or "lazy"), this controls the naming
of that bundle. needsStableName
indicates that the name should be stable over time, even when the
content of the bundle changes. This is useful for entries that a user would manually enter the URL
for, as well as for things like service workers or RSS feeds, where the URL must remain consistent
over time.
+isOptional: boolean,
Whether the dependency is optional. If the dependency cannot be resolved, this will not fail the build.
+isEntry: boolean,
Whether the dependency is an entry.
+loc: ?SourceLocation,
The location within the source file where the dependency was found.
+env: Environment,
The environment of the dependency.
+packageConditions: ?Array<string>,
A list of custom conditions to use when resolving package.json "exports" and "imports". This is combined with the conditions from the environment. However, it overrides the default "import" and "require" conditions inferred from the specifierType. To include those in addition to custom conditions, explicitly add them to this list.
+meta: Meta,
Plugin-specific metadata for the dependency.
+target: ?Target,
If this is an entry, this is the target that is associated with that entry.
+sourceAssetId: ?string,
The id of the asset with this dependency.
+sourcePath: ?FilePath,
The file path of the asset with this dependency.
+sourceAssetType: ?string,
The type of the asset that referenced this dependency.
+resolveFrom: ?FilePath,
The file path where the dependency should be resolved from. By default, this is the path of the source file where the dependency was specified.
+range: ?SemverRange,
The semver version range expected for the dependency.
+pipeline: ?string,
The pipeline defined in .parcelrc that the dependency should be processed with.
+symbols: MutableDependencySymbols,
}
Referenced by:
BaseAsset, Bundle, BundleGraph, BundleGraphTraversable, BundleTraversable, DependencyOptions, DependencySpecifier, MutableBundleGraph, Resolver, ResolvingProgressEvent, RuntimeAssetASTGenerator parcel/packages/core/types/index.js:639
type ASTGenerator = {|
type: string,
version: Semver,
|}
Referenced by:
BaseAssetBaseAsset parcel/packages/core/types/index.js:652
An asset represents a file or part of a file. It may represent any data type, including source code,
binary data, etc. Assets may exist in the file system or may be virtual.
interface BaseAsset {
+id: string,
The id of the asset.
+fs: FileSystem,
The file system where the source is located.
+filePath: FilePath,
The file path of the asset.
+type: string,
The asset's type. This initially corresponds to the source file extension, but it may be changed during transformation.
+query: URLSearchParams,
The transformer options for the asset from the dependency query string.
+env: Environment,
The environment of the asset.
+isSource: boolean,
Whether this asset is part of the project, and not an external dependency (e.g. in node_modules). This indicates that transformation using the project's configuration should be applied.
+meta: Meta,
Plugin-specific metadata for the asset.
+bundleBehavior: ?BundleBehavior,
Controls which bundle the asset is placed into.
- inline: The asset will be placed into a new inline bundle. Inline bundles are not written to a separate file, but embedded into the parent bundle.
- isolated: The asset will be isolated from its parents in a separate bundle. Shared assets will be duplicated.
+isBundleSplittable: boolean,
If the asset is used as a bundle entry, this controls whether that bundle can be split into multiple, or whether all of the dependencies must be placed in a single bundle.
+sideEffects: boolean,
Whether this asset can be omitted if none of its exports are being used. This is initially set by the resolver, but can be overridden by transformers.
+uniqueKey: ?string,
When a transformer returns multiple assets, it can give them unique keys to identify them. This can be used to find assets during packaging, or to create dependencies between multiple assets returned by a transformer by using the unique key as the dependency specifier.
+astGenerator: ?ASTGenerator,
The type of the AST.
+pipeline: ?string,
The pipeline defined in .parcelrc that the asset should be processed with.
+symbols: AssetSymbols,
The symbols that the asset exports.
getAST(): Promise<?AST>,
Returns the current AST.
getCode(): Promise<string>,
Returns the asset contents as a string.
getBuffer(): Promise<Buffer>,
Returns the asset contents as a buffer.
getStream(): Readable,
Returns the asset contents as a stream.
getMap(): Promise<?SourceMap>,
Returns the source map for the asset, if available.
getMapBuffer(): Promise<?Buffer>,
Returns a buffer representation of the source map, if available.
getDependencies(): $ReadOnlyArray<Dependency>,
Returns a list of dependencies for the asset.
}
Referenced by:
Asset, MutableAsset, ResolveResultMutableAsset parcel/packages/core/types/index.js:725
A mutable Asset, available during transformation.
interface MutableAsset extends BaseAsset {
type: string,
The asset's type. This initially corresponds to the source file extension, but it may be changed during transformation.
bundleBehavior: ?BundleBehavior,
Controls which bundle the asset is placed into.
- inline: The asset will be placed into a new inline bundle. Inline bundles are not written to a separate file, but embedded into the parent bundle.
- isolated: The asset will be isolated from its parents in a separate bundle. Shared assets will be duplicated.
isBundleSplittable: boolean,
If the asset is used as a bundle entry, this controls whether that bundle can be split into multiple, or whether all of the dependencies must be placed in a single bundle.
Default value:
sideEffects: boolean,
Whether this asset can be omitted if none of its exports are being used. This is initially set by the resolver, but can be overridden by transformers.
+symbols: MutableAssetSymbols,
The symbols that the asset exports.
addDependency(DependencyOptions): string,
Adds a dependency to the asset.
addURLDependency(url: string, opts: $Shape<DependencyOptions>): string,
Adds a url dependency to the asset. This is a shortcut for addDependency that sets the specifierType to 'url' and priority to 'lazy'.
invalidateOnFileChange(FilePath): void,
Invalidates the transformation when the given file is modified or deleted.
invalidateOnFileCreate(FileCreateInvalidation): void,
Invalidates the transformation when matched files are created.
invalidateOnEnvChange(string): void,
Invalidates the transformation when the given environment variable changes.
setCode(string): void,
Sets the asset contents as a string.
setBuffer(Buffer): void,
Sets the asset contents as a buffer.
setStream(Readable): void,
Sets the asset contents as a stream.
setAST(AST): void,
Sets the asset's AST.
isASTDirty(): boolean,
Returns whether the AST has been modified.
setMap(?SourceMap): void,
Sets the asset's source map.
setEnvironment(opts: EnvironmentOptions): void,
}
Referenced by:
TransformerAsset parcel/packages/core/types/index.js:785
An immutable Asset, available after transformation.
interface Asset extends BaseAsset {
+stats: Stats,
Statistics about the asset.
}
Referenced by:
BuildSuccessEvent, Bundle, BundleGraph, BundleGraphTraversable, BundleTraversable, BundledProgressEvent, CreateBundleOpts, DedicatedThreadValidator, MultiThreadValidator, MutableAsset, MutableBundleGraph, SymbolResolution, Transformer, TransformingProgressEventConfig parcel/packages/core/types/index.js:809
interface Config {
+isSource: boolean,
Whether this config is part of the project, and not an external dependency (e.g. in node_modules). This indicates that transformation using the project's configuration should be applied.
+searchPath: FilePath,
The path of the file to start searching for config from.
+env: Environment,
The environment
invalidateOnFileChange(FilePath): void,
Invalidates the config when the given file is modified or deleted.
invalidateOnFileCreate(FileCreateInvalidation): void,
Invalidates the config when matched files are created.
invalidateOnEnvChange(string): void,
Invalidates the config when the given environment variable changes.
invalidateOnStartup(): void,
Invalidates the config only when Parcel restarts.
invalidateOnBuild(): void,
Invalidates the config on every build.
addDevDependency(DevDepOptions): void,
Adds a dev dependency to the config. If the dev dependency or any of its dependencies change, the config will be invalidated.
setCacheKey(string): void,
Sets the cache key for the config. By default, this is computed as a hash of the files passed to invalidateOnFileChange or loaded by getConfig. If none, then a hash of the result returned from loadConfig is used. This method can be used to override this behavior and explicitly control the cache key. This can be useful in cases where only part of a file is used to avoid unnecessary invalidations, or when the result is not hashable (i.e. contains non-serializable properties like functions).
getConfig<T>(filePaths: Array<FilePath>, options?: {|
packageKey?: string,
parse?: boolean,
exclude?: boolean,
|}): Promise<?ConfigResultWithFilePath<T>>,
Searches for config files with the given names in all parent directories of the config's searchPath.
getConfigFrom<T>(searchPath: FilePath, filePaths: Array<FilePath>, options?: {|
packageKey?: string,
parse?: boolean,
exclude?: boolean,
|}): Promise<?ConfigResultWithFilePath<T>>,
Searches for config files with the given names in all parent directories of the passed searchPath.
getPackage(): Promise<?PackageJSON>,
Finds the nearest package.json from the config's searchPath.
}
Referenced by:
Bundler, Namer, Optimizer, Packager, Resolver, Runtime, TransformerGenerateOutput parcel/packages/core/types/index.js:882
type GenerateOutput = {|
+content: Blob,
+map?: ?SourceMap,
|}
Referenced by:
TransformerTransformerResult parcel/packages/core/types/index.js:896
Transformers can return multiple result objects to create new assets.
For example, a file may contain multiple parts of different types,
which should be processed by their respective transformation pipelines.
type TransformerResult = {|
+type: string,
The asset's type.
+content?: ?Blob,
The content of the asset. Either content or an AST is required.
+ast?: ?AST,
+map?: ?SourceMap,
The source map for the asset.
+dependencies?: $ReadOnlyArray<DependencyOptions>,
The dependencies of the asset.
+env?: EnvironmentOptions | Environment,
The environment of the asset. The options are merged with the input asset's environment.
+bundleBehavior?: ?BundleBehavior,
Controls which bundle the asset is placed into.
- inline: The asset will be placed into a new inline bundle. Inline bundles are not written to a separate file, but embedded into the parent bundle.
- isolated: The asset will be isolated from its parents in a separate bundle. Shared assets will be duplicated.
+isBundleSplittable?: boolean,
If the asset is used as a bundle entry, this controls whether that bundle can be split into multiple, or whether all of the dependencies must be placed in a single bundle.
+meta?: Meta,
Plugin-specific metadata for the asset.
+pipeline?: ?string,
The pipeline defined in .parcelrc that the asset should be processed with.
+sideEffects?: boolean,
Whether this asset can be omitted if none of its exports are being used. This is initially set by the resolver, but can be overridden by transformers.
+symbols?: $ReadOnlyMap<Symbol, {|
local: Symbol,
loc: ?SourceLocation,
|}>,
The symbols that the asset exports.
+uniqueKey?: ?string,
When a transformer returns multiple assets, it can give them unique keys to identify them. This can be used to find assets during packaging, or to create dependencies between multiple assets returned by a transformer by using the unique key as the dependency specifier.
|}
Referenced by:
TransformerResolveOptions parcel/packages/core/types/index.js:976
type ResolveOptions = {|
+specifierType?: SpecifierType,
How the specifier should be interpreted.
- esm: An ES module specifier. It is parsed as a URL, but bare specifiers are treated as node_modules.
- commonjs: A CommonJS specifier. It is not parsed as a URL.
- url: A URL that works as in a browser. Bare specifiers are treated as relative URLs.
- custom: A custom specifier. Must be handled by a custom resolver plugin.
+packageConditions?: Array<string>,
A list of custom conditions to use when resolving package.json "exports" and "imports".
|}
Referenced by:
ResolveFnResolveFn parcel/packages/core/types/index.js:992
Type
type ResolveFn = (from: FilePath, to: string, options?: ResolveOptions) => Promise<FilePath>;
Referenced by:
TransformerTransformer parcel/packages/core/types/index.js:1063
The methods for a transformer plugin.
type Transformer<ConfigType> = {|
loadConfig?: ({|
config: Config,
options: PluginOptions,
logger: PluginLogger,
|}) => Promise<ConfigType> | ConfigType,
canReuseAST?: ({|
ast: AST,
options: PluginOptions,
logger: PluginLogger,
|}) => boolean,
Whether an AST from a previous transformer can be reused (to prevent double-parsing)
parse?: ({|
asset: Asset,
config: ConfigType,
resolve: ResolveFn,
options: PluginOptions,
logger: PluginLogger,
|}) => Async<?AST>,
Parse the contents into an ast
transform({|
asset: MutableAsset,
config: ConfigType,
resolve: ResolveFn,
options: PluginOptions,
logger: PluginLogger,
|}): Async<Array<TransformerResult | MutableAsset>>,
Transform the asset and/or add new assets
postProcess?: ({|
assets: Array<MutableAsset>,
config: ConfigType,
resolve: ResolveFn,
options: PluginOptions,
logger: PluginLogger,
|}) => Async<Array<TransformerResult>>,
Do some processing after the transformation
generate?: ({|
asset: Asset,
ast: AST,
options: PluginOptions,
logger: PluginLogger,
|}) => Async<GenerateOutput>,
Stringify the AST
|}