save-remote-file-webpack-plugin
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -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
95903
8
115
45
+ Addeddownload@^7.1.0
+ Added@sindresorhus/is@0.7.0(transitive)
+ Addedarchive-type@4.0.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbl@1.2.3(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedbuffer-alloc@1.2.0(transitive)
+ Addedbuffer-alloc-unsafe@1.1.0(transitive)
+ Addedbuffer-crc32@0.2.13(transitive)
+ Addedbuffer-fill@1.0.0(transitive)
+ Addedcacheable-request@2.1.4(transitive)
+ Addedcaw@2.0.1(transitive)
+ Addedclone-response@1.0.2(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedconfig-chain@1.1.13(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddecode-uri-component@0.2.2(transitive)
+ Addeddecompress@4.2.1(transitive)
+ Addeddecompress-response@3.3.0(transitive)
+ Addeddecompress-tar@4.1.1(transitive)
+ Addeddecompress-tarbz2@4.1.1(transitive)
+ Addeddecompress-targz@4.1.1(transitive)
+ Addeddecompress-unzip@4.0.1(transitive)
+ Addeddownload@7.1.0(transitive)
+ Addedduplexer3@0.1.5(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedext-list@2.2.2(transitive)
+ Addedext-name@5.0.0(transitive)
+ Addedfd-slicer@1.1.0(transitive)
+ Addedfile-type@3.9.04.4.05.2.06.2.08.1.0(transitive)
+ Addedfilename-reserved-regex@2.0.0(transitive)
+ Addedfilenamify@2.1.0(transitive)
+ Addedfrom2@2.3.0(transitive)
+ Addedfs-constants@1.0.0(transitive)
+ Addedget-proxy@2.1.0(transitive)
+ Addedget-stream@2.3.13.0.0(transitive)
+ Addedgot@8.3.2(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-symbol-support-x@1.4.2(transitive)
+ Addedhas-to-string-tag-x@1.4.1(transitive)
+ Addedhttp-cache-semantics@3.8.1(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedini@1.3.8(transitive)
+ Addedinto-stream@3.1.0(transitive)
+ Addedis-natural-number@4.0.1(transitive)
+ Addedis-object@1.0.2(transitive)
+ Addedis-plain-obj@1.1.0(transitive)
+ Addedis-retry-allowed@1.2.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisurl@1.0.0(transitive)
+ Addedjson-buffer@3.0.0(transitive)
+ Addedkeyv@3.0.0(transitive)
+ Addedlowercase-keys@1.0.01.0.1(transitive)
+ Addedmake-dir@1.3.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmimic-response@1.0.1(transitive)
+ Addednormalize-url@2.0.1(transitive)
+ Addednpm-conf@1.1.3(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedp-cancelable@0.4.1(transitive)
+ Addedp-event@2.3.1(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedp-is-promise@1.1.0(transitive)
+ Addedp-timeout@2.0.1(transitive)
+ Addedpend@1.2.0(transitive)
+ Addedpify@2.3.03.0.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedprepend-http@2.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedproto-list@1.2.4(transitive)
+ Addedquery-string@5.1.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedresponselike@1.0.2(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedseek-bzip@1.0.6(transitive)
+ Addedsort-keys@1.1.22.0.0(transitive)
+ Addedsort-keys-length@1.0.1(transitive)
+ Addedstrict-uri-encode@1.1.0(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedstrip-dirs@2.1.0(transitive)
+ Addedstrip-outer@1.0.1(transitive)
+ Addedtar-stream@1.6.2(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedtimed-out@4.0.1(transitive)
+ Addedto-buffer@1.1.1(transitive)
+ Addedtrim-repeated@1.0.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedunbzip2-stream@1.4.3(transitive)
+ Addedurl-parse-lax@3.0.0(transitive)
+ Addedurl-to-options@1.0.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
+ Addedyauzl@2.10.0(transitive)
- Removeddownload-to-file@^2.1.0
- Removeddownload-to-file@2.1.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedpump@2.0.1(transitive)