Skip to content

Commit

Permalink
Fix node_modules + typescript + ESM issues for real
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
awong-dev committed Jun 30, 2024
1 parent 63027b6 commit 2b3060b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
4 changes: 3 additions & 1 deletion functions/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ const config: Config = {
// ],

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

extensionsToTreatAsEsm: ['.ts'],

Expand Down
11 changes: 11 additions & 0 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "run-s build:tsc build:esbuild",
"build:esbuild": "esbuild src/index.ts --bundle --platform=node --outfile=lib/index.js --format=esm --external:./node_modules/* --packages=external",
"build:esbuild": "esbuild src/index.ts --bundle --platform=node --outfile=lib/index.js --format=esm --external:./node_modules/*",
"build:tsc": "tsc",
"build:watch": "run-p 'build:tsc -- --watch --preserveWatchOutput' 'build:esbuild -- --watch'",
"dev": "run-p build:watch dev:*",
Expand All @@ -24,6 +24,7 @@
"@google-cloud/functions-framework": "^3.4.0",
"@google-cloud/pubsub": "^4.4.0",
"@types/node-gzip": "^1.1.3",
"csv-parse": "^5.5.6",
"csv-stringify": "^6.5.0",
"date-fns": "^2.30.0",
"firebase": "^10.12.2",
Expand All @@ -33,6 +34,7 @@
"lodash.isequal": "^4.5.0",
"lzma-native": "^8.0.6",
"sentence-splitter": "^5.0.0",
"source-map-support": "^0.5.21",
"youtubei.js": "^9.4.0"
},
"devDependencies": {
Expand All @@ -47,7 +49,6 @@
"firebase-functions-test": "^3.1.0",
"jest": "^29.7.0",
"npm-run-all": "^4.1.5",
"source-map-support": "^0.5.21",
"ts-jest": "^29.1.4",
"typescript": "^5.5.2"
},
Expand Down
14 changes: 14 additions & 0 deletions functions/src/transcript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as TestingUtils from './utils/testing';

describe('transcript', () => {
it('GET retrieves transcript', async () => {
const response = await TestingUtils.fetchEndpoint(
'transcript',
'GET',
{ category: 'sps-board',
vid: 'MT2zjpRbQJA' });
expect(response.status).toStrictEqual(200);
const responseJson = await response.json();
expect(responseJson.ok).toStrictEqual(true);
});
});
23 changes: 23 additions & 0 deletions functions/src/video_queue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as TestingUtils from './utils/testing';
import { getCategoryPrivateDb } from './utils/firebase';
import { getAllCategories } from './utils/path';

describe('video_queue', () => {
beforeAll(TestingUtils.beforeAll);

it('Access With auth_code', async () => {
const NEW_VIDS = ['a','b','c'];
const category = getAllCategories()[0];
getCategoryPrivateDb(category, 'new_vids').set(NEW_VIDS);
const response = await TestingUtils.fetchEndpoint(
'video_queue',
'GET',
{ user_id: TestingUtils.FAKE_USER_ID,
auth_code: TestingUtils.FAKE_AUTH_CODE });
expect(response.status).toStrictEqual(200);
const responseJson = await response.json();
expect(responseJson.ok).toStrictEqual(true);
expect(responseJson.data[category]).toEqual(NEW_VIDS);
});

});

0 comments on commit 2b3060b

Please sign in to comment.