Skip to content

Commit f269fd1

Browse files
authored
Merge pull request #184 from rollup/sync-74710745
docs(en): merge rollup/master into rollup-docs-cn/master @ 7471074
2 parents b419a5a + 5ced651 commit f269fd1

File tree

13 files changed

+142
-36
lines changed

13 files changed

+142
-36
lines changed

.github/workflows/build-and-tests.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,14 @@ jobs:
120120
cross: zig
121121
- host: ubuntu-latest
122122
target: armv7-linux-androideabi
123-
cross: zig
123+
cross: napi
124+
# There are compile issues when using the cache with Android
125+
cache-cargo: false
124126
- host: ubuntu-latest
125127
target: aarch64-linux-android
126-
cross: zig
128+
cross: napi
129+
# There are compile issues when using the cache with Android
130+
cache-cargo: false
127131
- host: ubuntu-latest
128132
target: riscv64gc-unknown-linux-gnu
129133
setup: |
@@ -210,6 +214,7 @@ jobs:
210214
uses: openharmony-rs/setup-ohos-sdk@52d50de65363f895558a43de0dceb1f8e3679b1c # v0.2.3
211215
- name: Restore Cargo cache
212216
uses: actions/cache/restore@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
217+
if: matrix.settings.cache-cargo != false
213218
with:
214219
path: |
215220
~/.cargo/registry/index/
@@ -258,7 +263,7 @@ jobs:
258263
if: ${{ !matrix.settings.docker && !matrix.settings.build }}
259264
shell: bash
260265
- name: Save Cargo cache
261-
if: github.ref == 'refs/heads/master'
266+
if: github.ref == 'refs/heads/master' && matrix.settings.cache-cargo != false
262267
uses: actions/cache/save@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
263268
with:
264269
path: |

docs/plugin-development/index.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ interface EmittedAsset {
14271427

14281428
当生成代码块或者资源文件时,可以提供 `name` 或者 `fileName`。如果提供了 `fileName`,它将不会被修改,而是直接作为生成文件的名称,如果这样会导致冲突,则会抛出错误。否则,如果提供了 `name`,它将被用作对应的 [`output.chunkFileNames`](../configuration-options/index.md#output-chunkfilenames) 或者 [`output.assetFileNames`](../configuration-options/index.md#output-assetfilenames) 模式中的 `[name]` 的替换,可能会在文件名的末尾添加一个唯一的数字,以避免冲突。如果既没有提供 `name` 也没有提供 `fileName`,则会使用默认名称。预构建的代码块必须始终有一个 `fileName`
14291429

1430-
你可以通过 `import.meta.ROLLUP_FILE_URL_referenceId` 在任何由 [`load`](#load) 或 [`transform`](#transform) 插件钩子返回的代码中引用生成的文件的 URL。有关更多详细信息和示例,请参见 [File URL](#file-urls)。
1430+
你可以通过 `import.meta.ROLLUP_FILE_URL_referenceId`(返回一个字符串)或 `import.meta.ROLLUP_FILE_URL_OBJ_referenceId`(返回一个 URL 对象),在任何由 [`load`](#load) 或 [`transform`](#transform) 插件钩子返回的代码中引用生成的文件的 URL。有关更多详细信息和示例,请参见 [File URL](#file-urls)。
14311431

14321432
替换 `import.meta.ROLLUP_FILE_URL_referenceId` 的生成代码可以通过 [`resolveFileUrl`](#resolvefileurl) 插件钩子进行自定义。你也可以使用 [`this.getFileName(referenceId)`](#this-getfilename) 在文件名可用时确定文件名。如果没有显式设置文件名,则
14331433

@@ -2123,6 +2123,17 @@ export const size = 6;
21232123

21242124
如果你构建上诉代码,则主块和工作块都将通过共享块从 `config.js` 共享代码。这使我们能够利用浏览器缓存来减少传输数据并加快加载工作块的速度。
21252125

2126+
你也可以使用 `import.meta.ROLLUP_FILE_URL_OBJ_referenceId` 直接获取一个 URL 对象,而不是一个字符串。当你需要 URL 对象本身时,这会更高效,因为它避免了重复创建对象:
2127+
2128+
```js
2129+
// 使用 ROLLUP_FILE_URL(返回字符串,需要通过 new URL() 包装使用)
2130+
const urlString = import.meta.ROLLUP_FILE_URL_referenceId;
2131+
const urlObject = new URL(urlString);
2132+
2133+
// 使用 ROLLUP_FILE_URL_OBJ(直接返回 URL 对象)
2134+
const urlObject = import.meta.ROLLUP_FILE_URL_OBJ_referenceId;
2135+
```
2136+
21262137
## 转换器 {#Transformers}
21272138

21282139
转换器插件(即返回 `transform` 函数以进行例如转换非 JS 文件的转换器)应支持 `options.include``options.exclude`,两者都可以是 minimatch 模式或 minimatch 模式数组。如果省略 `options.include` 或其长度为零,则默认情况下应包括文件;否则,只有 ID 与其中一个模式匹配时才应包括它们。

src/ast/nodes/MetaProperty.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type * as NodeType from './NodeType';
1616
import { NodeBase } from './shared/Node';
1717

1818
const FILE_PREFIX = 'ROLLUP_FILE_URL_';
19+
const FILE_OBJ_PREFIX = 'ROLLUP_FILE_URL_OBJ_';
1920
const IMPORT = 'import';
2021

2122
export default class MetaProperty extends NodeBase {
@@ -32,8 +33,12 @@ export default class MetaProperty extends NodeBase {
3233
meta: { name },
3334
metaProperty
3435
} = this;
35-
if (name === IMPORT && metaProperty?.startsWith(FILE_PREFIX)) {
36-
return outputPluginDriver.getFileName(metaProperty.slice(FILE_PREFIX.length));
36+
if (name === IMPORT) {
37+
if (metaProperty?.startsWith(FILE_OBJ_PREFIX)) {
38+
return outputPluginDriver.getFileName(metaProperty.slice(FILE_OBJ_PREFIX.length));
39+
} else if (metaProperty?.startsWith(FILE_PREFIX)) {
40+
return outputPluginDriver.getFileName(metaProperty.slice(FILE_PREFIX.length));
41+
}
3742
}
3843
return null;
3944
}
@@ -59,7 +64,9 @@ export default class MetaProperty extends NodeBase {
5964
parent instanceof MemberExpression && typeof parent.propertyKey === 'string'
6065
? parent.propertyKey
6166
: null);
62-
if (metaProperty?.startsWith(FILE_PREFIX)) {
67+
if (metaProperty?.startsWith(FILE_OBJ_PREFIX)) {
68+
this.referenceId = metaProperty.slice(FILE_OBJ_PREFIX.length);
69+
} else if (metaProperty?.startsWith(FILE_PREFIX)) {
6370
this.referenceId = metaProperty.slice(FILE_PREFIX.length);
6471
}
6572
}
@@ -87,10 +94,11 @@ export default class MetaProperty extends NodeBase {
8794
if (referenceId) {
8895
const fileName = pluginDriver.getFileName(referenceId);
8996
const relativePath = normalize(relative(dirname(chunkId), fileName));
97+
const isUrlObject = !!metaProperty?.startsWith(FILE_OBJ_PREFIX);
9098
const replacement =
9199
pluginDriver.hookFirstSync('resolveFileUrl', [
92100
{ chunkId, fileName, format, moduleId, referenceId, relativePath }
93-
]) || relativeUrlMechanisms[format](relativePath);
101+
]) || relativeUrlMechanisms[format](relativePath, isUrlObject);
94102

95103
code.overwrite(
96104
(parent as MemberExpression).start,
@@ -126,7 +134,9 @@ export default class MetaProperty extends NodeBase {
126134
): void {
127135
this.preliminaryChunkId = preliminaryChunkId;
128136
const accessedGlobals = (
129-
this.metaProperty?.startsWith(FILE_PREFIX) ? accessedFileUrlGlobals : accessedMetaUrlGlobals
137+
this.metaProperty?.startsWith(FILE_PREFIX) || this.metaProperty?.startsWith(FILE_OBJ_PREFIX)
138+
? accessedFileUrlGlobals
139+
: accessedMetaUrlGlobals
130140
)[format];
131141
if (accessedGlobals.length > 0) {
132142
this.scope.addAccessedGlobals(accessedGlobals, accessedGlobalsByScope);
@@ -154,13 +164,15 @@ const accessedFileUrlGlobals = {
154164
umd: ['document', 'require', 'URL']
155165
};
156166

157-
const getResolveUrl = (path: string, URL = 'URL') => `new ${URL}(${path}).href`;
167+
const getResolveUrl = (path: string, asObject: boolean, URL = 'URL') =>
168+
`new ${URL}(${path})${asObject ? '' : '.href'}`;
158169

159-
const getRelativeUrlFromDocument = (relativePath: string, umd = false) =>
170+
const getRelativeUrlFromDocument = (relativePath: string, asObject: boolean, umd = false) =>
160171
getResolveUrl(
161172
`'${escapeId(relativePath)}', ${
162173
umd ? `typeof document === 'undefined' ? location.href : ` : ''
163-
}document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI`
174+
}document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI`,
175+
asObject
164176
);
165177

166178
const getGenericImportMetaMechanism =
@@ -174,10 +186,11 @@ const getGenericImportMetaMechanism =
174186
: 'undefined';
175187
};
176188

177-
const getFileUrlFromFullPath = (path: string) => `require('u' + 'rl').pathToFileURL(${path}).href`;
189+
const getFileUrlFromFullPath = (path: string, asObject: boolean) =>
190+
`require('u' + 'rl').pathToFileURL(${path})${asObject ? '' : '.href'}`;
178191

179-
const getFileUrlFromRelativePath = (path: string) =>
180-
getFileUrlFromFullPath(`__dirname + '/${escapeId(path)}'`);
192+
const getFileUrlFromRelativePath = (path: string, asObject: boolean) =>
193+
getFileUrlFromFullPath(`__dirname + '/${escapeId(path)}'`, asObject);
181194

182195
const getUrlFromDocument = (chunkId: string, umd = false) =>
183196
`${
@@ -186,42 +199,39 @@ const getUrlFromDocument = (chunkId: string, umd = false) =>
186199
chunkId
187200
)}', document.baseURI).href)`;
188201

189-
const relativeUrlMechanisms: Record<InternalModuleFormat, (relativePath: string) => string> = {
190-
amd: relativePath => {
202+
const relativeUrlMechanisms: Record<
203+
InternalModuleFormat,
204+
(relativePath: string, asObject: boolean) => string
205+
> = {
206+
amd: (relativePath, asObject: boolean) => {
191207
if (relativePath[0] !== '.') relativePath = './' + relativePath;
192-
return getResolveUrl(`require.toUrl('${escapeId(relativePath)}'), document.baseURI`);
208+
return getResolveUrl(`require.toUrl('${escapeId(relativePath)}'), document.baseURI`, asObject);
193209
},
194-
cjs: relativePath =>
195-
`(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(
196-
relativePath
197-
)} : ${getRelativeUrlFromDocument(relativePath)})`,
198-
es: relativePath => getResolveUrl(`'${escapeId(relativePath)}', import.meta.url`),
199-
iife: relativePath => getRelativeUrlFromDocument(relativePath),
200-
system: relativePath => getResolveUrl(`'${escapeId(relativePath)}', module.meta.url`),
201-
umd: relativePath =>
202-
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(
203-
relativePath
204-
)} : ${getRelativeUrlFromDocument(relativePath, true)})`
210+
cjs: (relativePath, asObject: boolean) =>
211+
`(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(relativePath, asObject)} : ${getRelativeUrlFromDocument(relativePath, asObject)})`,
212+
es: (relativePath, asObject: boolean) =>
213+
getResolveUrl(`'${escapeId(relativePath)}', import.meta.url`, asObject),
214+
iife: (relativePath, asObject: boolean) => getRelativeUrlFromDocument(relativePath, asObject),
215+
system: (relativePath, asObject: boolean) =>
216+
getResolveUrl(`'${escapeId(relativePath)}', module.meta.url`, asObject),
217+
umd: (relativePath, asObject: boolean) =>
218+
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(relativePath, asObject)} : ${getRelativeUrlFromDocument(relativePath, asObject, true)})`
205219
};
206220

207221
const importMetaMechanisms: Record<
208222
string,
209223
(property: string | null, options: { chunkId: string; snippets: GenerateCodeSnippets }) => string
210224
> = {
211-
amd: getGenericImportMetaMechanism(() => getResolveUrl(`module.uri, document.baseURI`)),
225+
amd: getGenericImportMetaMechanism(() => getResolveUrl(`module.uri, document.baseURI`, false)),
212226
cjs: getGenericImportMetaMechanism(
213227
chunkId =>
214-
`(typeof document === 'undefined' ? ${getFileUrlFromFullPath(
215-
'__filename'
216-
)} : ${getUrlFromDocument(chunkId)})`
228+
`(typeof document === 'undefined' ? ${getFileUrlFromFullPath('__filename', false)} : ${getUrlFromDocument(chunkId)})`
217229
),
218230
iife: getGenericImportMetaMechanism(chunkId => getUrlFromDocument(chunkId)),
219231
system: (property, { snippets: { getPropertyAccess } }) =>
220232
property === null ? `module.meta` : `module.meta${getPropertyAccess(property)}`,
221233
umd: getGenericImportMetaMechanism(
222234
chunkId =>
223-
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromFullPath(
224-
'__filename'
225-
)} : ${getUrlFromDocument(chunkId, true)})`
235+
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromFullPath('__filename', false)} : ${getUrlFromDocument(chunkId, true)})`
226236
)
227237
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = defineTest({
2+
description: 'allows to use ROLLUP_FILE_URL_OBJ to get URL objects directly',
3+
options: {
4+
plugins: [
5+
{
6+
resolveId(id) {
7+
if (id === 'url-test') {
8+
return id;
9+
}
10+
},
11+
load(id) {
12+
if (id === 'url-test') {
13+
const assetId = this.emitFile({
14+
type: 'asset',
15+
name: 'test.txt',
16+
source: 'test content'
17+
});
18+
return `
19+
// Test string URL replacement
20+
export const assetString = import.meta.ROLLUP_FILE_URL_${assetId};
21+
// Test URL object replacement
22+
export const assetObject = import.meta.ROLLUP_FILE_URL_OBJ_${assetId};
23+
`;
24+
}
25+
}
26+
}
27+
]
28+
}
29+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test content
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
define(['require'], (function (require) { 'use strict';
2+
3+
// Test string URL replacement
4+
const assetString = new URL(require.toUrl('./assets/test-r6af3lUh.txt'), document.baseURI).href;
5+
// Test URL object replacement
6+
const assetObject = new URL(require.toUrl('./assets/test-r6af3lUh.txt'), document.baseURI);
7+
8+
console.log('String URL:', assetString);
9+
console.log('URL Object:', assetObject);
10+
11+
}));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test content
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
// Test string URL replacement
4+
const assetString = (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__dirname + '/assets/test-r6af3lUh.txt').href : new URL('assets/test-r6af3lUh.txt', document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI).href);
5+
// Test URL object replacement
6+
const assetObject = (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__dirname + '/assets/test-r6af3lUh.txt') : new URL('assets/test-r6af3lUh.txt', document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI));
7+
8+
console.log('String URL:', assetString);
9+
console.log('URL Object:', assetObject);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test content
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test string URL replacement
2+
const assetString = new URL('assets/test-r6af3lUh.txt', import.meta.url).href;
3+
// Test URL object replacement
4+
const assetObject = new URL('assets/test-r6af3lUh.txt', import.meta.url);
5+
6+
console.log('String URL:', assetString);
7+
console.log('URL Object:', assetObject);

0 commit comments

Comments
 (0)