-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d689a7
commit 3d65e4e
Showing
26 changed files
with
1,678 additions
and
949 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// @flow strict-local | ||
import nullthrows from 'nullthrows'; | ||
import type {AssetRequest, ParcelOptions} from '@parcel/types'; | ||
|
||
import path from 'path'; | ||
import Cache from '@parcel/cache'; | ||
import {resolveConfig} from '@parcel/utils'; | ||
|
||
import type Config from './public/Config'; | ||
import {report} from './ReporterRunner'; | ||
import InternalAsset from './Asset'; | ||
import type {NodeId, ConfigRequest} from './types'; | ||
import {Asset} from './public/Asset'; | ||
import summarizeRequest from './summarizeRequest'; | ||
|
||
export type ValidationOpts = {| | ||
request: AssetRequest, | ||
loadConfig: (ConfigRequest, NodeId) => Promise<Config>, | ||
parentNodeId: NodeId, | ||
options: ParcelOptions | ||
|}; | ||
|
||
export default class Validation { | ||
request: AssetRequest; | ||
configRequests: Array<ConfigRequest>; | ||
loadConfig: ConfigRequest => Promise<Config>; | ||
options: ParcelOptions; | ||
cache: Cache; | ||
impactfulOptions: $Shape<ParcelOptions>; | ||
|
||
constructor({request, loadConfig, parentNodeId, options}: ValidationOpts) { | ||
this.request = request; | ||
this.configRequests = []; | ||
this.loadConfig = configRequest => { | ||
this.configRequests.push(configRequest); | ||
return loadConfig(configRequest, parentNodeId); | ||
}; | ||
this.options = options; | ||
} | ||
|
||
async run(): Promise<void> { | ||
if (this.request.filePath.includes('node_modules')) return; | ||
|
||
report({ | ||
type: 'validation', | ||
request: this.request | ||
}); | ||
|
||
this.cache = new Cache(this.options.outputFS, this.options.cacheDir); | ||
|
||
let asset = await this.loadAsset(); | ||
let configRequest = { | ||
filePath: this.request.filePath, | ||
meta: { | ||
actionType: 'validation' | ||
} | ||
}; | ||
|
||
let config = await this.loadConfig(configRequest); | ||
let parcelConfig = nullthrows(config.result); | ||
|
||
let validators = await parcelConfig.getValidators(this.request.filePath); | ||
for (let validator of validators) { | ||
await validator.validate({ | ||
asset: new Asset(asset), | ||
options: this.options, | ||
resolveConfig: (configNames: Array<string>) => | ||
resolveConfig(this.options.inputFS, asset.filePath, configNames) | ||
}); | ||
} | ||
} | ||
|
||
async loadAsset(): Promise<InternalAsset> { | ||
let {filePath, env, code, sideEffects} = this.request; | ||
let {content, size, hash} = await summarizeRequest( | ||
this.options.inputFS, | ||
this.request | ||
); | ||
|
||
return new InternalAsset({ | ||
// If the transformer request passed code rather than a filename, | ||
// use a hash as the base for the id to ensure it is unique. | ||
idBase: code != null ? hash : filePath, | ||
fs: this.options.inputFS, | ||
filePath: filePath, | ||
type: path.extname(filePath).slice(1), | ||
cache: this.cache, | ||
ast: null, | ||
content, | ||
hash, | ||
env: env, | ||
stats: { | ||
time: 0, | ||
size | ||
}, | ||
sideEffects: sideEffects | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.