svelte-preprocess icon indicating copy to clipboard operation
svelte-preprocess copied to clipboard

Auto preprocess throws on ES6 module scripts

Open brunnerh opened this issue 3 years ago • 7 comments

Describe the bug When inserting a script in the HTML with type="module", the auto preprocessor will try to load a transformer for it (./transformers/module), which of course does not exist.

To Reproduce

  • Set up auto preprocessing.
  • Process a component like this:
<script></script>

<div>
    <script type="module">
         console.log('!');
    </script>
</div>

Expected behavior Scripts with type="module" are ignored.

Stacktraces

Stack trace
Error: Cannot find module './transformers/module'
Require stack:
- C:\project\node_modules\svelte-preprocess\dist\autoProcess.js
- C:\project\node_modules\svelte-preprocess\dist\index.js
- C:\project\webpack.config.ts
- C:\project\node_modules\webpack-cli\bin\utils\convert-argv.js
- C:\project\node_modules\webpack-cli\bin\cli.js
- C:\project\node_modules\webpack\bin\webpack.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
    at Function.Module._load (internal/modules/cjs/loader.js:686:27)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (C:\project\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
    at C:\project\node_modules\svelte-preprocess\dist\autoProcess.js:35:77
    at Object.exports.runTransformer (C:\project\node_modules\svelte-preprocess\dist\autoProcess.js:35:29)
    at C:\project\node_modules\svelte-preprocess\dist\autoProcess.js:96:29
    at script (C:\project\node_modules\svelte-preprocess\dist\autoProcess.js:121:33)
    at C:\project\node_modules\svelte\src\compiler\preprocess\index.ts:103:23
    at async Promise.all (index 0)
    at replace_async (C:\project\node_modules\svelte\src\compiler\preprocess\index.ts:60:48)
    at preprocess (C:\project\node_modules\svelte\src\compiler\preprocess\index.ts:95:12)
 @ ./src/test.ts 1:0-39 2:16-23

Information about your project:

  • Your operating system: Windows 10 64bit
  • svelte-preprocess version: 4.3.2
  • Whether your project uses Webpack or Rollup: Webpack

brunnerh avatar Oct 01 '20 16:10 brunnerh

Hey @brunnerh 👋

If I'm not mistaken you can pass preserve: ['module'] and it will be skipped.

Skipping type=module could be a default behavior, though 🤔

kaisermann avatar Oct 01 '20 17:10 kaisermann

Did not know that. I worked around it by simply not using auto-mode.

brunnerh avatar Oct 01 '20 21:10 brunnerh

Quick thought I had about this one:

I'm not sure if people would expect the babel transformer to run over script type="module" script tags that aren't at the root-level.

kaisermann avatar Oct 05 '20 13:10 kaisermann

I for one do not know; don't really use Babel. But for Svelte components the distinction between top-level and everything else seems to make sense.

brunnerh avatar Oct 05 '20 16:10 brunnerh

[In my case I "just" removed the type="module" I had (just grepped for it, and realized it wasn't serving a purpose anymore in that case). And that fixed it, of course. I know I'm kinda stating the obvious but I'm saying it in case someone's coming by here and unsure if they can just remove it. If you don't have many usages, or don't remember why you have them, you might just be able to remove them.]

floer32 avatar Dec 12 '20 01:12 floer32

I'm running into a problem where I'm using svelte-preprocess, and I have two async functions, one in <script context="module"> and another one in the main script tag. I'm getting Identifier 'asyncGeneratorStep' has already been declared (Note that you need plugins to import files that are not JavaScript). I can get rid of the async function in the preload script, or I can get rid of it in the body, but I can't use both... I'm tried to add preserve: ['module'] but that doesn't seem to work. Any thoughts?

janzheng avatar Dec 24 '20 00:12 janzheng

In case the same thing happens to anyone else searching for this error: It happened to me because I mistakenly left an extra <script> tag in the HTML area of one of my .svelte files.

DePasqualeOrg avatar Aug 29 '22 22:08 DePasqualeOrg