From 60cc9b17c47f1b640eb349085bba9e1b40f7bdba Mon Sep 17 00:00:00 2001 From: cenfun Date: Thu, 21 Dec 2023 11:45:10 +0800 Subject: [PATCH] add rollup case --- scripts/build-test.js | 49 +++++++++++++- test/mock/minify/comments.js | 14 ++-- test/mock/rollup/index.html | 19 ++++++ test/mock/src/index.js | 8 +-- test/mock/src/typescript.ts | 30 +++++---- test/mock/vite/index.html | 19 ++++++ test/package.json | 3 + test/test-istanbul.js | 2 +- test/test-v8-esbuild.js | 2 +- test/test-v8-minify.js | 2 +- test/test-v8-rollup.js | 122 +++++++++++++++++++++++++++++++++++ test/test-v8.js | 3 +- test/test.js | 2 + 13 files changed, 243 insertions(+), 32 deletions(-) create mode 100644 test/mock/rollup/index.html create mode 100644 test/mock/vite/index.html create mode 100644 test/test-v8-rollup.js diff --git a/scripts/build-test.js b/scripts/build-test.js index ca2990c6..4dafc258 100644 --- a/scripts/build-test.js +++ b/scripts/build-test.js @@ -3,12 +3,15 @@ const path = require('path'); const esbuild = require('esbuild'); const webpack = require('webpack'); +const { rollup } = require('rollup'); +const rollupTypescript = require('@rollup/plugin-typescript'); +const rollupCommonJs = require('@rollup/plugin-commonjs'); + const EC = require('eight-colors'); const webpackConfIstanbul = require('../test/webpack.config-istanbul.js'); const webpackConfV8 = require('../test/webpack.config-v8.js'); - const startWebpack = function(conf) { return new Promise(function(resolve) { webpack(conf, (err, stats) => { @@ -83,11 +86,53 @@ const runEsbuild = async () => { console.log(EC.green('finish esbuild')); }; +const runRollup = async () => { + + const entry = path.resolve('test/mock/src/index.js'); + + const inputOptions = { + input: entry, + plugins: [ + rollupCommonJs(), + rollupTypescript({ + sourceMap: true, + inlineSources: true + }) + ] + }; + + const outputOptions = { + file: path.resolve('test/mock/rollup/dist/coverage-rollup.js'), + name: 'coverageRollup', + format: 'iife', + sourcemap: true + }; + + let bundle; + let buildFailed = false; + try { + // create a bundle + bundle = await rollup(inputOptions); + + await bundle.write(outputOptions); + console.log(EC.green('finish rollup')); + + } catch (error) { + buildFailed = true; + // do some error reporting + console.error(error); + } + if (bundle) { + // closes the bundle + await bundle.close(); + } + process.exit(buildFailed ? 1 : 0); +}; const build = async () => { await runWebpackIstanbul(); await runWebpackV8(); await runEsbuild(); - + await runRollup(); }; diff --git a/test/mock/minify/comments.js b/test/mock/minify/comments.js index 752fb9cf..7dbee9d7 100644 --- a/test/mock/minify/comments.js +++ b/test/mock/minify/comments.js @@ -1,18 +1,18 @@ /* eslint-disable no-trailing-spaces,line-comment-position,no-inline-comments,indent,no-multi-spaces,no-multiple-empty-lines */ // LineComment -console.log('some//thing'); // comment /* +console.log('some"//"\\\'thing\\'); // comment /* - // comment /* -console.log('some//thing/*'); /* + // comment /* --- +console.log('some//thing/*'); /* ddd */ console.log('some/*/thing'); - +// console.log(` - /* - // - */ + '/* + "//" + */' `); /** diff --git a/test/mock/rollup/index.html b/test/mock/rollup/index.html new file mode 100644 index 00000000..941aa5ac --- /dev/null +++ b/test/mock/rollup/index.html @@ -0,0 +1,19 @@ + + + + + + + + coverage page + + + + +

+ Mock V8 Rollup Coverage +

+ + + + \ No newline at end of file diff --git a/test/mock/src/index.js b/test/mock/src/index.js index d3251f8c..8d7e0e84 100644 --- a/test/mock/src/index.js +++ b/test/mock/src/index.js @@ -1,9 +1,9 @@ -import './branch.js'; -import './typescript.ts'; - import '../minify/comments.js'; import '../minify/demo.js'; +import './branch.js'; +import ts from './typescript.ts'; + export function foo(argument) { console.log('this is foo'); @@ -96,4 +96,4 @@ const onload = (something) => { }; // one line but two statements -init(window._my_stop_key); onload(window._my_something); +init(window._my_stop_key); onload(window._my_something); ts(); diff --git a/test/mock/src/typescript.ts b/test/mock/src/typescript.ts index 3f557074..a044dde3 100644 --- a/test/mock/src/typescript.ts +++ b/test/mock/src/typescript.ts @@ -1,29 +1,31 @@ -var str: string = "hello world"; - // point type type Point = { x: number, y: number } -var p: Point = { - x: 1, - y: 2 -} - -var fun = (v: Point) => { - console.log(v); +const fun = (p: Point, pointIn: Point) => { + const str: string = "hello world"; + console.log(p, pointIn, str); + return str; } -fun(p); -var main = (p: Point, v: string) => { +const main = (p: Point, s: string) => { /* - block comment + typescript block comment */ + const pointIn: Point = { + x: 1, + y: 2 + } + + // typescript line comment + + const v = fun(p, pointIn); - console.log(p, v); + console.log(p, s, v); } -module.exports = main; \ No newline at end of file +export default main; \ No newline at end of file diff --git a/test/mock/vite/index.html b/test/mock/vite/index.html new file mode 100644 index 00000000..8d7d9116 --- /dev/null +++ b/test/mock/vite/index.html @@ -0,0 +1,19 @@ + + + + + + + + coverage page + + + + +

+ Mock V8 Vite Coverage +

+ + + + \ No newline at end of file diff --git a/test/package.json b/test/package.json index 86d4d8ce..74e733cc 100644 --- a/test/package.json +++ b/test/package.json @@ -8,6 +8,8 @@ "author": "", "dependencies": {}, "devDependencies": { + "@rollup/plugin-commonjs": "^25.0.7", + "@rollup/plugin-typescript": "^11.1.5", "babel-loader": "^9.1.3", "babel-plugin-istanbul": "^6.1.1", "esbuild": "^0.19.10", @@ -16,6 +18,7 @@ "koa-static-resolver": "^1.0.4", "marked": "^11.1.0", "playwright": "^1.40.1", + "rollup": "^4.9.1", "ts-loader": "^9.5.1", "typescript": "^5.3.3", "webpack": "^5.89.0" diff --git a/test/test-istanbul.js b/test/test-istanbul.js index 7221a0c2..fd695ff1 100644 --- a/test/test-istanbul.js +++ b/test/test-istanbul.js @@ -104,7 +104,7 @@ const generate = async () => { module.exports = async (serverUrl) => { // clean cache first if debug if (coverageOptions.logging === 'debug') { - await new CoverageReport(coverageOptions).clean(); + await new CoverageReport(coverageOptions).cleanCache(); } await Promise.all([ diff --git a/test/test-v8-esbuild.js b/test/test-v8-esbuild.js index 4864968e..95dc85b5 100644 --- a/test/test-v8-esbuild.js +++ b/test/test-v8-esbuild.js @@ -110,7 +110,7 @@ const generate = async () => { module.exports = async (serverUrl) => { // clean cache first if debug if (coverageOptions.logging === 'debug') { - await new CoverageReport(coverageOptions).clean(); + await new CoverageReport(coverageOptions).cleanCache(); } await Promise.all([ diff --git a/test/test-v8-minify.js b/test/test-v8-minify.js index 8a3435db..22cd1230 100644 --- a/test/test-v8-minify.js +++ b/test/test-v8-minify.js @@ -110,7 +110,7 @@ const generate = async () => { module.exports = async (serverUrl) => { // clean cache first if debug if (coverageOptions.logging === 'debug') { - await new CoverageReport(coverageOptions).clean(); + await new CoverageReport(coverageOptions).cleanCache(); } await Promise.all([ diff --git a/test/test-v8-rollup.js b/test/test-v8-rollup.js new file mode 100644 index 00000000..c808edd7 --- /dev/null +++ b/test/test-v8-rollup.js @@ -0,0 +1,122 @@ +const { chromium } = require('playwright'); +const EC = require('eight-colors'); + +const CoverageReport = require('../'); + +const coverageOptions = { + // logging: 'debug', + reports: [ + ['v8', { + name: 'My V8 Rollup Coverage Report', + assetsPath: '../assets' + }] + ], + outputDir: './docs/v8-rollup' +}; + +const test1 = async (serverUrl) => { + + console.log('start v8-rollup test1 ...'); + const browser = await chromium.launch({ + // headless: false + }); + const page = await browser.newPage(); + + await Promise.all([ + page.coverage.startJSCoverage({ + resetOnNavigation: false + }), + page.coverage.startCSSCoverage({ + resetOnNavigation: false + }) + ]); + + const url = `${serverUrl}/rollup/`; + + console.log(`goto ${url}`); + + await page.goto(url); + + await new Promise((resolve) => { + setTimeout(resolve, 500); + }); + + const [jsCoverage, cssCoverage] = await Promise.all([ + page.coverage.stopJSCoverage(), + page.coverage.stopCSSCoverage() + ]); + + const coverageList = [... jsCoverage, ... cssCoverage]; + + const results = await new CoverageReport(coverageOptions).add(coverageList); + console.log('v8-rollup coverage1 added', results.type); + + await browser.close(); +}; + + +const test2 = async (serverUrl) => { + + console.log('start v8-rollup test2 ...'); + const browser = await chromium.launch({ + // headless: false + }); + const page = await browser.newPage(); + + await Promise.all([ + page.coverage.startJSCoverage({ + resetOnNavigation: false + }), + page.coverage.startCSSCoverage({ + resetOnNavigation: false + }) + ]); + + const url = `${serverUrl}/rollup/`; + + console.log(`goto ${url}`); + + await page.goto(url); + + await new Promise((resolve) => { + setTimeout(resolve, 500); + }); + + const [jsCoverage, cssCoverage] = await Promise.all([ + page.coverage.stopJSCoverage(), + page.coverage.stopCSSCoverage() + ]); + + const coverageList = [... jsCoverage, ... cssCoverage]; + + const results = await new CoverageReport(coverageOptions).add(coverageList); + console.log('v8-rollup coverage2 added', results.type); + + await browser.close(); +}; + + +const generate = async () => { + + console.log('generate v8-rollup coverage reports ...'); + + const coverageResults = await new CoverageReport(coverageOptions).generate(); + console.log('reportPath', EC.magenta(coverageResults.reportPath)); + + console.log('v8-rollup coverage generated', coverageResults.summary); +}; + + +module.exports = async (serverUrl) => { + // clean cache first if debug + if (coverageOptions.logging === 'debug') { + await new CoverageReport(coverageOptions).cleanCache(); + } + + await Promise.all([ + test1(serverUrl), + test2(serverUrl) + ]); + + await generate(); +}; diff --git a/test/test-v8.js b/test/test-v8.js index 33ea0001..9d6a83a6 100644 --- a/test/test-v8.js +++ b/test/test-v8.js @@ -209,8 +209,7 @@ const generate = async () => { module.exports = async (serverUrl) => { // clean cache first if debug if (coverageOptions.logging === 'debug') { - const coverageReport = new CoverageReport(coverageOptions); - await coverageReport.cleanCache(); + await new CoverageReport(coverageOptions).cleanCache(); } await Promise.all([ diff --git a/test/test.js b/test/test.js index c7255440..1f7666fa 100644 --- a/test/test.js +++ b/test/test.js @@ -7,6 +7,7 @@ const Koa = require('koa'); const testV8 = require('./test-v8.js'); const testV8EsBuild = require('./test-v8-esbuild.js'); +const testV8Rollup = require('./test-v8-rollup.js'); const testV8Minify = require('./test-v8-minify.js'); const testIstanbul = require('./test-istanbul.js'); @@ -98,6 +99,7 @@ const test = async () => { testIstanbul(serverUrl), testV8Minify(serverUrl), testV8EsBuild(serverUrl), + testV8Rollup(serverUrl), testV8(serverUrl) ]);