Skip to content

Commit

Permalink
feat: initial implementation for copy-on-change
Browse files Browse the repository at this point in the history
  • Loading branch information
linbudu599 committed Mar 6, 2023
1 parent 3e2bc7e commit 8513dae
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 29 deletions.
73 changes: 64 additions & 9 deletions packages/esbuild-plugin-copy/src/lib/esbuild-plugin-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export const copy = (options: Partial<Options> = {}): Plugin => {
once = false,
resolveFrom = 'out',
dryRun = false,
watch: _globalWatchControl = false,
} = options;

let globalWatchControl = _globalWatchControl;

const verbose = dryRun === true || _verbose;

const formattedAssets = formatAssets(assets);
Expand Down Expand Up @@ -90,7 +93,30 @@ export const copy = (options: Partial<Options> = {}): Plugin => {
verbose
);

for (const { from, to } of formattedAssets) {
globalWatchControl
? verboseLog(
`Watching mode enabled for all asset pairs, you can disable it by set ${chalk.white(
'watch'
)} to false in specified asset pairs`,
verbose
)
: void 0;

if (!build.initialOptions.watch) {
verboseLog(
`Watching mode diabled. You need to enable ${chalk.white(
'build.watch'
)} option for watch mode to work.`,
verbose
);

globalWatchControl = false;
}

for (const { from, to, watch: localWatchControl } of formattedAssets) {
const useWatchModeForCurrentAssetPair =
globalWatchControl || localWatchControl;

const pathsCopyFrom = await globby(from, {
// we donot expand directories be default
expandDirectories: false,
Expand All @@ -112,14 +138,41 @@ export const copy = (options: Partial<Options> = {}): Plugin => {
);
}

const watcher = chokidar.watch(deduplicatedPaths, {
ignoreInitial: false,
disableGlobbing: true,
});
const executor = () => {
for (const fromPath of deduplicatedPaths) {
to.forEach((toPath) => {
copyOperationHandler(
outDirResolveFrom,
from,
fromPath,
toPath,
verbose,
dryRun
);
});
}
};

if (useWatchModeForCurrentAssetPair) {
verboseLog(
'Watching mode enabled for current asset pair, files will only be copied again on changes.',
verbose
);

executor();

const watcher = chokidar.watch(deduplicatedPaths, {
ignoreInitial: false,
});

watcher.on('change', (fromPath) => {
verboseLog(
`File ${chalk.white(
fromPath
)} changed, copy operation triggered.`,
verbose
);

for (const fromPath of deduplicatedPaths) {
// register watch for every fromPath, as fromPath can only be complete file path
watcher.on('change', (path) => {
to.forEach((toPath) => {
copyOperationHandler(
outDirResolveFrom,
Expand All @@ -131,8 +184,10 @@ export const copy = (options: Partial<Options> = {}): Plugin => {
);
});
});
} else {
executor();
process.env[PLUGIN_EXECUTED_FLAG] = 'true';
}
process.env[PLUGIN_EXECUTED_FLAG] = 'true';
}
});
},
Expand Down
8 changes: 5 additions & 3 deletions packages/esbuild-plugin-copy/src/lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ export function copyOperationHandler(
dryRun ? void 0 : fs.copyFileSync(sourcePath, composedDistDirPath);

verboseLog(
`${dryRun ? chalk.white('[DryRun] ') : ''}File copied: ${chalk.white(
sourcePath
)} -> ${chalk.white(composedDistDirPath)}`,
`${new Date().getTime()} ${
dryRun ? chalk.white('[DryRun] ') : ''
}File copied: ${chalk.white(sourcePath)} -> ${chalk.white(
composedDistDirPath
)}`,
verbose
);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/esbuild-plugin-copy/src/lib/typings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GlobbyOptions } from 'globby';
import type { WatchOptions } from 'chokidar';

export type MaybeArray<T> = T | T[];

Expand All @@ -14,6 +15,11 @@ export interface AssetPair {
* you can also set `resolveFrom` to change the base dir
*/
to: MaybeArray<string>;

/**
* control watch mode for current assets
*/
watch?: boolean | WatchOptions;
}

export interface Options {
Expand Down Expand Up @@ -70,4 +76,9 @@ export interface Options {
* @default false
*/
dryRun?: boolean;

/**
* enable watch mode
*/
watch?: boolean | WatchOptions;
}
3 changes: 2 additions & 1 deletion packages/esbuild-plugin-copy/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export function verboseLog(msg: string, verbose: boolean, lineBefore = false) {
export function formatAssets(assets: MaybeArray<AssetPair>) {
return ensureArray(assets)
.filter((asset) => asset.from && asset.to)
.map(({ from, to }) => ({
.map(({ from, to, watch }) => ({
from: ensureArray(from),
to: ensureArray(to),
watch: watch ?? false,
}));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-copy/tests/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('Oops!');
console.log('Ooddddfffffps!');
Loading

0 comments on commit 8513dae

Please sign in to comment.