Skip to content

Commit aa2b4e7

Browse files
committed
fix bare imports in format: 'cjs' mode (sveltejs#4050)
1 parent e996ef7 commit aa2b4e7

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

src/compiler/compile/create_module.ts

+28-22
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,34 @@ function cjs(
152152

153153
const internal_globals = get_internal_globals(globals, helpers);
154154

155-
const user_requires = imports.map(node => ({
156-
type: 'VariableDeclaration',
157-
kind: 'const',
158-
declarations: [{
159-
type: 'VariableDeclarator',
160-
id: node.specifiers[0].type === 'ImportNamespaceSpecifier'
161-
? { type: 'Identifier', name: node.specifiers[0].local.name }
162-
: {
163-
type: 'ObjectPattern',
164-
properties: node.specifiers.map(s => ({
165-
type: 'Property',
166-
method: false,
167-
shorthand: false,
168-
computed: false,
169-
key: s.type === 'ImportSpecifier' ? s.imported : { type: 'Identifier', name: 'default' },
170-
value: s.local,
171-
kind: 'init'
172-
}))
173-
},
174-
init: x`require("${edit_source(node.source.value, sveltePath)}")`
175-
}]
176-
}));
155+
const user_requires = imports.map(node => {
156+
const init = x`require("${edit_source(node.source.value, sveltePath)}")`;
157+
if (node.specifiers.length === 0) {
158+
return b`${init};`;
159+
}
160+
return {
161+
type: 'VariableDeclaration',
162+
kind: 'const',
163+
declarations: [{
164+
type: 'VariableDeclarator',
165+
id: node.specifiers[0].type === 'ImportNamespaceSpecifier'
166+
? { type: 'Identifier', name: node.specifiers[0].local.name }
167+
: {
168+
type: 'ObjectPattern',
169+
properties: node.specifiers.map(s => ({
170+
type: 'Property',
171+
method: false,
172+
shorthand: false,
173+
computed: false,
174+
key: s.type === 'ImportSpecifier' ? s.imported : { type: 'Identifier', name: 'default' },
175+
value: s.local,
176+
kind: 'init'
177+
}))
178+
},
179+
init
180+
}]
181+
};
182+
});
177183

178184
const exports = module_exports.map(x => b`exports.${{ type: 'Identifier', name: x.as }} = ${{ type: 'Identifier', name: x.name }};`);
179185

0 commit comments

Comments
 (0)