UNPKG

@remotion/bundler

Version:

Bundle Remotion compositions using Webpack

84 lines (83 loc) 3.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cacheExists = exports.clearCache = exports.getWebpackCacheName = exports.getWebpackCacheEnvDir = void 0; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const version_1 = require("remotion/version"); // Inlined from https://github.com/webpack/webpack/blob/4c2ee7a4ddb8db2362ca83b6c4190523387ba7ee/lib/config/defaults.js#L265 // An algorithm to determine where Webpack will cache the depencies const getWebpackCacheDir = (remotionRoot) => { let dir = remotionRoot; for (;;) { try { if (node_fs_1.default.statSync(node_path_1.default.join(dir, 'package.json')).isFile()) { break; } } catch (_a) { } const parent = node_path_1.default.dirname(dir); if (dir === parent) { dir = undefined; break; } dir = parent; } if (!dir) { return node_path_1.default.resolve(remotionRoot, '.cache/webpack'); } if (process.versions.pnp === '1') { return node_path_1.default.resolve(dir, '.pnp/.cache/webpack'); } if (process.versions.pnp === '3') { return node_path_1.default.resolve(dir, '.yarn/.cache/webpack'); } return node_path_1.default.resolve(dir, 'node_modules/.cache/webpack'); }; const getPrefix = (environment) => { return `remotion-${environment}-${version_1.VERSION}`; }; const getWebpackCacheEnvDir = (environment) => { return getPrefix(environment); }; exports.getWebpackCacheEnvDir = getWebpackCacheEnvDir; const getWebpackCacheName = (environment, hash) => { return [(0, exports.getWebpackCacheEnvDir)(environment), hash].join(node_path_1.default.sep); }; exports.getWebpackCacheName = getWebpackCacheName; const remotionCacheLocationForEnv = (remotionRoot, environment) => { return node_path_1.default.join(getWebpackCacheDir(remotionRoot), (0, exports.getWebpackCacheEnvDir)(environment)); }; const remotionCacheLocation = (remotionRoot, environment, hash) => { return node_path_1.default.join(getWebpackCacheDir(remotionRoot), (0, exports.getWebpackCacheName)(environment, hash)); }; const clearCache = (remotionRoot, env) => { return node_fs_1.default.promises.rm(remotionCacheLocationForEnv(remotionRoot, env), { recursive: true, }); }; exports.clearCache = clearCache; const hasOtherCache = ({ remotionRoot, environment, }) => { const cacheDir = node_fs_1.default.readdirSync(getWebpackCacheDir(remotionRoot)); if (cacheDir.find((c) => { return c.startsWith(getPrefix(environment)); })) { return true; } return false; }; const cacheExists = (remotionRoot, environment, hash) => { if (node_fs_1.default.existsSync(remotionCacheLocation(remotionRoot, environment, hash))) { return 'exists'; } if (!node_fs_1.default.existsSync(getWebpackCacheDir(remotionRoot))) { return 'does-not-exist'; } if (hasOtherCache({ remotionRoot, environment })) { return 'other-exists'; } return 'does-not-exist'; }; exports.cacheExists = cacheExists;