fix bare imports in `format: 'cjs'` mode (#4050)

pull/4062/head
Conduitry 5 years ago
parent e996ef750d
commit aa2b4e72f7

@ -152,28 +152,34 @@ function cjs(
const internal_globals = get_internal_globals(globals, helpers); const internal_globals = get_internal_globals(globals, helpers);
const user_requires = imports.map(node => ({ const user_requires = imports.map(node => {
type: 'VariableDeclaration', const init = x`require("${edit_source(node.source.value, sveltePath)}")`;
kind: 'const', if (node.specifiers.length === 0) {
declarations: [{ return b`${init};`;
type: 'VariableDeclarator', }
id: node.specifiers[0].type === 'ImportNamespaceSpecifier' return {
? { type: 'Identifier', name: node.specifiers[0].local.name } type: 'VariableDeclaration',
: { kind: 'const',
type: 'ObjectPattern', declarations: [{
properties: node.specifiers.map(s => ({ type: 'VariableDeclarator',
type: 'Property', id: node.specifiers[0].type === 'ImportNamespaceSpecifier'
method: false, ? { type: 'Identifier', name: node.specifiers[0].local.name }
shorthand: false, : {
computed: false, type: 'ObjectPattern',
key: s.type === 'ImportSpecifier' ? s.imported : { type: 'Identifier', name: 'default' }, properties: node.specifiers.map(s => ({
value: s.local, type: 'Property',
kind: 'init' method: false,
})) shorthand: false,
}, computed: false,
init: x`require("${edit_source(node.source.value, sveltePath)}")` 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 }};`); const exports = module_exports.map(x => b`exports.${{ type: 'Identifier', name: x.as }} = ${{ type: 'Identifier', name: x.name }};`);

Loading…
Cancel
Save