Skip to content

Commit 077b18d

Browse files
committed
CL-1543 | Patch the dynamic import code after tsc compile
1 parent 1bd1d5c commit 077b18d

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@rollup/plugin-commonjs": "^28.0.2",
3030
"@rollup/plugin-json": "^6.1.0",
3131
"@rollup/plugin-node-resolve": "^16.0.0",
32+
"@rollup/plugin-typescript": "^12.1.2",
3233
"@types/express": "^4.17.21",
3334
"@types/express-serve-static-core": "^4.17.34",
3435
"adm-zip": "^0.5.16",
@@ -92,7 +93,8 @@
9293
"version": "oclif readme && git add README.md",
9394
"build": "npm run clean && npm run compile",
9495
"clean": "rm -rf ./dist tsconfig.build.tsbuildinfo",
95-
"compile": "tsc -b tsconfig.json",
96+
"compile": "tsc -b tsconfig.json && npm run patch-load-data-url",
97+
"patch-load-data-url": "node scripts/patch-load-data-url-file.js",
9698
"prepack": "npm run build && oclif manifest && oclif readme",
9799
"test:unit": "mocha --forbid-only \"test/unit/**/*.test.ts\"",
98100
"test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\""
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// The tsc compiler currently compiles to "commonjs" output as per the tsconfig.
2+
// As a result, the use of dynamic import "import()" in the cloud-function logic, gets replaced with "require()" in the dist/ output.
3+
// "require()" however does not support importing modules from data url strings causing this logic to break.
4+
// Therefore, we patch the dist/ output to again use dynamic imports "import()" after tsc compilation.
5+
6+
const rollup = require('rollup');
7+
const typescript = require('@rollup/plugin-typescript');
8+
const path = require('path');
9+
10+
async function transpileTsFileToESM(inputFile, outputFile) {
11+
const bundle = await rollup.rollup({
12+
input: inputFile,
13+
plugins: [typescript({ module: 'esnext', declaration: false })],
14+
});
15+
16+
await bundle.write({
17+
file: outputFile,
18+
format: 'esm',
19+
sourcemap: false,
20+
});
21+
}
22+
23+
async function patchLoadDataURLFunction() {
24+
const loadDataURLInputFilePath = path.join(process.cwd(), 'src/util/cloud-function/load-data-url.ts');
25+
const loadDataURLOutputFilePath = path.join(process.cwd(), 'dist/util/cloud-function/load-data-url.js');
26+
await transpileTsFileToESM(loadDataURLInputFilePath, loadDataURLOutputFilePath);
27+
console.log(`${loadDataURLInputFilePath} : File patched successfully`);
28+
}
29+
30+
patchLoadDataURLFunction();

src/util/cloud-function/cloud-functions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import express, {
44
Response,
55
} from 'express';
66
import { Express } from 'express-serve-static-core';
7-
import path, { normalize } from "path";
7+
import path from "path";
88

99
import { CloudFunctionsValidator } from "./cloud-functions-validator";
1010
import {
@@ -20,6 +20,7 @@ import rollup from "rollup";
2020
import { nodeResolve } from "@rollup/plugin-node-resolve";
2121
import commonjs from "@rollup/plugin-commonjs";
2222
import json from "@rollup/plugin-json";
23+
import { loadDataURL } from './load-data-url';
2324

2425
export class CloudFunctions {
2526
private cloudFunctionsDirectoryPath: string;
@@ -189,7 +190,7 @@ export class CloudFunctions {
189190
private async buildHandlerForFilepath(cloudFunctionFilePath: string) {
190191
const bundle = await rollup.rollup({
191192
input: cloudFunctionFilePath,
192-
plugins: [nodeResolve({ preferBuiltins: false }), commonjs(), json()],
193+
plugins: [nodeResolve({ preferBuiltins: true }), commonjs(), json()],
193194
});
194195

195196
const { output } = await bundle.generate({
@@ -202,7 +203,7 @@ export class CloudFunctions {
202203
const builtCodeInDataURLFormat =
203204
"data:text/javascript;base64," + Buffer.from(builtCode).toString("base64");
204205

205-
const module = await import(builtCodeInDataURLFormat);
206+
const module = await loadDataURL(builtCodeInDataURLFormat);
206207

207208
let handler = null;
208209
const isDefaultExportESModuleFunction = typeof module.default === 'function';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function loadDataURL(dataURL: string) {
2+
return import(dataURL);
3+
}

0 commit comments

Comments
 (0)