How to use the @parcel/utils.loadConfig function in @parcel/utils

To help you get started, we’ve selected a few @parcel/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github parcel-bundler / parcel / packages / core / package-manager / src / installPackage.js View on Github external
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}),
    );
  }
github parcel-bundler / parcel / packages / core / core / src / TargetResolver.js View on Github external
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',
github parcel-bundler / parcel / packages / optimizers / terser / src / TerserOptimizer.js View on Github external
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) {
github parcel-bundler / parcel / packages / core / core / src / public / Config.js View on Github external
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);
      }
    }
github parcel-bundler / parcel / packages / core / core / src / InternalAsset.js View on Github external
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;
  }
github parcel-bundler / parcel / packages / optimizers / htmlnano / src / HTMLNanoOptimizer.js View on Github external
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,
    };
  },
});