Skip to content

Commit 2f1ce09

Browse files
authored
injected: replace rollup with esbuild (#1521)
* injected: replace rollup with esbuild * fix * remove suffix * poc: preserve comments in esbuild (#1565) * poc: preserve comments in esbuild * comment * more test cases * more test cases * additional test
1 parent 02a9553 commit 2f1ce09

File tree

16 files changed

+396
-904
lines changed

16 files changed

+396
-904
lines changed

CODEOWNERS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ injected/src/features/autofill-password-import.js @duckduckgo/content-scope-scri
1515
Sources/ @duckduckgo/content-scope-scripts-owners @duckduckgo/apple-devs
1616
injected/entry-points/android.js @duckduckgo/content-scope-scripts-owners @duckduckgo/android-devs
1717
injected/entry-points/extension-mv3.js @duckduckgo/content-scope-scripts-owners @kzar @sammacbeth
18-
injected/entry-points/chrome.js @duckduckgo/content-scope-scripts-owners @kzar @sammacbeth
1918
injected/entry-points/windows.js @duckduckgo/content-scope-scripts-owners @duckduckgo/team-windows-development
2019

2120
# Test owners

injected/entry-points/chrome.js

Lines changed: 0 additions & 165 deletions
This file was deleted.

injected/integration-test/type-helpers.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export class Build {
8181
'android',
8282
'android-autofill-password-import',
8383
'chrome-mv3',
84-
'chrome',
8584
'firefox',
8685
];
8786
if (items.includes(name)) {

injected/package.json

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,22 @@
33
"scripts": {
44
"postinstall": "npm run copy-sjcl",
55
"copy-sjcl": "node scripts/generateSJCL.js",
6+
"build": "npm run build-types && npm run build-locales && npm run bundle-trackers && npm run bundle-entry-points",
67
"bundle-config": "node scripts/bundleConfig.mjs",
7-
"build": "npm run build-types && npm run build-locales && npm run bundle-trackers && npm run build-firefox && npm run build-chrome && npm run build-apple && npm run build-android && npm run build-windows && npm run build-integration && npm run build-chrome-mv3",
8+
"bundle-entry-points": "node scripts/entry-points.js",
9+
"build-chrome-mv3": "node scripts/entry-points.js",
810
"build-locales": "node scripts/buildLocales.js",
9-
"build-firefox": "node scripts/entry-points.js --platform firefox",
10-
"build-chrome": "node scripts/entry-points.js --platform chrome",
11-
"build-chrome-mv3": "node scripts/entry-points.js --platform chrome-mv3",
12-
"build-apple": "node scripts/entry-points.js --platform apple && node scripts/entry-points.js --platform apple-isolated",
13-
"build-android": "node scripts/entry-points.js --platform android && node scripts/entry-points.js --platform android-autofill-password-import && node scripts/entry-points.js --platform android-broker-protection",
14-
"build-windows": "node scripts/entry-points.js --platform windows",
15-
"build-integration": "node scripts/entry-points.js --platform integration",
1611
"build-types": "node scripts/types.mjs",
1712
"bundle-trackers": "node scripts/bundleTrackers.mjs --output ../build/tracker-lookup.json",
1813
"test-unit": "jasmine --config=unit-test/config.json",
19-
"test-int": "npm run build-integration && npm run playwright",
14+
"test-int": "npm run playwright",
2015
"test-int-x": "xvfb-run --server-args='-screen 0 1024x768x24' npm run test-int",
2116
"test": "npm run lint && npm run test-unit && npm run test-int && npm run playwright",
2217
"serve": "http-server -c-1 --port 3220 integration-test/test-pages",
2318
"playwright": "playwright test --grep-invert '@screenshots'",
2419
"playwright-screenshots": "playwright test --grep '@screenshots'",
25-
"playwright-headed": "playwright test --headed",
26-
"preplaywright": "npm run build-windows && npm run build-apple && npm run build-android",
27-
"preplaywright-headed": "npm run build-windows && npm run build-apple && npm run build-android",
2820
"playwright-e2e": "playwright test -c playwright-e2e.config.js --project duckplayer-e2e",
29-
"playwright-e2e-headed": "npm run playwright-e2e -- --headed",
30-
"preplaywright-e2e": "npm run build-windows && npm run build-apple"
21+
"playwright-e2e-headed": "npm run playwright-e2e -- --headed"
3122
},
3223
"type": "module",
3324
"dependencies": {
@@ -39,18 +30,12 @@
3930
"@canvas/image-data": "^1.0.0",
4031
"@duckduckgo/privacy-configuration": "github:duckduckgo/privacy-configuration#main",
4132
"@fingerprintjs/fingerprintjs": "^4.5.1",
42-
"@rollup/plugin-commonjs": "^28.0.2",
43-
"@rollup/plugin-node-resolve": "^16.0.0",
44-
"@rollup/plugin-replace": "^6.0.2",
4533
"@types/chrome": "^0.0.308",
4634
"@types/jasmine": "^5.1.7",
4735
"@types/node": "^22.13.9",
4836
"@typescript-eslint/eslint-plugin": "^8.20.0",
4937
"fast-check": "^3.23.2",
5038
"jasmine": "^5.6.0",
51-
"minimist": "^1.2.8",
52-
"rollup": "^4.34.9",
53-
"rollup-plugin-import-css": "^3.5.8",
54-
"rollup-plugin-svg-import": "^3.0.0"
39+
"minimist": "^1.2.8"
5540
}
5641
}

injected/playwright.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default defineConfig({
5454
use: { injectName: 'android-autofill-password-import', platform: 'android', ...devices['Galaxy S5'] },
5555
},
5656
{
57-
name: 'chrome',
57+
name: 'chrome-mv3',
5858
testMatch: [
5959
'integration-test/remote-pages.spec.js',
6060
'integration-test/cookie.spec.js',
@@ -64,7 +64,7 @@ export default defineConfig({
6464
'integration-test/utils.spec.js',
6565
'integration-test/web-compat.spec.js',
6666
],
67-
use: { injectName: 'chrome', platform: 'extension', ...devices['Desktop Chrome'] },
67+
use: { injectName: 'chrome-mv3', platform: 'extension', ...devices['Desktop Chrome'] },
6868
},
6969
{
7070
name: 'firefox',
@@ -93,7 +93,7 @@ export default defineConfig({
9393
webServer: {
9494
reuseExistingServer: true,
9595
ignoreHTTPSErrors: true,
96-
command: 'npm run serve',
96+
command: 'npm run bundle-entry-points && npm run serve',
9797
port: 3220,
9898
},
9999
use: {

injected/scripts/entry-points.js

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { postProcess, rollupScript } from './utils/build.js';
1+
import { bundle } from './utils/build.js';
22
import { parseArgs, write } from '../../scripts/script-utils.js';
3-
import { camelcase } from '../src/utils.js';
4-
5-
const contentScopePath = 'src/content-scope-features.js';
6-
const contentScopeName = 'contentScopeFeatures';
73

84
/**
95
* @typedef Build
106
* @property {string} input
117
* @property {string[]} output
12-
* @property {boolean} [postProcess] - optional value to post-process an output file
138
*
149
* @typedef {Record<NonNullable<ImportMeta['injectName']>, Build>} BuildManifest
1510
*/
@@ -22,7 +17,6 @@ const builds = {
2217
},
2318
apple: {
2419
input: 'entry-points/apple.js',
25-
postProcess: true,
2620
output: ['../Sources/ContentScopeScripts/dist/contentScope.js'],
2721
},
2822
'apple-isolated': {
@@ -34,11 +28,11 @@ const builds = {
3428
output: ['../build/android/contentScope.js'],
3529
},
3630
'android-broker-protection': {
37-
input: 'entry-points/android',
31+
input: 'entry-points/android.js',
3832
output: ['../build/android/brokerProtection.js'],
3933
},
4034
'android-autofill-password-import': {
41-
input: 'entry-points/android',
35+
input: 'entry-points/android.js',
4236
output: ['../build/android/autofillPasswordImport.js'],
4337
},
4438
windows: {
@@ -57,65 +51,33 @@ const builds = {
5751
input: 'entry-points/extension-mv3.js',
5852
output: ['../build/chrome-mv3/inject.js'],
5953
},
60-
chrome: {
61-
input: 'entry-points/chrome.js',
62-
output: ['../build/chrome/inject.js'],
63-
},
6454
};
6555

66-
async function initOther(injectScriptPath, platformName) {
67-
const identName = `inject${camelcase(platformName)}`;
68-
const injectScript = await rollupScript({
69-
scriptPath: injectScriptPath,
70-
name: identName,
71-
platform: platformName,
72-
});
73-
const outputScript = injectScript;
74-
return outputScript;
75-
}
76-
77-
/**
78-
* @param {string} entry
79-
* @param {string} platformName
80-
*/
81-
async function initChrome(entry, platformName) {
82-
const replaceString = '/* global contentScopeFeatures */';
83-
const injectScript = await rollupScript({ scriptPath: entry, platform: platformName });
84-
const contentScope = await rollupScript({
85-
scriptPath: contentScopePath,
86-
name: contentScopeName,
87-
platform: platformName,
88-
});
89-
// Encode in URI format to prevent breakage (we could choose to just escape ` instead)
90-
// NB: .replace(/\r\n/g, "\n") is needed because in Windows rollup generates CRLF line endings
91-
const encodedString = encodeURI(contentScope.toString().replace(/\r\n/g, '\n'));
92-
const outputScript = injectScript.toString().replace(replaceString, '${decodeURI("' + encodedString + '")}');
93-
return outputScript;
94-
}
95-
9656
async function init() {
9757
// verify the input
98-
const requiredFields = ['platform'];
58+
const requiredFields = [];
9959
const args = parseArgs(process.argv.slice(2), requiredFields);
100-
const build = builds[args.platform];
101-
102-
if (!build) {
103-
throw new Error('unsupported platform: ' + args.platform);
104-
}
10560

106-
let output;
107-
if (args.platform === 'chrome') {
108-
output = await initChrome(build.input, args.platform);
109-
} else {
110-
output = await initOther(build.input, args.platform);
111-
if (build.postProcess) {
112-
const processResult = await postProcess(output);
113-
output = processResult.code;
61+
// if a platform was given as an argument, just build that platform
62+
if (args.platform) {
63+
const build = builds[args.platform];
64+
if (!build) {
65+
throw new Error('unsupported platform: ' + args.platform);
11466
}
67+
const output = await bundle({ scriptPath: build.input, platform: args.platform });
68+
69+
// bundle and write the output
70+
write([build.output], output);
71+
72+
return;
11573
}
11674

117-
// bundle and write the output
118-
write([build.output], output);
75+
// otherwise, just build them all
76+
for (const [injectName, build] of Object.entries(builds)) {
77+
const output = await bundle({ scriptPath: build.input, platform: injectName });
78+
write(build.output, output);
79+
console.log('✅', injectName, build.output[0]);
80+
}
11981
}
12082

12183
init();

0 commit comments

Comments
 (0)