How to use @parcel/utils - 10 common examples

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 / transformers / html / src / inline.js View on Github external
new PostHTML().walk.call(program, (node: PostHTMLNode) => {
    let parcelKey = md5FromString(`${asset.id}:${key++}`);
    if (node.tag === 'script' || node.tag === 'style') {
      let value = node.content && node.content.join('').trim();
      if (value != null) {
        let type;

        if (node.tag === 'style') {
          if (node.attrs && node.attrs.type) {
            type = node.attrs.type.split('/')[1];
          } else {
            type = 'css';
          }
        } else if (node.attrs && node.attrs.type) {
          // Skip JSON
          if (SCRIPT_TYPES[node.attrs.type] === false) {
            return node;
          }
github parcel-bundler / parcel / packages / resolvers / default / src / DefaultResolver.js View on Github external
lookupAlias(aliases, filename, dir) {
    // First, try looking up the exact filename
    let alias = aliases[filename];
    if (alias == null) {
      // Otherwise, try replacing glob keys
      for (let key in aliases) {
        let val = aliases[key];
        if (typeof val === 'string' && isGlob(key)) {
          let re = micromatch.makeRe(key, {capture: true});
          if (re.test(filename)) {
            alias = filename.replace(re, val);
            break;
          }
        }
      }
    }

    if (typeof alias === 'string') {
      return this.resolveFilename(alias, dir);
    } else if (alias === false) {
      return false;
    }

    return null;
github parcel-bundler / parcel / packages / core / workers / src / process / ProcessWorker.js View on Github external
send(data: WorkerMessage) {
    if (!this.processQueue) {
      this.sendQueue.push(data);
      return;
    }

    let result = this.child.send(serialize(data).toString('base64'), error => {
      if (error && error instanceof Error) {
        // Ignore this, the workerfarm handles child errors
        return;
      }

      this.processQueue = true;

      if (this.sendQueue.length > 0) {
        let queueCopy = this.sendQueue.slice(0);
        this.sendQueue = [];
        queueCopy.forEach(entry => this.send(entry));
      }
    });

    if (!result || /^win/.test(process.platform)) {
      // Queue is handling too much messages throttle it
github parcel-bundler / parcel / packages / core / register / src / hook.js View on Github external
}
  }

  let parcel = new Parcel({
    entries: [path.join(process.cwd(), 'index.js')],
    cliOpts: opts
  });

  let environment = new Environment({
    context: 'node',
    engines: {
      node: process.versions.node
    }
  });

  syncPromise(parcel.init());

  let isProcessing = false;

  // As Parcel is pretty much fully asynchronous, create an async function and wrap it in a syncPromise later...
  async function fileProcessor(code, filename) {
    if (isProcessing) {
      return code;
    }

    try {
      isProcessing = true;
      let result = await parcel.runTransform({
        filePath: filename,
        env: environment
      });
github parcel-bundler / parcel / packages / core / core / src / ConfigLoader.js View on Github external
let devDeps = [];
    switch (configRequest.meta.actionType) {
      case 'transformation':
        devDeps = parcelConfig.getTransformerNames(filePath, pipeline);
        break;
      case 'validation':
        devDeps = parcelConfig.getValidatorNames(filePath);
        break;
      case 'dependency':
        devDeps = parcelConfig.getResolverNames();
        break;
    }
    devDeps.forEach(devDep => publicConfig.addDevDependency(devDep));

    publicConfig.setResultHash(md5FromString(JSON.stringify(devDeps)));

    publicConfig.setWatchGlob('**/.parcelrc');

    // TODO: if extended config comes from a package, yarn.lock change should invalidate config request
    for (let extendedFile of extendedFiles) {
      publicConfig.addIncludedFile(extendedFile);
    }

    return config;
  }
github parcel-bundler / parcel / packages / core / core / src / PackagerRunner.js View on Github external
let optimizers = this.config.getOptimizerNames(filePath);
    let deps = Promise.all(
      [packager, ...optimizers].map(async pkg => {
        let {pkg: resolvedPkg} = await this.options.packageManager.resolve(
          `${pkg}/package.json`,
          `${this.config.filePath}/index`,
        );

        let version = nullthrows(resolvedPkg).version;
        return [pkg, version];
      }),
    );

    // TODO: add third party configs to the cache key
    let {minify, scopeHoist, sourceMaps} = this.options;
    return md5FromObject({
      parcelVersion: PARCEL_VERSION,
      deps,
      opts: {minify, scopeHoist, sourceMaps},
      hash: bundleGraph.getHash(bundle),
    });
  }
github parcel-bundler / parcel / packages / core / core / src / Environment.js View on Github external
export function getEnvironmentHash(env: Environment) {
  // context is excluded from hash so that assets can be shared between e.g. workers and browser.
  // Different engines should be sufficient to distinguish multi-target builds.
  return md5FromObject({
    engines: env.engines,
    includeNodeModules: env.includeNodeModules,
    outputFormat: env.outputFormat,
    isLibrary: env.isLibrary,
  });
}
github parcel-bundler / parcel / packages / core / core / src / BundlerRunner.js View on Github external
async getCacheKey(assetGraph: AssetGraph) {
    let bundler = this.config.bundler;
    let {pkg} = await this.options.packageManager.resolve(
      `${bundler}/package.json`,
      `${this.config.filePath}/index`, // TODO: is this right?
    );

    let version = nullthrows(pkg).version;
    return md5FromObject({
      parcelVersion: PARCEL_VERSION,
      bundler,
      version,
      hash: assetGraph.getHash(),
    });
  }
github parcel-bundler / parcel / packages / core / fs / src / fs.js View on Github external
const {promisify} = require('@parcel/utils');
const fs = require('fs');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');

exports.readFile = promisify(fs.readFile);
exports.writeFile = promisify(fs.writeFile);
exports.stat = promisify(fs.stat);
exports.readdir = promisify(fs.readdir);
exports.unlink = promisify(fs.unlink);
exports.rimraf = promisify(rimraf);
exports.realpath = async function(path) {
  const realpath = promisify(fs.realpath);
  try {
    path = await realpath(path);
  } catch (e) {
    // do nothing
  }
  return path;
};
exports.lstat = promisify(fs.lstat);

exports.exists = function(filename) {
  return new Promise(resolve => {
    fs.exists(filename, resolve);
github parcel-bundler / parcel / packages / core / fs / src / fs.js View on Github external
exports.realpath = async function(path) {
  const realpath = promisify(fs.realpath);
  try {
    path = await realpath(path);
  } catch (e) {
    // do nothing
  }
  return path;
};
exports.lstat = promisify(fs.lstat);