Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature-Request: Using with ES6 #6

Open
peey opened this issue Dec 25, 2017 · 3 comments
Open

Feature-Request: Using with ES6 #6

peey opened this issue Dec 25, 2017 · 3 comments

Comments

@peey
Copy link

peey commented Dec 25, 2017

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'
        ]
      }
@andres-arana
Copy link
Contributor

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?

@xyy94813
Copy link

@peey Any solution?

@xyy94813
Copy link

xyy94813 commented Aug 12, 2019

@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';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants