Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack 5 #17

Open
kpachbiu88 opened this issue Nov 9, 2023 · 2 comments
Open

Webpack 5 #17

kpachbiu88 opened this issue Nov 9, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@kpachbiu88
Copy link

You can use this code for add support Webpack v5

import { Compiler } from 'webpack';

interface SaveRemoteFilePluginOptions {
	url: string
	filepath: string
}

class SaveRemoteFilePlugin {
	pluginName = 'SaveRemoteFilePlugin'
	options: SaveRemoteFilePluginOptions[];

	constructor(options: SaveRemoteFilePluginOptions[]) {
		if (options instanceof Array) {
			this.options = options;
		} else {
			this.options = [options];
		}
	}

	apply(compiler: Compiler) {
		const { webpack } = compiler;
		const { WebpackError } = webpack;
		const { RawSource } = webpack.sources;

		compiler.hooks.compilation.tap(this.pluginName, async (compilation) => {
			compilation.hooks.additionalAssets.tapPromise(this.pluginName, async () => {
				const downloadFiles = async (option: SaveRemoteFilePluginOptions) => {

					try {
						const { url, filepath } = option;
						console.log(`Downloading file ${url}...`);
						const resp = await fetch(url);
						const arrBuffer = await resp.arrayBuffer();
						
						compilation.emitAsset(
							filepath,
							new RawSource(Buffer.from(arrBuffer))
						);
						console.log(`Downloading finished`);
					} catch (err) {
						console.log(err)
						if (err instanceof Error)
							compilation.errors.push(new WebpackError(err.message));
					}
				};

				for (const item of this.options) {
					await downloadFiles(item)
				}
			});
		});
	}
};

export default SaveRemoteFilePlugin;
@kpachbiu88 kpachbiu88 added the enhancement New feature or request label Nov 9, 2023
@khalwat
Copy link
Contributor

khalwat commented Jan 20, 2024

Would you be willing to do this via a PR?

@zervant-konstantin
Copy link

zervant-konstantin commented Jan 20, 2024

@khalwat

Unfortunately, I don't have free time for open-source contributing.

There was a task at work, but this library is only compatible with the old version of Webpack 4 and I made a plugin for Webpack 5, but in the end, the problem was solved differently.

I shared the code because it might be useful for someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants