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)

This solution still uses external node_modules.
  • Loading branch information
awong-dev committed Jul 1, 2024
1 parent e8e2c40 commit 257e410
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 5 deletions.
1 change: 0 additions & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"npm --prefix \"$RESOURCE_DIR\" run build"
],
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log",
Expand Down
2 changes: 1 addition & 1 deletion functions/.gcloudignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
.git
.gitignore

node_modules
#node_modules
#!include:.gitignore
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 257e410

Please sign in to comment.