@ -1,7 +1,7 @@
import list from '../utils/list' ;
import list from '../utils/list' ;
import { ModuleFormat } from '../interfaces' ;
import { ModuleFormat } from '../interfaces' ;
import { b , x } from 'code-red' ;
import { b , x } from 'code-red' ;
import { Identifier , ImportDeclaration } from 'estree' ;
import { Identifier , ImportDeclaration , ExportNamedDeclaration } from 'estree' ;
const wrappers = { esm , cjs } ;
const wrappers = { esm , cjs } ;
@ -19,20 +19,21 @@ export default function create_module(
helpers : Array < { name : string ; alias : Identifier } > ,
helpers : Array < { name : string ; alias : Identifier } > ,
globals : Array < { name : string ; alias : Identifier } > ,
globals : Array < { name : string ; alias : Identifier } > ,
imports : ImportDeclaration [ ] ,
imports : ImportDeclaration [ ] ,
module _exports : Export [ ]
module _exports : Export [ ] ,
exports_from : ExportNamedDeclaration [ ]
) {
) {
const internal_path = ` ${ sveltePath } /internal ` ;
const internal_path = ` ${ sveltePath } /internal ` ;
helpers . sort ( ( a , b ) = > ( a . name < b . name ) ? - 1 : 1 ) ;
helpers . sort ( ( a , b ) = > ( a . name < b . name ) ? - 1 : 1 ) ;
globals . sort ( ( a , b ) = > ( a . name < b . name ) ? - 1 : 1 ) ;
globals . sort ( ( a , b ) = > ( a . name < b . name ) ? - 1 : 1 ) ;
if ( format === 'esm' ) {
const formatter = wrappers [ format ] ;
return esm ( program , name , banner , sveltePath , internal_path , helpers , globals , imports , module _exports ) ;
}
if ( format === 'cjs' ) return cjs ( program , name , banner , sveltePath , internal_path , helpers , globals , imports , module _exports ) ;
if ( ! formatter ) {
throw new Error ( ` options.format is invalid (must be ${ list ( Object . keys ( wrappers ) ) } ) ` ) ;
throw new Error ( ` options.format is invalid (must be ${ list ( Object . keys ( wrappers ) ) } ) ` ) ;
}
return formatter ( program , name , banner , sveltePath , internal_path , helpers , globals , imports , module _exports , exports_from ) ;
}
}
function edit_source ( source , sveltePath ) {
function edit_source ( source , sveltePath ) {
@ -76,7 +77,8 @@ function esm(
helpers : Array < { name : string ; alias : Identifier } > ,
helpers : Array < { name : string ; alias : Identifier } > ,
globals : Array < { name : string ; alias : Identifier } > ,
globals : Array < { name : string ; alias : Identifier } > ,
imports : ImportDeclaration [ ] ,
imports : ImportDeclaration [ ] ,
module _exports : Export [ ]
module _exports : Export [ ] ,
exports_from : ExportNamedDeclaration [ ]
) {
) {
const import_declaration = {
const import_declaration = {
type : 'ImportDeclaration' ,
type : 'ImportDeclaration' ,
@ -94,6 +96,9 @@ function esm(
imports . forEach ( node = > {
imports . forEach ( node = > {
node . source . value = edit_source ( node . source . value , sveltePath ) ;
node . source . value = edit_source ( node . source . value , sveltePath ) ;
} ) ;
} ) ;
exports_from . forEach ( node = > {
node . source ! . value = edit_source ( node . source ! . value , sveltePath ) ;
} ) ;
const exports = module _exports.length > 0 && {
const exports = module _exports.length > 0 && {
type : 'ExportNamedDeclaration' ,
type : 'ExportNamedDeclaration' ,
@ -110,6 +115,7 @@ function esm(
$ { import_declaration }
$ { import_declaration }
$ { internal_globals }
$ { internal_globals }
$ { imports }
$ { imports }
$ { exports_from }
$ { program . body }
$ { program . body }
@ -127,7 +133,8 @@ function cjs(
helpers : Array < { name : string ; alias : Identifier } > ,
helpers : Array < { name : string ; alias : Identifier } > ,
globals : Array < { name : string ; alias : Identifier } > ,
globals : Array < { name : string ; alias : Identifier } > ,
imports : ImportDeclaration [ ] ,
imports : ImportDeclaration [ ] ,
module _exports : Export [ ]
module _exports : Export [ ] ,
exports_from : ExportNamedDeclaration [ ]
) {
) {
const internal_requires = {
const internal_requires = {
type : 'VariableDeclaration' ,
type : 'VariableDeclaration' ,
@ -183,6 +190,13 @@ function cjs(
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 } }; ` ) ;
const user_exports_from = exports_from . map ( node = > {
const init = x ` require(" ${ edit_source ( node . source . value , sveltePath ) } ") ` ;
return node . specifiers . map ( specifier = > {
return b ` exports. ${ specifier . exported } = ${ init } . ${ specifier . local } ; ` ;
} ) ;
} ) ;
program . body = b `
program . body = b `
/* ${banner} */
/* ${banner} */
@ -190,6 +204,7 @@ function cjs(
$ { internal_requires }
$ { internal_requires }
$ { internal_globals }
$ { internal_globals }
$ { user_requires }
$ { user_requires }
$ { user_exports_from }
$ { program . body }
$ { program . body }