diff --git a/README.md b/README.md index 06c25c0..731a0a5 100644 --- a/README.md +++ b/README.md @@ -375,47 +375,6 @@ console.log(transformModule(`console.log(import.meta.url)`), { url: 'test.mjs' } Options are same as `evalModule`. -## Code Generation - -Utilities to generate JavaScript code for ESM syntax. - -### `genImport` - -Generate a static import statement. - -```js -import { genImport } from 'mlly' - -// import { foo } from "pkg" -console.log(genImport('pkg', 'foo')) - -// import { a, b } from "pkg" -console.log(genImport('pkg', 'foo')) - -// import { foo as bar } from "pkg" -console.log(genImport('pkg', { name: 'foo', as: 'bar' })) -``` - -### `genDynamicImport` - -Generate a dynamic import statement. - -```js -import { genDynamicImport } from 'mlly' - -// () => import("pkg") -console.log(genDynamicImport('pkg')) - -// () => import("pkg").then(m => m.default || m) -console.log(genDynamicImport('pkg', { interopDefault: true })) - -// import("pkg") -console.log(genDynamicImport('pkg', { wrapper: false })) - -// () => import("pkg" /* webpackChunkName: "pkg" */) -console.log(genDynamicImport('pkg', { comment: 'webpackChunkName: "pkg"' })) -``` - ## Other Utils ### `fileURLToPath` diff --git a/src/gen.ts b/src/gen.ts deleted file mode 100644 index 54c7f0b..0000000 --- a/src/gen.ts +++ /dev/null @@ -1,47 +0,0 @@ -export interface CodegenOptions { - singleQuotes?: Boolean -} - -// genImport -export type Import = string | { name: string, as?: string } -export function genImport (specifier: string, imports?: Import | Import[], opts: CodegenOptions = {}) { - const specifierStr = genString(specifier, opts) - if (!imports) { - return `import ${specifierStr}` - } - - const _imports = (Array.isArray(imports) ? imports : [imports]).map((i: Import) => { - if (typeof i === 'string') { return { name: i } } - if (i.name === i.as) { i = { name: i.name } } - // TODO: Ensure `name` and `as` are valid identifiers - // TODO: Ensure `as` is provided for default import - return i - }) - - const importsStr = _imports.map(i => i.as ? `${i.name} as ${i.as}` : i.name).join(', ') - return `import { ${importsStr} } from ${genString(specifier, opts)}` -} - -// genDynamicImport -export interface DynamicImportOptions extends CodegenOptions { - comment?: string - wrapper?: boolean - interopDefault?: boolean -} -export function genDynamicImport (specifier: string, opts: DynamicImportOptions = {}) { - const commentStr = opts.comment ? ` /* ${opts.comment} */` : '' - const wrapperStr = (opts.wrapper === false) ? '' : '() => ' - const ineropStr = opts.interopDefault ? '.then(m => m.default || m)' : '' - return `${wrapperStr}import(${genString(specifier, opts)}${commentStr})${ineropStr}` -} - -// genImportName - -// Internal -function genString (input: string, opts: CodegenOptions = {}) { - const str = JSON.stringify(input) - if (!opts.singleQuotes) { - return JSON.stringify(input) - } - return `'${str.replace(/'/g, "\\'")}'` -} diff --git a/src/index.ts b/src/index.ts index d25d222..7c2e547 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ export * from './analyze' export * from './cjs' export * from './eval' -export * from './gen' export * from './resolve' export * from './syntax' export * from './utils' diff --git a/test/gen.test.mjs b/test/gen.test.mjs deleted file mode 100644 index c1b36f7..0000000 --- a/test/gen.test.mjs +++ /dev/null @@ -1,36 +0,0 @@ -import { expect } from 'chai' -import { genImport, genDynamicImport } from 'mlly' - -const genImportTests = [ - { imports: 'foo', code: 'import { foo } from "pkg"' }, - { imports: { name: 'foo', as: 'bar' }, code: 'import { foo as bar } from "pkg"' }, - { imports: { name: 'default', as: 'Test' }, code: 'import { default as Test } from "pkg"' } -] - -describe('genImport', () => { - for (const t of genImportTests) { - it(t.code, () => { - const code = genImport(t.specifier || 'pkg', t.imports || 'default', t.opts) - expect(code).to.equal(t.code) - }) - } -}) - -const genDynamicImportTests = [ - { code: '() => import("pkg")' }, - { opts: { wrapper: false }, code: 'import("pkg")' }, - { opts: { interopDefault: true }, code: '() => import("pkg").then(m => m.default || m)' }, - { - opts: { comment: 'webpackChunkName: "chunks/dynamic"' }, - code: '() => import("pkg" /* webpackChunkName: "chunks/dynamic" */)' - } -] - -describe('genDynamicImport', () => { - for (const t of genDynamicImportTests) { - it(t.code, () => { - const code = genDynamicImport(t.specifier || 'pkg', t.opts) - expect(code).to.equal(t.code) - }) - } -})