Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-and-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- name: Lint
run: npm run ci:lint
- name: Vulnerabilities
run: npm audit
run: npm audit --audit-level moderate
- name: Optional Dependencies
run: npm run test:package
- name: Generated Code
Expand Down
44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
# rollup changelog

## 4.43.0

_2025-06-11_

### Features

- Provide new `fs` option and `this.fs` API to replace file system (#5944)

### Pull Requests

- [#5944](https://github.com/rollup/rollup/pull/5944): feat(options): Add an option for overriding the file system module in the JS API (@EggDice, @lukastaegert)

## 4.42.0

_2025-06-06_

### Features

- Add option to allow the input to be located in the output in watch mode (#5966)

### Pull Requests

- [#5966](https://github.com/rollup/rollup/pull/5966): feat: watch mode add `allowInputInsideOutputPath` option (@btea, @lukastaegert)

## 4.41.2

_2025-06-06_

### Bug Fixes

- Detect named export usages in dynamic imports with `then` and non-arrow function expressions (#5977)
- Do not replace usages of constant variables with their values for readability (#5968)

### Pull Requests

- [#5968](https://github.com/rollup/rollup/pull/5968): fix: preserve constant identifiers in unary expressions instead of magic numbers (@OmkarJ13, @lukastaegert)
- [#5969](https://github.com/rollup/rollup/pull/5969): chore(deps): update dependency yargs-parser to v22 (@renovate[bot], @lukastaegert)
- [#5970](https://github.com/rollup/rollup/pull/5970): chore(deps): lock file maintenance minor/patch updates (@renovate[bot])
- [#5971](https://github.com/rollup/rollup/pull/5971): chore(deps): lock file maintenance (@renovate[bot])
- [#5976](https://github.com/rollup/rollup/pull/5976): Update README.md (@ftlno, @lukastaegert)
- [#5977](https://github.com/rollup/rollup/pull/5977): fix: consider function expression in thenable when tree-shaking dynamic imports (@TrickyPi)
- [#5981](https://github.com/rollup/rollup/pull/5981): fix(deps): lock file maintenance minor/patch updates (@renovate[bot])
- [#5982](https://github.com/rollup/rollup/pull/5982): Debug/fix watch pipeline (@lukastaegert)

## 4.41.1

_2025-05-24_
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Support this project by becoming a sponsor. Your logo will show up here with a l

## 特别赞助

<a href="https://www.tngtech.com/en/index.html" target="_blank"><img src="https://www.tngtech.com/fileadmin/Public/Images/Logos/TNG_Logo_medium_400x64.svg" alt="TNG Logo" width="280"/></a>
<a href="https://www.tngtech.com/en/index.html" target="_blank"><img src="https://avatars.githubusercontent.com/u/432256?s=200&v=4" alt="TNG Logo"/></a>

TNG 自 2017 年以来一直在支持 [Lukas Taegert-Atkinson](https://github.com/lukastaegert) 在 Rollup 上的工作。

Expand Down
2 changes: 1 addition & 1 deletion browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rollup/browser",
"version": "4.41.1",
"version": "4.43.0",
"description": "Next-generation ES module bundler browser build",
"main": "dist/rollup.browser.js",
"module": "dist/es/rollup.browser.js",
Expand Down
15 changes: 15 additions & 0 deletions browser/src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import type { RollupFsModule } from '../../src/rollup/types';
import { throwNoFileSystem } from './error';
import type * as FsType from './fs.ts';

// Ensure this satisfies the RollupFsModule API, will be removed by tree-shaking
const _typeTest = null as unknown as typeof FsType satisfies RollupFsModule;

export const appendFile = throwNoFileSystem('fs.appendFile');
export const copyFile = throwNoFileSystem('fs.copyFile');
export const mkdir = throwNoFileSystem('fs.mkdir');
export const mkdtemp = throwNoFileSystem('fs.mkdtemp');
export const readdir = throwNoFileSystem('fs.readdir');
export const readFile = throwNoFileSystem('fs.readFile');
export const realpath = throwNoFileSystem('fs.realpath');
export const rename = throwNoFileSystem('fs.rename');
export const rmdir = throwNoFileSystem('fs.rmdir');
export const stat = throwNoFileSystem('fs.stat');
export const lstat = throwNoFileSystem('fs.lstat');
export const unlink = throwNoFileSystem('fs.unlink');
export const writeFile = throwNoFileSystem('fs.writeFile');
32 changes: 0 additions & 32 deletions browser/src/resolveId.ts

This file was deleted.

10 changes: 1 addition & 9 deletions build-plugins/replace-browser-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ import type { Plugin } from 'vite';

const resolve = (path: string) => fileURLToPath(new URL(`../${path}`, import.meta.url));

const JS_REPLACED_MODULES = [
'fs',
'hookActions',
'path',
'performance',
'process',
'resolveId',
'initWasm'
];
const JS_REPLACED_MODULES = ['fs', 'hookActions', 'path', 'performance', 'process', 'initWasm'];

type ModulesMap = [string, string][];

Expand Down
2 changes: 2 additions & 0 deletions cli/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Basic options:
--no-treeshake.unknownGlobalSideEffects Assume unknown globals do not throw
--validate Validate output
--waitForBundleInput Wait for bundle input files
--watch.allowInputInsideOutputPath Whether the input path is allowed to be a
subpath of the output path
--watch.buildDelay <number> Throttle watch rebuilds
--no-watch.clearScreen Do not clear the screen when rebuilding
--watch.exclude <files> Exclude files from being watched
Expand Down
4 changes: 4 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ export default defineConfig({
link: '/es-module-syntax/',
text: 'ES 模块语法'
},
{
link: '/browser/',
text: '在浏览器中运行 Rollup'
},
{
link: '/faqs/',
text: '常见问题'
Expand Down
134 changes: 134 additions & 0 deletions docs/browser/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: 在浏览器中运行 Rollup
---

# {{ $frontmatter.title }}

[[toc]]

## 浏览器构建 {#the-browser-build}

虽然常规的 Rollup 构建依赖于一些 NodeJS 内置库,但也有一个仅使用浏览器 API 的浏览器构建可用。可以通过以下方式安装:

```shell
npm install @rollup/browser
```

在你的脚本中,可以通过以下方式导入:

```js
import { rollup } from '@rollup/browser';
```

或者,你也可以从 CDN 导入,例如对于 ESM 构建:

```js
import * as rollup from 'https://unpkg.com/@rollup/browser/dist/es/rollup.browser.js';
```

对于 UMD 构建:

```html
<script src="https://unpkg.com/@rollup/browser/dist/rollup.browser.js"></script>
```

这将创建一个全局变量 `window.rollup`。请注意,在每种情况下,你需要确保 `@rollup/browser` 包中的 `dist/bindings_wasm_bg.wasm` 文件与浏览器构建一起提供。

由于浏览器构建无法访问文件系统,你需要提供一个 [内存文件系统](#using-an-in-memory-file-system) 通过 [`fs`](../configuration-options/index.md#fs) 选项,或者你需要提供 [插件](#using-plugins-to-resolve-and-load-modules) 来解析和加载所有你想要打包的模块。

## 使用内存文件系统 {#using-an-in-memory-file-system}

Rollup 允许你提供一个内存文件系统实现,该实现需要实现 NodeJS `fs` API 的至少一个子集,参见 [`fs`](../configuration-options/index.md#fs) 选项。这使得浏览器构建的行为非常类似于 NodeJS 构建,甚至允许你使用某些依赖于文件系统的插件,前提是它们只能通过 [`this.fs`](../plugin-development/index.md#this-fs) 插件上下文属性访问它。下面是一个使用 [`memfs`](https://www.npmjs.com/package/memfs) 的示例:

```js twoslash
/** @type {import('rollup')} */
var rollup;
// ---cut---
import { rollup } from '@rollup/browser';
import { Volume } from 'memfs';

const vol = Volume.fromJSON({
'/main.js': "import foo from 'foo.js'; console.log(foo);",
'/foo.js': 'export default 42;'
});

rollup
.rollup({
input: '/main.js',
fs: vol.promises
})
.then(bundle => bundle.generate({ format: 'es' }))
.then(({ output }) => console.log(output[0].code));
```

## 使用插件解析和加载模块 {#using-plugins-to-resolve-and-load-modules}

你也可以通过插件解析和加载所有模块。下面是一个示例:

```js twoslash
/** @type {import('rollup')} */
var rollup;
// ---cut---
const modules = {
'main.js': "import foo from 'foo.js'; console.log(foo);",
'foo.js': 'export default 42;'
};

rollup
.rollup({
input: 'main.js',
plugins: [
{
name: 'loader',
resolveId(source) {
if (modules.hasOwnProperty(source)) {
return source;
}
},
load(id) {
if (modules.hasOwnProperty(id)) {
return modules[id];
}
}
}
]
})
.then(bundle => bundle.generate({ format: 'es' }))
.then(({ output }) => console.log(output[0].code));
```

这个示例只支持两个导入,`"main.js"` 和 `"foo.js"`,并且不支持相对导入。下面是一个使用绝对 URL 作为入口文件并支持相对导入的示例。在这种情况下,我们只是重新打包 Rollup 本身,但它可以用于任何其他暴露 ES 模块的 URL:

```js twoslash
/** @type {import('rollup')} */
var rollup;
// ---cut---
rollup
.rollup({
input: 'https://unpkg.com/rollup/dist/es/rollup.js',
plugins: [
{
name: 'url-resolver',
resolveId(source, importer) {
if (source[0] !== '.') {
try {
new URL(source);
// If it is a valid URL, return it
return source;
} catch {
// Otherwise make it external
return { id: source, external: true };
}
}
return new URL(source, importer).href;
},
async load(id) {
const response = await fetch(id);
return response.text();
}
}
]
})
.then(bundle => bundle.generate({ format: 'es' }))
.then(({ output }) => console.log(output));
```
4 changes: 3 additions & 1 deletion docs/command-line-interface/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ export default {
clearScreen,
exclude,
include,
skipWrite
skipWrite,
allowInputInsideOutputPath
}
};
```
Expand Down Expand Up @@ -459,6 +460,7 @@ export default {
--no-treeshake.unknownGlobalSideEffects 假设未知的全局变量不会抛出异常
--validate 验证输出
--waitForBundleInput 等待打包输入文件
--watch.allowInputInsideOutputPath 输入路径是否允许是输出路径的子路径
--watch.buildDelay <number> 节流观察重建
--no-watch.clearScreen 重建时不要清除屏幕
--watch.exclude <files> 排除要观察的文件
Expand Down
Loading
Loading