Monday, 15 February 2021

babel-loader: Module build failed: SyntaxError: Deleting local variable in strict mode

I'm using babel-loader in webpack with a custom babel-plugin to transform some thirdparty code into a format that passes through Webpack's bundler without trouble. However, when my code runs through babel's parser (babylon) to build the AST, I get the following error:

Module build failed: SyntaxError: Deleting local variable in strict mode

I found the line in bablyon that triggers this message: https://github.com/babel/babylon/blob/master/src/parser/expression.js#L236

Looking at that code, it seems like I should be able to disable strict mode parsing in babylon by setting this.state.strict to false. The problem is I don't know how to set this.state.strict from babel-loader. I'm hoping someone else knows more about this.

Here are some things I've tried so far:

  1. strict: false and strictMode: false in query

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            strict: false,
            plugins: [__dirname + '/babel-plugins/custom-plugin']
        }
    }
    
  2. strict: false and strictMode: false with plugin

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            plugins: [
                [__dirname + '/babel-plugins/custom-plugin', {strict: false}]
            ]
        }
    }
    
  3. Set state.opts.strict to false in Program within custom-plugin.js (but this shouldn't work because babylon parses the code and fails before passing the AST off for traversal)

    module.exports = function (params) {
        return {
            visitor: {
                Program: function (path, state) {
                    state.opts.strict = false;
                }
            }
        };
    };
    
  4. Use blacklist in webpack.config.js and .babelrc (which was removed in babel v6 so this shouldn't work anyway)

    {
        test: /\.js$/,
        include: /bower_components/, //only thirdparty
        loader: 'babel',
        query: {
            plugins: [__dirname + '/babel-plugins/custom-plugin']
        }
    }
    

I can think of some hacky solutions to this problem, but this flag should be accessible at the surface through babel-loader or .babelrc in some form or another.



from babel-loader: Module build failed: SyntaxError: Deleting local variable in strict mode

No comments:

Post a Comment