|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
| 4 | +import * as path from 'path'; |
4 | 5 | import * as url from 'url'; |
| 6 | +import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc'; |
5 | 7 | import { AzFuncSystemError } from './errors'; |
6 | 8 | import { PackageJson } from './parsers/parsePackageJson'; |
| 9 | +import { WorkerChannel } from './WorkerChannel'; |
| 10 | +import LogCategory = rpc.RpcLog.RpcLogCategory; |
| 11 | +import LogLevel = rpc.RpcLog.Level; |
7 | 12 |
|
8 | | -export async function loadScriptFile(filePath: string, packageJson: PackageJson): Promise<unknown> { |
9 | | - let script: unknown; |
10 | | - if (isESModule(filePath, packageJson)) { |
11 | | - const fileUrl = url.pathToFileURL(filePath); |
12 | | - if (fileUrl.href) { |
13 | | - // use eval so it doesn't get compiled into a require() |
14 | | - script = await eval('import(fileUrl.href)'); |
| 13 | +export async function loadScriptFile( |
| 14 | + channel: WorkerChannel, |
| 15 | + filePath: string, |
| 16 | + packageJson: PackageJson |
| 17 | +): Promise<unknown> { |
| 18 | + const start = Date.now(); |
| 19 | + try { |
| 20 | + let script: unknown; |
| 21 | + if (isESModule(filePath, packageJson)) { |
| 22 | + const fileUrl = url.pathToFileURL(filePath); |
| 23 | + if (fileUrl.href) { |
| 24 | + // use eval so it doesn't get compiled into a require() |
| 25 | + script = await eval('import(fileUrl.href)'); |
| 26 | + } else { |
| 27 | + throw new AzFuncSystemError(`'${filePath}' could not be converted to file URL (${fileUrl.href})`); |
| 28 | + } |
15 | 29 | } else { |
16 | | - throw new AzFuncSystemError(`'${filePath}' could not be converted to file URL (${fileUrl.href})`); |
| 30 | + script = require(/* webpackIgnore: true */ filePath); |
17 | 31 | } |
18 | | - } else { |
19 | | - script = require(/* webpackIgnore: true */ filePath); |
| 32 | + return script; |
| 33 | + } finally { |
| 34 | + warnIfLongLoadTime(channel, filePath, start); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function warnIfLongLoadTime(channel: WorkerChannel, filePath: string, start: number): void { |
| 39 | + const timeElapsed = Date.now() - start; |
| 40 | + const rfpName = 'WEBSITE_RUN_FROM_PACKAGE'; |
| 41 | + const rfpValue = process.env[rfpName]; |
| 42 | + if (timeElapsed > 1000 && (rfpValue === undefined || rfpValue === '0')) { |
| 43 | + channel.log({ |
| 44 | + message: `Loading "${path.basename(filePath)}" took ${timeElapsed}ms`, |
| 45 | + level: LogLevel.Warning, |
| 46 | + logCategory: LogCategory.System, |
| 47 | + }); |
| 48 | + channel.log({ |
| 49 | + message: `Set "${rfpName}" to "1" to significantly improve load times. Learn more here: https://aka.ms/AAjon54`, |
| 50 | + level: LogLevel.Warning, |
| 51 | + logCategory: LogCategory.System, |
| 52 | + }); |
20 | 53 | } |
21 | | - return script; |
22 | 54 | } |
23 | 55 |
|
24 | 56 | export function isESModule(filePath: string, packageJson: PackageJson): boolean { |
|
0 commit comments