Resolve Module has not been loaded yet error Babel async

I am using babeljs to transform my javascript code. I am using async-await to handle asynchronous ajax calls in jquery. I haven't been able to figure out from the Babeljs Documentation exactly what configurations I need in order to get this to work. Using the below configurations I am gettin the error

Module name "babel-polyfill" has not been loaded yet for context: _. Use require([])

What configuration changes (or code changes) do I need to make in order to correctly configure Babel? Any additional explanation of working with Babel/es2015+ would be appreciated.

.babelrc

{
  "plugins": [ "transform-async-to-generator" ],
  "presets": ["env"]
}

package.json

{
  "dependencies": {
    "@types/jquery": "^2.0.46",
    "@types/papaparse": "^4.1.28",
    "@types/xrm": "^8.2.5",
    "babel-polyfill": "^6.23.0",
    "bootstrap": "^3.3.7",
    "papaparse": "^4.3.3",
    "requirejs": "^2.3.3",
    "vue": "^2.3.3"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-preset-env": "^1.5.2"
  },
  "name": "mypackage",
  "private": true,
  "scripts": {
    "build": "babel WebResources -d build/CCSEQ"
  },
  "version": "1.0.0"
}

main.js (pre-babel)

/// <reference path="../node_modules/@types/jquery/index.d.ts" />
"use strict";

require("babel-polyfill");

requirejs.config({
    paths: {
        PapaParse: "/papaparse.min",
        Constants: "/build/CCSEQ/Constants",
        Model: "/build/CCSEQ/Model",
        WebAPI: "/build/CCSEQ/WebAPI",
        Xrm: "/build/CCSEQ/Xrm",
        Vue: "/node_modules/vue/dist/vue"
    }
})   

require(["PapaParse", "Constants", "Model", "WebAPI", "Xrm", "Vue"], function (Papa, Constants, Model, WebAPI, Xrm, Vue) {
function ImportExpenseTransaction(data) {
        let newExpenseTransactions = new Array();
        let entityID = Xrm.Page.getCurrentEntityID();
        data.forEach(async (expense) => {
            if (expense[0] !== "PR EMP ID") {
                let newExpenseTransaction = new Model.Entity();

                newExpenseTransaction.Type = Constants.EntityType.ExpenseTransaction;
                newExpenseTransaction.Attributes.push(new Model.EntityField("ccseq_employeeid",
                    await WebAPI.Get(new Model.QueryDetails(Constants.EntityType.SystemUser, expense[0], ["systemuserid"], [new Model.Condition("ccseq_chnnavid", expense[0], Constants.Condition.EQUALS)])),
                    Constants.EntityType.SystemUser));                    
                newExpenseTransactions.push(newExpenseTransaction);
            }
        });
});

main.js (post-babel)

/// <reference path="../node_modules/@types/jquery/index.d.ts" />
"use strict";

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

require("babel-polyfill");

requirejs.config({
    paths: {
        PapaParse: "/node_modules/papaparse/papaparse.min",
        Constants: "/build/CCSEQ/Constants",
        Model: "/build/CCSEQ/Model",
        WebAPI: "/build/CCSEQ/WebAPI",
        Xrm: "/build/CCSEQ/Xrm",
        Vue: "/node_modules/vue/dist/vue"
    }
});

require(["PapaParse", "Constants", "Model", "WebAPI", "Xrm", "Vue"], function (Papa, Constants, Model, WebAPI, Xrm, Vue) {
function ImportExpenseTransaction(data) {
        var _this = this;

        var newExpenseTransactions = new Array();
        var entityID = Xrm.Page.getCurrentEntityID();
        data.forEach(function () {
            var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(expense) {
                var newExpenseTransaction;
                return regeneratorRuntime.wrap(function _callee$(_context) {
                    while (1) {
                        switch (_context.prev = _context.next) {
                            case 0:
                                if (!(expense[0] !== "PR EMP ID")) {
                                    _context.next = 32;
                                    break;
                                }

                                newExpenseTransaction = new Model.Entity();


                                newExpenseTransaction.Type = Constants.EntityType.ExpenseTransaction;
                                _context.t0 = newExpenseTransaction.Attributes;
                                _context.t1 = Model.EntityField;
                                _context.next = 7;
                                return WebAPI.Get(new Model.QueryDetails(Constants.EntityType.SystemUser, expense[0], ["systemuserid"], [new Model.Condition("ccseq_chnnavid", expense[0], Constants.Condition.EQUALS)]));

                            case 7:
                                _context.t2 = _context.sent;
                                _context.t3 = Constants.EntityType.SystemUser;
                                _context.t4 = new _context.t1("ccseq_employeeid", _context.t2, _context.t3);

                                _context.t0.push.call(_context.t0, _context.t4);
                                newExpenseTransactions.push(newExpenseTransaction);

                            case 32:
                            case "end":
                                return _context.stop();
                        }
                    }
                }, _callee, _this);
            }));

            return function (_x) {
                return _ref.apply(this, arguments);
            };
        }());
});

One thing I can see is that you're not actually doing anything with

require("babel-polyfill")

as in require is returning the module's export for you to use. Simply having line 4 in main.js isn't enough.

That said I'm not sure exactly what you're supposed to do with that module in this context.

I ran into a polyfill issue with our test suite (we use karma) and had to work around it like this (from karma.conf.js)

module.exports = function (config) {
  config.set({
    // to run in additional browsers:
    // 1. install corresponding karma launcher
    //    http://karma-runner.github.io/0.13/config/browsers.html
    // 2. add it to the `browsers` array below.
    browsers: ['PhantomJS'],
    frameworks: ['mocha', 'sinon-chai'],
    reporters: ['spec', 'coverage'],
    files: [
       '../../node_modules/babel-polyfill/dist/polyfill.js',
      './index.js'
    ],
    preprocessors: {
      './index.js': ['webpack', 'sourcemap']
    },
    webpack: webpackConfig,
    webpackMiddleware: {
      noInfo: true
    },
    coverageReporter: {
      dir: './coverage',
      reporters: [
        { type: 'lcov', subdir: '.' },
        { type: 'text-summary' }
      ]
    }
  })
}

I had to add the relative path to polyfill.js (that lives in the babel-polyfill module under /dist) into the files property. Maybe you need to do something similar?

But you'll probably want to store the return value of require into something like

var polyfill = require("babel-polyfill")

and then reference that somewhere.

Hope this helps.

¯_(ツ)_/¯


maybe even in paths add something like:

Polyfill: "/node_modules/babel-polyfill/dist/polyfill.js"

链接地址: http://www.djcxy.com/p/32956.html

上一篇: 如何将Gulp包保存在源目录中?

下一篇: 解决模块尚未加载,但错误Babel异步