Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用webpack或babel cli导出ES6并作为AMD模块进行响应_Php_Webpack_Ecmascript 6_Babeljs_Amd - Fatal编程技术网

Php 使用webpack或babel cli导出ES6并作为AMD模块进行响应

Php 使用webpack或babel cli导出ES6并作为AMD模块进行响应,php,webpack,ecmascript-6,babeljs,amd,Php,Webpack,Ecmascript 6,Babeljs,Amd,编辑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: {

编辑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技术,而不是像react、immutable、redux等JQuery1.12。不过,我试图让构建链的最后一部分导出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;
    });
});
/******/ (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();
            }
        }
/***/ }
/******/ ]);
它输出:

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;
    });
});
/******/ (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插件内联导入插件的东西,我可以将其与babel cli一起用于多个文件,但我真的觉得这个网页更流畅。有人解决了这个问题吗

编辑1:我已将我的网页配置更改为简化配置,如下所示:

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"]
          }
      }]
    }
 };
现在,至少有一个Web包正在将其传输到以下位置:

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;

/***/ }
/******/ ]);
var-amd=
/******/(功能(模块){//WebPackageBootstrap
/******///模块缓存
/******/var installedModules={};
/******///require函数
/******/功能uu网页u需要uu(模块ID){
/******///检查模块是否在缓存中
/******/if(已安装的模块[moduleId])
/******/返回已安装的模块[moduleId]。导出;
/******///创建一个新模块(并将其放入缓存)
/******/变量模块=已安装的模块[moduleId]={
/******/导出:{},
/******/id:moduleId,
/******/加载:false
/******/        };
/******///执行模块函数
/******/模块[moduleId]。调用(module.exports、module、module.exports、\uuu网页包\uu require\uuuu);
/******///将模块标记为已加载
/******/module.loaded=true;
/******///返回模块的导出
/******/返回模块。导出;
/******/    }
/******///公开modules对象(\uuuu webpack\u modules\uuuu)
/******/\uuuu网页包\u需要\uuuuu.m=模块;
/******///公开模块缓存
/******/\uuuu webpack\u require\uuuuu.c=已安装的模块;
/******///\u网页包\u公共路径__
/******/_uuu网页包_uurequire_uuu.p=“”;
/******///加载输入模块并返回导出
/******/返回网页需要(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/功能(模块、导出){
“严格使用”;
Object.defineProperty(导出,“\uu esModule”{
值:true
});
var_createClass=function(){function defineProperties(target,props){for(var i=0;i

但是,根据amd标准,将一个名为amd的变量设置为im导出的值很难替代Define调用

我认为现在发生的事情是巴贝尔将AMD转换为Webpack,然后Webpack将AMD转换为“Webpack”\u require\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

您可能想做的是放弃
transform-es2015-modules-amd
插件并更新您的网页配置,以包含
output.libraryTarget='amd'
。然后Webpack将整个捆绑包导出为AMD


只要我有时间,我就要测试它,如果它真的有效,看起来很简单:-)嗨,我现在尝试了,我用尝试更新了我的问题,它没有给我AMD模块,只是给我一个名为AMD的变量,并将导出设置为该值,我不知道为什么这有意义。我也试过大写的amd,但没什么变化。我阅读了文档,然后输出。libraryTarget:“amd”起作用了,但图书馆并没有完全削减它。非常感谢!!!我会花几个小时在这些奇怪的解决方案上。@Denlilemand啊,是的,我的意思是
libraryTarget
,很高兴听到你让它工作起来了!