{"size": 1742, "ext": "js", "max_stars_repo_path": "packages/html-reporter/bundle.js", "max_stars_repo_name": "vitalics/playwright", "max_stars_repo_stars_event_min_datetime": "2021-11-16T18:20:25.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-14T06:38:19.000Z", "max_issues_repo_path": "packages/html-reporter/bundle.js", "max_issues_repo_name": "vitalics/playwright", "max_issues_repo_issues_event_min_datetime": "2022-03-30T12:59:39.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T13:08:07.000Z", "max_forks_repo_path": "packages/html-reporter/bundle.js", "max_forks_repo_name": "vitalics/playwright", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 32.8679245283, "max_line_length": 123, "alphanum_fraction": 0.6090700344}
/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import fs, { existsSync } from 'fs';
import path from 'path';

/**
 * @returns {import('vite').Plugin}
 */
export function bundle() {
  let config;
  return {
    name: 'playwright-bundle',
    config(c) {
      config = c;
    },
    transformIndexHtml: {
      transform(html, ctx) {
        if (!ctx || !ctx.bundle)
          return html;
        for (const [, value] of Object.entries(ctx.bundle)) {
          if (value.code)
            html = html.replace(/<script type="module".*<\/script>/, () => `<script type="module">${value.code}</script>`);
          else
            html = html.replace(/<link rel="stylesheet"[^>]*>/, () => `<style type='text/css'>${value.source}</style>`);
        }
        return html;
      },
    },
    closeBundle: () => {
      if (existsSync(path.join(config.build.outDir, 'index.html'))) {
        const targetDir = path.join(__dirname, '..', 'playwright-core', 'lib', 'webpack', 'htmlReport');
        fs.mkdirSync(targetDir, { recursive: true });
        fs.copyFileSync(
          path.join(config.build.outDir, 'index.html'),
          path.join(targetDir, 'index.html'));
      }
    },
  }
}