跳到主要内容
CNode

rollup 打包

分享
Ggocpplua发布于 5 年前最后回复 5 年前
1053530

一、rollup 概念

Rollup 是一个 JavaScript 模块打包器,可以将小块代码编译成大块复杂的代码,例如 library 或应用程序。

二、安装

npm install --global rollup

三、简单实例(对于NodeJS):

方式1
  1. 创建 main.ts
console.log('rollup')
  1. 打包
# compile to a CommonJS module ('cjs')
$ rollup main.ts --file main.js --format cjs
  1. 运行
node main.js
方式2
  1. 创建 main.ts
console.log('rollup')
  1. 创建rollup.config.js
// rollup.config.js
export default {
    input: 'main.ts',
    output: {
      file: 'main.js',
      format: 'cjs'
    }
  };
  1. 打包
rollup -c

备注:

  • 如果出错:Unexpected token (Note that you need plugins to import files that are not JavaScript)
  1. npm install rollup-plugin-typescript2
  2. rollup.config.js
import typescript from "rollup-plugin-typescript2";
export default {
    input: 'xrark.ts',
    output: {
      file: 'main.js',
      format: 'cjs'
    },
    plugins: [typescript()],
  };
查看回复

回复 (10)

G
gocpplua#105 年前
引用 InCodingNowLiu建议webpack吧,主流

@InCodingNowLiu webpack比较慢。 我现在是开发nodejs(typescript)服务器,需要打包成一个可执行文件。

G
gocpplua#95 年前
引用 niexqesbuild 也香

@niexq 我看确实速度可以。 赞

I
InCodingNowLiu#85 年前

建议webpack吧,主流

N
niexq#75 年前

WechatIMG955.png esbuild 也香

G
gocpplua#65 年前
引用 luojinxu520浪费了我一分钟

@luojinxu520 刚刚开始学习,见谅了

G
gocpplua#55 年前
引用 nomagick别用这些乱七八糟的垃圾来打包node.js

@nomagick 你是使用什么的?

L
luojinxu520#35 年前

浪费了我一分钟

N
nomagick#25 年前

别用这些乱七八糟的垃圾来打包node.js

I
i5ting#15 年前

多补充些文字,你会发现更好玩

参与回复

登录后即可参与回复。登录