webpack-contrib / imports-loader

Imports Loader
MIT License
520 stars 63 forks source link

'import' and 'export' may only appear at the top level #90

Closed chkpnt closed 4 years ago

chkpnt commented 4 years ago

Expected Behavior

According to #39, imports-loader should work with ES6 modules.

Actual Behavior

I see the same issue as described in #39:

[...]
ERROR in ./src/foobar.js 4:0
Module parse failed: 'import' and 'export' may only appear at the top level (4:0)
File was processed with these loaders:
 * ./node_modules/imports-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| 
| (function() {
> export let bla = {
|     doSomething: function() {
|         console.log("this is " + this);
 @ ./src/index.js 1:0-31 3:0-15

webpack 5.1.2 compiled with 1 error in 156 ms

Code

Please see the demonstration repo at https://github.com/chkpnt/imports-loader-issue.

How Do We Reproduce?

Just execute npm run-script dev in the demonstration repo.

cap-Bernardito commented 4 years ago

As stated in the error - 'import' and 'export' may only appear at the top level of your file. In other words, import/export don't work inside {}.

You can use CommonJS syntax instead:

let bla = {
    doSomething: function() {
        console.log("this is " + this);
    }
}

module.exports = bla;
const bla = require("./foobar");

bla.doSomething();

Quote from readme - Do not use this option if source code contains ES module import(s)

chkpnt commented 4 years ago

Thanks for the clarification! 👍