Skip to content

Commit a1722f2

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

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292
"version": "oclif readme && git add README.md",
9393
"build": "npm run clean && npm run compile",
9494
"clean": "rm -rf ./dist tsconfig.build.tsbuildinfo",
95-
"compile": "tsc -b tsconfig.json",
95+
"compile": "tsc -b tsconfig.json && npm run patch-load-data-url",
96+
"patch-load-data-url": "node scripts/patch-load-data-url-file.js",
9697
"prepack": "npm run build && oclif manifest && oclif readme",
9798
"test:unit": "mocha --forbid-only \"test/unit/**/*.test.ts\"",
9899
"test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\""
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
function replaceFileContent(filePath, newContent) {
5+
fs.writeFile(filePath, newContent, 'utf8', (err) => {
6+
if (err) {
7+
console.error(`Error writing to file: ${err.message}`);
8+
} else {
9+
console.log(`${filePath} : File content patched successfully!`);
10+
}
11+
});
12+
}
13+
14+
const loadDataURLFunctionFilePath = path.join(process.cwd(), 'dist/util/cloud-function/load-data-url.js');
15+
const loadDataURLFunctionCode = `export function loadDataURL(dataURL) { return import(dataURL); }`;
16+
17+
replaceFileContent(loadDataURLFunctionFilePath, loadDataURLFunctionCode);

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)