Skip to content

Commit 29e856f

Browse files
authored
Vendor workerbox to fix signals esm issue in some environments (#1155)
1 parent 91566ed commit 29e856f

File tree

17 files changed

+1018
-25
lines changed

17 files changed

+1018
-25
lines changed

.buildkite/pipeline.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ steps:
140140
- HUSKY=0 yarn install --immutable
141141
- echo "--- Build bundles"
142142
- yarn turbo run --filter='./packages/signals/*' build
143+
- echo "--- Assert Workerbox Up-to-Date"
144+
- yarn workspace @segment/analytics-signals run assert-workerbox-built
143145
- echo "+++ Run Lint"
144146
- yarn turbo run --filter='./packages/signals/*' lint
145147
- echo "+++ Run Tests"

.changeset/nasty-drinks-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-signals': minor
3+
---
4+
5+
- Fix npm installation esm error by vendoring esm-only module workerbox

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ reports/*
3131

3232
.changelog
3333
.turbo
34-
/test-results/
34+
test-results/
3535
playwright-report/
3636
playwright/.cache/
3737
tmp.tsconfig.json

packages/signals/signals-integration-tests/playwright.config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ const config: PlaywrightTestConfig = {
3434
reporter: 'html',
3535
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
3636
use: {
37-
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
38-
actionTimeout: 0,
3937
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
40-
trace: 'on-first-retry',
38+
trace: 'on',
4139
},
4240

4341
/* Configure projects for major browsers */

packages/signals/signals-integration-tests/src/helpers/base-page-object.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,7 @@ export class BasePage {
231231
},
232232
body: JSON.stringify({ foo: 'bar' }),
233233
...request,
234-
})
235-
.then(console.log)
236-
.catch(console.error)
234+
}).catch(console.error)
237235
},
238236
{ url, request }
239237
)

packages/signals/signals-integration-tests/src/helpers/log-console.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { Page } from '@playwright/test'
22

33
export const logConsole = (page: Page) => {
44
page.on('console', (msg) => {
5-
console.log(`[${msg.type()}]`, msg.text())
5+
console.log(`console.${msg.type()}:`, msg.text())
6+
})
7+
page.on('pageerror', (error) => {
8+
console.error('Page error:', error)
69
})
710
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
module.exports = require("@internal/config").lintStagedConfig
1+
module.exports = {
2+
...require("@internal/config").lintStagedConfig,
3+
'src/lib/workerbox/*.{js,ts,html}': ['yarn workerbox']
4+
}
5+

packages/signals/signals/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"build:cjs": "yarn tsc -p tsconfig.build.json --outDir ./dist/cjs --module commonjs",
3333
"build:bundle": "NODE_ENV=production yarn run webpack",
3434
"build:bundle-dev": "NODE_ENV=development yarn run webpack",
35+
"workerbox": "node scripts/build-workerbox.js",
36+
"assert-workerbox-built": "sh scripts/assert-workerbox-built.sh",
3537
"watch": "yarn concurrently 'yarn build:bundle-dev --watch' 'yarn build:esm --watch'",
3638
"version": "sh scripts/version.sh",
3739
"watch:test": "yarn test --watch",
@@ -44,8 +46,7 @@
4446
"dependencies": {
4547
"@segment/analytics-generic-utils": "1.2.0",
4648
"idb": "^8.0.0",
47-
"tslib": "^2.4.1",
48-
"workerboxjs": "^6.1.1"
49+
"tslib": "^2.4.1"
4950
},
5051
"peerDependencies": {
5152
"@segment/analytics-next": ">1.72.0"
@@ -60,6 +61,7 @@
6061
"@internal/config-webpack": "workspace:^",
6162
"@internal/test-helpers": "workspace:^",
6263
"fake-indexeddb": "^6.0.0",
64+
"minify": "^11.4.1",
6365
"node-fetch": "^2.6.7"
6466
}
6567
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
# A CI script to ensure people remember to rebuild workerbox related files if workerbox changes
3+
4+
node scripts/build-workerbox.js
5+
# Check for changes in the workerbox directory
6+
changed_files=$(git diff --name-only | grep 'lib/workerbox')
7+
8+
# Check for changes in the workerbox directory
9+
if [ -n "$changed_files" ]; then
10+
echo "Error: Changes detected in the workerbox directory. Please commit the changed files:"
11+
echo "$changed_files"
12+
exit 1
13+
else
14+
echo "Files have not changed"
15+
exit 0
16+
fi
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const fs = require('fs')
2+
const esbuild = require('esbuild')
3+
const path = require('path')
4+
5+
// Note: This was adopted from the https://github.com/markwylde/workerbox/blob/master/build.js
6+
console.log('Building workerbox...')
7+
8+
const DEBUG = process.env.DEBUG === 'true'
9+
if (DEBUG) console.log('Minification off.')
10+
11+
async function writeFileWithDirs(filePath, data) {
12+
// Extract the directory path from the file path
13+
const dir = path.dirname(filePath)
14+
15+
// Ensure the directory exists
16+
await fs.promises.mkdir(dir, { recursive: true })
17+
18+
// Write the file
19+
await fs.promises.writeFile(filePath, data, 'utf8')
20+
}
21+
22+
async function build() {
23+
console.log(new Date(), 'rebuilding...')
24+
25+
// clean up dist folder
26+
await fs.promises.rm('./src/lib/workerbox/dist', {
27+
recursive: true,
28+
force: true,
29+
})
30+
31+
await esbuild.build({
32+
entryPoints: ['./src/lib/workerbox/worker.ts'],
33+
bundle: true,
34+
outfile: './src/lib/workerbox/dist/worker.js',
35+
minify: !DEBUG,
36+
})
37+
38+
const jsData = await fs.promises.readFile(
39+
'./src/lib/workerbox/dist/worker.js',
40+
'utf8'
41+
)
42+
43+
const TEMPLATE_PLACEHOLDER = `{{WORKERSCRIPT}}`
44+
const htmlData = (
45+
await fs.promises.readFile('./src/lib/workerbox/worker.html', 'utf8')
46+
).replace(TEMPLATE_PLACEHOLDER, jsData)
47+
48+
await writeFileWithDirs('./src/lib/workerbox/dist/worker.html', htmlData)
49+
await writeFileWithDirs(
50+
'./src/lib/workerbox/worker.generated.ts',
51+
[
52+
'/* eslint-disable */',
53+
`// built from /dist/worker.html`,
54+
`export default atob('${Buffer.from(htmlData).toString('base64')}');`,
55+
].join('\n')
56+
)
57+
}
58+
59+
build()
60+
.then(() => console.log('Build successful'))
61+
.catch((err) => {
62+
console.error(err)
63+
process.exit(1)
64+
})

0 commit comments

Comments
 (0)