Skip to content

Commit

Permalink
fix: use .js and index.js in imports (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 8, 2022
1 parent 0a738b1 commit 96e9a95
Show file tree
Hide file tree
Showing 53 changed files with 216 additions and 205 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.cjs
Expand Up @@ -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',
Expand All @@ -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',
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/bin.ts
Expand Up @@ -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 () {
Expand Down
6 changes: 3 additions & 3 deletions 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[]
Expand Down
4 changes: 2 additions & 2 deletions 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 & {
Expand Down
18 changes: 9 additions & 9 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.ts
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/runtimes/detect_runtime.ts
Expand Up @@ -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 ({
Expand Down
4 changes: 2 additions & 2 deletions 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)
Expand Down
16 changes: 8 additions & 8 deletions src/runtimes/go/index.ts
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions 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<string, FunctionSource>
Expand Down
18 changes: 9 additions & 9 deletions src/runtimes/node/bundlers/esbuild/bundler.ts
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions 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,
Expand Down
Expand Up @@ -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<string, Promise<string | undefined>>

Expand Down
6 changes: 3 additions & 3 deletions src/runtimes/node/bundlers/esbuild/plugin_native_modules.ts
Expand Up @@ -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<string, Promise<NativeModuleCacheEntry>>
Expand Down
2 changes: 1 addition & 1 deletion 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']
Expand Down
10 changes: 5 additions & 5 deletions 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
Expand Down
18 changes: 9 additions & 9 deletions 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'

Expand Down
16 changes: 8 additions & 8 deletions src/runtimes/node/bundlers/nft/es_modules.ts
Expand Up @@ -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)))
Expand Down
18 changes: 9 additions & 9 deletions 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/**']
Expand Down
4 changes: 2 additions & 2 deletions 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
Expand Down
6 changes: 3 additions & 3 deletions 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,
Expand Down
6 changes: 3 additions & 3 deletions src/runtimes/node/bundlers/zisi/list_imports.ts
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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.
Expand Down

1 comment on commit 96e9a95

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 6.5s

largeDepsZisi: 51.1s

Please sign in to comment.