Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function installPeerDependencies(
fs: FileSystem,
filepath: FilePath,
name: string,
options,
) {
let basedir = path.dirname(filepath);
const {resolved} = await resolve(fs, name, {basedir});
const pkg = nullthrows(await loadConfig(fs, resolved, ['package.json']))
.config;
const peers = pkg.peerDependencies || {};
const modules = [];
for (const peer in peers) {
modules.push(`${peer}@${peers[peer]}`);
}
if (modules.length) {
await install(
fs,
modules,
filepath,
Object.assign({}, options, {installPeers: false}),
);
}
async resolvePackageTargets(rootDir: FilePath) {
let conf = await loadConfig(this.fs, path.join(rootDir, 'index'), [
'package.json',
]);
let pkg;
let pkgContents;
let pkgFilePath: ?FilePath;
let pkgDir: FilePath;
let pkgMap;
if (conf) {
pkg = (conf.config: PackageJSON);
let pkgFile = conf.files[0];
if (pkgFile == null) {
throw new ThrowableDiagnostic({
diagnostic: {
message: `Expected package.json file in ${rootDir}`,
origin: '@parcel/core',
async optimize({contents, map, bundle, options}) {
if (!options.minify) {
return {contents, map};
}
if (typeof contents !== 'string') {
throw new Error(
'TerserOptimizer: Only string contents are currently supported',
);
}
let userConfig = await loadConfig(
options.inputFS,
path.join(options.projectRoot, 'index'),
['.terserrc', '.uglifyrc', '.uglifyrc.js', '.terserrc.js'],
);
let config = {
warnings: true,
...userConfig?.config,
sourceMap: {
filename: path.relative(options.projectRoot, bundle.filePath),
},
module: bundle.env.outputFormat === 'esmodule',
};
let sourceMap = null;
if (options.sourceMaps) {
async getConfigFrom(
searchPath: FilePath,
filePaths: Array,
options: ?{|
packageKey?: string,
parse?: boolean,
exclude?: boolean,
|},
): Promise {
let parse = options && options.parse;
let conf = await loadConfig(
this.#options.inputFS,
searchPath,
filePaths,
parse == null ? null : {parse},
);
if (conf == null) {
return null;
}
if (!options || !options.exclude) {
if (this.#config.resolvedPath == null) {
this.setResolvedPath(conf.files[0].filePath);
} else {
this.addIncludedFile(conf.files[0].filePath);
}
}
options: ?{|
packageKey?: string,
parse?: boolean,
|},
): Promise {
let packageKey = options?.packageKey;
let parse = options && options.parse;
if (packageKey != null) {
let pkg = await this.getPackage();
if (pkg && pkg[packageKey]) {
return pkg[packageKey];
}
}
let conf = await loadConfig(
this.options.inputFS,
this.value.filePath,
filePaths,
parse == null ? null : {parse},
);
if (!conf) {
return null;
}
for (let file of conf.files) {
this.addIncludedFile(file);
}
return conf.config;
}
async optimize({contents, map, options}) {
if (!options.minify) {
return {contents, map};
}
if (typeof contents !== 'string') {
throw new Error(
'HTMLNanoOptimizer: Only string contents are currently supported',
);
}
let userConfig = await loadConfig(
options.inputFS,
path.join(options.rootDir, 'index.html'),
['.htmlnanorc', '.htmlnanorc.js'],
);
const htmlNanoConfig = {
minifyJs: false,
...userConfig?.config,
};
return {
contents: (await posthtml([htmlnano(htmlNanoConfig)]).process(contents))
.html,
};
},
});