text: /**
   * @remix-run/dev v1.1.3
   *
   * Copyright (c) Remix Software Inc.
   *
   * This source code is licensed under the MIT license found in the
   * LICENSE.md file in the root directory of this source tree.
   *
   * @license MIT
   */
  'use strict';
  
  Object.defineProperty(exports, '__esModule', { value: true });
  
  var fs = require('fs');
  var path = require('path');
  var remarkMdxFrontmatter = require('remark-mdx-frontmatter');
  var loaders = require('../loaders.js');
  
  function _interopNamespace(e) {
    if (e && e.__esModule) return e;
    var n = Object.create(null);
    if (e) {
      Object.keys(e).forEach(function (k) {
        if (k !== 'default') {
          var d = Object.getOwnPropertyDescriptor(e, k);
          Object.defineProperty(n, k, d.get ? d : {
            enumerable: true,
            get: function () { return e[k]; }
          });
        }
      });
    }
    n["default"] = e;
    return Object.freeze(n);
  }
  
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
  
  function mdxPlugin(config) {
    return {
      name: "remix-mdx",
  
      async setup(build) {
        let [xdm, {
          default: remarkFrontmatter
        }] = await Promise.all([import('xdm'), import('remark-frontmatter')]);
        build.onResolve({
          filter: /\.mdx?$/
        }, args => {
          return {
            path: args.path.startsWith("~/") ? path__namespace.resolve(config.appDirectory, args.path.replace(/^~\//, "")) : path__namespace.resolve(args.resolveDir, args.path),
            namespace: "mdx"
          };
        });
        build.onLoad({
          filter: /\.mdx?$/
        }, async args => {
          try {
            let contents = await fs.promises.readFile(args.path, "utf-8");
            let rehypePlugins = [];
            let remarkPlugins = [remarkFrontmatter, [remarkMdxFrontmatter.remarkMdxFrontmatter, {
              name: "attributes"
            }]];
  
            switch (typeof config.mdx) {
              case "object":
                rehypePlugins.push(...(config.mdx.rehypePlugins || []));
                remarkPlugins.push(...(config.mdx.remarkPlugins || []));
                break;
  
              case "function":
                let mdxConfig = await config.mdx(args.path);
                rehypePlugins.push(...((mdxConfig === null || mdxConfig === void 0 ? void 0 : mdxConfig.rehypePlugins) || []));
                remarkPlugins.push(...((mdxConfig === null || mdxConfig === void 0 ? void 0 : mdxConfig.remarkPlugins) || []));
                break;
            }
  
            let remixExports = `
  export const filename = ${JSON.stringify(path__namespace.basename(args.path))};
  export const headers = typeof attributes !== "undefined" && attributes.headers;
  export const meta = typeof attributes !== "undefined" && attributes.meta;
  export const links = undefined;
            `;
            let compiled = await xdm.compile(contents, {
              jsx: true,
              jsxRuntime: "classic",
              pragma: "React.createElement",
              pragmaFrag: "React.Fragment",
              rehypePlugins,
              remarkPlugins
            });
            contents = `
  ${compiled.value}
  ${remixExports}`;
            let errors = [];
            let warnings = [];
            compiled.messages.forEach(message => {
              let toPush = message.fatal ? errors : warnings;
              toPush.push({
                location: message.line || message.column ? {
                  column: typeof message.column === "number" ? message.column : undefined,
                  line: typeof message.line === "number" ? message.line : undefined
                } : undefined,
                text: message.message,
                detail: typeof message.note === "string" ? message.note : undefined
              });
            });
            return {
              errors: errors.length ? errors : undefined,
              warnings: warnings.length ? warnings : undefined,
              contents,
              resolveDir: path__namespace.dirname(args.path),
              loader: loaders.getLoaderForFile(args.path)
            };
          } catch (err) {
            return {
              errors: [{
                text: err.message
              }]
            };
          }
        });
      }
  
    };
  }
  
  exports.mdxPlugin = mdxPlugin;
  