Skip to content

Commit

Permalink
fix bare imports in format: 'cjs' mode (sveltejs#4050)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Dec 4, 2019
1 parent 13ef75b commit cf436c3
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions src/compiler/compile/create_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }};`);

Expand Down

0 comments on commit cf436c3

Please sign in to comment.