From cd376cdc1f88000b05e2558049cf023890d068cf Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 27 Jun 2024 20:21:11 +0900 Subject: [PATCH] fix(test): build configuration for content script --- .../adblocker-e2e-testing-content/index.html | 2 +- .../package.json | 3 +- .../rollup.config.ts | 30 +++++++ .../tsconfig.json | 2 +- packages/adblocker-e2e-testing/e2e.ts | 84 ++----------------- packages/adblocker-e2e-testing/package.json | 8 +- packages/adblocker-playwright/package.json | 2 +- 7 files changed, 46 insertions(+), 85 deletions(-) create mode 100644 packages/adblocker-e2e-testing-content/rollup.config.ts diff --git a/packages/adblocker-e2e-testing-content/index.html b/packages/adblocker-e2e-testing-content/index.html index e300958872..4c604b5dde 100644 --- a/packages/adblocker-e2e-testing-content/index.html +++ b/packages/adblocker-e2e-testing-content/index.html @@ -56,6 +56,6 @@

Scriptlet

- + diff --git a/packages/adblocker-e2e-testing-content/package.json b/packages/adblocker-e2e-testing-content/package.json index 3fe2a78c38..2647003426 100644 --- a/packages/adblocker-e2e-testing-content/package.json +++ b/packages/adblocker-e2e-testing-content/package.json @@ -15,7 +15,8 @@ "directory": "packages/adblocker-e2e-webpage" }, "scripts": { - "lint": "eslint script.ts" + "lint": "eslint script.ts", + "build": "tsc --build ./tsconfig.json && rollup --config ./rollup.config.ts --configPlugin typescript" }, "bugs": { "url": "https://github.com/ghostery/adblocker/issues" diff --git a/packages/adblocker-e2e-testing-content/rollup.config.ts b/packages/adblocker-e2e-testing-content/rollup.config.ts new file mode 100644 index 0000000000..d2b50e638e --- /dev/null +++ b/packages/adblocker-e2e-testing-content/rollup.config.ts @@ -0,0 +1,30 @@ +/*! + * Copyright (c) 2017-present Cliqz GmbH. All rights reserved. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +import resolve from '@rollup/plugin-node-resolve'; +import terser from '@rollup/plugin-terser'; + +export default [ + { + input: './dist/src/script.js', + output: { + file: './dist/script.iife.min.js', + format: 'iife', + name: 'script', + sourcemap: true, + }, + plugins: [ + resolve(), + terser({ + output: { + comments: false, + }, + }), + ], + }, +]; diff --git a/packages/adblocker-e2e-testing-content/tsconfig.json b/packages/adblocker-e2e-testing-content/tsconfig.json index c039fa1b21..857907b4b0 100644 --- a/packages/adblocker-e2e-testing-content/tsconfig.json +++ b/packages/adblocker-e2e-testing-content/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "module": "es2020", - "outDir": "dist", + "outDir": "dist/src", "inlineSourceMap": true, "isolatedModules": true, "noImplicitAny": true, diff --git a/packages/adblocker-e2e-testing/e2e.ts b/packages/adblocker-e2e-testing/e2e.ts index d0485f5389..d62f18ea1d 100644 --- a/packages/adblocker-e2e-testing/e2e.ts +++ b/packages/adblocker-e2e-testing/e2e.ts @@ -1,8 +1,6 @@ -import { Stats, createReadStream, existsSync, readFileSync, statSync } from 'node:fs'; +import { Stats, createReadStream, existsSync, statSync } from 'node:fs'; import * as http from 'node:http'; import * as path from 'node:path'; -import * as crypto from 'node:crypto'; -import ts from 'typescript'; import { minify } from 'terser'; // Shared types @@ -27,71 +25,17 @@ export type Result = { }; // Server -enum Extension { - TypeScript = 0, -} - -type AssetPipeline = { - contentType: string; - extension?: Extension; -}; - -function getAssetPipeline(file: string): AssetPipeline { - const data: AssetPipeline = { - contentType: 'text/plain', - }; - +function getContentType(file: string): string { switch (path.extname(file)) { - case '.ts': - data.contentType = 'text/javascript'; - data.extension = Extension.TypeScript; - break; case '.js': - data.contentType = 'text/javascript'; - break; + return 'text/javascript'; case '.css': - data.contentType = 'text/css'; - break; + return 'text/css'; case '.html': - data.contentType = 'text/html'; - break; - } - - return data; -} - -// Cache compile results -type TypeScriptCompliationCache = { - hash: string; - output: string; -}; - -const typescriptCompliationCache = new Map(); - -function compileTypeScript(key: string, script: string): string { - const hash = crypto.createHash('md5').update(script).digest('hex'); - const cache = typescriptCompliationCache.get(key); - - // Check if cache exists and the origin hash is identical to given content - if (cache !== undefined && cache.hash === hash) { - return cache.output; + return 'text/html'; } - const result = ts.transpileModule(script, { - compilerOptions: { - target: ts.ScriptTarget.ES2018, - module: ts.ModuleKind.Preserve, - inlineSourceMap: true, - lib: ['dom'], - }, - }); - - typescriptCompliationCache.set(key, { - hash, - output: result.outputText, - }); - - return result.outputText; + return 'text/pure'; } export function createServer(): http.Server { @@ -141,23 +85,9 @@ export function createServer(): http.Server { return; } - // Check if postprocessing is required for this asset - const assetPipeline = getAssetPipeline(assetPath); - switch (assetPipeline.extension) { - case Extension.TypeScript: { - const output = compileTypeScript(assetPath, readFileSync(assetPath, 'utf8')); - res.writeHead(200, { - 'content-type': assetPipeline.contentType, - 'content-length': output.length, - }); - res.end(output); - return; - } - } - // If postprocessing is not required, handle this as stream res.writeHead(200, { - 'content-type': assetPipeline.contentType, + 'content-type': getContentType(assetPath), 'content-length': stats.size, }); diff --git a/packages/adblocker-e2e-testing/package.json b/packages/adblocker-e2e-testing/package.json index 0f85f5e08f..fce9ea5740 100644 --- a/packages/adblocker-e2e-testing/package.json +++ b/packages/adblocker-e2e-testing/package.json @@ -37,8 +37,8 @@ "url": "https://github.com/ghostery/adblocker/issues" }, "dependencies": { - "terser": "^5.31.1", - "typescript": "^5.4.5" + "rollup": "^4.17.2", + "terser": "^5.31.1" }, "devDependencies": { "@rollup/plugin-node-resolve": "^15.2.3", @@ -51,8 +51,8 @@ "mocha": "^10.2.0", "nyc": "^17.0.0", "rimraf": "^5.0.7", - "rollup": "^4.17.2", - "ts-node": "^10.9.1" + "ts-node": "^10.9.1", + "typescript": "^5.4.5" }, "contributors": [ { diff --git a/packages/adblocker-playwright/package.json b/packages/adblocker-playwright/package.json index e9c918d585..525d1a5d52 100644 --- a/packages/adblocker-playwright/package.json +++ b/packages/adblocker-playwright/package.json @@ -32,7 +32,7 @@ "clean": "rimraf dist coverage", "lint": "eslint adblocker.ts", "build": "tsc --build ./tsconfig.json && rollup --config ./rollup.config.ts --configPlugin typescript", - "test": "nyc mocha --config ../../.mocharc.json" + "test": "yarn playwright install && nyc mocha --config ../../.mocharc.json" }, "bugs": { "url": "https://github.com/ghostery/adblocker/issues"