Skip to content

Commit 257e410

Browse files
committed
Fix node_modules + typescript + ESM issues for real
There were some weird issues with esbuild earlier resolving node_modules in cjs form incorrectly leading to require() statements inside the bundled index.js. Due to production issues, hacks were made to allow a deploy but those have been reverted and now this is an attempt to correctly fix the resolution issues. If this does not work, we should look at things like shimming require() as listed in evanw/esbuild#1921 (comment) and explained in: evanw/esbuild#3637 (comment) This solution still uses external node_modules.
1 parent e8e2c40 commit 257e410

7 files changed

+55
-5
lines changed

firebase.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"npm --prefix \"$RESOURCE_DIR\" run build"
99
],
1010
"ignore": [
11-
"node_modules",
1211
".git",
1312
"firebase-debug.log",
1413
"firebase-debug.*.log",

functions/.gcloudignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
.git
1414
.gitignore
1515

16-
node_modules
16+
#node_modules
1717
#!include:.gitignore

functions/jest.config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ const config: Config = {
9494
// ],
9595

9696
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
97-
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
97+
moduleNameMapper: pathsToModuleNameMapper(
98+
Object.fromEntries(Object.entries(compilerOptions.paths).filter(e => e[0] !== '*')),
99+
{ prefix: '<rootDir>/' }),
98100

99101
extensionsToTreatAsEsm: ['.ts'],
100102

functions/package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"scripts": {
55
"lint": "eslint --ext .js,.ts .",
66
"build": "run-s build:tsc build:esbuild",
7-
"build:esbuild": "esbuild src/index.ts --bundle --platform=node --outfile=lib/index.js --format=esm --external:./node_modules/* --packages=external",
7+
"build:esbuild": "esbuild src/index.ts --bundle --platform=node --outfile=lib/index.js --format=esm --external:./node_modules/*",
88
"build:tsc": "tsc",
99
"build:watch": "run-p 'build:tsc -- --watch --preserveWatchOutput' 'build:esbuild -- --watch'",
1010
"dev": "run-p build:watch dev:*",
@@ -24,6 +24,7 @@
2424
"@google-cloud/functions-framework": "^3.4.0",
2525
"@google-cloud/pubsub": "^4.4.0",
2626
"@types/node-gzip": "^1.1.3",
27+
"csv-parse": "^5.5.6",
2728
"csv-stringify": "^6.5.0",
2829
"date-fns": "^2.30.0",
2930
"firebase": "^10.12.2",
@@ -33,6 +34,7 @@
3334
"lodash.isequal": "^4.5.0",
3435
"lzma-native": "^8.0.6",
3536
"sentence-splitter": "^5.0.0",
37+
"source-map-support": "^0.5.21",
3638
"youtubei.js": "^9.4.0"
3739
},
3840
"devDependencies": {
@@ -47,7 +49,6 @@
4749
"firebase-functions-test": "^3.1.0",
4850
"jest": "^29.7.0",
4951
"npm-run-all": "^4.1.5",
50-
"source-map-support": "^0.5.21",
5152
"ts-jest": "^29.1.4",
5253
"typescript": "^5.5.2"
5354
},

functions/src/transcript.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as TestingUtils from './utils/testing';
2+
3+
describe('transcript', () => {
4+
it('GET retrieves transcript', async () => {
5+
const response = await TestingUtils.fetchEndpoint(
6+
'transcript',
7+
'GET',
8+
{ category: 'sps-board',
9+
vid: 'MT2zjpRbQJA' });
10+
expect(response.status).toStrictEqual(200);
11+
const responseJson = await response.json();
12+
expect(responseJson.ok).toStrictEqual(true);
13+
});
14+
});

functions/src/video_queue.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as TestingUtils from './utils/testing';
2+
import { getCategoryPrivateDb } from './utils/firebase';
3+
import { getAllCategories } from './utils/path';
4+
5+
describe('video_queue', () => {
6+
beforeAll(TestingUtils.beforeAll);
7+
8+
it('Access With auth_code', async () => {
9+
const NEW_VIDS = ['a','b','c'];
10+
const category = getAllCategories()[0];
11+
getCategoryPrivateDb(category, 'new_vids').set(NEW_VIDS);
12+
const response = await TestingUtils.fetchEndpoint(
13+
'video_queue',
14+
'GET',
15+
{ user_id: TestingUtils.FAKE_USER_ID,
16+
auth_code: TestingUtils.FAKE_AUTH_CODE });
17+
expect(response.status).toStrictEqual(200);
18+
const responseJson = await response.json();
19+
expect(responseJson.ok).toStrictEqual(true);
20+
expect(responseJson.data[category]).toEqual(NEW_VIDS);
21+
});
22+
23+
});

0 commit comments

Comments
 (0)