|
|
@ -7,7 +7,10 @@ interface Dependency {
|
|
|
|
source: string;
|
|
|
|
source: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function getModuleWrapper(
|
|
|
|
const wrappers = { es, amd, cjs, iife, umd, eval: expr };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function wrapModule(
|
|
|
|
|
|
|
|
code: string,
|
|
|
|
format: ModuleFormat,
|
|
|
|
format: ModuleFormat,
|
|
|
|
name: string,
|
|
|
|
name: string,
|
|
|
|
options: CompileOptions,
|
|
|
|
options: CompileOptions,
|
|
|
@ -16,8 +19,8 @@ export default function getModuleWrapper(
|
|
|
|
helpers: { name: string, alias: string }[],
|
|
|
|
helpers: { name: string, alias: string }[],
|
|
|
|
imports: Node[],
|
|
|
|
imports: Node[],
|
|
|
|
source: string
|
|
|
|
source: string
|
|
|
|
) {
|
|
|
|
): string {
|
|
|
|
if (format === 'es') return getEsWrapper(name, options, banner, sharedPath, helpers, imports, source);
|
|
|
|
if (format === 'es') return es(code, name, options, banner, sharedPath, helpers, imports, source);
|
|
|
|
|
|
|
|
|
|
|
|
const dependencies = imports.map((declaration, i) => {
|
|
|
|
const dependencies = imports.map((declaration, i) => {
|
|
|
|
const defaultImport = declaration.specifiers.find(
|
|
|
|
const defaultImport = declaration.specifiers.find(
|
|
|
@ -56,16 +59,17 @@ export default function getModuleWrapper(
|
|
|
|
return { name, statements, source: declaration.source.value };
|
|
|
|
return { name, statements, source: declaration.source.value };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (format === 'amd') return getAmdWrapper(name, options, banner, dependencies);
|
|
|
|
if (format === 'amd') return amd(code, name, options, banner, dependencies);
|
|
|
|
if (format === 'cjs') return getCjsWrapper(name, options, banner, sharedPath, helpers, dependencies);
|
|
|
|
if (format === 'cjs') return cjs(code, name, options, banner, sharedPath, helpers, dependencies);
|
|
|
|
if (format === 'iife') return getIifeWrapper(name, options, banner, dependencies);
|
|
|
|
if (format === 'iife') return iife(code, name, options, banner, dependencies);
|
|
|
|
if (format === 'umd') return getUmdWrapper(name, options, banner, dependencies);
|
|
|
|
if (format === 'umd') return umd(code, name, options, banner, dependencies);
|
|
|
|
if (format === 'eval') return getEvalWrapper(name, options, banner, dependencies);
|
|
|
|
if (format === 'eval') return expr(code, name, options, banner, dependencies);
|
|
|
|
|
|
|
|
|
|
|
|
throw new Error(`Not implemented: ${format}`);
|
|
|
|
throw new Error(`Not implemented: ${format}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getEsWrapper(
|
|
|
|
function es(
|
|
|
|
|
|
|
|
code: string,
|
|
|
|
name: string,
|
|
|
|
name: string,
|
|
|
|
options: CompileOptions,
|
|
|
|
options: CompileOptions,
|
|
|
|
banner: string,
|
|
|
|
banner: string,
|
|
|
@ -78,25 +82,25 @@ function getEsWrapper(
|
|
|
|
`import { ${helpers.map(h => h.name === h.alias ? h.name : `${h.name} as ${h.alias}`).join(', ')} } from ${JSON.stringify(sharedPath)};`
|
|
|
|
`import { ${helpers.map(h => h.name === h.alias ? h.name : `${h.name} as ${h.alias}`).join(', ')} } from ${JSON.stringify(sharedPath)};`
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const importBlock = imports
|
|
|
|
const importBlock = imports.length > 0 && (
|
|
|
|
|
|
|
|
imports
|
|
|
|
.map((declaration: Node) => source.slice(declaration.start, declaration.end))
|
|
|
|
.map((declaration: Node) => source.slice(declaration.start, declaration.end))
|
|
|
|
.join('\n');
|
|
|
|
.join('\n')
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return deindent`
|
|
|
|
intro: deindent`
|
|
|
|
|
|
|
|
${banner}
|
|
|
|
${banner}
|
|
|
|
${importHelpers}
|
|
|
|
${importHelpers}
|
|
|
|
${importBlock}
|
|
|
|
${importBlock}
|
|
|
|
|
|
|
|
|
|
|
|
export default (function() {
|
|
|
|
export default (function() {
|
|
|
|
`,
|
|
|
|
${code}
|
|
|
|
outro: deindent`
|
|
|
|
|
|
|
|
return ${name};
|
|
|
|
return ${name};
|
|
|
|
}());`
|
|
|
|
}());`;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getAmdWrapper(
|
|
|
|
function amd(
|
|
|
|
|
|
|
|
code: string,
|
|
|
|
name: string,
|
|
|
|
name: string,
|
|
|
|
options: CompileOptions,
|
|
|
|
options: CompileOptions,
|
|
|
|
banner: string,
|
|
|
|
banner: string,
|
|
|
@ -108,20 +112,17 @@ function getAmdWrapper(
|
|
|
|
|
|
|
|
|
|
|
|
const id = options.amd && options.amd.id;
|
|
|
|
const id = options.amd && options.amd.id;
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return deindent`
|
|
|
|
intro: deindent`
|
|
|
|
|
|
|
|
define(${id ? `"${id}", ` : ''}${sourceString}function(${paramString(dependencies)}) { "use strict";
|
|
|
|
define(${id ? `"${id}", ` : ''}${sourceString}function(${paramString(dependencies)}) { "use strict";
|
|
|
|
|
|
|
|
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
|
|
|
|
|
|
|
|
`,
|
|
|
|
${code}
|
|
|
|
outro: deindent`
|
|
|
|
|
|
|
|
return ${name};
|
|
|
|
return ${name};
|
|
|
|
});`
|
|
|
|
});`;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getCjsWrapper(
|
|
|
|
function cjs(
|
|
|
|
|
|
|
|
code: string,
|
|
|
|
name: string,
|
|
|
|
name: string,
|
|
|
|
options: CompileOptions,
|
|
|
|
options: CompileOptions,
|
|
|
|
banner: string,
|
|
|
|
banner: string,
|
|
|
@ -137,12 +138,13 @@ function getCjsWrapper(
|
|
|
|
}).join('\n')
|
|
|
|
}).join('\n')
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const requireBlock = dependencies
|
|
|
|
const requireBlock = dependencies.length > 0 && (
|
|
|
|
|
|
|
|
dependencies
|
|
|
|
.map(d => `var ${d.name} = require("${d.source}");`)
|
|
|
|
.map(d => `var ${d.name} = require("${d.source}");`)
|
|
|
|
.join('\n\n');
|
|
|
|
.join('\n\n')
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const intro = requireBlock ?
|
|
|
|
return deindent`
|
|
|
|
deindent`
|
|
|
|
|
|
|
|
${banner}
|
|
|
|
${banner}
|
|
|
|
"use strict";
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
@ -150,20 +152,13 @@ function getCjsWrapper(
|
|
|
|
${requireBlock}
|
|
|
|
${requireBlock}
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
|
|
|
|
|
|
|
|
` :
|
|
|
|
${code}
|
|
|
|
deindent`
|
|
|
|
|
|
|
|
${banner}
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
${helperBlock}
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const outro = `module.exports = ${name};`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { intro, outro };
|
|
|
|
module.exports = ${name};`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getIifeWrapper(
|
|
|
|
function iife(
|
|
|
|
|
|
|
|
code: string,
|
|
|
|
name: string,
|
|
|
|
name: string,
|
|
|
|
options: CompileOptions,
|
|
|
|
options: CompileOptions,
|
|
|
|
banner: string,
|
|
|
|
banner: string,
|
|
|
@ -175,22 +170,18 @@ function getIifeWrapper(
|
|
|
|
|
|
|
|
|
|
|
|
const globals = getGlobals(dependencies, options);
|
|
|
|
const globals = getGlobals(dependencies, options);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return deindent`
|
|
|
|
intro: deindent`
|
|
|
|
|
|
|
|
${banner}
|
|
|
|
${banner}
|
|
|
|
var ${options.name} = (function(${paramString(dependencies)}) { "use strict";
|
|
|
|
var ${options.name} = (function(${paramString(dependencies)}) { "use strict";
|
|
|
|
|
|
|
|
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
|
|
|
|
|
|
|
|
`,
|
|
|
|
${code}
|
|
|
|
|
|
|
|
|
|
|
|
outro: deindent`
|
|
|
|
|
|
|
|
return ${name};
|
|
|
|
return ${name};
|
|
|
|
}(${globals.join(', ')}));`
|
|
|
|
}(${globals.join(', ')}));`;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getUmdWrapper(
|
|
|
|
function umd(
|
|
|
|
|
|
|
|
code: string,
|
|
|
|
name: string,
|
|
|
|
name: string,
|
|
|
|
options: CompileOptions,
|
|
|
|
options: CompileOptions,
|
|
|
|
banner: string,
|
|
|
|
banner: string,
|
|
|
@ -212,8 +203,7 @@ function getUmdWrapper(
|
|
|
|
|
|
|
|
|
|
|
|
const globals = getGlobals(dependencies, options);
|
|
|
|
const globals = getGlobals(dependencies, options);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return deindent`
|
|
|
|
intro: deindent`
|
|
|
|
|
|
|
|
${banner}
|
|
|
|
${banner}
|
|
|
|
(function(global, factory) {
|
|
|
|
(function(global, factory) {
|
|
|
|
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory(${cjsDeps}) :
|
|
|
|
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory(${cjsDeps}) :
|
|
|
@ -223,16 +213,15 @@ function getUmdWrapper(
|
|
|
|
|
|
|
|
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
|
|
|
|
|
|
|
|
`,
|
|
|
|
${code}
|
|
|
|
|
|
|
|
|
|
|
|
outro: deindent`
|
|
|
|
|
|
|
|
return ${name};
|
|
|
|
return ${name};
|
|
|
|
|
|
|
|
|
|
|
|
})));`
|
|
|
|
})));`;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getEvalWrapper(
|
|
|
|
function expr(
|
|
|
|
|
|
|
|
code: string,
|
|
|
|
name: string,
|
|
|
|
name: string,
|
|
|
|
options: CompileOptions,
|
|
|
|
options: CompileOptions,
|
|
|
|
banner: string,
|
|
|
|
banner: string,
|
|
|
@ -240,17 +229,16 @@ function getEvalWrapper(
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
const globals = getGlobals(dependencies, options);
|
|
|
|
const globals = getGlobals(dependencies, options);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return deindent`
|
|
|
|
intro: deindent`
|
|
|
|
|
|
|
|
(function (${paramString(dependencies)}) { "use strict";
|
|
|
|
(function (${paramString(dependencies)}) { "use strict";
|
|
|
|
${banner}
|
|
|
|
${banner}
|
|
|
|
|
|
|
|
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
${getCompatibilityStatements(dependencies)}
|
|
|
|
|
|
|
|
|
|
|
|
`,
|
|
|
|
${code}
|
|
|
|
|
|
|
|
|
|
|
|
outro: `return ${name};\n\n}(${globals.join(', ')}))`
|
|
|
|
return ${name};
|
|
|
|
};
|
|
|
|
}(${globals.join(', ')}))`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function paramString(dependencies: Dependency[]) {
|
|
|
|
function paramString(dependencies: Dependency[]) {
|