kozily/nearley-loader

Feature-Request: Using with ES6

Opened this issue · 3 comments

peey commented

I am facing an issue that this loader does not further process the generated parser with babel. This means if you have post-processing functions using ES6 (arrow functions among other things) in your .ne file then it basically won't work in older browsers.

Here's what I'm using as a temporary workaround:

Directly chaining it with babel-loader but it doesn't work. I had to write a custom loader to serve as an intermediary between this and babel loader.

// source for "intermediary-loader"
//https://stackoverflow.com/a/13647409/1412255
function removeLastLine(x) {
  if(x.lastIndexOf("\n")>0) {
      return x.substring(0, x.lastIndexOf("\n"));
  } else {
      return x;
  }
}

function removeLastNLines(x, n) {
  var result = x
  for (var i = 0; i < n; i++) result = removeLastLine(result);
  return result
}

// Identity loader with SourceMap support
module.exports = function(source, map) {
  const temp = removeLastNLines(source, 7) + "\n return grammar ;})();"
  const final = "export default " + temp
  this.callback(null, final, map);
};

// your webpack config:

      {
        test: /\.ne$/,
        use: [
          'babel-loader',
          'intermediary-loader',
          'nearley-loader'
        ]
      }

Seems to me that there might be a babel setting which is messing up parsing the output from nearley. I'll investigate a bit and see if we can bake something here without needing the custom loader messing up with the source code, as I think it's a bit fragile to do so.

Could you provide the exact babel config you are using?

@peey Any solution?

@andres-arana I got same problem

When i try to use it with babel-loader like this.

// one of my Webpack config rule
{
    test: /\.ne$/,
    use: [
      { loader: 'babel-loader', options: babelLoaderOptions },
      { loader: 'nearley-loader', options: nearleyLoaderOptions },
    ],
  }

I got a error:

Failed to compile.

./src/utils/MyNearley/index.js
Attempted import error: './MyNearley.ne' does not contain a default export (imported as 'Grammar').

MyNearley/index.js file:

import Grammar from './MyNearley.ne';