Skip to content

Instantly share code, notes, and snippets.

@Venryx
Created September 8, 2019 03:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Venryx/c473c60ea0f10a2620f2d9d00c06e6c7 to your computer and use it in GitHub Desktop.
Save Venryx/c473c60ea0f10a2620f2d9d00c06e6c7 to your computer and use it in GitHub Desktop.
Example inline-plugin to replace CopyWebpackPlugin
[...]
let fs = require("fs-extra");
webpackConfig.plugins.push({
apply(compiler) {
console.log(`Valid hook names: ${Object.keys(compiler.hooks)}`);
//compiler.hooks.beforeRun.tap("CopyPlugin_Custom", params=>{
compiler.hooks.shouldEmit.tap("CopyPlugin_Custom", params=>{
console.log(`Copying some difficult files (eg. *.wasm) from "node_modules/..." to "Resources/FromNodeModules/...".`);
async function CopyFile(source, dest) {
const source_final = path.resolve(source);
const dest_final = path.resolve(dest);
const sourceStat = await fs.stat(source_final);
const destStat = fs.existsSync(dest_final) ? await fs.stat(dest_final) : null;
/*let destStat;
try {
destStat = await fs.stat(dest_final);
} catch (err) {} // if path does not exist*/
//console.log(`Source stat: ${JSON.stringify(sourceStat)} Dest stat: ${JSON.stringify(destStat)}`);
const sourceModDateVSDest = sourceStat && destStat ? sourceStat.mtime - destStat.mtime : null;
//if (destStat == undefined || Math.abs(sourceModDateVSDest) > 1000 || sourceStat.size != destStat.size) {
if (destStat == undefined || sourceModDateVSDest != 0 || sourceStat.size != destStat.size) {
// Ensure parent directory exists.
await fs.mkdirp(`${dest_final}/..`);
console.log(`Copying from "${source_final}" to "${dest_final}".`);
await fs.copyFile(source_final, dest_final);
// Set mtime to be equal to the source file.
await fs.utimes(dest_final, new Date(), sourceStat.mtime);
}
}
//CopyFile("node_modules/opus-media-recorder/encoderWorker.umd.js", "Resources/FromNodeModules/encoderWorker.umd.js");
CopyFile("node_modules/opus-media-recorder/OggOpusEncoder.wasm", "Resources/FromNodeModules/OggOpusEncoder.wasm");
CopyFile("node_modules/opus-media-recorder/WebMOpusEncoder.wasm", "Resources/FromNodeModules/WebMOpusEncoder.wasm");
});
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment