Skip to content

Commit

Permalink
Fix webpack watching
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Feb 5, 2025
1 parent 451464e commit 78bc6e2
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions dev-packages/native-webpack-plugin/src/native-webpack-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,11 @@ export class NativeWebpackPlugin {
let ripgrepIssuer: string | undefined;
compiler.hooks.initialize.tap(NativeWebpackPlugin.name, async () => {
const directory = path.resolve(compiler.outputPath, 'native-webpack-plugin');
if (fs.existsSync(directory)) {
await fs.promises.rm(directory, {
force: true,
if (!fs.existsSync(directory)) {
await fs.promises.mkdir(directory, {
recursive: true
});
}
await fs.promises.mkdir(directory, {
recursive: true
});
const bindingsFile = (issuer: string) => buildFile(directory, 'bindings.js', bindingsReplacement(issuer, Array.from(this.bindings.entries())));
const ripgrepFile = () => buildFile(directory, 'ripgrep.js', ripgrepReplacement(this.options.out));
const keymappingFile = () => Promise.resolve('./build/Release/keymapping.node');
Expand Down Expand Up @@ -97,14 +93,6 @@ export class NativeWebpackPlugin {
}
}
});
nmf.hooks.afterResolve.tapPromise(NativeWebpackPlugin.name, async result => {
const createData = result.createData;
for (const [file, replacement] of Object.entries(replacements)) {
if (createData.resource === file) {
createData.resource = await replacement(result.contextInfo.issuer);
}
}
});
}
);
compiler.hooks.afterEmit.tapPromise(NativeWebpackPlugin.name, async () => {
Expand Down Expand Up @@ -183,7 +171,18 @@ function findNativeWatcherFile(issuer: string): string {

async function buildFile(root: string, name: string, content: string): Promise<string> {
const tmpFile = path.join(root, name);
await fs.promises.writeFile(tmpFile, content);
let write = true;
try {
const existing = await fs.promises.readFile(tmpFile, 'utf8');
if (existing === content) {
write = false;
}
} catch {
// ignore
}
if (write) {
await fs.promises.writeFile(tmpFile, content);
}
return tmpFile;
}

Expand Down

0 comments on commit 78bc6e2

Please sign in to comment.