通过 webpack 使用远程入口 [英] Using a remote entry with webpack

查看:41
本文介绍了通过 webpack 使用远程入口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在服务器环境中设置一个 webpack 配置,该配置可以从远程源输出包.源是一个 .js 文件,将通过 GET 请求获取.这可能吗?


远程文件 - example.com/file.js

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

const remoteSource = await fetchFromURL('http://example.com/file.js');

webpack({
  entry: remoteSource,
  output: {
    filename: 'output.js',
    path: path.resolve(__dirname, 'src'),
    libraryTarget: "umd",
  }
})

感谢任何想法!

推荐答案

我四处寻找解决方案,但你是唯一给我提示的人.所有其他提案都基于 externals,这在我的情况下无效.我完成了使用一个单独的 JS 文件,该文件负责将所需的文件下载到本地目录中.然后 WebPack 扫描该目录并将下载的文件与应用程序捆绑在一起.

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",
...

这篇关于通过 webpack 使用远程入口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆