How to use @parcel/workers - 10 common examples

To help you get started, we’ve selected a few @parcel/workers 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 / core / src / ReporterRunner.js View on Github external
export function report(event: ReporterEvent) {
  if (event.bundle == null) {
    bus.emit('reporterEvent', event);
  } else {
    // Convert any public api bundles to their internal equivalents for
    // easy serialization
    bus.emit('reporterEvent', {
      ...event,
      bundle: bundleToInternalBundle(event.bundle),
    });
  }
}
github parcel-bundler / parcel / packages / core / core / src / ReporterRunner.js View on Github external
export function report(event: ReporterEvent) {
  if (event.bundle == null) {
    bus.emit('reporterEvent', event);
  } else {
    // Convert any public api bundles to their internal equivalents for
    // easy serialization
    bus.emit('reporterEvent', {
      ...event,
      bundle: bundleToInternalBundle(event.bundle),
    });
  }
}
github parcel-bundler / parcel / src / core / core / src / Bundler.js View on Github external
if (this.options.watch) {
      this.watcher = new Watcher();
      // Wait for ready event for reliable testing on watcher
      if (process.env.NODE_ENV === 'test' && !this.watcher.ready) {
        await new Promise(resolve => this.watcher.once('ready', resolve));
      }
      this.watcher.on('change', this.onChange.bind(this));
    }

    if (this.options.hmr) {
      this.hmr = new HMRServer();
      this.options.hmrPort = await this.hmr.start(this.options);
    }

    this.farm = WorkerFarm.getShared(this.options, {
      workerPath: require.resolve('./worker')
    });
  }
github parcel-bundler / parcel / packages / core / fs / src / MemoryFS.js View on Github external
constructor(id: number, handleFn: HandleFunction) {
    // TODO Make this not a subclass
    // $FlowFixMe
    super();
    this.id = id;
    this.handleFn = handleFn;

    handleFn('_registerWorker', [
      WorkerFarm.getWorkerApi().createReverseHandle(event => {
        switch (event.type) {
          case 'writeFile':
            this.files.set(event.path, event.entry);
            break;
          case 'unlink':
            this.files.delete(event.path);
            this.dirs.delete(event.path);
            this.symlinks.delete(event.path);
            break;
          case 'mkdir':
            this.dirs.set(event.path, new Directory());
            break;
          case 'symlink':
            this.symlinks.set(event.path, event.target);
            break;
        }
github parcel-bundler / parcel / src / core / logger / src / Logger.js View on Github external
if (align === 'right') {
    return pad + text;
  }

  return text + pad;
}

// Count visible characters in a string
function stringWidth(string) {
  return countBreaks(stripAnsi('' + string));
}

// If we are in a worker, make a proxy class which will
// send the logger calls to the main process via IPC.
// These are handled in WorkerFarm and directed to handleMessage above.
if (WorkerFarm.isWorker()) {
  class LoggerProxy {}
  for (let method of Object.getOwnPropertyNames(Logger.prototype)) {
    LoggerProxy.prototype[method] = (...args) => {
      WorkerFarm.callMaster(
        {
          location: __filename,
          method,
          args
        },
        false
      );
    };
  }

  module.exports = new LoggerProxy();
} else {
github parcel-bundler / parcel / packages / core / core / src / ReporterRunner.js View on Github external
constructor(opts: Opts) {
    this.config = opts.config;
    this.options = opts.options;
    this.pluginOptions = new PluginOptions(this.options);

    logger.onLog(event => this.report(event));

    // Convert any internal bundles back to their public equivalents as reporting
    // is public api
    bus.on('reporterEvent', event => {
      if (event.bundle == null) {
        this.report(event);
      } else {
        this.report({
          ...event,
          bundle: new NamedBundle(
            event.bundle,
            event.bundleGraph,
            this.options,
          ),
        });
      }
    });

    if (this.options.patchConsole) {
      patchConsole();
github parcel-bundler / parcel / packages / core / parcel-bundler / src / utils / installPackage.js View on Github external
module.exports = async function(...args) {
  // Ensure that this function is always called on the master process so we
  // don't call multiple installs in parallel.
  if (WorkerFarm.isWorker()) {
    await WorkerFarm.callMaster({
      location: __filename,
      args,
    });
    return;
  }

  queue.add(...args);
  return queue.run();
};
github parcel-bundler / parcel / packages / core / parcel-bundler / src / utils / installPackage.js View on Github external
module.exports = async function(...args) {
  // Ensure that this function is always called on the master process so we
  // don't call multiple installs in parallel.
  if (WorkerFarm.isWorker()) {
    await WorkerFarm.callMaster({
      location: __filename,
      args,
    });
    return;
  }

  queue.add(...args);
  return queue.run();
};
github parcel-bundler / parcel / packages / core / core / src / Parcel.js View on Github external
export function createWorkerFarm(options: $Shape = {}) {
  return new WorkerFarm({
    ...options,
    workerPath: require.resolve('./worker'),
  });
}
github parcel-bundler / parcel / packages / core / parcel-bundler / src / Bundler.js View on Github external
const fs = require('@parcel/fs');
const Resolver = require('./Resolver');
const Parser = require('./Parser');
const WorkerFarm = require('@parcel/workers').default;
const Path = require('path');
const Bundle = require('./Bundle');
const Watcher = require('@parcel/watcher').default;
const FSCache = require('./FSCache');
const HMRServer = require('./HMRServer');
const Server = require('./Server');
const {EventEmitter} = require('events');
const logger = require('@parcel/logger');
const PackagerRegistry = require('./packagers');
const localRequire = require('./utils/localRequire');
const config = require('./utils/config');
const loadEnv = require('./utils/env');
const PromiseQueue = require('./utils/PromiseQueue');
const installPackage = require('./utils/installPackage');
const bundleReport = require('./utils/bundleReport');
const prettifyTime = require('./utils/prettifyTime');