我有一个分布式系统,所有JS文件都通过HTTP公开。因此,普通模块如下所示:
http://example.com/path/to/main.js
import * as core from 'http://local.example.com/path/to/core.js';
import * as redux from 'http://cdn.example.com/redux.js@version';
// code
export default {
...
}
因此,每次导入将使用系统的本地资源或使用CDN的远程可用资源。
运行Webpack时出现此错误:
试图解析具有此类内容的本地生成的文件:
import * as main from 'http://example.com/path/to/main.js';
./ src / index.js中的错误,未找到模块:错误:无法解析'/home/.../index.js']中的'http://example.com/path/to/main.js'
是否可以告诉webpack提取URL并将其包含在捆绑包中...虽然目前打包cdn url没什么大不了,但是如果我可以简单地忽略带有特定URL的URL,我会很高兴。 。
认为能够将所有http://找到的文件捆绑在一起是一个好的开始。
此外,链接到其他资源的任何远程资源也应该递归加载远程链接的资源。
这是我当前的webpack配置(这里没什么可看的):
const path = require('path'); module.exports = { mode: 'development', entry: './src/index.js', output: { filename: 'main.js', path: path.resolve(__dirname, 'dist'), }, module: { rules: [ ] }, };
编辑:阅读一会之后,我开始编写解析器,但现在又被卡住了:
const path = require('path'); const fetch = require('node-fetch'); const url = require('url') const fs = require('promise-fs'); const sha1 = require('sha1') class CustomResolver { async download_save(request, resolveContext) { console.log(request, resolveContext) var target = url.parse(request.request) var response = await fetch(request.request) var content = await response.text() try { await fs.stat('_remote') } catch(exc) { await fs.mkdir('_remote') } var filename = `${sha1(request.request)}.js` var file_path = `_remote/${filename}` await fs.writeFile(file_path, content) var abs_path = path.resolve(file_path) var url_path = `${target.protocol}://${target.hostname}/` var obj = { path: abs_path, request: request.request, query: '', } console.log(`${request.request} saved to ${abs_path}`) return obj } apply(resolver) { var self = this const target = resolver.ensureHook("resolved") resolver.getHook("module") .tapAsync("FetchResolverPlugin", (request, resolveContext, callback) => { self.download_save(request, resolveContext) .then((obj) => resolver.doResolve(target, obj, resolveContext, callback)) .catch((err) => { console.log(err) callback() }) }) } }
目前,它确实会获取以https://开头的网址,但似乎很难解析相对于http资源的网址。例如
ERROR in _remote/88f978ae6c4a58e98a0a39996416d923ef9ca531.js Module not found: Error: Can't resolve '/-/@pika/[email protected]/dist=es2017/polyfill.js' in '_remote/' @ _remote/88f978ae6c4a58e98a0a39996416d923ef9ca531.js 25:0-58 @ _remote/f80b922b2dd42bdfaaba4e9f4fc3c84b9cc04fca.js @ ./src/index.js
它似乎不尝试将相对路径解析为已解析的文件。有没有办法告诉解析器尝试解决所有问题?
我有一个分布式系统,所有JS文件都通过HTTP公开。因此,正常的模块如下所示:http://example.com/path/to/main.js import *作为核心,来自'http://local.example.com / ...
如果您有CDN文件-您不需要捆绑器