text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.createMainPlugin = void 0;
  const history = require("connect-history-api-fallback");
  const utils_1 = require("@vuepress/utils");
  const resolveAlias_1 = require("./resolveAlias");
  const resolveDefine_1 = require("./resolveDefine");
  // packages that include client code, which should not
  // be optimized nor externalized
  const clientPackages = [
      '@vuepress/client',
      '@vuepress/plugin-active-header-links',
      '@vuepress/plugin-back-to-top',
      '@vuepress/plugin-container',
      '@vuepress/plugin-debug',
      '@vuepress/plugin-docsearch',
      '@vuepress/plugin-git',
      '@vuepress/plugin-google-analytics',
      '@vuepress/plugin-medium-zoom',
      '@vuepress/plugin-nprogress',
      '@vuepress/plugin-palette',
      '@vuepress/plugin-prismjs',
      '@vuepress/plugin-pwa',
      '@vuepress/plugin-pwa-popup',
      '@vuepress/plugin-register-components',
      '@vuepress/plugin-search',
      '@vuepress/plugin-shiki',
      '@vuepress/plugin-theme-data',
      '@vuepress/plugin-toc',
      '@vuepress/theme-default',
  ];
  const createMainPlugin = ({ app, options, isServer, isBuild, }) => ({
      name: 'vuepress:main',
      config: async () => {
          // create a temp index.html as dev entry point
          if (!isBuild) {
              await app.writeTemp('vite-root/index.html', utils_1.fs
                  .readFileSync(app.options.templateDev)
                  .toString()
                  .replace(/<\/body>/, `\
  <script type="module">
  import '@vuepress/client/lib/app.js'
  </script>
  </body>`));
          }
          return {
              root: app.dir.temp('vite-root'),
              base: app.options.base,
              mode: isBuild ? 'production' : 'development',
              define: await resolveDefine_1.resolveDefine({ app, isServer }),
              publicDir: app.dir.public(),
              cacheDir: app.dir.cache(),
              resolve: {
                  alias: await resolveAlias_1.resolveAlias({ app }),
              },
              css: {
                  postcss: {
                      plugins: isServer
                          ? []
                          : [
                              require('autoprefixer'),
                              ...(isBuild ? [require('postcss-csso')] : []),
                          ],
                  },
              },
              server: {
                  host: app.options.host,
                  port: app.options.port,
                  open: app.options.open,
                  // TODO: remove after https://github.com/vitejs/vite/issues/3373 is solved
                  fsServe: {
                      root: process.cwd(),
                  },
              },
              build: {
                  ssr: isServer,
                  outDir: isServer ? app.dir.dest('.server') : app.dir.dest(),
                  cssCodeSplit: false,
                  rollupOptions: {
                      input: app.dir.client('lib/app.js'),
                      preserveEntrySignatures: 'allow-extension',
                  },
                  minify: isServer ? false : !app.env.isDebug,
              },
              optimizeDeps: {
                  include: ['@vuepress/shared'],
                  exclude: clientPackages,
              },
              ssr: {
                  noExternal: clientPackages,
              },
          };
      },
      generateBundle(_, bundle) {
          // delete all asset outputs in server build
          if (isServer) {
              Object.keys(bundle).forEach((key) => {
                  if (bundle[key].type === 'asset') {
                      delete bundle[key];
                  }
              });
          }
      },
      configureServer(server) {
          return () => {
              // fallback all `.html` requests to `/index.html`
              server.middlewares.use(history({
                  rewrites: [
                      {
                          from: /\.html$/,
                          to: '/index.html',
                      },
                  ],
              }));
          };
      },
  });
  exports.createMainPlugin = createMainPlugin;
  