"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.constructParcel = constructParcel;
exports.compileGatsbyFiles = compileGatsbyFiles;
exports.isCompileGatsbyFilesFlagSet = isCompileGatsbyFilesFlagSet;
exports.gatsbyFileRegex = exports.PARCEL_CACHE_DIR = exports.COMPILED_CACHE_DIR = void 0;

var _core = require("@parcel/core");

var _reporter = _interopRequireDefault(require("gatsby-cli/lib/reporter"));

var _fsExtra = require("fs-extra");

const COMPILED_CACHE_DIR = `.cache/compiled`;
exports.COMPILED_CACHE_DIR = COMPILED_CACHE_DIR;
const PARCEL_CACHE_DIR = `.cache/.parcel-cache`;
exports.PARCEL_CACHE_DIR = PARCEL_CACHE_DIR;
const gatsbyFileRegex = `gatsby-+(node|config).{ts,js}`;
/**
 * Construct Parcel with config.
 * @see {@link https://parceljs.org/features/targets/}
 */

exports.gatsbyFileRegex = gatsbyFileRegex;

function constructParcel(siteRoot) {
  return new _core.Parcel({
    entries: [`${siteRoot}/${gatsbyFileRegex}`, `${siteRoot}/plugins/**/${gatsbyFileRegex}`],
    defaultConfig: `gatsby-parcel-config`,
    mode: `production`,
    targets: {
      root: {
        outputFormat: `commonjs`,
        includeNodeModules: false,
        sourceMap: false,
        engines: {
          node: `>= 14.15.0`
        },
        distDir: `${siteRoot}/${COMPILED_CACHE_DIR}`
      }
    },
    cacheDir: `${siteRoot}/${PARCEL_CACHE_DIR}`
  });
}
/**
 * Compile known gatsby-* files (e.g. `gatsby-config`, `gatsby-node`)
 * and output in `<SITE_ROOT>/.cache/compiled`.
 */


async function compileGatsbyFiles(siteRoot) {
  const parcel = constructParcel(siteRoot);

  try {
    await (0, _fsExtra.ensureDir)(`${siteRoot}/${COMPILED_CACHE_DIR}`);
    await parcel.run();
  } catch (error) {
    _reporter.default.panic(`Failed to compile gatsby files.`, error);
  }
}

function isCompileGatsbyFilesFlagSet() {
  return Boolean(process.env.GATSBY_TS_GATSBY_FILES) && process.env.GATSBY_TS_GATSBY_FILES !== `false` && process.env.GATSBY_TS_GATSBY_FILES !== `0`;
}
//# sourceMappingURL=compile-gatsby-files.js.map