8

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.

d4nyll
  • 11,811
  • 6
  • 54
  • 68
Kevin Koshiol
  • 162
  • 1
  • 9
  • maybe you should look at fixing the cause of the error, rather than a way around it – Jaromanda X Oct 11 '16 at 23:05
  • or tried `strict:false` at the first level of that object (i.e. same level as test:, loader:, include: etc) – Jaromanda X Oct 11 '16 at 23:09
  • 1
    Fixing the cause of the error would mean modifying the source of a thirdparty library, which is not what I'm trying to do. I suppose I could submit a pull request with the changes that make this work then pull in the newest version of the library if the pull request gets approved, but I think removing the `delete someIdentifier;` statements would change the desired behavior of the library without a significant overhaul. – Kevin Koshiol Oct 12 '16 at 14:00
  • `strict: false` adjacent to `test`, `include`, `loader`, and `query` doesn't work because `babel-loader` doesn't pass any of those properties to babel or my custom babel plugin. – Kevin Koshiol Oct 12 '16 at 14:04
  • Removal of `delete identifier` would not alter the behavior since using delete on an unqualified identifier does nothing and returns false, or throws an error in strict mode. Delete works with object properties not with variables. To be fair that's a problem with the 3rd party module that you could revoke yourself or should ask the author to do so. You could also try using some old version without this code if one's available. – Michał Łuczak Feb 20 '21 at 15:27
  • Also note that ES modules and classes are in strict mode by default. – Michał Łuczak Feb 20 '21 at 15:35
  • The package you are using is in archive mode. Maybe going to the new package will eliminate the issue. – gaitat Feb 20 '21 at 22:55

1 Answers1

0

Just change your presets. This could help.

presets: [
'es2015'
]

to be

presets: [
['es2015', {modules: false}]
]
desertnaut
  • 57,590
  • 26
  • 140
  • 166
lmwenda
  • 11
  • 3