Каков правильный шаблон регулярных выражений для игнорирования в @ babel / register? - PullRequest
0 голосов
/ 13 декабря 2018

Я запускаю mocha на некоторых компонентах React, которые содержат openlayers, новое обновление ol модулей все написано только в ES6, и их необходимо перенести перед передачей в mocha для тестирования.

Я использую @babel/register, и мне требуется файл babel-register.js, в котором мне требуется @ babel / register и введите все параметры и пресеты / плагины.Я не перемещаюсь и получаю ошибку, которую вы увидите ниже.Все, что я хотел бы, это требовать только node_modules / ol и рекурсивно все файлы и подпапки для переноса, правильное ли регулярное выражение?Что-то еще не так?Мой файл babel-register.js выглядит следующим образом:

require("@babel/polyfill");
require("@babel/register")({
    ignore: [
      // When a file path matches this regex then it is **not** compiled
      'node_modules/(?!(ol)/)'
    ],

presets : ["react-app", "@babel/preset-env"],
plugins : [
    "@babel/plugin-transform-modules-commonjs", 
    "inline-react-svg",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-transform-runtime"
],

//root: __dirname,
extensions: [".es6", ".es", ".jsx", ".js", ".mjs"],

// Setting this to false will disable the cache.
cache: false,
});

Ниже приведено сообщение об ошибке, которое я получаю после преобразования (я не уверен, что это из-за регулярного выражения):

/home/Development/test-openlayers/node_modules/@babel/runtime/helpers/typeof.js:8
  if (typeof Symbol === "function" && (0, _typeof4.default)(Symbol.iterator) === "symbol") {
                                                           ^

TypeError: (0 , _typeof4.default) is not a function
    at _typeof2 (/home/alessandro/Development/test-openlayers/node_modules/@babel/runtime/helpers/typeof.js:1:62)
    at _typeof2 (/home/alessandro/Development/test-openlayers/node_modules/@babel/runtime/helpers/typeof.js:4:39)
    at Object.<anonymous> (/home/alessandro/Development/test-openlayers/node_modules/lodash.sortby/index.js:95:18)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.require (/home/alessandro/Development/test-openlayers/node_modules/whatwg-url/lib/URLSearchParams-impl.js:2:22)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.require (/home/alessandro/Development/test-openlayers/node_modules/whatwg-url/lib/URLSearchParams.js:432:14)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.require (/home/alessandro/Development/test-openlayers/node_modules/whatwg-url/lib/URL-impl.js:4:25)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.require (/home/alessandro/Development/test-openlayers/node_modules/whatwg-url/lib/URL.js:335:14)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.require (/home/alessandro/Development/test-openlayers/node_modules/whatwg-url/lib/public-api.js:3:15)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.require (/home/alessandro/Development/test-openlayers/node_modules/jsdom/lib/api.js:7:19)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.require (/home/alessandro/Development/test-openlayers/setup.js:1:19)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/home/alessandro/Development/test-openlayers/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at requires.forEach.mod (/home/alessandro/Development/test-openlayers/node_modules/mocha/bin/_mocha:511:3)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/home/alessandro/Development/test-openlayers/node_modules/mocha/bin/_mocha:510:10)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

Ответы [ 2 ]

0 голосов
/ 13 декабря 2018

Итак, благодаря @Thomas Skubicki, слэши регулярных выражений должны были быть экранированы, еще два слэша должны были быть помещены в начало и конец выражения reg, а одинарные кавычки должны быть удалены, это правильная рабочая версияфайл babel-register.js.

require("@babel/polyfill");
require("@babel/register")({
    // Array of ignore conditions, either a regex or a function. (Optional)
    ignore: [
      // When a file path matches this regex then it is **not** compiled
      /node_modules\/(?!(ol)\/)/

      // The file's path is also passed to any ignore functions. It will
      // **not** be compiled if `true` is returned.
      //function(filepath) {
        //return filepath !== "/path/to/es6-file.js";
      //},
    ],

    presets : ["react-app"],
    plugins : [
        "@babel/plugin-transform-modules-commonjs", 
        "inline-react-svg",
    ],

    //root: __dirname,

    // Optional only regex - if any filenames **don't** match this regex then they
    // aren't compiled
    //only: /my_es6_folder/,

    // Setting this will remove the currently hooked extensions of `.es6`, `.es`, `.jsx`, `.mjs`
    // and .js so you'll have to add them back if you want them to be used again.
    extensions: [".es6", ".es", ".jsx", ".js", ".mjs"],

    // Setting this to false will disable the cache.
    cache: false,
  });
0 голосов
/ 13 декабря 2018

Эти косые черты в этом регулярном выражении, возможно, должны быть экранированы

 'node_modules\/(?!(ol)\/)'

Это регулярное выражение выдает ошибки в regex101.com

...