Socket
Socket
Sign inDemoInstall

save-remote-file-webpack-plugin

Package Overview
Dependencies
99
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.0.1

CHANGELOG.md

41

dist/index.js

@@ -7,3 +7,4 @@ 'use strict';

var download = require('download-to-file');
var crypto = require('crypto');
var download = require('download');
var path = require('path');

@@ -23,2 +24,9 @@

_createClass(SaveRemoteFilePlugin, [{
key: 'appendHashToPath',
value: function appendHashToPath(path, hash) {
var newPath = path.substring(0, path.lastIndexOf('.')) + '.' + hash + path.substring(path.lastIndexOf('.'));
return newPath;
}
}, {
key: 'apply',

@@ -28,21 +36,34 @@ value: function apply(compiler) {

compiler.hooks.beforeRun.tapAsync({
compiler.hooks.emit.tapAsync({
name: 'SaveRemoteFilePlugin',
context: true
}, function (context, compilation, callback) {
var count = _this.options.length;
var downloadFiles = function downloadFiles(option) {
var reportProgress = context && context.reportProgress;
var filepath = path.join(compiler.options.output.path, option.filepath);
download(option.url, filepath, function (err, filepath) {
if (err) {
compilation.errors.push(new Error(err));
} else {
if (reportProgress) {
reportProgress(100.0, 'Remote files saved to: ', filepath);
download(option.url).then(function (data) {
var hash = crypto.createHash('md5').update(data).digest("hex");
var newPath = _this.appendHashToPath(option.filepath, hash);
compilation.assets[newPath] = {
size: function size() {
return data.length;
},
source: function source() {
return data;
}
};
if (reportProgress) {
reportProgress(95.0, 'Remote file downloaded: ', newPath);
}
// Issue the calback after all files have been processed
count--;
if (count === 0) {
callback();
}
}).catch(function (error) {
compilation.errors.push(new Error(error));
callback();
});
};
_this.options.forEach(downloadFiles);
callback();
});

@@ -49,0 +70,0 @@ }

{
"name": "save-remote-file-webpack-plugin",
"version": "1.0.0",
"version": "1.0.1",
"description": "webpack 4 plugin to download & save remote files locally",
"main": "dist/index.js",
"dependencies": {
"download-to-file": "^2.1.0"
"download": "^7.1.0"
},

@@ -9,0 +9,0 @@ "homepage": "https://github.com/nystudio107/save-remote-file-webpack-plugin",

@@ -0,1 +1,13 @@

# SaveRemoteFile webpack plugin
## Installing
```
yarn add save-remote-file-webpack-plugin --dev
```
or
```
npm install save-remote-file-webpack-plugin --save-dev
```
## How it works

@@ -5,2 +17,6 @@

It was written to allow for the downloading of [https://google-analytics.com/analytics.js](https://google-analytics.com/analytics.js) so that it can be served locally, but it can be used to locally download any remote file as part of the webpack build process.
The resulting file has a content hash appended to the file name after it is downloaded, and is added to your `manifest.json` if you are using `manifest-webpack-plugin`
## Usage

@@ -7,0 +23,0 @@

@@ -1,2 +0,3 @@

const download = require('download-to-file')
const crypto = require('crypto');
const download = require('download');
const path = require('path');

@@ -13,4 +14,13 @@

appendHashToPath(path, hash) {
const newPath = path.substring(0, path.lastIndexOf('.'))
+ '.'
+ hash
+ path.substring(path.lastIndexOf('.'));
return newPath;
}
apply(compiler) {
compiler.hooks.beforeRun.tapAsync(
compiler.hooks.emit.tapAsync(
{

@@ -21,19 +31,28 @@ name: 'SaveRemoteFilePlugin',

(context, compilation, callback) => {
let count = this.options.length;
const downloadFiles = (option) => {
const reportProgress = context && context.reportProgress;
const filepath = path.join(compiler.options.output.path, option.filepath);
download(option.url, filepath, (err, filepath) => {
if (err) {
compilation.errors.push(new Error(err));
} else {
if (reportProgress) {
reportProgress(100.0, 'Remote files saved to: ', filepath);
}
download(option.url).then(data => {
const hash = crypto.createHash('md5').update(data).digest("hex");
const newPath = this.appendHashToPath(option.filepath, hash);
compilation.assets[newPath] = {
size: () => data.length,
source: () => data
}
if (reportProgress) {
reportProgress(95.0, 'Remote file downloaded: ', newPath);
}
// Issue the calback after all files have been processed
count--;
if (count === 0) {
callback();
}
}).catch(error => {
compilation.errors.push(new Error(error));
callback();
});
};
this.options.forEach(downloadFiles);
callback();
});
}
};

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc