@netlify/zip-it-and-ship-it 4.28.1 → 4.28.2

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.
@@ -15,6 +15,7 @@ declare type BundleFunction = (args: {
15
15
  repositoryRoot?: string;
16
16
  } & FunctionSource) => Promise<{
17
17
  aliases?: Map<string, string>;
18
+ rewrites?: Map<string, string>;
18
19
  basePath: string;
19
20
  bundlerWarnings?: BundlerWarning[];
20
21
  cleanupFunction?: CleanupFunction;
@@ -0,0 +1,4 @@
1
+ import { NodeFileTraceReasons } from '@vercel/nft';
2
+ import { FsCache } from '../../../../utils/fs';
3
+ declare const getPatchedESMPackages: (esmPaths: Set<string>, reasons: NodeFileTraceReasons, fsCache: FsCache, basePath?: string | undefined) => Promise<Map<any, any>>;
4
+ export { getPatchedESMPackages };
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getPatchedESMPackages = void 0;
13
+ const path_1 = require("path");
14
+ const fs_1 = require("../../../../utils/fs");
15
+ const getESMPackageJsons = (esmPaths, reasons, basePath) => {
16
+ const packageJsons = [...reasons.entries()]
17
+ .filter(([, reason]) => {
18
+ if (reason.type !== 'resolve') {
19
+ return false;
20
+ }
21
+ const hasESMParent = [...reason.parents].some((parentPath) => esmPaths.has(parentPath));
22
+ return hasESMParent;
23
+ })
24
+ .map(([path]) => (basePath ? (0, path_1.resolve)(basePath, path) : (0, path_1.resolve)(path)));
25
+ return packageJsons;
26
+ };
27
+ const getPatchedESMPackages = (esmPaths, reasons, fsCache, basePath) => __awaiter(void 0, void 0, void 0, function* () {
28
+ const packages = getESMPackageJsons(esmPaths, reasons, basePath);
29
+ const patchedPackages = yield Promise.all(packages.map((path) => patchESMPackage(path, fsCache)));
30
+ const patchedPackagesMap = new Map();
31
+ packages.forEach((packagePath, index) => {
32
+ patchedPackagesMap.set(packagePath, patchedPackages[index]);
33
+ });
34
+ return patchedPackagesMap;
35
+ });
36
+ exports.getPatchedESMPackages = getPatchedESMPackages;
37
+ const patchESMPackage = (path, fsCache) => __awaiter(void 0, void 0, void 0, function* () {
38
+ const file = (yield (0, fs_1.cachedReadFile)(fsCache, path, 'utf8'));
39
+ const packageJson = JSON.parse(file);
40
+ const patchedPackageJson = Object.assign(Object.assign({}, packageJson), { type: 'commonjs' });
41
+ return JSON.stringify(patchedPackageJson);
42
+ });
43
+ //# sourceMappingURL=es_modules.js.map
@@ -20,6 +20,7 @@ const unixify_1 = __importDefault(require("unixify"));
20
20
  const fs_1 = require("../../../../utils/fs");
21
21
  const base_path_1 = require("../../utils/base_path");
22
22
  const included_files_1 = require("../../utils/included_files");
23
+ const es_modules_1 = require("./es_modules");
23
24
  const transpile_1 = require("./transpile");
24
25
  // Paths that will be excluded from the tracing process.
25
26
  const ignore = ['node_modules/aws-sdk/**'];
@@ -27,7 +28,7 @@ const appearsToBeModuleName = (name) => !name.startsWith('.');
27
28
  const bundle = ({ basePath, config, mainFile, pluginsModulesPath, repositoryRoot = basePath, }) => __awaiter(void 0, void 0, void 0, function* () {
28
29
  const { includedFiles = [], includedFilesBasePath } = config;
29
30
  const { exclude: excludedPaths, paths: includedFilePaths } = yield (0, included_files_1.getPathsOfIncludedFiles)(includedFiles, includedFilesBasePath || basePath);
30
- const { cleanupFunction, paths: dependencyPaths, transpilation, } = yield traceFilesAndTranspile({
31
+ const { paths: dependencyPaths, rewrites } = yield traceFilesAndTranspile({
31
32
  basePath: repositoryRoot,
32
33
  config,
33
34
  mainFile,
@@ -36,13 +37,12 @@ const bundle = ({ basePath, config, mainFile, pluginsModulesPath, repositoryRoot
36
37
  const filteredIncludedPaths = (0, included_files_1.filterExcludedPaths)([...dependencyPaths, ...includedFilePaths], excludedPaths);
37
38
  const dirnames = filteredIncludedPaths.map((filePath) => (0, path_1.normalize)((0, path_1.dirname)(filePath))).sort();
38
39
  // Sorting the array to make the checksum deterministic.
39
- const srcFiles = [...filteredIncludedPaths, ...transpilation.keys()].sort();
40
+ const srcFiles = [...filteredIncludedPaths].sort();
40
41
  return {
41
- aliases: transpilation,
42
42
  basePath: (0, base_path_1.getBasePath)(dirnames),
43
- cleanupFunction,
44
43
  inputs: dependencyPaths,
45
44
  mainFile,
45
+ rewrites,
46
46
  srcFiles,
47
47
  };
48
48
  });
@@ -52,13 +52,10 @@ const ignoreFunction = (path) => {
52
52
  return shouldIgnore;
53
53
  };
54
54
  const traceFilesAndTranspile = function ({ basePath, config, mainFile, pluginsModulesPath, }) {
55
- var _a;
56
55
  return __awaiter(this, void 0, void 0, function* () {
57
56
  const fsCache = {};
58
- const cache = {};
59
- const { fileList: dependencyPaths } = yield (0, nft_1.nodeFileTrace)([mainFile], {
57
+ const { fileList: dependencyPaths, esmFileList, reasons, } = yield (0, nft_1.nodeFileTrace)([mainFile], {
60
58
  base: basePath,
61
- cache,
62
59
  ignore: ignoreFunction,
63
60
  readFile: (path) => __awaiter(this, void 0, void 0, function* () {
64
61
  try {
@@ -89,26 +86,13 @@ const traceFilesAndTranspile = function ({ basePath, config, mainFile, pluginsMo
89
86
  }),
90
87
  });
91
88
  const normalizedDependencyPaths = [...dependencyPaths].map((path) => basePath ? (0, path_1.resolve)(basePath, path) : (0, path_1.resolve)(path));
92
- // We look at the cache object to find any paths corresponding to ESM files.
93
- const esmPaths = [...(((_a = cache.analysisCache) === null || _a === void 0 ? void 0 : _a.entries()) || [])].filter(([, { isESM }]) => isESM).map(([path]) => path);
94
- // After transpiling the ESM files, we get back a `Map` mapping the path of
95
- // each transpiled to its original path.
96
- const transpilation = yield (0, transpile_1.transpileMany)(esmPaths, config);
97
- // Creating a `Set` with the original paths of the transpiled files so that
98
- // we can do a O(1) lookup.
99
- const originalPaths = new Set(transpilation.values());
100
- // We remove the transpiled paths from the list of traced files, otherwise we
101
- // would end up with duplicate files in the archive.
102
- const filteredDependencyPaths = normalizedDependencyPaths.filter((path) => !originalPaths.has(path));
103
- // The cleanup function will delete all the temporary files that were created
104
- // as part of the transpilation process.
105
- const cleanupFunction = () => __awaiter(this, void 0, void 0, function* () {
106
- yield Promise.all([...transpilation.keys()].map(fs_1.safeUnlink));
107
- });
89
+ const esmPaths = [...esmFileList].map((path) => (basePath ? (0, path_1.resolve)(basePath, path) : (0, path_1.resolve)(path)));
90
+ const transpiledPaths = yield (0, transpile_1.transpileMany)(esmPaths, config);
91
+ const patchedESMPackages = yield (0, es_modules_1.getPatchedESMPackages)(esmFileList, reasons, fsCache, basePath);
92
+ const rewrites = new Map([...transpiledPaths, ...patchedESMPackages]);
108
93
  return {
109
- cleanupFunction,
110
- paths: filteredDependencyPaths,
111
- transpilation,
94
+ paths: normalizedDependencyPaths,
95
+ rewrites,
112
96
  };
113
97
  });
114
98
  };
@@ -11,34 +11,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.transpileMany = void 0;
13
13
  const esbuild_1 = require("@netlify/esbuild");
14
- const tmp_promise_1 = require("tmp-promise");
15
- const fs_1 = require("../../../../utils/fs");
16
14
  const bundler_target_1 = require("../esbuild/bundler_target");
17
15
  const transpile = (path, config) => __awaiter(void 0, void 0, void 0, function* () {
18
- const targetPath = yield (0, tmp_promise_1.tmpName)({ postfix: '.js' });
19
- const cleanupFn = () => (0, fs_1.safeUnlink)(targetPath);
20
16
  // The version of ECMAScript to use as the build target. This will determine
21
17
  // whether certain features are transpiled down or left untransformed.
22
18
  const nodeTarget = (0, bundler_target_1.getBundlerTarget)(config.nodeVersion);
23
- yield (0, esbuild_1.build)({
19
+ const transpiled = yield (0, esbuild_1.build)({
24
20
  bundle: false,
25
21
  entryPoints: [path],
26
22
  format: 'cjs',
27
23
  logLevel: 'error',
28
- outfile: targetPath,
29
24
  platform: 'node',
30
25
  target: [nodeTarget],
26
+ write: false,
31
27
  });
32
- return {
33
- cleanupFn,
34
- path: targetPath,
35
- };
28
+ return transpiled.outputFiles[0].text;
36
29
  });
37
30
  const transpileMany = (paths, config) => __awaiter(void 0, void 0, void 0, function* () {
38
31
  const transpiledPaths = new Map();
39
32
  yield Promise.all(paths.map((path) => __awaiter(void 0, void 0, void 0, function* () {
40
33
  const transpiled = yield transpile(path, config);
41
- transpiledPaths.set(transpiled.path, path);
34
+ transpiledPaths.set(path, transpiled);
42
35
  })));
43
36
  return transpiledPaths;
44
37
  });
@@ -70,7 +70,7 @@ const zipFunction = function ({ archiveFormat, basePath, config = {}, destFolder
70
70
  yield (0, cp_file_1.default)(srcPath, destPath);
71
71
  return { config, path: destPath };
72
72
  }
73
- const { aliases, cleanupFunction, basePath: finalBasePath, bundlerWarnings, inputs, mainFile: finalMainFile = mainFile, nativeNodeModules, nodeModulesWithDynamicImports, srcFiles, } = yield bundler.bundle({
73
+ const { aliases, cleanupFunction, basePath: finalBasePath, bundlerWarnings, inputs, mainFile: finalMainFile = mainFile, nativeNodeModules, nodeModulesWithDynamicImports, rewrites, srcFiles, } = yield bundler.bundle({
74
74
  basePath,
75
75
  config,
76
76
  extension,
@@ -94,6 +94,7 @@ const zipFunction = function ({ archiveFormat, basePath, config = {}, destFolder
94
94
  filename,
95
95
  mainFile: finalMainFile,
96
96
  pluginsModulesPath,
97
+ rewrites,
97
98
  srcFiles,
98
99
  });
99
100
  yield (cleanupFunction === null || cleanupFunction === void 0 ? void 0 : cleanupFunction());
@@ -11,6 +11,7 @@ interface PackageJson {
11
11
  files?: string[];
12
12
  gypfile?: boolean;
13
13
  binary?: boolean;
14
+ type?: string;
14
15
  }
15
16
  declare const getPackageJson: (srcDir: string) => Promise<PackageJson>;
16
17
  export { getPackageJson, PackageJson };
@@ -7,6 +7,7 @@ interface ZipNodeParameters {
7
7
  filename: string;
8
8
  mainFile: string;
9
9
  pluginsModulesPath?: string;
10
+ rewrites?: Map<string, string>;
10
11
  srcFiles: string[];
11
12
  }
12
13
  declare const zipNodeJs: ({ archiveFormat, ...options }: ZipNodeParameters & {
@@ -43,7 +43,7 @@ const COPY_FILE_CONCURRENCY = os_1.default.cpus().length === 0 ? 2 : os_1.defaul
43
43
  // Sub-directory to place all user-defined files (i.e. everything other than
44
44
  // the entry file generated by zip-it-and-ship-it).
45
45
  const DEFAULT_USER_SUBDIRECTORY = 'src';
46
- const createDirectory = function ({ aliases = new Map(), basePath, destFolder, extension, filename, mainFile, pluginsModulesPath, srcFiles, }) {
46
+ const createDirectory = function ({ aliases = new Map(), basePath, destFolder, extension, filename, mainFile, pluginsModulesPath, rewrites = new Map(), srcFiles, }) {
47
47
  return __awaiter(this, void 0, void 0, function* () {
48
48
  const { contents: entryContents, filename: entryFilename } = getEntryFile({
49
49
  commonPrefix: basePath,
@@ -59,20 +59,23 @@ const createDirectory = function ({ aliases = new Map(), basePath, destFolder, e
59
59
  yield pWriteFile((0, path_1.join)(functionFolder, entryFilename), entryContents);
60
60
  // Copying source files.
61
61
  yield (0, p_map_1.default)(srcFiles, (srcFile) => {
62
- const srcPath = aliases.get(srcFile) || srcFile;
63
- const normalizedSrcPath = normalizeFilePath({
62
+ const destPath = aliases.get(srcFile) || srcFile;
63
+ const normalizedDestPath = normalizeFilePath({
64
64
  commonPrefix: basePath,
65
- path: srcPath,
65
+ path: destPath,
66
66
  pluginsModulesPath,
67
67
  userNamespace: DEFAULT_USER_SUBDIRECTORY,
68
68
  });
69
- const destPath = (0, path_1.join)(functionFolder, normalizedSrcPath);
70
- return (0, cp_file_1.default)(srcFile, destPath);
69
+ const absoluteDestPath = (0, path_1.join)(functionFolder, normalizedDestPath);
70
+ if (rewrites.has(srcFile)) {
71
+ return pWriteFile(absoluteDestPath, rewrites.get(srcFile));
72
+ }
73
+ return (0, cp_file_1.default)(srcFile, absoluteDestPath);
71
74
  }, { concurrency: COPY_FILE_CONCURRENCY });
72
75
  return functionFolder;
73
76
  });
74
77
  };
75
- const createZipArchive = function ({ aliases, basePath, destFolder, extension, filename, mainFile, pluginsModulesPath, srcFiles, }) {
78
+ const createZipArchive = function ({ aliases, basePath, destFolder, extension, filename, mainFile, pluginsModulesPath, rewrites, srcFiles, }) {
76
79
  return __awaiter(this, void 0, void 0, function* () {
77
80
  const destPath = (0, path_1.join)(destFolder, `${(0, path_1.basename)(filename, extension)}.zip`);
78
81
  const { archive, output } = (0, archive_1.startZip)(destPath);
@@ -96,7 +99,16 @@ const createZipArchive = function ({ aliases, basePath, destFolder, extension, f
96
99
  // We ensure this is not async, so that the archive's checksum is
97
100
  // deterministic. Otherwise it depends on the order the files were added.
98
101
  srcFilesInfos.forEach(({ srcFile, stat }) => {
99
- zipJsFile({ srcFile, commonPrefix: basePath, pluginsModulesPath, archive, stat, aliases, userNamespace });
102
+ zipJsFile({
103
+ aliases,
104
+ archive,
105
+ commonPrefix: basePath,
106
+ pluginsModulesPath,
107
+ rewrites,
108
+ srcFile,
109
+ stat,
110
+ userNamespace,
111
+ });
100
112
  });
101
113
  yield (0, archive_1.endZip)(archive, output);
102
114
  return destPath;
@@ -129,10 +141,15 @@ const getEntryFile = ({ commonPrefix, filename, mainFile, userNamespace, }) => {
129
141
  filename: entryFilename,
130
142
  };
131
143
  };
132
- const zipJsFile = function ({ aliases = new Map(), archive, commonPrefix, pluginsModulesPath, stat, srcFile, userNamespace, }) {
133
- const filename = aliases.get(srcFile) || srcFile;
134
- const normalizedFilename = normalizeFilePath({ commonPrefix, path: filename, pluginsModulesPath, userNamespace });
135
- (0, archive_1.addZipFile)(archive, srcFile, normalizedFilename, stat);
144
+ const zipJsFile = function ({ aliases = new Map(), archive, commonPrefix, pluginsModulesPath, rewrites = new Map(), stat, srcFile, userNamespace, }) {
145
+ const destPath = aliases.get(srcFile) || srcFile;
146
+ const normalizedDestPath = normalizeFilePath({ commonPrefix, path: destPath, pluginsModulesPath, userNamespace });
147
+ if (rewrites.has(srcFile)) {
148
+ (0, archive_1.addZipContent)(archive, rewrites.get(srcFile), normalizedDestPath);
149
+ }
150
+ else {
151
+ (0, archive_1.addZipFile)(archive, srcFile, normalizedDestPath, stat);
152
+ }
136
153
  };
137
154
  // `adm-zip` and `require()` expect Unix paths.
138
155
  // We remove the common path prefix.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/zip-it-and-ship-it",
3
- "version": "4.28.1",
3
+ "version": "4.28.2",
4
4
  "description": "Zip it and ship it",
5
5
  "main": "./dist/main.js",
6
6
  "bin": {
@@ -94,7 +94,7 @@
94
94
  },
95
95
  "devDependencies": {
96
96
  "@babel/types": "^7.15.6",
97
- "@netlify/eslint-config-node": "^3.3.4",
97
+ "@netlify/eslint-config-node": "^3.3.5",
98
98
  "@types/archiver": "^5.1.1",
99
99
  "@types/end-of-stream": "^1.4.1",
100
100
  "@types/resolve": "^1.20.1",