Javascript 使用webpack创建供应商和应用程序包,并使用requirejs加载
是否可以使用webpack创建多个捆绑包“供应商”和“应用程序”,并使用requirejs加载它们?在本例中,我可以创建捆绑包,但当我尝试使用requirejs加载它们时,返回的模块是未定义的 webpack.config.js: index.js index.htmlJavascript 使用webpack创建供应商和应用程序包,并使用requirejs加载,javascript,webpack,requirejs,Javascript,Webpack,Requirejs,是否可以使用webpack创建多个捆绑包“供应商”和“应用程序”,并使用requirejs加载它们?在本例中,我可以创建捆绑包,但当我尝试使用requirejs加载它们时,返回的模块是未定义的 webpack.config.js: index.js index.html 当index.js要求加载'app'dist/app.bundle时,网络选项卡显示,但返回的app对象未定义。可以在不加载requirejs的情况下加载app bundle供应商bundle必须首先包括: index.html
当index.js要求加载'app'dist/app.bundle时,网络选项卡显示,但返回的app对象未定义。可以在不加载requirejs的情况下加载app bundle供应商bundle必须首先包括: index.html
module.exports = {
entry: {
app: './app.js',
//vendor: ['bootstrap', 'popper.js','jquery','underscore']
},
devtool: 'source-map',
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js',
libraryTarget: 'umd',
globalObject: 'this',
umdNamedDefine: true,
pathinfo: true
},
resolve: {
alias: {
popper: "popper.js",
},
extensions: ['.js'] // File types
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
]
},
plugins: [
],
optimization: {
namedModules: true,
splitChunks: {
chunks: "async",
cacheGroups: {
app: {
reuseExistingChunk: true,
},
vendor: {
test: /node_modules/,
chunks: 'all',
name: 'vendor',
},
}
},
},
};
requirejs.config({
paths: {
'vendor': './dist/vendor.bundle',
requireLib: 'require',
'all_app': './dist/app.bundle'
},
bundles: {
'all_app': ['app','./menuTooltips','./NavMenu'],
'vendor': [/*'bootstrap',*/ 'jquery' /*, 'underscore',*/ /*, 'popper'*/ ],
},
nodeIdCompat: true,
});
define(['require', './app'], function (require, app) {
console.log(app); // app is undefined!!!!
app.Init();
});
<script type="text/javascript" src="./lib/require.min.js" data-main="index.js"></script>
<script type="text/javascript" src="./dist/vendor.bundle.js"></script>
<script type="text/javascript" src="./dist/app.bundle.js"></script>
<script>
window.onload = function () {
MyLib.app.Init();
};
</script>
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js',
libraryTarget: 'umd',
globalObject: 'this',
umdNamedDefine: true,
library: ["MyLib", "[name]"]
},