deno 打包可执行文件 vs golang 编译可执行文件

1,956 阅读2分钟

deno在v1.6.0中添加脚本打包一个可执行二进制文件特性,下面尝试该功能

1、安装deno、确保deno版本1.6.0以上

2、在cmd中执行deno -h,打,打印出下列信息表示安装成功

PS E:\github-repo> deno
Deno 1.6.1
exit using ctrl+d or close()
>     

3、在cmd中执行deno -h,打印帮助信息,有一条compile子命令

PS E:\github-repo> deno -h                                                                                              deno 1.6.1
A secure JavaScript and TypeScript runtime

Docs: https://deno.land/manual
Modules: https://deno.land/std/ https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues

To start the REPL:
  deno

To execute a script:
  deno run https://deno.land/std/examples/welcome.ts

To evaluate code in the shell:
  deno eval "console.log(30933 + 404)"

USAGE:
    deno [OPTIONS] [SUBCOMMAND]

OPTIONS:
    -h, --help                     Prints help information
    -L, --log-level <log-level>    Set log level [possible values: debug, info]
    -q, --quiet                    Suppress diagnostic output
        --unstable                 Enable unstable features and APIs
    -V, --version                  Prints version information

SUBCOMMANDS:
    bundle         Bundle module and dependencies into single file
    cache          Cache the dependencies
    compile        Compile the script into a self contained executable
    completions    Generate shell completions
    doc            Show documentation for a module
    eval           Eval script
    fmt            Format source files
    help           Prints this message or the help of the given subcommand(s)
    info           Show info about cache or info related to source file
    install        Install script as an executable
    lint           Lint source files
    lsp            Start the language server
    repl           Read Eval Print Loop
    run            Run a program given a filename or url to the module. Use '-' as a filename to read from stdin.
    test           Run tests
    types          Print runtime TypeScript declarations
    upgrade        Upgrade deno executable to given version

ENVIRONMENT VARIABLES:
    DENO_DIR             Set the cache directory
    DENO_INSTALL_ROOT    Set deno install's output directory
                         (defaults to $HOME/.deno/bin)
    DENO_CERT            Load certificate authority from PEM encoded file
    NO_COLOR             Set to disable color
    HTTP_PROXY           Proxy address for HTTP requests
                         (module downloads, fetch)
    HTTPS_PROXY          Proxy address for HTTPS requests
                         (module downloads, fetch)
    NO_PROXY             Comma-separated list of hosts which do not use a proxy
                         (module downloads, fetch)
PS E:\github-repo>

3、在deno-study文件夹新建deno-compile文件夹存放测试代码,查看源码deno-study,目录结构如下

deno-study
  -- deno-compile
     -- de.ts
	 -- index.ts
     -- deno-compile.exe // deno compile index.ts --unstable 得到文件
// de.ts
const name = "chenzhen";
export default name;
// index.ts, 注意保持代码一值在执行,否则打开可执行文件,会立马关闭弹窗
import name from "./de.ts"
while(true){
    console.log(name)
}

4、cmd到deno-compile目录下, 执行 deno compile index.ts --unstable 生成deno-compile.exe

PS E:\github-repo\deno-study\deno-compile> deno compile index.ts --unstable
Check file:///E:/github-repo/deno-study/deno-compile/index.ts
Bundle file:///E:/github-repo/deno-study/deno-compile/index.ts
Compile file:///E:/github-repo/deno-study/deno-compile/index.ts
Emit deno-compile
PS E:\github-repo\deno-study\deno-compile> 

5、点击deno-compile.exe自动打开cmd程序窗口,显示如下信息

6、我们来看下deno-compile.exe文件信息

7、我在来看下基本相同的代码在go下编译成build.exe文件信息,查看源码go-study

8、从上面可以看出编译后的产物大小相差很多,deno为31.4MB,go为2.02MB,deno依赖运行时比较go要大得多