Exemplo n.º 1
0
const Bundler = require('parcel-bundler');
const assert = require('assert');
const vm = require('vm');
const fs = require('@parcel/fs');
const nodeFS = require('fs');
const path = require('path');
const WebSocket = require('ws');
const Module = require('module');

const {promisify} = require('@parcel/utils');
//const {sleep} = require('@parcel/test-utils');
const rimraf = promisify(require('rimraf'));
const ncp = promisify(require('ncp'));

const chalk = new (require('chalk')).constructor({enabled: true});
const warning = chalk.keyword('orange');
// eslint-disable-next-line no-console
console.warn = (...args) => {
  // eslint-disable-next-line no-console
  console.error(warning(...args));
};

async function removeDistDirectory(count = 0) {
  try {
    await rimraf(path.join(process.cwd(), 'test/dist'));
  } catch (e) {
    if (count > 8) {
      // eslint-disable-next-line no-console
      console.warn('WARNING: Unable to remove dist directory:', e.message);
      return;
    }
Exemplo n.º 2
0
  async parse(code) {
    // node-sass or dart-sass should be installed locally in the module that's being required
    let sass = await getSassRuntime(this.name);
    let render = promisify(sass.render.bind(sass));
    const resolver = new Resolver({
      extensions: ['.scss', '.sass'],
      rootDir: this.options.rootDir
    });

    let opts =
      (await this.getConfig(['.sassrc', '.sassrc.js'], {packageKey: 'sass'})) ||
      {};
    opts.includePaths = (opts.includePaths
      ? opts.includePaths.map(includePath => path.resolve(includePath))
      : []
    ).concat(path.dirname(this.name));
    opts.data = opts.data ? opts.data + os.EOL + code : code;
    let type = this.options.rendition
      ? this.options.rendition.type
      : path
          .extname(this.name)
          .toLowerCase()
          .replace('.', '');
    opts.indentedSyntax =
      typeof opts.indentedSyntax === 'boolean'
        ? opts.indentedSyntax
        : type === 'sass';

    opts.importer = opts.importer || [];
    opts.importer = Array.isArray(opts.importer)
      ? opts.importer
      : [opts.importer];
    opts.importer.push((url, prev, done) => {
      url = url.replace(/^file:\/\//, '');
      url = parseCSSImport(url);
      resolver
        .resolve(url, prev === 'stdin' ? this.name : prev)
        .then(resolved => resolved.path)
        .catch(() => url)
        .then(file => done({file}))
        .catch(err => done(normalizeError(err)));
    });

    if (this.options.sourceMaps) {
      opts.sourceMap = true;
      opts.file = this.name;
      opts.outFile = this.name;
      opts.omitSourceMapUrl = true;
      opts.sourceMapContents = true;
    }

    try {
      return await render(opts);
    } catch (err) {
      // Format the error so it can be handled by parcel's prettyError
      if (err.formatted) {
        throw sassToCodeFrame(err);
      }
      // Throw original error if there is no codeFrame
      throw err;
    }
  }