@netlify/zip-it-and-ship-it 9.22.0 → 9.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,14 @@
1
- import { basename, dirname, join, normalize, resolve, extname } from 'path';
1
+ import { basename, dirname, extname, join, normalize, resolve } from 'path';
2
2
  import { nodeFileTrace } from '@vercel/nft';
3
3
  import resolveDependency from '@vercel/nft/out/resolve-dependency.js';
4
4
  import { cachedReadFile, getPathWithExtension } from '../../../../utils/fs.js';
5
5
  import { minimatch } from '../../../../utils/matching.js';
6
6
  import { getBasePath } from '../../utils/base_path.js';
7
7
  import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js';
8
- import { MODULE_FORMAT, MODULE_FILE_EXTENSION, tsExtensions } from '../../utils/module_format.js';
8
+ import { MODULE_FILE_EXTENSION, tsExtensions } from '../../utils/module_format.js';
9
9
  import { getNodeSupportMatrix } from '../../utils/node_version.js';
10
- import { getClosestPackageJson } from '../../utils/package_json.js';
11
10
  import { processESM } from './es_modules.js';
12
- import { transpileTS } from './transpile.js';
11
+ import { transform, getTransformer } from './transformer.js';
13
12
  const appearsToBeModuleName = (name) => !name.startsWith('.');
14
13
  const bundle = async ({ basePath, cache, config, featureFlags, mainFile, name, pluginsModulesPath, repositoryRoot = basePath, runtimeAPIVersion, }) => {
15
14
  const { includedFiles = [], includedFilesBasePath } = config;
@@ -53,65 +52,9 @@ const getIgnoreFunction = (config) => {
53
52
  return shouldIgnore;
54
53
  };
55
54
  };
56
- /**
57
- * Returns the module format that should be used when transpiling a TypeScript
58
- * file.
59
- */
60
- const getTSModuleFormat = async (mainFile, runtimeAPIVersion, repositoryRoot) => {
61
- // TODO: This check should go away. We should always respect the format from
62
- // the extension. We'll do this at a later stage, after we roll out the V2
63
- // API with no side-effects on V1 functions.
64
- if (runtimeAPIVersion === 2) {
65
- if (extname(mainFile) === MODULE_FILE_EXTENSION.MTS) {
66
- return MODULE_FORMAT.ESM;
67
- }
68
- if (extname(mainFile) === MODULE_FILE_EXTENSION.CTS) {
69
- return MODULE_FORMAT.COMMONJS;
70
- }
71
- }
72
- // At this point, we need to infer the module type from the `type` field in
73
- // the closest `package.json`.
74
- try {
75
- const packageJSON = await getClosestPackageJson(dirname(mainFile), repositoryRoot);
76
- if (packageJSON?.contents.type === 'module') {
77
- return MODULE_FORMAT.ESM;
78
- }
79
- }
80
- catch {
81
- // no-op
82
- }
83
- return MODULE_FORMAT.COMMONJS;
84
- };
85
- const getTypeScriptTransformer = async (runtimeAPIVersion, mainFile, repositoryRoot) => {
86
- const isTypeScript = tsExtensions.has(extname(mainFile));
87
- if (!isTypeScript) {
88
- return;
89
- }
90
- const format = await getTSModuleFormat(mainFile, runtimeAPIVersion, repositoryRoot);
91
- const aliases = new Map();
92
- const rewrites = new Map();
93
- const transformer = {
94
- aliases,
95
- format,
96
- rewrites,
97
- };
98
- if (runtimeAPIVersion === 2) {
99
- // For V2 functions, we want to emit a main file with an unambiguous
100
- // extension (i.e. `.cjs` or `.mjs`), so that the file is loaded with
101
- // the correct format regardless of what is set in `package.json`.
102
- const newExtension = format === MODULE_FORMAT.COMMONJS ? MODULE_FILE_EXTENSION.CJS : MODULE_FILE_EXTENSION.MJS;
103
- const newMainFile = getPathWithExtension(mainFile, newExtension);
104
- return {
105
- ...transformer,
106
- bundle: true,
107
- bundledPaths: [],
108
- newMainFile,
109
- };
110
- }
111
- return transformer;
112
- };
113
55
  const traceFilesAndTranspile = async function ({ basePath, cache, config, featureFlags, mainFile, pluginsModulesPath, name, repositoryRoot, runtimeAPIVersion, }) {
114
- const tsTransformer = await getTypeScriptTransformer(runtimeAPIVersion, mainFile, repositoryRoot);
56
+ const isTSFunction = tsExtensions.has(extname(mainFile));
57
+ const transformer = runtimeAPIVersion === 2 || isTSFunction ? await getTransformer(runtimeAPIVersion, mainFile, repositoryRoot) : null;
115
58
  const { fileList: dependencyPaths, esmFileList, reasons, } = await nodeFileTrace([mainFile], {
116
59
  // Default is 1024. Allowing double the fileIO in parallel makes nft faster, but uses a little more memory.
117
60
  fileIOConcurrency: 2048,
@@ -120,30 +63,31 @@ const traceFilesAndTranspile = async function ({ basePath, cache, config, featur
120
63
  ignore: getIgnoreFunction(config),
121
64
  readFile: async (path) => {
122
65
  try {
123
- const extension = extname(path);
124
- if (tsExtensions.has(extension)) {
125
- const { bundledPaths, transpiled } = await transpileTS({
126
- bundle: tsTransformer?.bundle,
66
+ const isMainFile = path === mainFile;
67
+ // Transform this file if this is the main file and we're processing a
68
+ // V2 functions (which always bundle local imports), or if this path is
69
+ // a TypeScript file (which should only happen for V1 TS functions that
70
+ // set the bundler to "nft").
71
+ if ((isMainFile && transformer) || tsExtensions.has(extname(path))) {
72
+ const { bundledPaths, transpiled } = await transform({
73
+ bundle: transformer?.bundle,
127
74
  config,
128
75
  name,
129
- format: tsTransformer?.format,
76
+ format: transformer?.format,
130
77
  path,
131
78
  });
132
- const isMainFile = path === mainFile;
133
79
  // If this is the main file, the final path of the compiled file may
134
80
  // have been set by the transformer. It's fine to do this, since the
135
81
  // only place where this file will be imported from is our entry file
136
82
  // and we'll know the right path to use.
137
- const newPath = isMainFile && tsTransformer?.newMainFile
138
- ? tsTransformer.newMainFile
139
- : getPathWithExtension(path, MODULE_FILE_EXTENSION.JS);
83
+ const newPath = transformer?.newMainFile ?? getPathWithExtension(path, MODULE_FILE_EXTENSION.JS);
140
84
  // Overriding the contents of the `.ts` file.
141
- tsTransformer?.rewrites.set(path, transpiled);
85
+ transformer?.rewrites.set(path, transpiled);
142
86
  // Rewriting the `.ts` path to `.js` in the bundle.
143
- tsTransformer?.aliases.set(path, newPath);
87
+ transformer?.aliases.set(path, newPath);
144
88
  // Registering the input files that were bundled into the transpiled
145
89
  // file.
146
- tsTransformer?.bundledPaths?.push(...bundledPaths);
90
+ transformer?.bundledPaths?.push(...bundledPaths);
147
91
  return transpiled;
148
92
  }
149
93
  return await cachedReadFile(cache.fileCache, path);
@@ -172,13 +116,13 @@ const traceFilesAndTranspile = async function ({ basePath, cache, config, featur
172
116
  },
173
117
  });
174
118
  const normalizedTracedPaths = [...dependencyPaths].map((path) => (basePath ? resolve(basePath, path) : resolve(path)));
175
- if (tsTransformer) {
119
+ if (transformer) {
176
120
  return {
177
- aliases: tsTransformer.aliases,
178
- bundledPaths: tsTransformer.bundledPaths,
179
- mainFile: tsTransformer.newMainFile ?? getPathWithExtension(mainFile, MODULE_FILE_EXTENSION.JS),
180
- moduleFormat: tsTransformer.format,
181
- rewrites: tsTransformer.rewrites,
121
+ aliases: transformer.aliases,
122
+ bundledPaths: transformer.bundledPaths,
123
+ mainFile: transformer.newMainFile ?? getPathWithExtension(mainFile, MODULE_FILE_EXTENSION.JS),
124
+ moduleFormat: transformer.format,
125
+ rewrites: transformer.rewrites,
182
126
  tracedPaths: normalizedTracedPaths,
183
127
  };
184
128
  }
@@ -0,0 +1,23 @@
1
+ import type { FunctionConfig } from '../../../../config.js';
2
+ import { ModuleFormat } from '../../utils/module_format.js';
3
+ type Transformer = {
4
+ aliases: Map<string, string>;
5
+ bundle?: boolean;
6
+ bundledPaths?: string[];
7
+ format: ModuleFormat;
8
+ newMainFile?: string;
9
+ rewrites: Map<string, string>;
10
+ };
11
+ export declare const getTransformer: (runtimeAPIVersion: number, mainFile: string, repositoryRoot?: string) => Promise<Transformer | undefined>;
12
+ interface TransformOptions {
13
+ bundle?: boolean;
14
+ config: FunctionConfig;
15
+ format?: ModuleFormat;
16
+ name: string;
17
+ path: string;
18
+ }
19
+ export declare const transform: ({ bundle, config, format, name, path }: TransformOptions) => Promise<{
20
+ bundledPaths: string[];
21
+ transpiled: string;
22
+ }>;
23
+ export {};
@@ -0,0 +1,101 @@
1
+ import { dirname, extname, resolve } from 'path';
2
+ import { build } from 'esbuild';
3
+ import { FunctionBundlingUserError } from '../../../../utils/error.js';
4
+ import { getPathWithExtension } from '../../../../utils/fs.js';
5
+ import { RUNTIME } from '../../../runtime.js';
6
+ import { CJS_SHIM } from '../../utils/esm_cjs_compat.js';
7
+ import { MODULE_FORMAT, MODULE_FILE_EXTENSION } from '../../utils/module_format.js';
8
+ import { getClosestPackageJson } from '../../utils/package_json.js';
9
+ import { getBundlerTarget } from '../esbuild/bundler_target.js';
10
+ import { NODE_BUNDLER } from '../types.js';
11
+ /**
12
+ * Returns the module format that should be used for a given function file.
13
+ */
14
+ const getModuleFormat = async (mainFile, runtimeAPIVersion, repositoryRoot) => {
15
+ const extension = extname(mainFile);
16
+ // TODO: This check should go away. We should always respect the format from
17
+ // the extension. We'll do this at a later stage, after we roll out the V2
18
+ // API with no side-effects on V1 functions.
19
+ if (runtimeAPIVersion === 2) {
20
+ if (extension === MODULE_FILE_EXTENSION.MJS || extension === MODULE_FILE_EXTENSION.MTS) {
21
+ return MODULE_FORMAT.ESM;
22
+ }
23
+ if (extension === MODULE_FILE_EXTENSION.CTS || extension === MODULE_FILE_EXTENSION.CTS) {
24
+ return MODULE_FORMAT.COMMONJS;
25
+ }
26
+ }
27
+ // At this point, we need to infer the module type from the `type` field in
28
+ // the closest `package.json`.
29
+ try {
30
+ const packageJSON = await getClosestPackageJson(dirname(mainFile), repositoryRoot);
31
+ if (packageJSON?.contents.type === 'module') {
32
+ return MODULE_FORMAT.ESM;
33
+ }
34
+ }
35
+ catch {
36
+ // no-op
37
+ }
38
+ return MODULE_FORMAT.COMMONJS;
39
+ };
40
+ export const getTransformer = async (runtimeAPIVersion, mainFile, repositoryRoot) => {
41
+ const format = await getModuleFormat(mainFile, runtimeAPIVersion, repositoryRoot);
42
+ const aliases = new Map();
43
+ const rewrites = new Map();
44
+ const transformer = {
45
+ aliases,
46
+ format,
47
+ rewrites,
48
+ };
49
+ if (runtimeAPIVersion === 2) {
50
+ // For V2 functions, we want to emit a main file with an unambiguous
51
+ // extension (i.e. `.cjs` or `.mjs`), so that the file is loaded with
52
+ // the correct format regardless of what is set in `package.json`.
53
+ const newExtension = format === MODULE_FORMAT.COMMONJS ? MODULE_FILE_EXTENSION.CJS : MODULE_FILE_EXTENSION.MJS;
54
+ const newMainFile = getPathWithExtension(mainFile, newExtension);
55
+ return {
56
+ ...transformer,
57
+ bundle: true,
58
+ bundledPaths: [],
59
+ newMainFile,
60
+ };
61
+ }
62
+ return transformer;
63
+ };
64
+ export const transform = async ({ bundle = false, config, format, name, path }) => {
65
+ // The version of ECMAScript to use as the build target. This will determine
66
+ // whether certain features are transpiled down or left untransformed.
67
+ const nodeTarget = getBundlerTarget(config.nodeVersion);
68
+ const bundleOptions = {
69
+ bundle: false,
70
+ };
71
+ if (bundle) {
72
+ bundleOptions.bundle = true;
73
+ bundleOptions.packages = 'external';
74
+ if (format === MODULE_FORMAT.ESM) {
75
+ bundleOptions.banner = { js: CJS_SHIM };
76
+ }
77
+ }
78
+ try {
79
+ const transpiled = await build({
80
+ ...bundleOptions,
81
+ entryPoints: [path],
82
+ format,
83
+ logLevel: 'error',
84
+ metafile: true,
85
+ platform: 'node',
86
+ sourcemap: Boolean(config.nodeSourcemap),
87
+ target: [nodeTarget],
88
+ write: false,
89
+ });
90
+ const bundledPaths = bundle ? Object.keys(transpiled.metafile.inputs).map((inputPath) => resolve(inputPath)) : [];
91
+ return { bundledPaths, transpiled: transpiled.outputFiles[0].text };
92
+ }
93
+ catch (error) {
94
+ throw FunctionBundlingUserError.addCustomErrorInfo(error, {
95
+ functionName: name,
96
+ runtime: RUNTIME.JAVASCRIPT,
97
+ bundler: NODE_BUNDLER.NFT,
98
+ });
99
+ }
100
+ };
101
+ //# sourceMappingURL=transformer.js.map
@@ -1,20 +1,8 @@
1
1
  import type { FunctionConfig } from '../../../../config.js';
2
- import { ModuleFormat } from '../../utils/module_format.js';
3
2
  interface TranspileESMToCJSOptions {
4
3
  config: FunctionConfig;
5
4
  name: string;
6
5
  path: string;
7
6
  }
8
7
  export declare const transpileESMToCJS: ({ config, name, path }: TranspileESMToCJSOptions) => Promise<string>;
9
- interface TranspileTSOptions {
10
- bundle?: boolean;
11
- config: FunctionConfig;
12
- format?: ModuleFormat;
13
- name: string;
14
- path: string;
15
- }
16
- export declare const transpileTS: ({ bundle, config, format, name, path }: TranspileTSOptions) => Promise<{
17
- bundledPaths: string[];
18
- transpiled: string;
19
- }>;
20
8
  export {};
@@ -1,8 +1,6 @@
1
- import { resolve } from 'path';
2
1
  import { build } from 'esbuild';
3
2
  import { FunctionBundlingUserError } from '../../../../utils/error.js';
4
3
  import { RUNTIME } from '../../../runtime.js';
5
- import { CJS_SHIM } from '../../utils/esm_cjs_compat.js';
6
4
  import { MODULE_FORMAT } from '../../utils/module_format.js';
7
5
  import { getBundlerTarget } from '../esbuild/bundler_target.js';
8
6
  import { NODE_BUNDLER } from '../types.js';
@@ -31,41 +29,4 @@ export const transpileESMToCJS = async ({ config, name, path }) => {
31
29
  });
32
30
  }
33
31
  };
34
- export const transpileTS = async ({ bundle = false, config, format, name, path }) => {
35
- // The version of ECMAScript to use as the build target. This will determine
36
- // whether certain features are transpiled down or left untransformed.
37
- const nodeTarget = getBundlerTarget(config.nodeVersion);
38
- const bundleOptions = {
39
- bundle: false,
40
- };
41
- if (bundle) {
42
- bundleOptions.bundle = true;
43
- bundleOptions.packages = 'external';
44
- if (format === MODULE_FORMAT.ESM) {
45
- bundleOptions.banner = { js: CJS_SHIM };
46
- }
47
- }
48
- try {
49
- const transpiled = await build({
50
- ...bundleOptions,
51
- entryPoints: [path],
52
- format,
53
- logLevel: 'error',
54
- metafile: true,
55
- platform: 'node',
56
- sourcemap: Boolean(config.nodeSourcemap),
57
- target: [nodeTarget],
58
- write: false,
59
- });
60
- const bundledPaths = bundle ? Object.keys(transpiled.metafile.inputs).map((inputPath) => resolve(inputPath)) : [];
61
- return { bundledPaths, transpiled: transpiled.outputFiles[0].text };
62
- }
63
- catch (error) {
64
- throw FunctionBundlingUserError.addCustomErrorInfo(error, {
65
- functionName: name,
66
- runtime: RUNTIME.JAVASCRIPT,
67
- bundler: NODE_BUNDLER.NFT,
68
- });
69
- }
70
- };
71
32
  //# sourceMappingURL=transpile.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/zip-it-and-ship-it",
3
- "version": "9.22.0",
3
+ "version": "9.23.0",
4
4
  "description": "Zip it and ship it",
5
5
  "main": "./dist/main.js",
6
6
  "type": "module",