作者:鹏大1111 | 来源:互联网 | 2023-02-02 16:37
我正在为现有项目开发一个新模块,该模块仍然使用requireJS进行模块加载.我正在尝试为我的新模块使用新技术,例如webpack(允许我使用es6导入器使用es6加载器).似乎webpack无法与requireJS语法协调.它会说:"找不到模块:错误:无法解决".
问题:Webpack不会捆绑带有requireJS/AMD语法的文件.
问题:有没有什么方法可以让webpack在requireJS上玩得很好?
我的最终输出必须是AMD格式才能让项目正确加载它.谢谢.
1> softvar..:
我有同样的问题,我设法实现了它.下面是同一个webpack.config.js
文件.
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
let basePath = path.join(__dirname, '/');
let cOnfig= {
// Entry, file to be bundled
entry: {
'main': basePath + '/src/main.js',
},
devtool: 'source-map',
output: {
// Output directory
path: basePath + '/dist/',
library: '[name]',
// [hash:6] with add a SHA based on file changes if the env is build
filename: env === EnvEnum.BUILD ? '[name]-[hash:6].min.js' : '[name].min.js',
libraryTarget: 'amd',
umdNamedDefine: true
},
module: {
rules: [{
test: /(\.js)$/,
exclude: /(node_modules|bower_components)/,
use: {
// babel-loader to convert ES6 code to ES5 + amdCleaning requirejs code into simple JS code, taking care of modules to load as desired
loader: 'babel-loader',
options: {
presets: ['es2015'],
plugins: []
}
}
}, { test: /jQuery/, loader: 'expose-loader?$' },
{ test: /application/, loader: 'expose-loader?application' },
{ test: /base64/, loader: 'exports-loader?Base64' }
]
},
resolve: {
alias: {
'jQuery': 'bower_components/jquery/dist/jquery.min',
'application': 'main',
'base64': 'vendor/base64'
},
modules: [
// Files path which will be referenced while bundling
'src/**/*.js',
'src/bower_components',
path.resolve('./src')
],
extensions: ['.js'] // File types
},
plugins: [
]
};
module.exports = config;