deno_emit icon indicating copy to clipboard operation
deno_emit copied to clipboard

Deno.emit tree shaking issues

Open jsejcksn opened this issue 2 years ago • 13 comments

I'll provide an example repo, attached as a zip file and inline files here for examination:

bundle-example.zip

Files

example-1.ts:

import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";

assert(true);

example-2-asserts.ts:

export class AssertionError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "AssertionError";
  }
}

/** Make an assertion, error will be thrown if `expr` does not have truthy value. */
export function assert(expr: unknown, msg = ""): asserts expr {
  if (!expr) {
    throw new AssertionError(msg);
  }
}

example-2.ts:

import { assert } from "./example-2-asserts.ts";

assert(true);

main.ts:

import { assertStrictEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

async function bundleModule(filePath: string): Promise<string> {
  const emitResult = await Deno.emit(filePath, { bundle: "module" });
  return emitResult.files["deno:///bundle.js"];
}

const filePaths = ["example-1.ts", "example-2.ts"];

const bundles = await Promise.all(
  filePaths.map((filePath) => bundleModule(filePath)),
);

try {
  assertStrictEquals(...bundles as [string, string]);
  console.log("Bundles are identical");
} catch (ex) {
  console.error(ex.message);
}

run:

deno run --unstable --allow-net=deno.land --allow-read=. main.ts

I created two modules which import assert and invoke it:

  • one which imports assert from https://deno.land/[email protected]/testing/asserts.ts
  • one which imports from a local module where I manually copied from the source: only the assert function (and the AssertionError class on which it depends)

I then bundled them both using Deno.emit and compared the bundles with assertStrictEquals:

bundle-example % deno --version
deno 1.12.0 (release, x86_64-apple-darwin)
v8 9.2.230.14
typescript 4.3.2

bundle-example % ./run 
Values are not strictly equal:


    [Diff] Actual / Expected


-   const ANSI_PATTERN = new RegExp([
-       "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
-       "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))", 
-   ].join("|"), "g");
-   var DiffType;
-   (function(DiffType1) {
-       DiffType1["removed"] = "removed";
-       DiffType1["common"] = "common";
-       DiffType1["added"] = "added";
-   })(DiffType || (DiffType = {
-   }));
    class AssertionError extends Error {
        constructor(message){
            super(message);
            this.name = "AssertionError";
        }
    }
    function assert(expr, msg = "") {
        if (!expr) {
            throw new AssertionError(msg);
        }
    }
    assert(true);

The one using the remote import results in an unrelated regex and string enum being emitted.

jsejcksn avatar Jul 13 '21 18:07 jsejcksn

@kdy1 Any ideas?

jsejcksn avatar Jul 16 '21 08:07 jsejcksn

No. I don't have any idea as I don't know what Deno.emit does.

kdy1 avatar Jul 16 '21 08:07 kdy1

No. I don't have any idea as I don't know what Deno.emit does.

@kdy1 I forgot to say that the result is the same for deno bundle when I mentioned you.

jsejcksn avatar Jul 16 '21 11:07 jsejcksn

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 14 '21 15:09 stale[bot]

Not stale. Still waiting for engagement by others.

jsejcksn avatar Sep 14 '21 17:09 jsejcksn

@bartlomieju Can you reopen this?

jsejcksn avatar Sep 21 '21 19:09 jsejcksn

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 21 '21 00:11 stale[bot]

This issue still persists in Deno v1.16.2:

$ deno --version
deno 1.16.2 (release, x86_64-unknown-linux-gnu)
v8 9.7.106.2
typescript 4.4.2

$ ./run
Values are not strictly equal:


    [Diff] Actual / Expected


-   const { Deno  } = globalThis;
-   typeof Deno?.noColor === "boolean" ? Deno.noColor : true;
-   new RegExp([
-       "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
-       "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
-   ].join("|"), "g");
-   var DiffType;
-   (function(DiffType) {
-       DiffType["removed"] = "removed";
-       DiffType["common"] = "common";
-       DiffType["added"] = "added";
-   })(DiffType || (DiffType = {
-   }));
    class AssertionError extends Error {
        constructor(message){
            super(message);
            this.name = "AssertionError";
        }
    }
    function assert(expr, msg = "") {
        if (!expr) {
            throw new AssertionError(msg);
        }
    }
    assert(true);

jsejcksn avatar Nov 21 '21 04:11 jsejcksn

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 20 '22 05:01 stale[bot]

This issue still persists in Deno v1.17.3:

% deno --version
deno 1.17.3 (release, x86_64-apple-darwin)
v8 9.7.106.15
typescript 4.5.2

% ./run         
Values are not strictly equal:


    [Diff] Actual / Expected


-   const { Deno  } = globalThis;
-   typeof Deno?.noColor === "boolean" ? Deno.noColor : true;
-   new RegExp([
-       "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
-       "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))", 
-   ].join("|"), "g");
-   var DiffType;
-   (function(DiffType1) {
-       DiffType1["removed"] = "removed";
-       DiffType1["common"] = "common";
-       DiffType1["added"] = "added";
-   })(DiffType || (DiffType = {}));
    class AssertionError extends Error {
        constructor(message){
            super(message);
            this.name = "AssertionError";
        }
    }
    function assert(expr, msg = "") {
        if (!expr) {
            throw new AssertionError(msg);
        }
    }
    assert(true);

jsejcksn avatar Jan 20 '22 05:01 jsejcksn

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 21 '22 15:03 stale[bot]

The issue persists in Deno v1.20.1:
% deno --version
deno 1.20.1 (release, x86_64-apple-darwin)
v8 10.0.139.6
typescript 4.6.2

% ./run         
Values are not strictly equal:


    [Diff] Actual / Expected


    // deno-fmt-ignore-file
    // deno-lint-ignore-file
    // This code was bundled using `deno bundle` and it's not recommended to edit it manually
    
-   const { Deno  } = globalThis;
-   typeof Deno?.noColor === "boolean" ? Deno.noColor : true;
-   new RegExp([
-       "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
-       "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))", 
-   ].join("|"), "g");
-   var DiffType;
-   (function(DiffType1) {
-       DiffType1["removed"] = "removed";
-       DiffType1["common"] = "common";
-       DiffType1["added"] = "added";
-   })(DiffType || (DiffType = {}));
    class AssertionError extends Error {
        constructor(message){
            super(message);
            this.name = "AssertionError";
        }
    }
    function assert(expr, msg = "") {
        if (!expr) {
            throw new AssertionError(msg);
        }
    }
    assert(true);

jsejcksn avatar Mar 21 '22 16:03 jsejcksn

This issue should be migrated to https://github.com/denoland/deno_emit

jsejcksn avatar May 20 '22 22:05 jsejcksn