"use strict";

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

exports.__esModule = true;
exports.createGraphqlEngineBundle = createGraphqlEngineBundle;

var path = _interopRequireWildcard(require("path"));

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

var _webpack = _interopRequireDefault(require("webpack"));

var _printPlugins = require("./print-plugins");

var _module = _interopRequireDefault(require("module"));

var _webpackLogging = require("../../utils/webpack/plugins/webpack-logging");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/* eslint-disable @typescript-eslint/naming-convention */
const extensions = [`.mjs`, `.js`, `.json`, `.node`, `.ts`, `.tsx`];
const outputDir = path.join(process.cwd(), `.cache`, `query-engine`);
const cacheLocation = path.join(process.cwd(), `.cache`, `webpack`, `query-engine`);

async function createGraphqlEngineBundle(rootDir, reporter, isVerbose) {
  var _process$env$GATSBY_W;

  const schemaSnapshotString = await fs.readFile(path.join(rootDir, `.cache`, `schema.gql`), `utf-8`);
  await (0, _printPlugins.printQueryEnginePlugins)();
  const compiler = (0, _webpack.default)({
    name: `Query Engine`,
    // mode: `production`,
    mode: `none`,
    entry: path.join(__dirname, `entry.js`),
    output: {
      path: outputDir,
      filename: `index.js`,
      libraryTarget: `commonjs`
    },
    target: `node`,
    externalsPresets: {
      node: false
    },
    cache: {
      type: `filesystem`,
      name: `graphql-engine`,
      cacheLocation,
      buildDependencies: {
        config: [__filename]
      }
    },
    // those are required in some runtime paths, but we don't need them
    externals: [`cbor-x`, // optional dep of lmdb-store, but we are using `msgpack` (default) encoding, so we don't need it
    `babel-runtime/helpers/asyncToGenerator`, // undeclared dep of yurnalist (but used in code path we don't use)
    `electron`, // :shrug: `got` seems to have electron specific code path
    _module.default.builtinModules.reduce((acc, builtinModule) => {
      if (builtinModule === `fs`) {
        acc[builtinModule] = `global _actualFsWrapper`;
      } else {
        acc[builtinModule] = `commonjs ${builtinModule}`;
      }

      return acc;
    }, {})],
    module: {
      rules: [{
        test: /node_modules[/\\]lmdb[/\\]/,
        parser: {
          amd: false
        },
        use: [{
          loader: require.resolve(`@vercel/webpack-asset-relocator-loader`),
          options: {
            outputAssetBase: `assets`
          }
        }, {
          loader: require.resolve(`./lmdb-bundling-patch`)
        }]
      }, {
        test: /\.m?js$/,
        type: `javascript/auto`,
        resolve: {
          byDependency: {
            esm: {
              fullySpecified: false
            }
          }
        }
      }, {
        // For node binary relocations, include ".node" files as well here
        test: /\.(m?js|node)$/,
        // it is recommended for Node builds to turn off AMD support
        parser: {
          amd: false
        },
        use: {
          loader: require.resolve(`@vercel/webpack-asset-relocator-loader`),
          options: {
            outputAssetBase: `assets`
          }
        }
      }, {
        test: /\.txt/,
        type: `asset/resource`
      }]
    },
    resolve: {
      extensions,
      alias: {
        ".cache": process.cwd() + `/.cache/`,
        [require.resolve(`gatsby-cli/lib/reporter/loggers/ink/index.js`)]: false,
        inquirer: false
      }
    },
    plugins: [new _webpack.default.DefinePlugin({
      // "process.env.GATSBY_LOGGER": JSON.stringify(`yurnalist`),
      "process.env.GATSBY_EXPERIMENTAL_LMDB_STORE": `true`,
      "process.env.GATSBY_SKIP_WRITING_SCHEMA_TO_FILE": `true`,
      SCHEMA_SNAPSHOT: JSON.stringify(schemaSnapshotString),
      "process.env.GATSBY_LOGGER": JSON.stringify(`yurnalist`)
    }), ((_process$env$GATSBY_W = process.env.GATSBY_WEBPACK_LOGGING) === null || _process$env$GATSBY_W === void 0 ? void 0 : _process$env$GATSBY_W.includes(`query-engine`)) && new _webpackLogging.WebpackLoggingPlugin(rootDir, reporter, isVerbose)].filter(Boolean)
  });
  return new Promise((resolve, reject) => {
    compiler.run((err, stats) => {
      compiler.close(closeErr => {
        if (err) {
          return reject(err);
        }

        if (closeErr) {
          return reject(closeErr);
        }

        return resolve(stats === null || stats === void 0 ? void 0 : stats.compilation);
      });
    });
  });
}
//# sourceMappingURL=bundle-webpack.js.map