I have the following file:
src/index.js
let foo = null ?? "waffles"
And the following webpack config:
import path from 'path'
const defaultConf = {
name: 'default',
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist')
}
}
export default defaultConf
Build fails with:
ERROR in ./src/index.js 2:18
Module parse failed: Unexpected token (2:18)
Environment is:
node v14.8.0
webpack@4.44.1
Why does my build fail?
How can I debug what makes it fail?
How can I fix it?
Edit: Said syntax is supported by node (+all major browsers). The expectation is the build will succeed.
Edit: Based on Anthony's answer I can make the questions more specific:
- Based on the answer evidently, wepback is attempting to transpile that code. Why does webpack attempt to transpile that code? Is it operating on some default I'm unaware of? What is that default?
- If I'm tripping over some default, how can I find out what it is? Webpack docs say the following:
Out of the box, webpack won't require you to use a configuration file. However, it will assume the entry point of your project is
src/index.js
and will output the result indist/main.js
minified and optimized for production.
What other assumptions does it make? What does "optimized" mean? How does it minify the code? How can I inspect the default config? Potentially debug the build process (i.e. step-by-step debugging)?
- How can I prevent webpack from attempting to transpile this code? How can I set the language level? E.g. to es2020, of which nullish coalescing is part of.
Note: The point of this question is not to end up with a working piece of code. If I wanted that I'd just do foo = null ? null : "waffles"
instead of null coalescing. The point is to demystify webpack.