编辑2:解决了问题,对接受的答案略有修改,现在我的webpack.config.js
看起来像是这样:
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: {
adminpanel: path.join(__dirname, 'theme/peakbalance/es6/adminpanel.js')
},
output: {
path: path.join(__dirname, 'theme/peakbalance/amd/src'),
filename: '[name]_bundle.js',
libraryTarget: 'amd'
},
module: {
loaders: [{
test:/\.(js|jsx)$/,
loader:'babel',
query: {
presets: ["es2015", "stage-0", "react"]
}
}]
}
};
所以我一直在努力解决这个问题已有一段时间了。我在一个名为moodle的旧php系统中进行编码,它与AMD系统紧密耦合,我想使用新的JS技术而不是jquery 1.12,如react,immutable,redux等,所以我想做最后一个构建链的一部分导出一个AMD模块。我现在拥有的:
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: {
adminpanel: path.join(__dirname, 'theme/peakbalance/amd/es6/adminpanel.js')
},
output: {
path: path.join(__dirname, 'theme/peakbalance/amd/src'),
filename: '[name]_bundle.js'
},
module: {
loaders: [{
test:/\.(js|jsx)$/,
loader:'babel',
query: {
presets: ['es2015', 'stage-0', 'react'],
plugins: ['transform-es2015-modules-amd']
},
include:path.join(__dirname,'./theme/peakbalance/amd/es6')
}, {
test:/\.json$/,
loaders:['json-loader'],
include:path.join(__dirname,'./theme/peakbalance/amd/es6')
}, {
test:/\.(png|jpg)$/,
loader:'url?limit=25000'
}, {
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: "file"
}, {
test: /\.(woff|woff2)/,
loader:"url?prefix=font/&limit=5000"
}, {
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/octet-stream"
}, {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=image/svg+xml"
}]
}
};
正如您所看到的,我正在尝试使用transform-es2015-modules-amd插件来加载器,如果我将这个插件与babel-cli一起使用,这个插件是完美的:
babel --plugins transform-es2015-modules-amd ./theme/peakbalance/es6/adminpanel.js
然后输出:
define(["exports"], function (exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var _createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var AdminPanel = function () {
function AdminPanel() {
_classCallCheck(this, AdminPanel);
this.init = this.init.bind(this);
}
_createClass(AdminPanel, [{
key: "init",
value: function init(herpderp) {
console.log("herpderp42424");
console.log("herp");
var app = new App();
}
}]);
return AdminPanel;
}();
exports.default = AdminPanel;
});
});
当它通过webpack运行时只需:
webpack -w
输出:
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {
export default class AdminPanel {
constructor() {
this.init = this.init.bind(this);
}
init(herpderp) {
console.log("herpderp42424");
console.log("herp");
console.log("alright alright");
let app = new App();
}
}
/***/ }
/******/ ]);
所以现在它甚至没有将它从es6转换为es5,而且它绝对不是AMD模块结构。
我基本上只想要与babel cli完全相同的行为,但是webpack正在发挥它的魔力,因为我可能会超越1个文件,所以我需要将文件连接起来,我看了类似于babel-plugin-inline-import插件,我可以将babel-cli用于多个文件,但我真的认为webpack更流畅。有谁解决了这个问题?
编辑1:我已将我的webpack配置更改为简化的内容:
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: {
adminpanel: path.join(__dirname, 'theme/peakbalance/es6/adminpanel.js')
},
output: {
path: path.join(__dirname, 'theme/peakbalance/amd/src'),
filename: '[name]_bundle.js',
library: 'amd'
},
module: {
loaders: [{
test:/\.(js|jsx)$/,
loader:'babel',
query: {
presets: ["es2015", "stage-0", "react"]
}
}]
}
};
现在至少有一个webpack将它转移到以下内容:
var amd =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var AdminPanel = function () {
function AdminPanel() {
_classCallCheck(this, AdminPanel);
this.init = this.init.bind(this);
}
_createClass(AdminPanel, [{
key: "init",
value: function init(herpderp) {
console.log("herpderp42424");
console.log("herp");
console.log("alright alright");
var app = new App();
}
}]);
return AdminPanel;
}();
exports.default = AdminPanel;
/***/ }
/******/ ]);
但是根据AMD标准设置一个名为amd的变量等于im输出,几乎不能替代Define调用。
答案 0 :(得分:1)
我认为正在发生的事情是babel转换为AMD,然后Webpack将AMD转换为__webpack_require__,为浏览器做好准备。
您可能想要做的是放弃transform-es2015-modules-amd
插件并更新您的Webpack配置以包含output.libraryTarget = 'amd'
。然后Webpack将整个捆绑包导出为AMD。
https://webpack.github.io/docs/configuration.html#output-library