原文链接: node ncc 打包

上一篇: nodejs 下载 bilibili和youtube的视频

下一篇: quickjs windows安装

https://github.com/zeit/ncc#readme

gcc的node版, 由webpack和rullup两位作者开发

用于js和ts的打包,  零配置, 不过还是需要tsonfig.json

想使用import的形式引入node模块, 需要安装 @types/node

简单版

{"compilerOptions": {"target": "es6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */"module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */"outDir": "./lib",                        /* Redirect output structure to the directory. */"rootDir": "./src",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */"strict": true,                           /* Enable all strict type-checking options. */"noImplicitAny": false,                 /* Raise error on expressions and declarations with an implied 'any' type. */"esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */},"exclude": ["node_modules", "**/*.test.ts"]
}

支持 ?. !.等高级语法

let obj = {a:{b:{c:'d'}}
}console.log(obj?.a)
console.log(obj?.a!.b)
console.log(obj?.a?.b!)

自己用js实现打包,

const ncc = require('@zeit/ncc');
const fs = require('fs')
function build() {ncc('../src/index.ts', {// provide a custom cache path or disable cachingcache: './custom/cache/path' | false,// externals to leave as requires of the buildexternals: ['externalpackage'],// directory outside of which never to emit assetsfilterAssetBase: process.cwd(), // defaultminify: false, // defaultsourceMap: false, // defaultsourceMapBasePrefix: '../', // default treats sources as output-relative// when outputting a sourcemap, automatically include// source-map-support in the output file (increases output by 32kB).sourceMapRegister: true, // defaultwatch: false, // defaultv8cache: false, // defaultquiet: false, // defaultdebugLog: false, // default}).then(({code, map, assets}) => {// 将输出代码写入文件并执行console.log(code);fs.writeFileSync('./t.js',code,'utf8')// Assets is an object of asset file names to { source, permissions, symlinks }// expected relative to the output code (if any)});
}build()

每次改变代码都会自动编译后执行, 可以加入节流

const ncc = require('@zeit/ncc');
const fs = require('fs');
function build() {return ncc('./src/index.ts', {// provide a custom cache path or disable cachingcache: './custom/cache/path' | false,// externals to leave as requires of the buildexternals: ['externalpackage'],// directory outside of which never to emit assetsfilterAssetBase: process.cwd(), // defaultminify: false, // defaultsourceMap: false, // defaultsourceMapBasePrefix: '../', // default treats sources as output-relative// when outputting a sourcemap, automatically include// source-map-support in the output file (increases output by 32kB).sourceMapRegister: true, // defaultwatch: true, // defaultv8cache: false, // defaultquiet: false, // defaultdebugLog: false, // default});
}let res = build();
console.log(res);
res.handler(({code}) => {console.log('handler');eval(code);
});res.rebuild(() => {console.log('rebuild');
});// 关闭 watch
// res.close();

导入json

{"compilerOptions": {"target": "es6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */"module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */"outDir": "./lib",                        /* Redirect output structure to the directory. */"rootDir": "./src",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */"strict": true,                           /* Enable all strict type-checking options. */"noImplicitAny": false,                 /* Raise error on expressions and declarations with an implied 'any' type. */"esModuleInterop": true,                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */"resolveJsonModule": true  // 使用import  导入 json},"exclude": ["node_modules", "**/*.test.ts"]
}

node ncc 打包相关推荐

  1. node pkg 打包一个为一个可执行程序(linux、windows、mac)

    概要:如果将node项目打包为一个可执行的文件,那么对于部署.安装来说,以及源码的保护性都有很多的好处 1.全局安装pkg库,也可以不局部安装,全局安装为了方便(npm install pkg -g) ...

  2. node 项目打包工具ncc

    ncc是一个简单的打包工具,可以把一个node项目,打包成单个的js文件. 安装 npm i -g @zeit/ncc 使用 1.cd到工程目录下,就是你的项目目录,比如说app.js或者说 inde ...

  3. electron 打包把node代理服务打包进去_专题:让C++给node做技术加持(三)编译electron本地模块踩坑记

    欢迎来到我的专题文章:<让C++给node做技术加持>系列.更多内容,持续更新中,欢迎关注. 往期目录: 专题:让C++给node做技术加持(一)环境搭建,项目运行 专题:让C++给nod ...

  4. node.js打包环境部署CentOS7.4

    公司新项目用的nodejs进行打包,为了对接jenkins,需要在linux下搭建node环境,做个记录 1.环境说明 因为这里要用14的版本,所以用的是centos7,主要是6的系统下一直报libs ...

  5. node项目打包如何隐藏html后缀,详解webpack打包nodejs项目(前端代码)

    随着前端体积越来越大,功能越来越丰富,这时候就需要将前端工程化,而webpack就是用于将前端各种文件打包起来.一个简单的webpack应该包含以下几个概念 入口起点 配置 组件 加载器 模块 模块热 ...

  6. node app 打包工具 pkg

    node 打包可执行文件的工具有很多.node-packer 是国人写的,但一年前就停止更新了.而 nexe 一到fetching prebuilt binary 就报错,放弃了,最终选择了 pkg. ...

  7. 使用Node.js打包前端

    1.下载并安装node.js 下载地址 2.准备配置文件 在demo-html目录下,创建一个名为.editorconfig的文件,内容如下: root = true[*] charset = utf ...

  8. NPM node vue打包笔记

    1.设置淘宝的镜像 npm config set registry https://registry.npmmirror.com 2.查看是否配置成功 npm config get registry ...

  9. electron 打包把node代理服务打包进去_用 Node.js 官方镜像打包一个 express 服务

    最近感觉确实有必要了解一下 Docker,在 CI/CD 这个阶段很有用,所以记录一下. 先放上 Node.js 的官方 Docker 镜像. Docker Hub​hub.docker.com 需要 ...

最新文章

  1. MQTT 协议 Client ID 长度不能超过23个字符
  2. 不盲目依赖人工智能,海信帮欧尚开了近 300 家无人便利店
  3. 【LVS】负载均衡集群
  4. IAAS、SAAS、PAAS
  5. 纪念9.11十周年 奥巴马诵读圣经原文
  6. 经典C语言程序100例之二
  7. 如何利用数据分析买到好房子?
  8. 具有Azure功能的无服务器API
  9. vscode的背景图设置 简单版
  10. DongLiORM 介绍
  11. GPL协议、LGPL协议与BSD协议的法律区别
  12. I/O多路复用技术(select/poll/epoll)
  13. android实现标题栏弹框,Android:Dialog对话框、Builder、showDialog、模板方法设计模式...
  14. Linux之常用操作命令总结一
  15. centos转换linux格式,CentOS 下转换网易云音乐ncm格式为mp3
  16. 2021-09-27 win10 IPV6连接:无网络访问权限 怎么解决
  17. 解决pychram:卡在Updating Python Interpreter
  18. 安卓各版本大变化(Android 6.0到10.0),兼容适配
  19. pwm超详细解读,大佬细说pwm的控制方式
  20. 微信公众平台js算法逆向

热门文章

  1. 构建信用卡反欺诈预测模型——机器学习
  2. 对 oc 学习的 阶段反思
  3. 毕业论文word排版技巧
  4. 一如昨日的时光,一如昨日的酒--
  5. 平安居家养老服务上市
  6. ReactHooks--踩坑1 :React Hook xx is called in function xx which is neither a React function component
  7. 【434.a + b】
  8. 汇编语言——伪指令详解
  9. 传统语音识别介绍【一】—— 前端处理
  10. 白盒测试概述及其方法简介