问题
I'm trying to set up a webpack config in a server enviroment that can output a bundle from a remote source. The source is a .js file and will be fetched through GET-requests. Is this possible?
Remote file - example.com/file.js
export default () => console.log('Hello world');
Webpack with pseudocode
const remoteSource = await fetchFromURL('http://example.com/file.js');
webpack({
entry: remoteSource,
output: {
filename: 'output.js',
path: path.resolve(__dirname, 'src'),
libraryTarget: "umd",
}
})
Any ideas are appreciated!
回答1:
I have looked around for a solution but you were the only one who gave me a hint. All other proposals were based on externals
, which is not valid in my case. I finished using a separate JS file which is responsible for downloading the desired file into a local directory. Then WebPack scans this directory and bundles the downloaded files together with the application.
download.js
const https = require('https');
const fs = require('fs');
const download = (url, dest, callback) => {
let file = fs.createWriteStream(dest);
https.get(url, function(response) {
// noinspection JSUnresolvedFunction
response.pipe(file);
file.on('finish', function() {
file.close();
callback();
});
});
}
download('http://www.mycdn.com/my-file.js', './generated-soruces/src/js/', () => {
console.log('Downloaded File!')
});
package.json
...
"clean": "rimraf dist",
"clean:all": "rimraf dist node_config node_modules",
"build": "export NODE_ENV=dev && npm run clean && node ./download.js && webpack",
...
来源:https://stackoverflow.com/questions/60590904/using-a-remote-entry-with-webpack