有没有办法从使用 Webpack 编译的模块(在运行时通过网络)加载 AMD 模块?
Is there a way to load AMD modules from modules compiled with Webpack (at runtime over the network)?
有没有办法从使用 Webpack 编译的模块中加载 AMD 模块(在运行时通过网络)?
f.e.,我有
import Blah from './Blah'
import AmdModule from './AmdModule'
其中 AmdModule
是一个 AMD 模块,其中有一个 define()
调用。我不希望 webpack 编译那个文件,也不希望 webpack 将它包含在包中。也许该文件在编译时甚至不存在,但会存在于资产服务器上。我希望在运行时通过网络加载该文件。
有没有办法让 Webpack 做那种事?我在想也许可以用 RequireJS 导入那个文件,但是它会破坏 Webpack 模块,因为没有办法等待模块加载然后在 webpack 模块中异步导出一些东西。
理想情况下,我希望 webpack 在执行需要它的模块之前等待这些文件从网络加载。
这样的事情可能吗?
根据要求,无法编译网络加载文件,它必须保持为通过网络加载的 AMD define() 模块,保持原样,不需要源映射。
是的,你可以,只需添加到外部:
module.exports = {
... (all config stuff),
output: {
filename: "[name].js",
path: path,
libraryTarget: 'amd'
},
externals : [
'module the AmdModule is requiring'
]
}
我正在使用它来打包我的 module/plugin 以加载到框架中,并且模块使用目标框架中的一些模块,因此 webpack 不应包含这些模块。 Externals 确实避免在我的插件模块中包含框架模块,并且仍在等待这些模块在 运行 时间加载。
外部设备正适合它:
Applications and externals
You can also use the externals option to import an existing API into applications. For example, if you want to use jQuery from a CDN via a separate tag while still explicitly declaring it as a dependency via require("jquery"), you would specify it as external like so: { externals: { jquery: "jQuery" } }.
@NicoD:外部指示不应由 webPack 捆绑但仍由生成的捆绑包请求的依赖项。要获取模块,您必须使用加载器作为 script.js
Webpack 可以指定不同的方法(AMD 和 CommonJs)来按需加载代码。
这是 the webpack guide 的摘录:
To clarify a common misunderstanding: Code Splitting is not just about
extracting common code into a shared chunk. The more notable feature
is that Code Splitting can be used to split code into an on demand
loaded chunk.
关于如何配置外部设备的公开方式,您可以查看此示例 (webpack/examples/externals):
module.exports = {
output: {
libraryTarget: "umd"
},
externals: [
"add",
{
"subtract": {
root: "subtract",
commonjs2: "./subtract",
commonjs: ["./math", "subtract"],
amd: "subtract"
}
}
]
}
有没有办法从使用 Webpack 编译的模块中加载 AMD 模块(在运行时通过网络)?
f.e.,我有
import Blah from './Blah'
import AmdModule from './AmdModule'
其中 AmdModule
是一个 AMD 模块,其中有一个 define()
调用。我不希望 webpack 编译那个文件,也不希望 webpack 将它包含在包中。也许该文件在编译时甚至不存在,但会存在于资产服务器上。我希望在运行时通过网络加载该文件。
有没有办法让 Webpack 做那种事?我在想也许可以用 RequireJS 导入那个文件,但是它会破坏 Webpack 模块,因为没有办法等待模块加载然后在 webpack 模块中异步导出一些东西。
理想情况下,我希望 webpack 在执行需要它的模块之前等待这些文件从网络加载。
这样的事情可能吗?
根据要求,无法编译网络加载文件,它必须保持为通过网络加载的 AMD define() 模块,保持原样,不需要源映射。
是的,你可以,只需添加到外部:
module.exports = {
... (all config stuff),
output: {
filename: "[name].js",
path: path,
libraryTarget: 'amd'
},
externals : [
'module the AmdModule is requiring'
]
}
我正在使用它来打包我的 module/plugin 以加载到框架中,并且模块使用目标框架中的一些模块,因此 webpack 不应包含这些模块。 Externals 确实避免在我的插件模块中包含框架模块,并且仍在等待这些模块在 运行 时间加载。
外部设备正适合它:
Applications and externals
You can also use the externals option to import an existing API into applications. For example, if you want to use jQuery from a CDN via a separate tag while still explicitly declaring it as a dependency via require("jquery"), you would specify it as external like so: { externals: { jquery: "jQuery" } }.
@NicoD:外部指示不应由 webPack 捆绑但仍由生成的捆绑包请求的依赖项。要获取模块,您必须使用加载器作为 script.js
Webpack 可以指定不同的方法(AMD 和 CommonJs)来按需加载代码。
这是 the webpack guide 的摘录:
To clarify a common misunderstanding: Code Splitting is not just about extracting common code into a shared chunk. The more notable feature is that Code Splitting can be used to split code into an on demand loaded chunk.
关于如何配置外部设备的公开方式,您可以查看此示例 (webpack/examples/externals):
module.exports = {
output: {
libraryTarget: "umd"
},
externals: [
"add",
{
"subtract": {
root: "subtract",
commonjs2: "./subtract",
commonjs: ["./math", "subtract"],
amd: "subtract"
}
}
]
}