How to use the @parcel/fs.NodeFS function in @parcel/fs

To help you get started, we’ve selected a few @parcel/fs 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 / test-utils / src / utils.js View on Github external
import Parcel, {createWorkerFarm} from '@parcel/core';
import defaultConfigContents from '@parcel/config-default';
import assert from 'assert';
import vm from 'vm';
import {NodeFS, MemoryFS, OverlayFS, ncp as _ncp} from '@parcel/fs';
import path from 'path';
import WebSocket from 'ws';
import nullthrows from 'nullthrows';

import {makeDeferredWithPromise} from '@parcel/utils';
import _chalk from 'chalk';
import resolve from 'resolve';
import {NodePackageManager} from '@parcel/package-manager';

const workerFarm = createWorkerFarm();
export const inputFS = new NodeFS();
export let outputFS = new MemoryFS(workerFarm);
export let overlayFS = new OverlayFS(outputFS, inputFS);

beforeEach(() => {
  outputFS = new MemoryFS(workerFarm);
  overlayFS = new OverlayFS(outputFS, inputFS);
});

// Recursively copies a directory from the inputFS to the outputFS
export async function ncp(source: FilePath, destination: FilePath) {
  await _ncp(inputFS, source, outputFS, destination);
}

// Mocha is currently run with exit: true because of this issue preventing us
// from properly ending the workerfarm after the test run:
// https://github.com/nodejs/node/pull/28788
github parcel-bundler / parcel / packages / core / local-require / src / localRequire.js View on Github external
// @flow strict-local

import type {FilePath, PackageJSON} from '@parcel/types';
import type {FileSystem} from '@parcel/fs';

import installPackage from '@parcel/install-package';
import {dirname} from 'path';
import {resolve} from '@parcel/utils';
import {NodeFS} from '@parcel/fs';

const cache: Map = new Map();
const nodeFS = new NodeFS();

export default async function localRequire(
  name: string,
  path: FilePath,
  triedInstall: boolean = false
  // $FlowFixMe this must be dynamic
): Promise {
  let [resolved] = await localResolve(name, path, nodeFS, triedInstall);
  // $FlowFixMe this must be dynamic
  return require(resolved);
}

export async function localResolve(
  name: string,
  path: FilePath,
  fs: FileSystem = nodeFS,
github parcel-bundler / parcel / packages / core / core / src / resolveOptions.js View on Github external
export default async function resolveOptions(
  initialOptions: InitialParcelOptions
): Promise {
  let entries: Array;
  if (initialOptions.entries == null || initialOptions.entries === '') {
    entries = [];
  } else if (Array.isArray(initialOptions.entries)) {
    entries = initialOptions.entries.map(entry => path.resolve(entry));
  } else {
    entries = [path.resolve(initialOptions.entries)];
  }

  let inputFS = initialOptions.inputFS || new NodeFS();
  let outputFS = initialOptions.outputFS || new NodeFS();

  let packageManager =
    initialOptions.packageManager || new NodePackageManager(inputFS);

  let rootDir =
    initialOptions.rootDir != null
      ? path.resolve(initialOptions.rootDir)
      : getRootDir(entries);

  let projectRootFile =
    (await resolveConfig(inputFS, path.join(rootDir, 'index'), [
      ...LOCK_FILE_NAMES,
      '.git',
      '.hg'
    ])) || path.join(inputFS.cwd(), 'index'); // ? Should this just be rootDir
github parcel-bundler / parcel / packages / core / core / src / resolveOptions.js View on Github external
export default async function resolveOptions(
  initialOptions: InitialParcelOptions
): Promise {
  let entries: Array;
  if (initialOptions.entries == null || initialOptions.entries === '') {
    entries = [];
  } else if (Array.isArray(initialOptions.entries)) {
    entries = initialOptions.entries.map(entry => path.resolve(entry));
  } else {
    entries = [path.resolve(initialOptions.entries)];
  }

  let inputFS = initialOptions.inputFS || new NodeFS();
  let outputFS = initialOptions.outputFS || new NodeFS();

  let packageManager =
    initialOptions.packageManager || new NodePackageManager(inputFS);

  let rootDir =
    initialOptions.rootDir != null
      ? path.resolve(initialOptions.rootDir)
      : getRootDir(entries);

  let projectRootFile =
    (await resolveConfig(inputFS, path.join(rootDir, 'index'), [
      ...LOCK_FILE_NAMES,
      '.git',
      '.hg'
    ])) || path.join(inputFS.cwd(), 'index'); // ? Should this just be rootDir
github parcel-bundler / parcel / packages / core / register / src / register.js View on Github external
import {NodePackageManager} from '@parcel/package-manager';
import {NodeFS} from '@parcel/fs';
// $FlowFixMe this is untyped
import defaultConfigContents from '@parcel/config-default';
// $FlowFixMe this is untyped
import Module from 'module';
import path from 'path';
// $FlowFixMe this is untyped
import {addHook} from 'pirates';
import Parcel, {INTERNAL_RESOLVE, INTERNAL_TRANSFORM} from '@parcel/core';

import syncPromise from './syncPromise';

let hooks = {};
let lastDisposable;
let packageManager = new NodePackageManager(new NodeFS());
let defaultConfig = {
  ...defaultConfigContents,
  filePath: packageManager.resolveSync('@parcel/config-default', __filename)
    .resolved,
};

function register(inputOpts?: InitialParcelOptions): IDisposable {
  let opts: InitialParcelOptions = {
    ...defaultConfig,
    ...(inputOpts || {}),
  };

  // Replace old hook, as this one likely contains options.
  if (lastDisposable) {
    lastDisposable.dispose();
  }
github parcel-bundler / parcel / packages / transformers / sass / src / SassTransformer.js View on Github external
// @flow
import type {PluginLogger} from '@parcel/logger';

import {Transformer} from '@parcel/plugin';
import {promisify, resolve} from '@parcel/utils';
import {dirname} from 'path';
import {NodeFS} from '@parcel/fs';

// E.g: ~library/file.sass
const WEBPACK_ALIAS_RE = /^~[^/]/;
const fs = new NodeFS();

let didWarnAboutNodeSass = false;

async function warnAboutNodeSassBeingUnsupported(
  filePath,
  logger: PluginLogger,
) {
  if (!didWarnAboutNodeSass) {
    try {
      // TODO: replace this with the actual filesystem later
      await resolve(fs, 'node-sass', {basedir: dirname(filePath)});
      logger.warn({
        origin: '@parcel/transformer-sass',
        message:
          '`node-sass` is unsupported in Parcel 2, it will use Dart Sass a.k.a. `sass`',
      });
github parcel-bundler / parcel / packages / core / parcel / src / cli.js View on Github external
async function run(entries: Array, command: any) {
  entries = entries.map(entry => path.resolve(entry));

  if (entries.length === 0) {
    console.log('No entries found');
    return;
  }
  let Parcel = require('@parcel/core').default;
  let packageManager = new NodePackageManager(new NodeFS());
  let defaultConfig: ParcelConfigFile = await packageManager.require(
    '@parcel/config-default',
    __filename
  );
  let parcel = new Parcel({
    entries,
    packageManager,
    defaultConfig: {
      ...defaultConfig,
      filePath: (await packageManager.resolve(
        '@parcel/config-default',
        __filename
      )).resolved
    },
    patchConsole: true,
    ...(await normalizeOptions(command))