Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

ecmascript 6 - Using webpack and mocha with ES6 modules

I have a frontend JS project. I want to use Mocha for testing and webpack for bundling. I use ES6 modules in my project.

Mocha requires to set the type of my package to module as described here. While webpack fails in this case with

[webpack-cli] Failed to load 'webpack.config.js'
[webpack-cli] TypeError: Invalid host defined options
...

How can I use mocha and webpack in an ES6 project?

question from:https://stackoverflow.com/questions/65881217/using-webpack-and-mocha-with-es6-modules

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

TL;DR

Rename webpack.config.js to webpack.config.cjs


The problem is that webpack doesn't support ES6 config file. This means that when you set the type of a package to module webpack.config.js interpreted as an ES6 module, which is not supported yet.

Solutions/workarounds:

  1. Set the type of a package to module and rename webpack.config.js to webpack.config.cjs which results that entire your project will interpreted as ES6 modules, except the webpack.config.cjs which remains a commonjs file.

  2. Keep the type of a package commonjs and rename all of your files to mjs (except webpack.config.js)

  3. Use a different bundler, eg, rollup.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...