UNPKG

@swc/cli

Version:
136 lines (135 loc) 4.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.globSources = globSources; exports.isCompilableExtension = isCompilableExtension; exports.splitCompilableAndCopyable = splitCompilableAndCopyable; exports.requireChokidar = requireChokidar; exports.watchSources = watchSources; const tinyglobby_1 = require("tinyglobby"); const slash_1 = __importDefault(require("slash")); const fs_1 = require("fs"); const path_1 = require("path"); const minimatch_1 = require("minimatch"); /** * Find all input files based on source globs */ async function globSources(sources, only, ignore, includeDotfiles = false) { const globConfig = { dot: includeDotfiles, ignore, expandDirectories: false, }; const files = await Promise.all(sources .filter(source => includeDotfiles || source === "." || !(0, path_1.basename)(source).startsWith(".")) .map(source => { return new Promise(resolve => { (0, fs_1.stat)(source, (err, stat) => { if (err) { resolve([]); return; } if (!stat.isDirectory()) { resolve([source]); } else { (0, tinyglobby_1.glob)((0, slash_1.default)((0, path_1.join)(source, "**")), globConfig) .then(matches => resolve(matches)) .catch(() => resolve([])); } }); }); })); const f = files.flat().filter(filename => { return (!only || only.length === 0 || only.some(only => (0, minimatch_1.minimatch)((0, slash_1.default)(filename), only))); }); return Array.from(new Set(f)); } /** * Test if a filename ends with a compilable extension. */ function isCompilableExtension(filename, allowedExtension) { const ext = (0, path_1.extname)(filename); return allowedExtension.includes(ext); } /** * Split file list to files that can be compiled and copied */ function splitCompilableAndCopyable(files, allowedExtension, copyFiles) { const compilable = []; const copyable = []; for (const file of files) { const isCompilable = isCompilableExtension(file, allowedExtension); if (isCompilable) { compilable.push(file); } else if (copyFiles) { copyable.push(file); } } return [compilable, copyable]; } async function requireChokidar() { try { const { default: chokidar } = await Promise.resolve().then(() => __importStar(require("chokidar"))); return chokidar; } catch (err) { console.error("The optional dependency chokidar is not installed and is required for " + "--watch. Chokidar is likely not supported on your platform."); throw err; } } async function watchSources(sources, includeDotfiles = false, only = [], ignore = []) { const chokidar = await requireChokidar(); const sourceFiles = await globSources(sources, only, ignore, includeDotfiles); return chokidar.watch(sourceFiles, { ignored: includeDotfiles ? undefined : (filename) => (0, path_1.basename)(filename).startsWith("."), ignoreInitial: true, awaitWriteFinish: { stabilityThreshold: 50, pollInterval: 10, }, }); }