power-assert-js / espower-source

Power Assert instrumentor from code to code, with SourceMap.
MIT License
11 stars 7 forks source link

SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' with webpack 2 #15

Closed ktsn closed 7 years ago

ktsn commented 7 years ago

Hello.

I faced an issue when using power-assert with webpack2 (using webpack-espower-loader). When I compile test code that includes ES Module syntax, it always throws error.

As the error message says, I suspect the option of acorn in espower-source is the root cause but I'm not so much sure about it. Could you take a look into it?

Error log
$ webpack && mocha test.build.js
Hash: 8b49b24f326999f24741
Version: webpack 2.4.1
Time: 115ms
        Asset     Size  Chunks             Chunk Names
test.build.js  3.98 kB       0  [emitted]  main
   [0] ./test.js 1.35 kB {0} [built] [failed] [1 error]

ERROR in ./test.js
Module build failed: SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
    at Parser.pp$4.raise (/Users/katashin/dev/playground/espower-source-test/node_modules/acorn/dist/acorn.js:2488:13)
    at Parser.pp$1.parseStatement (/Users/katashin/dev/playground/espower-source-test/node_modules/acorn/dist/acorn.js:762:14)
    at Parser.parseStatement (/Users/katashin/dev/playground/espower-source-test/node_modules/acorn-es7-plugin/acorn-v4.js:66:25)
    at Parser.pp$1.parseTopLevel (/Users/katashin/dev/playground/espower-source-test/node_modules/acorn/dist/acorn.js:672:23)
    at Parser.parse (/Users/katashin/dev/playground/espower-source-test/node_modules/acorn/dist/acorn.js:529:15)
    at Parser.parse (/Users/katashin/dev/playground/espower-source-test/node_modules/acorn-es7-plugin/acorn-v4.js:45:25)
    at Object.parse (/Users/katashin/dev/playground/espower-source-test/node_modules/acorn/dist/acorn.js:3378:37)
    at instrument (/Users/katashin/dev/playground/espower-source-test/node_modules/espower-source/index.js:150:23)
    at espowerSource (/Users/katashin/dev/playground/espower-source-test/node_modules/espower-source/index.js:197:24)
    at Object.module.exports (/Users/katashin/dev/playground/espower-source-test/node_modules/webpack-espower-loader/index.js:27:28)
error Command failed with exit code 2.

The reproduction project is here. Below is the step to reproduce it.

  1. Clone it
  2. In the cloned project directory, $ npm install
  3. $ npm test

Thanks.

twada commented 7 years ago

@ktsn Thank you for reporting and reproduction case! I'll investigate it.

Determining sourceType (script or module) needs a design decision, so is a bit hard problem but I have to tackle with it.

Thanks a lot!

twada commented 7 years ago

Thank you for reporting. Just released espower-source 2.2.0, and webpack-espower-loader 1.1.0 is coming soon.

ktsn commented 7 years ago

@twada Thank you for the fix! I'm looking forward the next release 🙂

twada commented 7 years ago

@ktsn Sorry for waiting so long. upgrade espower-source to ^2.0.0 has done and webpack-espower-loader 1.1.0 is out. Would you check it out?

ktsn commented 7 years ago

@twada I've try the new version of webpack-espower-loader but there is another error 😞

Error log
$ webpack && mocha test.build.js
Hash: 952ee050f6040cd3f5da
Version: webpack 2.4.1
Time: 143ms
        Asset     Size  Chunks             Chunk Names
test.build.js  4.17 kB       0  [emitted]  main
   [0] ./test.js 1.53 kB {0} [built] [failed] [1 error]

ERROR in ./test.js
Module build failed: AssertionError: ImportDeclaration should appear when the mode is ES6 and in the module context.
    at Referencer.ImportDeclaration (/Users/katashin/dev/playground/espower-source-test/node_modules/escope/lib/referencer.js:591:34)
    at Referencer.Visitor.visit (/Users/katashin/dev/playground/espower-source-test/node_modules/esrecurse/esrecurse.js:122:34)
    at Referencer.Visitor.visitChildren (/Users/katashin/dev/playground/espower-source-test/node_modules/esrecurse/esrecurse.js:101:38)
    at Referencer.Program (/Users/katashin/dev/playground/espower-source-test/node_modules/escope/lib/referencer.js:419:18)
    at Referencer.Visitor.visit (/Users/katashin/dev/playground/espower-source-test/node_modules/esrecurse/esrecurse.js:122:34)
    at Object.analyze (/Users/katashin/dev/playground/espower-source-test/node_modules/escope/lib/index.js:153:16)
    at Instrumentor.createVisitor (/Users/katashin/dev/playground/espower-source-test/node_modules/espower/lib/instrumentor.js:31:31)
    at Function.createVisitor (/Users/katashin/dev/playground/espower-source-test/node_modules/espower/index.js:43:25)
    at instrument (/Users/katashin/dev/playground/espower-source-test/node_modules/espower-source/index.js:186:17)
    at espowerSource (/Users/katashin/dev/playground/espower-source-test/node_modules/espower-source/index.js:235:24)
    at Object.module.exports (/Users/katashin/dev/playground/espower-source-test/node_modules/webpack-espower-loader/index.js:27:28)
error Command failed with exit code 2.
  

I've updated the reproduction. https://github.com/ktsn/espower-source-test

twada commented 7 years ago

@ktsn OMG I should have checked on your repro case... so sorry for that. The problem is now on escope usage and I'll fix it ASAP.

ktsn commented 7 years ago

Take it easy! I can transpile test codes to commonjs for now 🙂

twada commented 7 years ago

Just released espower 2.1.0 to fix current problem. @ktsn Would you give it a try? (in other words, rm -rf node_modules && npm install && npm test again?)

ktsn commented 7 years ago

@twada It works perfectly now. Thank you so much for your help!

twada commented 7 years ago

@ktsn glad to hear that :)