Dou*_*las 5 javascript amd webpack
我正在尝试使用命名的 AMD 模块,exports
因为这是 TypeScript 1.8 在--outFile
指定时用于将 AMD 模块写入单个文件的格式。但是,exports
如果模块在同一文件中定义,则 webpack 似乎无法正确处理。
这是在content.js
:
define('messages', [
'require',
'exports'
], function(require, exports) {
'use strict';
exports.itWorks = "It works from a local module?";
});
define([
'require',
'exports',
'messages'
], function(require, exports, messages) {
'use strict';
exports.message = messages.itWorks;
});
Run Code Online (Sandbox Code Playgroud)
有一个运行时错误:
bundle.js:67 Uncaught TypeError: Cannot read property 'itWorks' of undefined
Run Code Online (Sandbox Code Playgroud)
这会中断,因为在捆绑代码中__WEBPACK_LOCAL_MODULE_0__
是未定义的,因为'messages'
函数使用exports
而不是返回值。
如果'messages'
在单独的文件中,这一切都很好。如何exports
在单个文件中与 webpack 一起使用?
这是相关的构建输出,稍微格式化以提高可读性。相同的exports
对象被传递到两个模块(将它们的导出合并在一起??)并__WEBPACK_LOCAL_MODULE_0__
设置为undefined
第一个模块不会传递到第二个模块:
function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_LOCAL_MODULE_0__;
var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [__webpack_require__, exports],
__WEBPACK_LOCAL_MODULE_0__ = (function(require, exports) {
'use strict';
exports.itWorks = "It works from a local module?";
}.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)));
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [__webpack_require__, exports, __WEBPACK_LOCAL_MODULE_0__],
__WEBPACK_AMD_DEFINE_RESULT__ = function(require, exports, messages) {
'use strict';
exports.message = messages.itWorks;
}.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
}
Run Code Online (Sandbox Code Playgroud)