Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno.emit() not respecting compilerOptions.target when use with bundle:esm #44

Open
yw662 opened this issue Mar 2, 2021 · 6 comments
Open
Labels
bug Something isn't working public API

Comments

@yw662
Copy link

yw662 commented Mar 2, 2021

The code to compile :

export class A {
    static a = 'b'
}

Static member should be compiled into A.a = 'b' if targeting lower versions.

The code calling Deno.emit:

#! /usr/bin/env deno run -A --unstable
const sources = {
  '/foo.ts': `
        export class A {
            static a = 'b'
        }
        `
}
console.log(
  '\nno bundling:\n',
  (await Deno.emit('/foo.ts', { sources })).files['file:///foo.ts.js'],
  '\nbundle:esm:\n',
  (await Deno.emit('/foo.ts', { sources, bundle: 'esm' })).files[
    'deno:///bundle.js'
  ],
  '\nbundle:esm, target:es6:\n',
  (
    await Deno.emit('/foo.ts', {
      sources,
      bundle: 'esm',
      compilerOptions: { target: 'es6' }
    })
  ).files['deno:///bundle.js']
)

The output:

no bundling:
 export class A {
}
A.a = 'b';
 
bundle:esm:
 class A1 {
    static a = 'b';
}
export { A1 as A };
 
bundle:esm, target:es6:
 class A1 {
    static a = 'b';
}
export { A1 as A };

The behavior of bundle:esm, target:es6 is incorrect, static a = 'b' should be compiled into A.a = 'b', but actually not.

@kitsonk kitsonk added bug Something isn't working public API labels Mar 2, 2021
@yw662
Copy link
Author

yw662 commented Mar 4, 2021

I found a work around for that, which I would call "double emit":

const sources = {
  '/foo.ts': `
        export class A {
            static a = 'b'
        }
        `
}
Deno.emit('/foo.ts', {
  sources,
  bundle: 'esm',
  compilerOptions: { target: 'es6' }
})
  .then(result => result.files['deno:///bundle.js'])
  .then(src =>
    Deno.emit('/src.js', {
      sources: {
        '/src.js': src
      },
      compilerOptions: { target: 'es6' }
    })
  )
  .then(result => result.files['file:///src.js.js'])
  .then(src => console.log(src))

This may not be efficient but can be use as a workaround.

@yw662
Copy link
Author

yw662 commented May 18, 2021

This issue still exists in deno 1.10.2. compilerOptions.target still does not work with bundle:module.

@kitsonk
Copy link
Contributor

kitsonk commented May 19, 2021

That is why the issue isn't closed.

@dgreensp
Copy link

Same with "bundle": "classic", FWIW. I'm grateful for the workaround. I wonder if this renders source maps worthless (I haven't gotten them working yet).

@KnorpelSenf
Copy link
Contributor

The workaround did not work for me (Deno 1.16.3). The transpilation was skipped.

I could fix it by using /src.ts instead of /src.js as the intermediate file name. New workaround:

const source = "/path/or/url/to/mod.ts";
// bundling
const bundled = await Deno.emit(source, { bundle: "module" });
const bundledCode = bundled.files["deno:///bundle.js"];
// transpilation
const transpiled = await Deno.emit("/src.ts", {
    sources: { "/src.ts": bundledCode },
    compilerOptions: { target: "es6" },
});
const transpiledCode = transpiled.files["file:///src.ts.js"];
// write output file
await Deno.writeTextFile("/tmp/bundle.js", out);

@johnspurlock
Copy link

Just ran into this issue on deno 1.18.2

The workaround from @KnorpelSenf worked for me - but it would be excellent if it wasn't needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working public API
Projects
None yet
Development

No branches or pull requests

5 participants