From aa2b4e72f7f0991cb7ec89c533c9ebd6dfe2e7cf Mon Sep 17 00:00:00 2001 From: Conduitry Date: Wed, 4 Dec 2019 08:50:44 -0500 Subject: [PATCH] fix bare imports in `format: 'cjs'` mode (#4050) --- src/compiler/compile/create_module.ts | 50 +++++++++++++++------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/compiler/compile/create_module.ts b/src/compiler/compile/create_module.ts index 632206652a..80e6308263 100644 --- a/src/compiler/compile/create_module.ts +++ b/src/compiler/compile/create_module.ts @@ -152,28 +152,34 @@ function cjs( const internal_globals = get_internal_globals(globals, helpers); - const user_requires = imports.map(node => ({ - type: 'VariableDeclaration', - kind: 'const', - declarations: [{ - type: 'VariableDeclarator', - id: node.specifiers[0].type === 'ImportNamespaceSpecifier' - ? { type: 'Identifier', name: node.specifiers[0].local.name } - : { - type: 'ObjectPattern', - properties: node.specifiers.map(s => ({ - type: 'Property', - method: false, - shorthand: false, - computed: false, - key: s.type === 'ImportSpecifier' ? s.imported : { type: 'Identifier', name: 'default' }, - value: s.local, - kind: 'init' - })) - }, - init: x`require("${edit_source(node.source.value, sveltePath)}")` - }] - })); + const user_requires = imports.map(node => { + const init = x`require("${edit_source(node.source.value, sveltePath)}")`; + if (node.specifiers.length === 0) { + return b`${init};`; + } + return { + type: 'VariableDeclaration', + kind: 'const', + declarations: [{ + type: 'VariableDeclarator', + id: node.specifiers[0].type === 'ImportNamespaceSpecifier' + ? { type: 'Identifier', name: node.specifiers[0].local.name } + : { + type: 'ObjectPattern', + properties: node.specifiers.map(s => ({ + type: 'Property', + method: false, + shorthand: false, + computed: false, + key: s.type === 'ImportSpecifier' ? s.imported : { type: 'Identifier', name: 'default' }, + value: s.local, + kind: 'init' + })) + }, + init + }] + }; + }); const exports = module_exports.map(x => b`exports.${{ type: 'Identifier', name: x.as }} = ${{ type: 'Identifier', name: x.name }};`);