Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bare Imports fail when using CJS format #4050

Closed
clintwood opened this issue Dec 4, 2019 · 3 comments · Fixed by #4051
Closed

Bare Imports fail when using CJS format #4050

clintwood opened this issue Dec 4, 2019 · 3 comments · Fixed by #4051
Labels

Comments

@clintwood
Copy link

Describe the bug
When using a bare import in a component script (also with script in module context as in thsi example) there are no specifiers on the node at this line which causes this error: TypeError: Cannot read property 'type' of undefined

To Reproduce
a.js:

// any code here - empty is fine too

Component.svelte:

<script context="module">
  import './a';
</script>

compile.js:

const { readFileSync } = require('fs');
const svelte = require('svelte/compiler');

const source = readFileSync('./Component.svelte', 'utf8');

const result = svelte.compile(source, {
  format: 'cjs',
});

console.log(result);

Expected behavior
Should compile and generate require('./a.js'); in the example provided above.

Stacktrace

/project-dir/node_modules/svelte/compiler.js:21095
	                id: node.specifiers[0].type === 'ImportNamespaceSpecifier'
	                                       ^

TypeError: Cannot read property 'type' of undefined
    at /project-dir/node_modules/svelte/compiler.js:21095:41
    at Array.map (<anonymous>)
    at cjs (/project-dir/node_modules/svelte/compiler.js:21090:36)
    at create_module (/project-dir/node_modules/svelte/compiler.js:21002:17)
    at Component.generate (/project-dir/node_modules/svelte/compiler.js:24900:14)
    at Object.compile (/project-dir/node_modules/svelte/compiler.js:25965:23)
    at Object.<anonymous> (/project-dir/compile.js:6:23)
    at Module._compile (internal/modules/cjs/loader.js:1121:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
    at Module.load (internal/modules/cjs/loader.js:976:32)

Information about your Svelte project:
Not relevant but I'm using Svelte v3.16.0

Severity
Blocking - I'm using Jest for testing which requires cjs format with jest-transform-svelte which forces cjs format which breaks on compile when using a bare import.

@Conduitry Conduitry added the bug label Dec 4, 2019
@Conduitry
Copy link
Member

Presumably, we just need to handle the case here of node.specifiers being an empty array, and return only the require().

Conduitry added a commit to Conduitry/svelte that referenced this issue Dec 4, 2019
Conduitry added a commit to Conduitry/svelte that referenced this issue Dec 4, 2019
Conduitry added a commit to Conduitry/svelte that referenced this issue Dec 4, 2019
@clintwood
Copy link
Author

Wow thank you for the swift response and PR already!

@Conduitry
Copy link
Member

Fixed in 3.16.1.

taylorzane pushed a commit to taylorzane/svelte that referenced this issue Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants