diff --git a/package-lock.json b/package-lock.json index 366b58b577..5fb0d39a95 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7841,7 +7841,9 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "packages/html-reporter": {}, + "packages/html-reporter": { + "version": "0.0.0" + }, "packages/playwright": { "version": "1.21.0-next", "hasInstallScript": true, diff --git a/packages/html-reporter/.gitignore b/packages/html-reporter/.gitignore index 466e24805a..a547bf36d8 100644 --- a/packages/html-reporter/.gitignore +++ b/packages/html-reporter/.gitignore @@ -1 +1,24 @@ -out/ \ No newline at end of file +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/packages/html-reporter/bundle.js b/packages/html-reporter/bundle.js new file mode 100644 index 0000000000..478e48357f --- /dev/null +++ b/packages/html-reporter/bundle.js @@ -0,0 +1,53 @@ +/** + * 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(/`); + else + html = html.replace(/]*>/, () => ``); + } + 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')); + } + }, + } +} \ No newline at end of file diff --git a/packages/html-reporter/bundleJsPlugin.js b/packages/html-reporter/bundleJsPlugin.js deleted file mode 100644 index bd47fd5993..0000000000 --- a/packages/html-reporter/bundleJsPlugin.js +++ /dev/null @@ -1,66 +0,0 @@ -/* - 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. -*/ - -const path = require('path'); -const HtmlWebpackPlugin = require('html-webpack-plugin'); - -class BundleJsPlugin { - constructor() { - } - - apply(compiler) { - compiler.hooks.compilation.tap('bundle-js-plugin', compilation => { - HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tapAsync('bundle-js-plugin', (htmlPluginData, callback) => { - callback(null, this.processTags(compilation, htmlPluginData)); - }); - }); - }; - - processTags(compilation, pluginData) { - const headTags = pluginData.headTags.map(tag => this.processTag(compilation, tag)); - const bodyTags = pluginData.bodyTags.map(tag => this.processTag(compilation, tag)); - return { ...pluginData, headTags, bodyTags }; - } - - processTag(compilation, tag) { - if (tag.tagName !== 'script' || !tag.attributes.src) - return tag; - - const asset = getAssetByName(compilation.assets, tag.attributes.src); - const innerHTML = asset.source().replace(/(<)(\/script>)/g, '\\x3C$2'); - return { - tagName: 'script', - attributes: { - type: 'text/javascript' - }, - closeTag: true, - innerHTML, - }; - } -} - -function getAssetByName (assets, assetName) { - for (var key in assets) { - if (assets.hasOwnProperty(key)) { - var processedKey = path.posix.relative('', key); - if (processedKey === assetName) { - return assets[key]; - } - } - } -} - -module.exports = BundleJsPlugin; diff --git a/packages/html-reporter/src/index.html b/packages/html-reporter/index.html similarity index 91% rename from packages/html-reporter/src/index.html rename to packages/html-reporter/index.html index 143c311aaa..054507220c 100644 --- a/packages/html-reporter/src/index.html +++ b/packages/html-reporter/index.html @@ -23,6 +23,7 @@