Terser plugin: "Export" statement may only appear at the top level using `lodash-es`

We’re trying to register custom blocks specifically for our theme, which uses the times function from lodash-es. If we import it via

import { times } from 'lodash-es'

or

const times = require('lodash/times')

we either get a failing build due to a Terser error or a console error when in the gutenberg editor (require is not defined).

From what I understand, @babel/preset-env should handle both of these scenarios, but we’re not able to get this working. My assumption is that it’s because babel-loader is set to ignore files within node_modules, but I don’t know how to modify the exclude option within the babel config in bud.config.js to let babel transform imports from node_modules. We’ve done this in create-guten-block with no issue before, and the webpack config between that and bud are pretty similar overall, so we’re at a loss for how to get this work, here.

How should we approach this?

The lodash call is resolving to an uncompiled module. I’m not sure why.

Bud excludes a couple modules from being parsed, and so the uncompiled source is being included directly.

The fix for this is likely for you to add the following to your budfile:

app.hooks.on('build.module.noParse', [])

I can’t find the other issue that was related to this, but there is another discourse topic that was also related to module.noParse.

In all likelihood I’ll be removing this optimization in an upcoming release – by my count three users, including yourself, have bumped into problems with module.noParse.