esbuild-copy-files
Version:
esbuild plugin to copy static files
129 lines (124 loc) • 4.64 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
copy: () => esbuild_copy_files_default
});
module.exports = __toCommonJS(src_exports);
// src/esbuild-copy-files.ts
var import_chokidar = __toESM(require("chokidar"));
// src/utils.ts
var import_path = __toESM(require("path"));
var import_fs_extra = __toESM(require("fs-extra"));
var import_anymatch = __toESM(require("anymatch"));
var ensureArray = (value) => Array.isArray(value) ? value : [value];
var forceCopy = async (sourcePath, destPath) => {
try {
await import_fs_extra.default.copy(sourcePath, destPath);
} catch (error) {
if (error.code === "ENOENT") {
return false;
}
}
};
var copyFiles = async ({ to = [], ignore = [], source }) => {
if (!source)
return;
const currentRoot = process.cwd();
const sourcePath = import_path.default.resolve(currentRoot, source);
for (const dest of ensureArray(to)) {
const destPath = import_path.default.resolve(currentRoot, dest);
await import_fs_extra.default.ensureDir(destPath);
await forceCopy(sourcePath, destPath);
const dirEntries = await import_fs_extra.default.readdir(sourcePath, { withFileTypes: true });
const ignoreArray = ensureArray(ignore);
if (!ignoreArray.length)
return;
for (const entry of dirEntries) {
const match = (0, import_anymatch.default)(ignoreArray, entry.name);
if (match) {
if (entry.isDirectory() || entry.isFile()) {
await import_fs_extra.default.remove(destPath + "/" + entry.name);
}
}
}
}
};
var copyFilesOnChange = ({ to = [], source, ignore }) => async (watchedFilePath) => {
const fileName = import_path.default.basename(watchedFilePath);
if (!ensureArray(ignore).length) {
await copyFiles({ to, source, ignore });
return;
}
;
const ignoreMatch = (0, import_anymatch.default)(ensureArray(ignore), fileName);
if (!ignoreMatch) {
await copyFiles({ to, source, ignore });
}
};
// src/esbuild-copy-files.ts
var copy = ({ patterns = [], stopWatching = false, watch = false }) => ({
name: "esbuild-copy-files",
setup: (build) => {
build.onEnd(async () => {
try {
for (const pattern of patterns) {
const { from = [], to = [], ignore = [], watch: patternWatch } = pattern;
for (const source of ensureArray(from)) {
await copyFiles({ to, source, ignore });
if (watch && patternWatch) {
const watcher = import_chokidar.default.watch(source, {
disableGlobbing: false,
usePolling: true,
interval: 200,
ignorePermissionErrors: true
});
watcher.on("change", copyFilesOnChange({ to, source, ignore }));
watcher.on("add", copyFilesOnChange({ to, source, ignore }));
if (stopWatching) {
await watcher.close();
}
}
}
}
} catch (error) {
if (error.code === "EBUSY") {
return;
}
console.error(error);
}
});
}
});
var esbuild_copy_files_default = copy;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
copy
});