-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expose transform and resolveapi from parcel #9193
Changes from 1 commit
c008f5f
b8739e2
17f1c27
cbd76ee
6bdbd54
2938b37
494414f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
// @flow strict-local | ||
|
||
import type { | ||
Asset, | ||
AsyncSubscription, | ||
BuildEvent, | ||
BuildSuccessEvent, | ||
InitialParcelOptions, | ||
PackagedBundle as IPackagedBundle, | ||
ParcelTransformOptions, | ||
ParcelResolveOptions, | ||
ParcelResolveResult, | ||
} from '@parcel/types'; | ||
import path from 'path'; | ||
import type {ParcelOptions} from './types'; | ||
// eslint-disable-next-line no-unused-vars | ||
import type {FarmOptions, SharedReference} from '@parcel/workers'; | ||
|
@@ -37,10 +42,18 @@ import RequestTracker, { | |
} from './RequestTracker'; | ||
import createValidationRequest from './requests/ValidationRequest'; | ||
import createParcelBuildRequest from './requests/ParcelBuildRequest'; | ||
import createAssetRequest from './requests/AssetRequest'; | ||
import createPathRequest from './requests/PathRequest'; | ||
import {createEnvironment} from './Environment'; | ||
import {createDependency} from './Dependency'; | ||
import {Disposable} from '@parcel/events'; | ||
import {init as initSourcemaps} from '@parcel/source-map'; | ||
import {init as initHash} from '@parcel/hash'; | ||
import {toProjectPath} from './projectPath'; | ||
import { | ||
fromProjectPath, | ||
toProjectPath, | ||
fromProjectPathRelative, | ||
} from './projectPath'; | ||
import {tracer} from '@parcel/profiler'; | ||
|
||
registerCoreWithSerializer(); | ||
|
@@ -437,6 +450,78 @@ export default class Parcel { | |
logger.info({origin: '@parcel/core', message: 'Taking heap snapshot...'}); | ||
return this.#farm.takeHeapSnapshot(); | ||
} | ||
|
||
async transform(options: ParcelTransformOptions): Promise<Array<Asset>> { | ||
if (!this.#initialized) { | ||
await this._init(); | ||
} | ||
|
||
let projectRoot = nullthrows(this.#resolvedOptions).projectRoot; | ||
let request = createAssetRequest({ | ||
...options, | ||
filePath: toProjectPath(projectRoot, options.filePath), | ||
optionsRef: this.#optionsRef, | ||
env: createEnvironment({ | ||
...options.env, | ||
loc: | ||
options.env?.loc != null | ||
? { | ||
...options.env.loc, | ||
filePath: toProjectPath(projectRoot, options.env.loc.filePath), | ||
} | ||
: undefined, | ||
}), | ||
}); | ||
|
||
let res = await this.#requestTracker.runRequest(request, {force: true}); | ||
return res.map(asset => | ||
assetFromValue(asset, nullthrows(this.#resolvedOptions)), | ||
); | ||
} | ||
|
||
async resolve(request: ParcelResolveOptions): Promise<?ParcelResolveResult> { | ||
if (!this.#initialized) { | ||
await this._init(); | ||
} | ||
|
||
let projectRoot = nullthrows(this.#resolvedOptions).projectRoot; | ||
if (request.resolveFrom == null && path.isAbsolute(request.specifier)) { | ||
request.specifier = fromProjectPathRelative( | ||
toProjectPath(projectRoot, request.specifier), | ||
); | ||
} | ||
|
||
let dependency = createDependency(projectRoot, { | ||
...request, | ||
env: createEnvironment({ | ||
...request.env, | ||
loc: | ||
request.env?.loc != null | ||
? { | ||
...request.env.loc, | ||
filePath: toProjectPath(projectRoot, request.env.loc.filePath), | ||
} | ||
: undefined, | ||
}), | ||
}); | ||
|
||
let req = createPathRequest({ | ||
dependency, | ||
name: 'test', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what the purpose of this @devongovett Any input on what this should be? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed it to The name seem to be used to construct the Tho, change to specifier should be a lil bit more meaningful. |
||
}); | ||
|
||
let res = await this.#requestTracker.runRequest(req, {force: true}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here about |
||
if (!res) { | ||
return null; | ||
} | ||
|
||
return { | ||
filePath: fromProjectPath(projectRoot, res.filePath), | ||
code: res.code, | ||
query: res.query, | ||
sideEffects: res.sideEffects, | ||
}; | ||
} | ||
} | ||
|
||
export class BuildError extends ThrowableDiagnostic { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'test'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as idx from './index.js'; | ||
new URL('index.js', import.meta.url); | ||
import('index.js') |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -645,6 +645,28 @@ export type ASTGenerator = {| | |
|
||
export type BundleBehavior = 'inline' | 'isolated'; | ||
|
||
export type ParcelTransformOptions = {| | ||
filePath: FilePath, | ||
code?: string, | ||
env?: EnvironmentOptions, | ||
pipeline?: ?string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if pipeline makes sense as a public option? Shouldn't this be inferred from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am happy to remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed. |
||
query?: ?string, | ||
|}; | ||
|
||
export type ParcelResolveOptions = {| | ||
specifier: DependencySpecifier, | ||
specifierType: SpecifierType, | ||
env?: EnvironmentOptions, | ||
resolveFrom?: FilePath, | ||
|}; | ||
|
||
export type ParcelResolveResult = {| | ||
filePath: FilePath, | ||
code?: string, | ||
query?: ?string, | ||
sideEffects?: boolean, | ||
|}; | ||
|
||
/** | ||
* 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -442,6 +442,7 @@ export default (new Transformer({ | |
is_esm_output: asset.env.outputFormat === 'esmodule', | ||
trace_bailouts: options.logLevel === 'verbose', | ||
is_swc_helpers: /@swc[/\\]helpers/.test(asset.filePath), | ||
standalone: asset.query.has('standalone'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about using a query param to drive this behaviour. Maybe this should be part of the environment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did think about it, for now using query provides some flexibility & less moving part to change the environment. Kinda like a feature flag for |
||
}); | ||
|
||
let convertLoc = (loc): SourceLocation => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use
force
here? My understanding is thatforce
means the cache is ignored.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can update that, but it was originally copied from the intiial branch, I will leave this Q to @devongovett on if this is required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just changed it to a configurable value. Follows the config of
shouldCacheDisabled
from the main config.I guess this could be helpful to debug transform problems.