diff --git a/src/sys/node/node-copy-tasks.ts b/src/sys/node/node-copy-tasks.ts index d65f241b985..c7fe19b6683 100644 --- a/src/sys/node/node-copy-tasks.ts +++ b/src/sys/node/node-copy-tasks.ts @@ -1,5 +1,5 @@ import { buildError, catchError, flatOne, isGlob, normalizePath } from '@utils'; -import { glob, type GlobOptions } from 'glob'; +import { glob } from 'glob'; import path from 'path'; import type * as d from '../../declarations'; @@ -13,7 +13,7 @@ export async function nodeCopyTasks(copyTasks: Required[], srcDir: s }; try { - copyTasks = flatOne(await Promise.all(copyTasks.map((task) => processGlobTask(task, srcDir)))); + copyTasks = flatOne(await Promise.all(copyTasks.map((task) => processGlobs(task, srcDir)))); const allCopyTasks: d.CopyTask[] = []; @@ -44,21 +44,31 @@ export async function nodeCopyTasks(copyTasks: Required[], srcDir: s return results; } +async function processGlobs(copyTask: Required, srcDir: string): Promise[]> { + return isGlob(copyTask.src) + ? await processGlobTask(copyTask, srcDir) + : [ + { + src: getSrcAbsPath(srcDir, copyTask.src), + dest: copyTask.keepDirStructure ? path.join(copyTask.dest, copyTask.src) : copyTask.dest, + warn: copyTask.warn, + ignore: copyTask.ignore, + keepDirStructure: copyTask.keepDirStructure, + }, + ]; +} + +function getSrcAbsPath(srcDir: string, src: string) { + if (path.isAbsolute(src)) { + return src; + } + return path.join(srcDir, src); +} + async function processGlobTask(copyTask: Required, srcDir: string): Promise[]> { - /** - * To properly match all files within a certain directory we have to ensure to attach a `/**` to - * the end of the pattern. However we only want to do this if the `src` entry is not a glob pattern - * already or a file with an extension. - */ - const pattern = - isGlob(copyTask.src) || path.extname(copyTask.src).length > 0 - ? copyTask.src - : './' + path.relative(srcDir, path.join(path.resolve(srcDir, copyTask.src), '**')).replaceAll(path.sep, '/'); - - const files = await asyncGlob(pattern, { + const files = await asyncGlob(copyTask.src, { cwd: srcDir, nodir: true, - absolute: false, ignore: copyTask.ignore, }); return files.map((globRelPath) => createGlobCopyTask(copyTask, srcDir, globRelPath)); @@ -89,7 +99,7 @@ async function processCopyTask(results: d.CopyResults, allCopyTasks: d.CopyTask[ } await processCopyTaskDirectory(results, allCopyTasks, copyTask); - } else { + } else if (!shouldIgnore(copyTask)) { // this is a file we should copy if (!results.filePaths.includes(copyTask.dest)) { results.filePaths.push(copyTask.dest); @@ -162,6 +172,11 @@ function addMkDir(mkDirs: string[], destDir: string) { const ROOT_DIR = normalizePath(path.resolve('/')); -export async function asyncGlob(pattern: string, opts: GlobOptions): Promise { - return glob(pattern, { ...opts, withFileTypes: false }); +function shouldIgnore({ src, ignore = [] }: d.CopyTask) { + const filePath = src.trim().toLowerCase(); + return ignore.some((ignoreFile) => filePath.endsWith(ignoreFile)); +} + +export function asyncGlob(pattern: string, opts: any) { + return glob(pattern, opts); } diff --git a/test/copy-task/stencil.config.ts b/test/copy-task/stencil.config.ts index 51c7a1b2bbe..64b832ae6c1 100644 --- a/test/copy-task/stencil.config.ts +++ b/test/copy-task/stencil.config.ts @@ -7,7 +7,7 @@ export const config: Config = { type: 'dist-custom-elements', copy: [ { - src: './utils', + src: './utils/**', dest: './dist/utilsExtra', }, ],