From 96e9a95a250711127825c8f5d814b3641abe7eef Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 8 Feb 2022 20:07:15 +0100 Subject: [PATCH] fix: use `.js` and `index.js` in imports (#974) --- .eslintrc.cjs | 5 ++++ src/bin.ts | 4 ++-- src/config.ts | 6 ++--- src/function.ts | 4 ++-- src/main.ts | 18 +++++++------- src/manifest.ts | 2 +- src/runtimes/detect_runtime.ts | 4 ++-- src/runtimes/go/builder.ts | 4 ++-- src/runtimes/go/index.ts | 16 ++++++------- src/runtimes/index.ts | 18 +++++++------- src/runtimes/node/bundlers/esbuild/bundler.ts | 18 +++++++------- src/runtimes/node/bundlers/esbuild/index.ts | 16 ++++++------- .../esbuild/plugin_dynamic_imports.ts | 2 +- .../bundlers/esbuild/plugin_native_modules.ts | 6 ++--- .../node/bundlers/esbuild/special_cases.ts | 2 +- .../node/bundlers/esbuild/src_files.ts | 10 ++++---- src/runtimes/node/bundlers/index.ts | 18 +++++++------- src/runtimes/node/bundlers/nft/es_modules.ts | 16 ++++++------- src/runtimes/node/bundlers/nft/index.ts | 18 +++++++------- src/runtimes/node/bundlers/nft/transpile.ts | 4 ++-- src/runtimes/node/bundlers/zisi/index.ts | 6 ++--- .../node/bundlers/zisi/list_imports.ts | 6 ++--- src/runtimes/node/bundlers/zisi/nested.ts | 2 +- src/runtimes/node/bundlers/zisi/side_files.ts | 2 +- src/runtimes/node/bundlers/zisi/src_files.ts | 24 +++++++++---------- src/runtimes/node/bundlers/zisi/traverse.ts | 18 +++++++------- src/runtimes/node/bundlers/zisi/tree_shake.ts | 2 +- src/runtimes/node/finder.ts | 6 ++--- src/runtimes/node/in_source_config/index.ts | 10 ++++---- .../in_source_config/properties/schedule.ts | 2 +- src/runtimes/node/index.ts | 14 +++++------ src/runtimes/node/parser/exports.ts | 4 ++-- src/runtimes/node/parser/imports.ts | 4 ++-- src/runtimes/node/parser/index.ts | 2 +- .../node/utils/detect_native_module.ts | 2 +- src/runtimes/node/utils/entry_file.ts | 4 ++-- src/runtimes/node/utils/zip.ts | 10 ++++---- src/runtimes/runtime.ts | 16 ++++++------- src/runtimes/rust/builder.ts | 10 ++++---- src/runtimes/rust/index.ts | 20 ++++++++-------- src/utils/archive_size.ts | 2 +- src/utils/format_result.ts | 6 ++--- src/utils/fs.ts | 2 +- src/zip.ts | 20 ++++++++-------- src/zip_binary.ts | 4 ++-- tests/bin.js | 2 +- tests/feature_flags.js | 2 +- tests/fixtures/list/five/index.ts | 3 +++ .../node-typescript-with-imports/function.ts | 3 +++ tests/helpers/main.js | 4 ++-- tests/main.js | 14 +++++------ .../unit/runtimes/node/utils/node_version.js | 2 +- .../unit/runtimes/node/utils/package_json.js | 2 +- 53 files changed, 216 insertions(+), 205 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index bf6aacf1c..bb79bdb88 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -3,6 +3,8 @@ const { overrides } = require('@netlify/eslint-config-node') module.exports = { extends: '@netlify/eslint-config-node', rules: { + 'import/extensions': ['error', 'ignorePackages'], + 'node/no-missing-import': 'off', // This rule enforces using Buffers with `JSON.parse()`. However, TypeScript // does not recognize yet that `JSON.parse()` accepts Buffers as argument. 'unicorn/prefer-json-parse-buffer': 'off', @@ -12,6 +14,9 @@ module.exports = { { files: '*.ts', rules: { + // Pure ES modules with TypeScript require using `.js` instead of `.ts` + // in imports + 'import/extensions': 'off', 'import/no-namespace': 'off', }, }, diff --git a/src/bin.ts b/src/bin.ts index 1785c78c0..413c618b8 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -4,8 +4,8 @@ import { argv, exit } from 'process' import yargs from 'yargs' import { hideBin } from 'yargs/helpers' -import type { ArchiveFormat } from './archive' -import { zipFunctions } from './main' +import type { ArchiveFormat } from './archive.js' +import { zipFunctions } from './main.js' // CLI entry point const runCli = async function () { diff --git a/src/config.ts b/src/config.ts index 09f995370..03a1e8157 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,9 +1,9 @@ import mergeOptions from 'merge-options' import minimatch from 'minimatch' -import { FunctionSource } from './function' -import type { NodeVersionString } from './runtimes/node' -import type { NodeBundlerName } from './runtimes/node/bundlers' +import { FunctionSource } from './function.js' +import type { NodeBundlerName } from './runtimes/node/bundlers/index.js' +import type { NodeVersionString } from './runtimes/node/index.js' export interface FunctionConfig { externalNodeModules?: string[] diff --git a/src/function.ts b/src/function.ts index 47ae72ddb..4192070fe 100644 --- a/src/function.ts +++ b/src/function.ts @@ -1,7 +1,7 @@ import { Stats } from 'fs' -import type { FunctionConfig } from './config' -import type { Runtime, ZipFunctionResult } from './runtimes/runtime' +import type { FunctionConfig } from './config.js' +import type { Runtime, ZipFunctionResult } from './runtimes/runtime.js' // A function that has been processed and turned into an archive. export type FunctionArchive = ZipFunctionResult & { diff --git a/src/main.ts b/src/main.ts index 81ff23a51..90f1caf41 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,14 +1,14 @@ import { extname } from 'path' -import { Config } from './config' -import { FeatureFlags, getFlags } from './feature_flags' -import { FunctionSource } from './function' -import { getFunctionFromPath, getFunctionsFromPaths } from './runtimes' -import { findISCDeclarationsInPath, ISCValues } from './runtimes/node/in_source_config' -import { GetSrcFilesFunction, RuntimeName } from './runtimes/runtime' -import { listFunctionsDirectories, resolveFunctionsDirectories } from './utils/fs' - -export { zipFunction, zipFunctions } from './zip' +import { Config } from './config.js' +import { FeatureFlags, getFlags } from './feature_flags.js' +import { FunctionSource } from './function.js' +import { getFunctionFromPath, getFunctionsFromPaths } from './runtimes/index.js' +import { findISCDeclarationsInPath, ISCValues } from './runtimes/node/in_source_config/index.js' +import { GetSrcFilesFunction, RuntimeName } from './runtimes/runtime.js' +import { listFunctionsDirectories, resolveFunctionsDirectories } from './utils/fs.js' + +export { zipFunction, zipFunctions } from './zip.js' interface ListedFunction { name: string diff --git a/src/manifest.ts b/src/manifest.ts index 46c165e5d..65d8a1ff2 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -2,7 +2,7 @@ import { promises as fs } from 'fs' import { resolve } from 'path' import { arch, platform } from 'process' -import { FunctionResult } from './utils/format_result' +import { FunctionResult } from './utils/format_result.js' interface ManifestFunction { mainFile: string diff --git a/src/runtimes/detect_runtime.ts b/src/runtimes/detect_runtime.ts index 316b0bd2d..bf3bc6cd0 100644 --- a/src/runtimes/detect_runtime.ts +++ b/src/runtimes/detect_runtime.ts @@ -2,9 +2,9 @@ import type { Buffer } from 'buffer' import { detect, Runtime } from 'elf-cam' -import { cachedReadFile, FsCache } from '../utils/fs' +import { cachedReadFile, FsCache } from '../utils/fs.js' -import type { RuntimeName } from './runtime' +import type { RuntimeName } from './runtime.js' // Try to guess the runtime by inspecting the binary file. export const detectBinaryRuntime = async function ({ diff --git a/src/runtimes/go/builder.ts b/src/runtimes/go/builder.ts index eb23658b2..0e295e933 100644 --- a/src/runtimes/go/builder.ts +++ b/src/runtimes/go/builder.ts @@ -1,8 +1,8 @@ import { promises as fs } from 'fs' import { basename } from 'path' -import { shellUtils } from '../../utils/shell' -import type { RuntimeName } from '../runtime' +import { shellUtils } from '../../utils/shell.js' +import type { RuntimeName } from '../runtime.js' export const build = async ({ destPath, mainFile, srcDir }: { destPath: string; mainFile: string; srcDir: string }) => { const functionName = basename(srcDir) diff --git a/src/runtimes/go/index.ts b/src/runtimes/go/index.ts index 9ce78ea30..5bb431fc1 100644 --- a/src/runtimes/go/index.ts +++ b/src/runtimes/go/index.ts @@ -3,14 +3,14 @@ import { basename, dirname, extname, join } from 'path' import cpFile from 'cp-file' -import { SourceFile } from '../../function' -import { cachedLstat, cachedReaddir, FsCache } from '../../utils/fs' -import { nonNullable } from '../../utils/non_nullable' -import { zipBinary } from '../../zip_binary' -import { detectBinaryRuntime } from '../detect_runtime' -import { FindFunctionInPathFunction, FindFunctionsInPathsFunction, Runtime, ZipFunction } from '../runtime' - -import { build } from './builder' +import { SourceFile } from '../../function.js' +import { cachedLstat, cachedReaddir, FsCache } from '../../utils/fs.js' +import { nonNullable } from '../../utils/non_nullable.js' +import { zipBinary } from '../../zip_binary.js' +import { detectBinaryRuntime } from '../detect_runtime.js' +import { FindFunctionInPathFunction, FindFunctionsInPathsFunction, Runtime, ZipFunction } from '../runtime.js' + +import { build } from './builder.js' interface GoBinary { path: string diff --git a/src/runtimes/index.ts b/src/runtimes/index.ts index 5e9e762c1..0466dfe20 100644 --- a/src/runtimes/index.ts +++ b/src/runtimes/index.ts @@ -1,14 +1,14 @@ import { extname, basename } from 'path' -import { Config, getConfigForFunction } from '../config' -import { defaultFlags, FeatureFlags } from '../feature_flags' -import { FunctionSource } from '../function' -import { FsCache } from '../utils/fs' - -import goRuntime from './go' -import jsRuntime from './node' -import type { Runtime } from './runtime' -import rustRuntime from './rust' +import { Config, getConfigForFunction } from '../config.js' +import { defaultFlags, FeatureFlags } from '../feature_flags.js' +import { FunctionSource } from '../function.js' +import { FsCache } from '../utils/fs.js' + +import goRuntime from './go/index.js' +import jsRuntime from './node/index.js' +import type { Runtime } from './runtime.js' +import rustRuntime from './rust/index.js' // A `Map` of functions, indexed by their name. type FunctionMap = Map diff --git a/src/runtimes/node/bundlers/esbuild/bundler.ts b/src/runtimes/node/bundlers/esbuild/bundler.ts index 78339b4f4..227db25b7 100644 --- a/src/runtimes/node/bundlers/esbuild/bundler.ts +++ b/src/runtimes/node/bundlers/esbuild/bundler.ts @@ -3,15 +3,15 @@ import { basename, dirname, extname, resolve, join } from 'path' import { build, Metafile } from '@netlify/esbuild' import { tmpName } from 'tmp-promise' -import type { NodeBundlerName } from '..' -import type { FunctionConfig } from '../../../../config' -import { getPathWithExtension, safeUnlink } from '../../../../utils/fs' -import type { RuntimeName } from '../../../runtime' - -import { getBundlerTarget } from './bundler_target' -import { getDynamicImportsPlugin } from './plugin_dynamic_imports' -import { getNativeModulesPlugin } from './plugin_native_modules' -import { getNodeBuiltinPlugin } from './plugin_node_builtin' +import type { FunctionConfig } from '../../../../config.js' +import { getPathWithExtension, safeUnlink } from '../../../../utils/fs.js' +import type { RuntimeName } from '../../../runtime.js' +import type { NodeBundlerName } from '../index.js' + +import { getBundlerTarget } from './bundler_target.js' +import { getDynamicImportsPlugin } from './plugin_dynamic_imports.js' +import { getNativeModulesPlugin } from './plugin_native_modules.js' +import { getNodeBuiltinPlugin } from './plugin_node_builtin.js' // Maximum number of log messages that an esbuild instance will produce. This // limit is important to avoid out-of-memory errors due to too much data being diff --git a/src/runtimes/node/bundlers/esbuild/index.ts b/src/runtimes/node/bundlers/esbuild/index.ts index 4314c8259..32b6bd92a 100644 --- a/src/runtimes/node/bundlers/esbuild/index.ts +++ b/src/runtimes/node/bundlers/esbuild/index.ts @@ -1,14 +1,14 @@ import { dirname, normalize } from 'path' -import type { BundleFunction } from '..' -import type { FunctionConfig } from '../../../../config' -import { getPathWithExtension } from '../../../../utils/fs' -import { nonNullable } from '../../../../utils/non_nullable' -import { getBasePath } from '../../utils/base_path' +import type { FunctionConfig } from '../../../../config.js' +import { getPathWithExtension } from '../../../../utils/fs.js' +import { nonNullable } from '../../../../utils/non_nullable.js' +import { getBasePath } from '../../utils/base_path.js' +import type { BundleFunction } from '../index.js' -import { bundleJsFile } from './bundler' -import { getExternalAndIgnoredModulesFromSpecialCases } from './special_cases' -import { getSrcFiles } from './src_files' +import { bundleJsFile } from './bundler.js' +import { getExternalAndIgnoredModulesFromSpecialCases } from './special_cases.js' +import { getSrcFiles } from './src_files.js' const getFunctionBasePath = ({ basePathFromConfig, diff --git a/src/runtimes/node/bundlers/esbuild/plugin_dynamic_imports.ts b/src/runtimes/node/bundlers/esbuild/plugin_dynamic_imports.ts index c17c67a02..49e17f8c6 100644 --- a/src/runtimes/node/bundlers/esbuild/plugin_dynamic_imports.ts +++ b/src/runtimes/node/bundlers/esbuild/plugin_dynamic_imports.ts @@ -5,7 +5,7 @@ import findUp from 'find-up' import readPackageJson from 'read-package-json-fast' import unixify from 'unixify' -import { parseExpression } from '../../parser' +import { parseExpression } from '../../parser/index.js' type PackageCache = Map> diff --git a/src/runtimes/node/bundlers/esbuild/plugin_native_modules.ts b/src/runtimes/node/bundlers/esbuild/plugin_native_modules.ts index ce1508d14..9a8eb118b 100644 --- a/src/runtimes/node/bundlers/esbuild/plugin_native_modules.ts +++ b/src/runtimes/node/bundlers/esbuild/plugin_native_modules.ts @@ -3,9 +3,9 @@ import path from 'path' import type { Plugin } from '@netlify/esbuild' import readPackageJson from 'read-package-json-fast' -import type { NativeNodeModules } from '..' -import { isNativeModule } from '../../utils/detect_native_module' -import { PackageJson } from '../../utils/package_json' +import { isNativeModule } from '../../utils/detect_native_module.js' +import { PackageJson } from '../../utils/package_json.js' +import type { NativeNodeModules } from '../index.js' type NativeModuleCacheEntry = [boolean | undefined, PackageJson] type NativeModuleCache = Record> diff --git a/src/runtimes/node/bundlers/esbuild/special_cases.ts b/src/runtimes/node/bundlers/esbuild/special_cases.ts index a637e3c18..583e5655e 100644 --- a/src/runtimes/node/bundlers/esbuild/special_cases.ts +++ b/src/runtimes/node/bundlers/esbuild/special_cases.ts @@ -1,4 +1,4 @@ -import { getPackageJsonIfAvailable, PackageJson } from '../../utils/package_json' +import { getPackageJsonIfAvailable, PackageJson } from '../../utils/package_json.js' const EXTERNAL_MODULES = ['@prisma/client'] const IGNORED_MODULES = ['aws-sdk'] diff --git a/src/runtimes/node/bundlers/esbuild/src_files.ts b/src/runtimes/node/bundlers/esbuild/src_files.ts index b86dac761..578f516f5 100644 --- a/src/runtimes/node/bundlers/esbuild/src_files.ts +++ b/src/runtimes/node/bundlers/esbuild/src_files.ts @@ -1,8 +1,8 @@ -import type { GetSrcFilesFunction } from '..' -import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files' -import { getPackageJson, PackageJson } from '../../utils/package_json' -import { getNewCache, TraversalCache } from '../../utils/traversal_cache' -import { getDependencyPathsForDependency } from '../zisi/traverse' +import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js' +import { getPackageJson, PackageJson } from '../../utils/package_json.js' +import { getNewCache, TraversalCache } from '../../utils/traversal_cache.js' +import type { GetSrcFilesFunction } from '../index.js' +import { getDependencyPathsForDependency } from '../zisi/traverse.js' export const getSrcFiles: GetSrcFilesFunction = async ({ config, mainFile, pluginsModulesPath, srcDir }) => { const { externalNodeModules = [], includedFiles = [], includedFilesBasePath } = config diff --git a/src/runtimes/node/bundlers/index.ts b/src/runtimes/node/bundlers/index.ts index 3a2b4dac0..25968a7c3 100644 --- a/src/runtimes/node/bundlers/index.ts +++ b/src/runtimes/node/bundlers/index.ts @@ -1,14 +1,14 @@ import type { Message } from '@netlify/esbuild' -import { FunctionConfig } from '../../../config' -import { FeatureFlag, FeatureFlags } from '../../../feature_flags' -import { FunctionSource } from '../../../function' -import { detectEsModule } from '../utils/detect_es_module' -import { ModuleFormat } from '../utils/module_format' - -import esbuildBundler from './esbuild' -import nftBundler from './nft' -import zisiBundler from './zisi' +import { FunctionConfig } from '../../../config.js' +import { FeatureFlag, FeatureFlags } from '../../../feature_flags.js' +import { FunctionSource } from '../../../function.js' +import { detectEsModule } from '../utils/detect_es_module.js' +import { ModuleFormat } from '../utils/module_format.js' + +import esbuildBundler from './esbuild/index.js' +import nftBundler from './nft/index.js' +import zisiBundler from './zisi/index.js' export type NodeBundlerName = 'esbuild' | 'esbuild_zisi' | 'nft' | 'zisi' diff --git a/src/runtimes/node/bundlers/nft/es_modules.ts b/src/runtimes/node/bundlers/nft/es_modules.ts index 817ab19b2..6d0587f7b 100644 --- a/src/runtimes/node/bundlers/nft/es_modules.ts +++ b/src/runtimes/node/bundlers/nft/es_modules.ts @@ -2,14 +2,14 @@ import { basename, dirname, resolve } from 'path' import { NodeFileTraceReasons } from '@vercel/nft' -import type { FunctionConfig } from '../../../../config' -import { FeatureFlags } from '../../../../feature_flags' -import { cachedReadFile, FsCache } from '../../../../utils/fs' -import { ModuleFormat } from '../../utils/module_format' -import { getNodeSupportMatrix } from '../../utils/node_version' -import { getPackageJsonIfAvailable, PackageJson } from '../../utils/package_json' - -import { transpile } from './transpile' +import type { FunctionConfig } from '../../../../config.js' +import { FeatureFlags } from '../../../../feature_flags.js' +import { cachedReadFile, FsCache } from '../../../../utils/fs.js' +import { ModuleFormat } from '../../utils/module_format.js' +import { getNodeSupportMatrix } from '../../utils/node_version.js' +import { getPackageJsonIfAvailable, PackageJson } from '../../utils/package_json.js' + +import { transpile } from './transpile.js' const getPatchedESMPackages = async (packages: string[], fsCache: FsCache) => { const patchedPackages = await Promise.all(packages.map((path) => patchESMPackage(path, fsCache))) diff --git a/src/runtimes/node/bundlers/nft/index.ts b/src/runtimes/node/bundlers/nft/index.ts index f45144987..535bb9cdb 100644 --- a/src/runtimes/node/bundlers/nft/index.ts +++ b/src/runtimes/node/bundlers/nft/index.ts @@ -1,19 +1,19 @@ import { basename, dirname, join, normalize, resolve } from 'path' import { nodeFileTrace } from '@vercel/nft' -import resolveDependency from '@vercel/nft/out/resolve-dependency' +import resolveDependency from '@vercel/nft/out/resolve-dependency.js' import minimatch from 'minimatch' import unixify from 'unixify' -import type { BundleFunction } from '..' -import type { FunctionConfig } from '../../../../config' -import { FeatureFlags } from '../../../../feature_flags' -import { cachedReadFile, FsCache } from '../../../../utils/fs' -import type { GetSrcFilesFunction } from '../../../runtime' -import { getBasePath } from '../../utils/base_path' -import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files' +import type { FunctionConfig } from '../../../../config.js' +import { FeatureFlags } from '../../../../feature_flags.js' +import { cachedReadFile, FsCache } from '../../../../utils/fs.js' +import type { GetSrcFilesFunction } from '../../../runtime.js' +import { getBasePath } from '../../utils/base_path.js' +import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js' +import type { BundleFunction } from '../index.js' -import { processESM } from './es_modules' +import { processESM } from './es_modules.js' // Paths that will be excluded from the tracing process. const ignore = ['node_modules/aws-sdk/**'] diff --git a/src/runtimes/node/bundlers/nft/transpile.ts b/src/runtimes/node/bundlers/nft/transpile.ts index 1ccb996e9..068f7b449 100644 --- a/src/runtimes/node/bundlers/nft/transpile.ts +++ b/src/runtimes/node/bundlers/nft/transpile.ts @@ -1,7 +1,7 @@ import { build } from '@netlify/esbuild' -import type { FunctionConfig } from '../../../../config' -import { getBundlerTarget } from '../esbuild/bundler_target' +import type { FunctionConfig } from '../../../../config.js' +import { getBundlerTarget } from '../esbuild/bundler_target.js' export const transpile = async (path: string, config: FunctionConfig) => { // The version of ECMAScript to use as the build target. This will determine diff --git a/src/runtimes/node/bundlers/zisi/index.ts b/src/runtimes/node/bundlers/zisi/index.ts index 85bebc2f4..d1e318f0d 100644 --- a/src/runtimes/node/bundlers/zisi/index.ts +++ b/src/runtimes/node/bundlers/zisi/index.ts @@ -1,9 +1,9 @@ import { dirname, normalize } from 'path' -import type { BundleFunction } from '..' -import { getBasePath } from '../../utils/base_path' +import { getBasePath } from '../../utils/base_path.js' +import type { BundleFunction } from '../index.js' -import { getSrcFiles } from './src_files' +import { getSrcFiles } from './src_files.js' const bundle: BundleFunction = async ({ basePath, diff --git a/src/runtimes/node/bundlers/zisi/list_imports.ts b/src/runtimes/node/bundlers/zisi/list_imports.ts index 53b0571e7..8b3a5b1a7 100644 --- a/src/runtimes/node/bundlers/zisi/list_imports.ts +++ b/src/runtimes/node/bundlers/zisi/list_imports.ts @@ -2,9 +2,9 @@ import * as esbuild from '@netlify/esbuild' import isBuiltinModule from 'is-builtin-module' import { tmpName } from 'tmp-promise' -import type { NodeBundlerName } from '..' -import { safeUnlink } from '../../../../utils/fs' -import type { RuntimeName } from '../../../runtime' +import { safeUnlink } from '../../../../utils/fs.js' +import type { RuntimeName } from '../../../runtime.js' +import type { NodeBundlerName } from '../index.js' // Maximum number of log messages that an esbuild instance will produce. This // limit is important to avoid out-of-memory errors due to too much data being diff --git a/src/runtimes/node/bundlers/zisi/nested.ts b/src/runtimes/node/bundlers/zisi/nested.ts index 3f5ee8e3a..71c929922 100644 --- a/src/runtimes/node/bundlers/zisi/nested.ts +++ b/src/runtimes/node/bundlers/zisi/nested.ts @@ -1,6 +1,6 @@ import semver from 'semver' -import { PackageJson } from '../../utils/package_json' +import { PackageJson } from '../../utils/package_json.js' // Apply the Node.js module logic recursively on its own dependencies, using // the `package.json` `dependencies`, `peerDependencies` and diff --git a/src/runtimes/node/bundlers/zisi/side_files.ts b/src/runtimes/node/bundlers/zisi/side_files.ts index 0480c0b31..5e3b97b9e 100644 --- a/src/runtimes/node/bundlers/zisi/side_files.ts +++ b/src/runtimes/node/bundlers/zisi/side_files.ts @@ -1,4 +1,4 @@ -import { getPublishedFiles } from './published' +import { getPublishedFiles } from './published.js' // Some modules generate source files on `postinstall` that are not located // inside the module's directory itself. diff --git a/src/runtimes/node/bundlers/zisi/src_files.ts b/src/runtimes/node/bundlers/zisi/src_files.ts index 9b787916a..976a55abe 100644 --- a/src/runtimes/node/bundlers/zisi/src_files.ts +++ b/src/runtimes/node/bundlers/zisi/src_files.ts @@ -4,18 +4,18 @@ import { dirname, basename, normalize } from 'path' import { not as notJunk } from 'junk' import precinct from 'precinct' -import type { GetSrcFilesFunction } from '..' -import { FeatureFlags } from '../../../../feature_flags' -import { nonNullable } from '../../../../utils/non_nullable' -import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files' -import { getPackageJson, PackageJson } from '../../utils/package_json' -import { getNewCache, TraversalCache } from '../../utils/traversal_cache' - -import { listImports } from './list_imports' -import { resolvePathPreserveSymlinks } from './resolve' -import { getDependencyPathsForDependency } from './traverse' -import { getTreeFiles } from './tree_files' -import { shouldTreeShake } from './tree_shake' +import { FeatureFlags } from '../../../../feature_flags.js' +import { nonNullable } from '../../../../utils/non_nullable.js' +import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js' +import { getPackageJson, PackageJson } from '../../utils/package_json.js' +import { getNewCache, TraversalCache } from '../../utils/traversal_cache.js' +import type { GetSrcFilesFunction } from '../index.js' + +import { listImports } from './list_imports.js' +import { resolvePathPreserveSymlinks } from './resolve.js' +import { getDependencyPathsForDependency } from './traverse.js' +import { getTreeFiles } from './tree_files.js' +import { shouldTreeShake } from './tree_shake.js' // Retrieve the paths to the Node.js files to zip. // We only include the files actually needed by the function because AWS Lambda diff --git a/src/runtimes/node/bundlers/zisi/traverse.ts b/src/runtimes/node/bundlers/zisi/traverse.ts index 209548159..7ce9798c3 100644 --- a/src/runtimes/node/bundlers/zisi/traverse.ts +++ b/src/runtimes/node/bundlers/zisi/traverse.ts @@ -1,15 +1,15 @@ import { promises as fs } from 'fs' import { dirname } from 'path' -import { nonNullable } from '../../../../utils/non_nullable' -import { getModuleName } from '../../utils/module' -import { PackageJson } from '../../utils/package_json' -import { TraversalCache } from '../../utils/traversal_cache' - -import { getNestedDependencies, handleModuleNotFound } from './nested' -import { getPublishedFiles } from './published' -import { resolvePackage } from './resolve' -import { getSideFiles } from './side_files' +import { nonNullable } from '../../../../utils/non_nullable.js' +import { getModuleName } from '../../utils/module.js' +import { PackageJson } from '../../utils/package_json.js' +import { TraversalCache } from '../../utils/traversal_cache.js' + +import { getNestedDependencies, handleModuleNotFound } from './nested.js' +import { getPublishedFiles } from './published.js' +import { resolvePackage } from './resolve.js' +import { getSideFiles } from './side_files.js' const EXCLUDED_MODULES = new Set(['aws-sdk']) diff --git a/src/runtimes/node/bundlers/zisi/tree_shake.ts b/src/runtimes/node/bundlers/zisi/tree_shake.ts index aec72a4b3..a64281911 100644 --- a/src/runtimes/node/bundlers/zisi/tree_shake.ts +++ b/src/runtimes/node/bundlers/zisi/tree_shake.ts @@ -1,4 +1,4 @@ -import { getModuleName } from '../../utils/module' +import { getModuleName } from '../../utils/module.js' const LOCAL_IMPORT_REGEXP = /^(\.|\/)/ diff --git a/src/runtimes/node/finder.ts b/src/runtimes/node/finder.ts index ac8298656..a105411f6 100644 --- a/src/runtimes/node/finder.ts +++ b/src/runtimes/node/finder.ts @@ -3,9 +3,9 @@ import { join, dirname, basename, extname } from 'path' import locatePath from 'locate-path' -import { SourceFile } from '../../function' -import { nonNullable } from '../../utils/non_nullable' -import { FindFunctionsInPathsFunction, FindFunctionInPathFunction } from '../runtime' +import { SourceFile } from '../../function.js' +import { nonNullable } from '../../utils/non_nullable.js' +import { FindFunctionsInPathsFunction, FindFunctionInPathFunction } from '../runtime.js' // List of extensions that this runtime will look for, in order of precedence. const allowedExtensions = ['.js', '.zip', '.cjs', '.mjs', '.ts'] diff --git a/src/runtimes/node/in_source_config/index.ts b/src/runtimes/node/in_source_config/index.ts index 797ee68a5..338fd185d 100644 --- a/src/runtimes/node/in_source_config/index.ts +++ b/src/runtimes/node/in_source_config/index.ts @@ -1,11 +1,11 @@ import { ArgumentPlaceholder, Expression, SpreadElement, JSXNamespacedName } from '@babel/types' -import { nonNullable } from '../../../utils/non_nullable' -import { safelyParseFile } from '../parser' -import { getMainExport } from '../parser/exports' -import { getImports } from '../parser/imports' +import { nonNullable } from '../../../utils/non_nullable.js' +import { getMainExport } from '../parser/exports.js' +import { getImports } from '../parser/imports.js' +import { safelyParseFile } from '../parser/index.js' -import { parse as parseSchedule } from './properties/schedule' +import { parse as parseSchedule } from './properties/schedule.js' export const IN_SOURCE_CONFIG_MODULE = '@netlify/functions' diff --git a/src/runtimes/node/in_source_config/properties/schedule.ts b/src/runtimes/node/in_source_config/properties/schedule.ts index 20dba1dd2..6adbe1233 100644 --- a/src/runtimes/node/in_source_config/properties/schedule.ts +++ b/src/runtimes/node/in_source_config/properties/schedule.ts @@ -1,4 +1,4 @@ -import type { ISCHandlerArg } from '..' +import type { ISCHandlerArg } from '../index.js' export const parse = ({ args }: { args: ISCHandlerArg[] }) => { const [expression] = args diff --git a/src/runtimes/node/index.ts b/src/runtimes/node/index.ts index 30d139930..ca9fde985 100644 --- a/src/runtimes/node/index.ts +++ b/src/runtimes/node/index.ts @@ -2,15 +2,15 @@ import { join } from 'path' import cpFile from 'cp-file' -import { GetSrcFilesFunction, Runtime, ZipFunction } from '../runtime' +import { GetSrcFilesFunction, Runtime, ZipFunction } from '../runtime.js' -import { getBundler, getDefaultBundler } from './bundlers' -import { findFunctionsInPaths, findFunctionInPath } from './finder' -import { findISCDeclarationsInPath } from './in_source_config' -import { createAliases as createPluginsModulesPathAliases, getPluginsModulesPath } from './utils/plugin_modules_path' -import { zipNodeJs } from './utils/zip' +import { getBundler, getDefaultBundler } from './bundlers/index.js' +import { findFunctionsInPaths, findFunctionInPath } from './finder.js' +import { findISCDeclarationsInPath } from './in_source_config/index.js' +import { createAliases as createPluginsModulesPathAliases, getPluginsModulesPath } from './utils/plugin_modules_path.js' +import { zipNodeJs } from './utils/zip.js' -export { NodeVersionString } from './utils/node_version' +export { NodeVersionString } from './utils/node_version.js' // A proxy for the `getSrcFiles` function which adds a default `bundler` using // the `getDefaultBundler` function. diff --git a/src/runtimes/node/parser/exports.ts b/src/runtimes/node/parser/exports.ts index c453dbdca..cfd30a8e2 100644 --- a/src/runtimes/node/parser/exports.ts +++ b/src/runtimes/node/parser/exports.ts @@ -1,8 +1,8 @@ import { CallExpression, Statement } from '@babel/types' -import type { ISCExport } from '../in_source_config' +import type { ISCExport } from '../in_source_config/index.js' -import { isModuleExports } from './helpers' +import { isModuleExports } from './helpers.js' // Finds the main handler export in an AST. export const getMainExport = (nodes: Statement[]) => { diff --git a/src/runtimes/node/parser/imports.ts b/src/runtimes/node/parser/imports.ts index 168c7e62a..30572518e 100644 --- a/src/runtimes/node/parser/imports.ts +++ b/src/runtimes/node/parser/imports.ts @@ -1,8 +1,8 @@ import { Statement } from '@babel/types' -import { nonNullable } from '../../../utils/non_nullable' +import { nonNullable } from '../../../utils/non_nullable.js' -import { isImport, isRequire } from './helpers' +import { isImport, isRequire } from './helpers.js' // Finds import/require statements of a given path in an AST. export const getImports = (node: Statement, importPath: string) => { diff --git a/src/runtimes/node/parser/index.ts b/src/runtimes/node/parser/index.ts index 36e735f1c..1cf669103 100644 --- a/src/runtimes/node/parser/index.ts +++ b/src/runtimes/node/parser/index.ts @@ -5,7 +5,7 @@ import { join, relative, resolve } from 'path' import { parse } from '@babel/parser' import type { BinaryExpression, CallExpression, Expression, PrivateName, TemplateLiteral, TSType } from '@babel/types' -import { nonNullable } from '../../../utils/non_nullable' +import { nonNullable } from '../../../utils/non_nullable.js' const GLOB_WILDCARD = '**' diff --git a/src/runtimes/node/utils/detect_native_module.ts b/src/runtimes/node/utils/detect_native_module.ts index d69f2b544..c67d38fb9 100644 --- a/src/runtimes/node/utils/detect_native_module.ts +++ b/src/runtimes/node/utils/detect_native_module.ts @@ -1,6 +1,6 @@ import { extname } from 'path' -import { PackageJson } from './package_json' +import { PackageJson } from './package_json.js' const markerModules = ['bindings', 'nan', 'node-gyp', 'node-gyp-build', 'node-pre-gyp', 'prebuild'] diff --git a/src/runtimes/node/utils/entry_file.ts b/src/runtimes/node/utils/entry_file.ts index 91d53985d..4aa8c1fec 100644 --- a/src/runtimes/node/utils/entry_file.ts +++ b/src/runtimes/node/utils/entry_file.ts @@ -1,7 +1,7 @@ import { basename, extname } from 'path' -import type { ModuleFormat } from './module_format' -import { normalizeFilePath } from './normalize_path' +import type { ModuleFormat } from './module_format.js' +import { normalizeFilePath } from './normalize_path.js' export interface EntryFile { contents: string diff --git a/src/runtimes/node/utils/zip.ts b/src/runtimes/node/utils/zip.ts index b13d3b626..0ed6275f5 100644 --- a/src/runtimes/node/utils/zip.ts +++ b/src/runtimes/node/utils/zip.ts @@ -7,12 +7,12 @@ import copyFile from 'cp-file' import deleteFiles from 'del' import pMap from 'p-map' -import { startZip, addZipFile, addZipContent, endZip, ZipArchive } from '../../../archive' -import { mkdirAndWriteFile } from '../../../utils/fs' +import { startZip, addZipFile, addZipContent, endZip, ZipArchive } from '../../../archive.js' +import { mkdirAndWriteFile } from '../../../utils/fs.js' -import { EntryFile, getEntryFile } from './entry_file' -import type { ModuleFormat } from './module_format' -import { normalizeFilePath } from './normalize_path' +import { EntryFile, getEntryFile } from './entry_file.js' +import type { ModuleFormat } from './module_format.js' +import { normalizeFilePath } from './normalize_path.js' // Taken from https://www.npmjs.com/package/cpy. const COPY_FILE_CONCURRENCY = os.cpus().length === 0 ? 2 : os.cpus().length * 2 diff --git a/src/runtimes/runtime.ts b/src/runtimes/runtime.ts index 6c754d754..eec3e5996 100644 --- a/src/runtimes/runtime.ts +++ b/src/runtimes/runtime.ts @@ -1,11 +1,11 @@ -import { ArchiveFormat } from '../archive' -import { FunctionConfig } from '../config' -import { FeatureFlags } from '../feature_flags' -import { FunctionSource, SourceFile } from '../function' -import { FsCache } from '../utils/fs' - -import type { NodeBundlerName } from './node/bundlers' -import type { ISCValues } from './node/in_source_config' +import { ArchiveFormat } from '../archive.js' +import { FunctionConfig } from '../config.js' +import { FeatureFlags } from '../feature_flags.js' +import { FunctionSource, SourceFile } from '../function.js' +import { FsCache } from '../utils/fs.js' + +import type { NodeBundlerName } from './node/bundlers/index.js' +import type { ISCValues } from './node/in_source_config/index.js' export type RuntimeName = 'go' | 'js' | 'rs' diff --git a/src/runtimes/rust/builder.ts b/src/runtimes/rust/builder.ts index d235b4339..f7c3e5fe6 100644 --- a/src/runtimes/rust/builder.ts +++ b/src/runtimes/rust/builder.ts @@ -4,12 +4,12 @@ import { basename, join } from 'path' import tmp from 'tmp-promise' import toml from 'toml' -import { FunctionConfig } from '../../config' -import { shellUtils } from '../../utils/shell' -import type { RuntimeName } from '../runtime' +import { FunctionConfig } from '../../config.js' +import { shellUtils } from '../../utils/shell.js' +import type { RuntimeName } from '../runtime.js' -import { CargoManifest } from './cargo_manifest' -import { BUILD_TARGET, MANIFEST_NAME } from './constants' +import { CargoManifest } from './cargo_manifest.js' +import { BUILD_TARGET, MANIFEST_NAME } from './constants.js' const runtimeName: RuntimeName = 'rs' diff --git a/src/runtimes/rust/index.ts b/src/runtimes/rust/index.ts index d7ee2084e..825099107 100644 --- a/src/runtimes/rust/index.ts +++ b/src/runtimes/rust/index.ts @@ -1,16 +1,16 @@ import type { Stats } from 'fs' import { join, extname, dirname, basename } from 'path' -import { FeatureFlags } from '../../feature_flags' -import { SourceFile } from '../../function' -import { cachedLstat, cachedReaddir, FsCache } from '../../utils/fs' -import { nonNullable } from '../../utils/non_nullable' -import { zipBinary } from '../../zip_binary' -import { detectBinaryRuntime } from '../detect_runtime' -import { FindFunctionsInPathsFunction, FindFunctionInPathFunction, Runtime, ZipFunction } from '../runtime' - -import { build } from './builder' -import { MANIFEST_NAME } from './constants' +import { FeatureFlags } from '../../feature_flags.js' +import { SourceFile } from '../../function.js' +import { cachedLstat, cachedReaddir, FsCache } from '../../utils/fs.js' +import { nonNullable } from '../../utils/non_nullable.js' +import { zipBinary } from '../../zip_binary.js' +import { detectBinaryRuntime } from '../detect_runtime.js' +import { FindFunctionsInPathsFunction, FindFunctionInPathFunction, Runtime, ZipFunction } from '../runtime.js' + +import { build } from './builder.js' +import { MANIFEST_NAME } from './constants.js' const detectRustFunction = async ({ fsCache, path }: { fsCache: FsCache; path: string }) => { const stat = await cachedLstat(fsCache, path) diff --git a/src/utils/archive_size.ts b/src/utils/archive_size.ts index c9283c648..6ac570019 100644 --- a/src/utils/archive_size.ts +++ b/src/utils/archive_size.ts @@ -1,7 +1,7 @@ import { promises as fs } from 'fs' import { extname } from 'path' -import type { FunctionArchive } from '../function' +import type { FunctionArchive } from '../function.js' // Returns the input object with an additional `size` property containing the // size of the file at `path` when it is a ZIP archive. diff --git a/src/utils/format_result.ts b/src/utils/format_result.ts index 743be9b7b..a7feb6b2b 100644 --- a/src/utils/format_result.ts +++ b/src/utils/format_result.ts @@ -1,7 +1,7 @@ -import { FunctionArchive } from '../function' -import { RuntimeName } from '../runtimes/runtime' +import { FunctionArchive } from '../function.js' +import { RuntimeName } from '../runtimes/runtime.js' -import { removeUndefined } from './remove_undefined' +import { removeUndefined } from './remove_undefined.js' export type FunctionResult = Omit & { runtime: RuntimeName diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 36186c331..852f195a2 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -1,7 +1,7 @@ import { promises as fs } from 'fs' import { dirname, format, join, parse, resolve } from 'path' -import { nonNullable } from './non_nullable' +import { nonNullable } from './non_nullable.js' export type FsCache = Record diff --git a/src/zip.ts b/src/zip.ts index 8820846d2..084eca003 100644 --- a/src/zip.ts +++ b/src/zip.ts @@ -3,16 +3,16 @@ import { resolve } from 'path' import pMap from 'p-map' -import { ArchiveFormat } from './archive' -import { Config } from './config' -import { FeatureFlags, getFlags } from './feature_flags' -import { FunctionSource } from './function' -import { createManifest } from './manifest' -import { getFunctionsFromPaths } from './runtimes' -import { addArchiveSize } from './utils/archive_size' -import { formatZipResult } from './utils/format_result' -import { listFunctionsDirectories, resolveFunctionsDirectories } from './utils/fs' -import { nonNullable } from './utils/non_nullable' +import { ArchiveFormat } from './archive.js' +import { Config } from './config.js' +import { FeatureFlags, getFlags } from './feature_flags.js' +import { FunctionSource } from './function.js' +import { createManifest } from './manifest.js' +import { getFunctionsFromPaths } from './runtimes/index.js' +import { addArchiveSize } from './utils/archive_size.js' +import { formatZipResult } from './utils/format_result.js' +import { listFunctionsDirectories, resolveFunctionsDirectories } from './utils/fs.js' +import { nonNullable } from './utils/non_nullable.js' interface ZipFunctionOptions { archiveFormat?: ArchiveFormat diff --git a/src/zip_binary.ts b/src/zip_binary.ts index ada1216f7..535f7802e 100644 --- a/src/zip_binary.ts +++ b/src/zip_binary.ts @@ -1,7 +1,7 @@ import type { Stats } from 'fs' -import { startZip, addZipFile, addZipContent, endZip } from './archive' -import { Runtime } from './runtimes/runtime' +import { startZip, addZipFile, addZipContent, endZip } from './archive.js' +import { Runtime } from './runtimes/runtime.js' // Zip a binary function file export const zipBinary = async function ({ diff --git a/tests/bin.js b/tests/bin.js index 44e74f7a5..3222be039 100644 --- a/tests/bin.js +++ b/tests/bin.js @@ -5,7 +5,7 @@ const test = require('ava') const execa = require('execa') const { tmpName } = require('tmp-promise') -const { FIXTURES_DIR, BINARY_PATH } = require('./helpers/main') +const { FIXTURES_DIR, BINARY_PATH } = require('./helpers/main.js') const ROOT_PACKAGE_JSON = `${__dirname}/../package.json` diff --git a/tests/feature_flags.js b/tests/feature_flags.js index 372c934fa..26af29224 100644 --- a/tests/feature_flags.js +++ b/tests/feature_flags.js @@ -1,6 +1,6 @@ const test = require('ava') -const { getFlags } = require('../dist/feature_flags') +const { getFlags } = require('../dist/feature_flags.js') test('Respects default value of flags', (t) => { const flags = getFlags({}, { someFlag: false }) diff --git a/tests/fixtures/list/five/index.ts b/tests/fixtures/list/five/index.ts index 85df84462..ae30aa8d4 100644 --- a/tests/fixtures/list/five/index.ts +++ b/tests/fixtures/list/five/index.ts @@ -1,3 +1,6 @@ +// We do not rename to `./util.js` because `@vercel/nft` does not manage to +// find the dependency `util.ts` then, even though using `.js` file extensions +// is the recommended way to use pure ES modules with Typescript. import { type } from './util' const obj = { type } diff --git a/tests/fixtures/node-typescript-with-imports/function.ts b/tests/fixtures/node-typescript-with-imports/function.ts index ca8fc4279..393671bad 100644 --- a/tests/fixtures/node-typescript-with-imports/function.ts +++ b/tests/fixtures/node-typescript-with-imports/function.ts @@ -1 +1,4 @@ +// We do not rename to `./util.js` because `@vercel/nft` does not manage to +// find the dependency `util.ts` then, even though using `.js` file extensions +// is the recommended way to use pure ES modules with Typescript. export { type } from './lib/util' diff --git a/tests/helpers/main.js b/tests/helpers/main.js index 2d743aab2..ba2cf5e9b 100644 --- a/tests/helpers/main.js +++ b/tests/helpers/main.js @@ -6,8 +6,8 @@ const { pathToFileURL } = require('url') const execa = require('execa') const { dir: getTmpDir } = require('tmp-promise') -const { zipFunctions } = require('../..') -const { listImports } = require('../../dist/runtimes/node/bundlers/zisi/list_imports') +const { zipFunctions } = require('../../dist/main.js') +const { listImports } = require('../../dist/runtimes/node/bundlers/zisi/list_imports.js') const FIXTURES_DIR = join(__dirname, '..', 'fixtures') const BINARY_PATH = join(__dirname, '..', '..', 'dist', 'bin.js') diff --git a/tests/main.js b/tests/main.js index 82da2e867..62afa23b3 100644 --- a/tests/main.js +++ b/tests/main.js @@ -22,15 +22,15 @@ require('source-map-support').install() // We must require this file first because we need to stub it before the main // functions are required. // eslint-disable-next-line import/order -const { shellUtils } = require('../dist/utils/shell') +const { shellUtils } = require('../dist/utils/shell.js') const shellUtilsStub = sinon.stub(shellUtils, 'runCommand') // eslint-disable-next-line import/order -const { zipFunction, listFunctions, listFunctionsFiles, listFunction } = require('..') +const { zipFunction, listFunctions, listFunctionsFiles, listFunction } = require('../dist/main.js') -const { ESBUILD_LOG_LIMIT } = require('../dist/runtimes/node/bundlers/esbuild/bundler') -const { detectEsModule } = require('../dist/runtimes/node/utils/detect_es_module') +const { ESBUILD_LOG_LIMIT } = require('../dist/runtimes/node/bundlers/esbuild/bundler.js') +const { detectEsModule } = require('../dist/runtimes/node/utils/detect_es_module.js') const { getRequires, @@ -41,9 +41,9 @@ const { FIXTURES_DIR, BINARY_PATH, importFunctionFile, -} = require('./helpers/main') -const { computeSha1 } = require('./helpers/sha') -const { makeTestMany } = require('./helpers/test_many') +} = require('./helpers/main.js') +const { computeSha1 } = require('./helpers/sha.js') +const { makeTestMany } = require('./helpers/test_many.js') const EXECUTABLE_PERMISSION = 0o755 diff --git a/tests/unit/runtimes/node/utils/node_version.js b/tests/unit/runtimes/node/utils/node_version.js index 8cc8bafbb..df409ed0d 100644 --- a/tests/unit/runtimes/node/utils/node_version.js +++ b/tests/unit/runtimes/node/utils/node_version.js @@ -4,7 +4,7 @@ const { DEFAULT_NODE_VERSION, getNodeVersion, parseVersion, -} = require('../../../../../dist/runtimes/node/utils/node_version') +} = require('../../../../../dist/runtimes/node/utils/node_version.js') test('getNodeVersion', (t) => { t.is(getNodeVersion('nodejs12.x'), 12) diff --git a/tests/unit/runtimes/node/utils/package_json.js b/tests/unit/runtimes/node/utils/package_json.js index eb837e3ba..41616502e 100644 --- a/tests/unit/runtimes/node/utils/package_json.js +++ b/tests/unit/runtimes/node/utils/package_json.js @@ -1,6 +1,6 @@ const test = require('ava') -const { sanitisePackageJson } = require('../../../../../dist/runtimes/node/utils/package_json') +const { sanitisePackageJson } = require('../../../../../dist/runtimes/node/utils/package_json.js') test('sanitisePackageJson', (t) => { t.deepEqual(