Skip to content

Commit cc5ca4b

Browse files
authored
Script v2: Apply prettier (#5718)
* Apply prettier * Fix issue with quotes in test setup * Fix other issues with customSubmitHandlerStub * Fix format with cookies fixtures
1 parent 6e9bc6e commit cc5ca4b

87 files changed

Lines changed: 2313 additions & 1299 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/node.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ jobs:
3232
- run: npm run check-format --prefix ./assets
3333
- run: npm run test --prefix ./assets
3434
- run: npm run lint --prefix ./tracker
35-
# - run: npm run check-format --prefix ./tracker
35+
- run: npm run check-format --prefix ./tracker
3636
- run: npm run deploy --prefix ./tracker

tracker/ARCHITECTURE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Plausible provides a web 'snippet' users can include on their site, tooling for
3232
As Plausible doesn't have the workforce to maintain multiple code bases, everything is built from the same underlying source code.
3333

3434
This is achieved by:
35+
3536
- Having a canonical list of compiled variants in `tracker/compiler/variants.json`
3637
- Using `COMPILE` globals to toggle certain functionality on/off depending on the variant.
3738
- Having the script minifier drop dead branches of the code from each variant.
@@ -74,7 +75,7 @@ Contains source code to verify that the tracker script is installed correctly wi
7475

7576
### `tracker/compiler/variants.json`
7677

77-
Contains a list of variants and associated global `COMPILE` flags.
78+
Contains a list of variants and associated global `COMPILE` flags.
7879

7980
- `manualVariants` are the main variants we need to care for, containing both the web snippet and npm package.
8081
- `legacyVariants` is a list of all the legacy variants we support, generated by `tracker/compiler/generate-variants.js`

tracker/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Use `node compile.js --watch` to watch for changes.
2525
Use `node compile.js --web-snippet` if you need to update web snippet code.
2626

2727
### Tests
28+
2829
Tests can be run in UI mode via `npm run playwright --ui`. This helps with debugging.
2930

3031
### NPM package
@@ -40,10 +41,12 @@ More instructions can be found in [yalc repo](https://github.com/wclr/yalc).
4041
## Cloud deployment
4142

4243
Handled via PRs. When making tracker changes, it's required to:
44+
4345
- Tag your PR with a `tracker release:` label
4446
- Update `tracker/CHANGELOG.md`
4547

4648
After merge github actions automatically:
49+
4750
- includes the updated tracker scripts in the next cloud deploy
4851
- updates npm package package.json and CHANGELOG.md with the new version
4952
- releases the new package version on NPM.

tracker/compile.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,39 @@ import chokidar from 'chokidar'
44

55
const { values } = parseArgs({
66
options: {
7-
'watch': {
7+
watch: {
88
type: 'boolean',
99
short: 'w'
1010
},
11-
'help': {
12-
type: 'boolean',
11+
help: {
12+
type: 'boolean'
1313
},
14-
'suffix': {
14+
suffix: {
1515
type: 'string',
1616
default: ''
1717
},
1818
'web-snippet': {
19-
type: 'boolean',
19+
type: 'boolean'
2020
}
2121
}
2222
})
2323

2424
if (values.help) {
2525
console.log('Usage: node compile.js [flags]')
2626
console.log('Options:')
27-
console.log(' --watch, -w Watch src/ directory for changes and recompile')
28-
console.log(' --suffix, -s Suffix to add to the output file name. Used for testing script size changes')
29-
console.log(' --help Show this help message')
30-
console.log(' --web-snippet Compile and output the web snippet')
31-
process.exit(0);
27+
console.log(
28+
' --watch, -w Watch src/ directory for changes and recompile'
29+
)
30+
console.log(
31+
' --suffix, -s Suffix to add to the output file name. Used for testing script size changes'
32+
)
33+
console.log(
34+
' --help Show this help message'
35+
)
36+
console.log(
37+
' --web-snippet Compile and output the web snippet'
38+
)
39+
process.exit(0)
3240
}
3341

3442
if (values['web-snippet']) {

tracker/compiler/analyze-sizes.js

Lines changed: 87 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ import variantsFile from './variants.json' with { type: 'json' }
2323

2424
const { values } = parseArgs({
2525
options: {
26-
'help': {
27-
type: 'boolean',
26+
help: {
27+
type: 'boolean'
2828
},
29-
'currentSuffix': {
29+
currentSuffix: {
3030
type: 'string',
3131
default: 'current'
3232
},
33-
'baselineSuffix': {
33+
baselineSuffix: {
3434
type: 'string',
3535
default: 'master'
3636
},
37-
'usePreviousData': {
37+
usePreviousData: {
3838
type: 'boolean',
3939
default: false
4040
}
@@ -43,31 +43,43 @@ const { values } = parseArgs({
4343

4444
const { currentSuffix, baselineSuffix } = values
4545
const __dirname = path.dirname(fileURLToPath(import.meta.url))
46-
const TRACKER_FILES_DIR = path.join(__dirname, "../../priv/tracker/js/")
47-
const NPM_PACKAGE_FILES_DIR = path.join(__dirname, "../npm_package")
46+
const TRACKER_FILES_DIR = path.join(__dirname, '../../priv/tracker/js/')
47+
const NPM_PACKAGE_FILES_DIR = path.join(__dirname, '../npm_package')
4848

4949
const HEADER = ['', 'Brotli', 'Gzip', 'Uncompressed']
5050

5151
if (values.help) {
5252
console.log('Usage: node analyze-sizes.js master current')
5353
console.log('Options:')
5454
console.log(' --help Show this help message')
55-
console.log(' --currentSuffix The suffix of the current script variants (see suffix flag for compile.js). Default: current')
56-
console.log(' --baselineSuffix The suffix of the previous script variants (see suffix flag for compile.js). Default: master')
57-
console.log(' --usePreviousData Use data from a previous run, speeding up the analysis.')
58-
process.exit(0);
55+
console.log(
56+
' --currentSuffix The suffix of the current script variants (see suffix flag for compile.js). Default: current'
57+
)
58+
console.log(
59+
' --baselineSuffix The suffix of the previous script variants (see suffix flag for compile.js). Default: master'
60+
)
61+
console.log(
62+
' --usePreviousData Use data from a previous run, speeding up the analysis.'
63+
)
64+
process.exit(0)
5965
}
6066

61-
6267
let fileData
6368
if (values.usePreviousData) {
64-
fileData = JSON.parse(fs.readFileSync(path.join(__dirname, '.analyze-sizes.json'), 'utf8'))
69+
fileData = JSON.parse(
70+
fs.readFileSync(path.join(__dirname, '.analyze-sizes.json'), 'utf8')
71+
)
6572
} else {
6673
fileData = readPlausibleScriptSizes()
67-
fs.writeFileSync(path.join(__dirname, '.analyze-sizes.json'), JSON.stringify(fileData))
74+
fs.writeFileSync(
75+
path.join(__dirname, '.analyze-sizes.json'),
76+
JSON.stringify(fileData)
77+
)
6878
}
6979

70-
const manualVariants = variantsFile.manualVariants.map((variant) => `'${variant.name}'`).join(', ')
80+
const manualVariants = variantsFile.manualVariants
81+
.map((variant) => `'${variant.name}'`)
82+
.join(', ')
7183
const ctes = `
7284
WITH
7385
array(${manualVariants}) as manual_variants,
@@ -99,21 +111,27 @@ WITH
99111
)
100112
`
101113

102-
const mainVariantResults = clickhouseLocal(`
114+
const mainVariantResults = clickhouseLocal(
115+
`
103116
${ctes}
104117
SELECT *
105118
FROM data
106119
WHERE not is_legacy_variant
107120
ORDER BY variant
108-
`, fileData)
121+
`,
122+
fileData
123+
)
109124

110-
const legacyVariantResults = clickhouseLocal(`
125+
const legacyVariantResults = clickhouseLocal(
126+
`
111127
${ctes}
112128
SELECT *
113129
FROM data
114130
WHERE is_legacy_variant AND has(important_variants, variant)
115131
ORDER BY length(variant)
116-
`, fileData)
132+
`,
133+
fileData
134+
)
117135

118136
const rowAsMap = `
119137
map(
@@ -133,7 +151,8 @@ map(
133151
)
134152
`
135153

136-
const [summary] = clickhouseLocal(`
154+
const [summary] = clickhouseLocal(
155+
`
137156
${ctes}
138157
SELECT
139158
count() AS total_variants,
@@ -168,26 +187,45 @@ const [summary] = clickhouseLocal(`
168187
'brotli_increase_percentage', toString(median(brotli_increase_percentage))
169188
) AS median_result
170189
FROM data
171-
`, fileData)
190+
`,
191+
fileData
192+
)
172193

173-
console.log(`Analyzed ${summary.total_variants} tracker script variants for size changes.`)
174-
console.log(`The following tables summarize the results, with comparison with the baseline version in parentheses.\n`)
194+
console.log(
195+
`Analyzed ${summary.total_variants} tracker script variants for size changes.`
196+
)
197+
console.log(
198+
`The following tables summarize the results, with comparison with the baseline version in parentheses.\n`
199+
)
175200

176-
console.log("Main variants:")
201+
console.log('Main variants:')
177202
console.log(createMarkdownTable(mainVariantResults))
178203

179-
console.log("\nImportant legacy variants:")
204+
console.log('\nImportant legacy variants:')
180205
console.log(createMarkdownTable(legacyVariantResults))
181206

182-
console.log("\nSummary:")
183-
console.log(createMarkdownTable([
184-
{ ...summary.largest_variant, variant: `Largest variant (${summary.largest_variant.variant})`},
185-
{ ...summary.max_increase_variant, variant: `Max change (${summary.max_increase_variant.variant})`},
186-
{ ...summary.min_increase_variant, variant: `Min change (${summary.min_increase_variant.variant})`},
187-
summary.median_result
188-
]))
207+
console.log('\nSummary:')
208+
console.log(
209+
createMarkdownTable([
210+
{
211+
...summary.largest_variant,
212+
variant: `Largest variant (${summary.largest_variant.variant})`
213+
},
214+
{
215+
...summary.max_increase_variant,
216+
variant: `Max change (${summary.max_increase_variant.variant})`
217+
},
218+
{
219+
...summary.min_increase_variant,
220+
variant: `Min change (${summary.min_increase_variant.variant})`
221+
},
222+
summary.median_result
223+
])
224+
)
189225

190-
console.log(`\nIn total, ${summary.brotli_increase_percentaged_variants} variants brotli size increased and ${summary.brotli_decreased_variants} variants brotli size decreased.`)
226+
console.log(
227+
`\nIn total, ${summary.brotli_increase_percentaged_variants} variants brotli size increased and ${summary.brotli_decreased_variants} variants brotli size decreased.`
228+
)
191229

192230
function createMarkdownTable(rows) {
193231
return markdownTable([HEADER].concat(rows.map(markdownRow)))
@@ -208,7 +246,6 @@ function markdownRow(row) {
208246
]
209247
}
210248

211-
212249
function sizeColumn(row, key) {
213250
const currentSize = row[`current_${key}`]
214251
const previousIncrease = row[`${key}_increase`]
@@ -230,11 +267,13 @@ function addSign(value) {
230267
}
231268

232269
function readPlausibleScriptSizes() {
233-
const trackerFileSizes = fs.readdirSync(TRACKER_FILES_DIR)
270+
const trackerFileSizes = fs
271+
.readdirSync(TRACKER_FILES_DIR)
234272
.filter(isRelevantFile)
235273
.map((filename) => readFileSize(filename, TRACKER_FILES_DIR))
236274

237-
const npmPackageFileSizes = fs.readdirSync(NPM_PACKAGE_FILES_DIR)
275+
const npmPackageFileSizes = fs
276+
.readdirSync(NPM_PACKAGE_FILES_DIR)
238277
.filter(isRelevantFile)
239278
.map((filename) => readFileSize(filename, NPM_PACKAGE_FILES_DIR))
240279

@@ -246,7 +285,10 @@ function readFileSize(filename, basepath) {
246285
const [_, variant, suffix] = /(.*)[.]js(.*)/.exec(filename)
247286

248287
return {
249-
variant: (basepath === TRACKER_FILES_DIR ? `${variant}.js` : 'npm_package/plausible.js'),
288+
variant:
289+
basepath === TRACKER_FILES_DIR
290+
? `${variant}.js`
291+
: 'npm_package/plausible.js',
250292
suffix,
251293
uncompressed: fs.statSync(filePath).size,
252294
gzip: execSync(`gzip -c -9 "${filePath}"`).length,
@@ -255,18 +297,23 @@ function readFileSize(filename, basepath) {
255297
}
256298

257299
function isRelevantFile(filename) {
258-
return !['.gitkeep', 'p.js'].includes(filename) &&
300+
return (
301+
!['.gitkeep', 'p.js'].includes(filename) &&
259302
filename.includes('.js') &&
260303
(filename.includes(currentSuffix) || filename.includes(baselineSuffix))
304+
)
261305
}
262306

263307
function clickhouseLocal(sql, inputLines = null) {
264308
const options = {}
265309
if (inputLines) {
266-
options.input = inputLines.map(JSON.stringify).join("\n")
310+
options.input = inputLines.map(JSON.stringify).join('\n')
267311
}
268312

269-
const result = execSync(`clickhouse-local --query="${sql}" --format=JSON ${inputLines ? "--input-format=JSONLines" : ""}`, options)
313+
const result = execSync(
314+
`clickhouse-local --query="${sql}" --format=JSON ${inputLines ? '--input-format=JSONLines' : ''}`,
315+
options
316+
)
270317
const json = JSON.parse(result.toString())
271318

272319
return json.data

tracker/compiler/can-skip-compile.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ const COMPILE_DEPENDENCIES = [
1515
]
1616

1717
function currentHash() {
18-
const combinedHash = crypto.createHash('sha256');
18+
const combinedHash = crypto.createHash('sha256')
1919

2020
for (const filePath of COMPILE_DEPENDENCIES) {
2121
try {
22-
const fileContent = fs.readFileSync(filePath);
23-
const fileHash = crypto.createHash('sha256').update(fileContent).digest();
24-
combinedHash.update(fileHash);
22+
const fileContent = fs.readFileSync(filePath)
23+
const fileHash = crypto.createHash('sha256').update(fileContent).digest()
24+
combinedHash.update(fileHash)
2525
} catch (error) {
26-
throw new Error(`Failed to read or hash ${filePath}: ${error.message}`);
26+
throw new Error(`Failed to read or hash ${filePath}: ${error.message}`)
2727
}
2828
}
2929

30-
return combinedHash.digest('hex');
30+
return combinedHash.digest('hex')
3131
}
3232

3333
function lastHash() {

0 commit comments

Comments
 (0)