Skip to content

Commit

Permalink
fix(swc): fix file watching (out dir path resolution)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jul 4, 2023
1 parent b3317e7 commit 2d0c6fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/compiler/defaults/swc-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const swcDefaultsFactory = (
swcrc: true,
},
cliOptions: {
outDir: tsOptions.outDir ?? 'dist',
outDir: tsOptions.outDir ? convertPath(tsOptions.outDir) : 'dist',
filenames: [configuration?.sourceRoot ?? 'src'],
sync: false,
extensions: ['.js', '.ts'],
Expand All @@ -46,3 +46,14 @@ export const swcDefaultsFactory = (
},
};
};

/**
* Converts Windows specific file paths to posix
* @param windowsPath
*/
function convertPath(windowsPath: string) {
return windowsPath
.replace(/^\\\\\?\\/, '')
.replace(/\\/g, '/')
.replace(/\/\/+/g, '/');
}
8 changes: 6 additions & 2 deletions lib/compiler/swc/swc-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fork } from 'child_process';
import * as chokidar from 'chokidar';
import { readFileSync } from 'fs';
import { join } from 'path';
import { isAbsolute } from 'path/posix';
import * as ts from 'typescript';
import { Configuration } from '../../configuration';
import { ERROR_PREFIX } from '../../ui';
Expand Down Expand Up @@ -236,8 +237,11 @@ export class SwcCompiler extends BaseCompiler {
options: ReturnType<typeof swcDefaultsFactory>,
onChange: () => void,
) {
const outDir = join(process.cwd(), options.cliOptions.outDir!, '**/*.js');
const watcher = chokidar.watch(outDir, {
const dir = isAbsolute(options.cliOptions.outDir!)
? options.cliOptions.outDir!
: join(process.cwd(), options.cliOptions.outDir!);
const paths = join(dir, '**/*.js');
const watcher = chokidar.watch(paths, {
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 50,
Expand Down

0 comments on commit 2d0c6fd

Please sign in to comment.