DRY out internal_globals

pull/4005/head
daszgfz 6 years ago
parent 86b3776d6e
commit 5bc014f9bf

@ -41,28 +41,11 @@ function edit_source(source, sveltePath) {
: source; : source;
} }
function esm( function get_internal_globals(
program: any,
name: Identifier,
banner: string,
sveltePath: string,
internal_path: string,
helpers: Array<{ name: string; alias: Identifier }>,
globals: Array<{ name: string; alias: Identifier }>, globals: Array<{ name: string; alias: Identifier }>,
imports: ImportDeclaration[], helpers: Array<{ name: string; alias: Identifier }>
module_exports: Export[]
) { ) {
const import_declaration = { return globals.length > 0 && {
type: 'ImportDeclaration',
specifiers: helpers.map(h => ({
type: 'ImportSpecifier',
local: h.alias,
imported: { type: 'Identifier', name: h.name }
})),
source: { type: 'Literal', value: internal_path }
};
const internal_globals = globals.length > 0 && {
type: 'VariableDeclaration', type: 'VariableDeclaration',
kind: 'const', kind: 'const',
declarations: [{ declarations: [{
@ -82,6 +65,30 @@ function esm(
init: helpers.find(({ name }) => name === 'globals').alias init: helpers.find(({ name }) => name === 'globals').alias
}] }]
}; };
}
function esm(
program: any,
name: Identifier,
banner: string,
sveltePath: string,
internal_path: string,
helpers: Array<{ name: string; alias: Identifier }>,
globals: Array<{ name: string; alias: Identifier }>,
imports: ImportDeclaration[],
module_exports: Export[]
) {
const import_declaration = {
type: 'ImportDeclaration',
specifiers: helpers.map(h => ({
type: 'ImportSpecifier',
local: h.alias,
imported: { type: 'Identifier', name: h.name }
})),
source: { type: 'Literal', value: internal_path }
};
const internal_globals = get_internal_globals(globals, helpers);
// edit user imports // edit user imports
imports.forEach(node => { imports.forEach(node => {
@ -143,26 +150,7 @@ function cjs(
}] }]
}; };
const internal_globals = globals.length > 0 && { const internal_globals = get_internal_globals(globals, helpers);
type: 'VariableDeclaration',
kind: 'const',
declarations: [{
type: 'VariableDeclarator',
id: {
type: 'ObjectPattern',
properties: globals.map(g => ({
type: 'Property',
method: false,
shorthand: false,
computed: false,
key: { type: 'Identifier', name: g.name },
value: g.alias,
kind: 'init'
}))
},
init: helpers.find(({ name }) => name === 'globals').alias
}]
};
const user_requires = imports.map(node => ({ const user_requires = imports.map(node => ({
type: 'VariableDeclaration', type: 'VariableDeclaration',

Loading…
Cancel
Save