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
55 changes: 55 additions & 0 deletions e2e/cases/html/script-loading/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,58 @@ test('should allow to set scriptLoading to "module"', async ({ build }) => {

expect(html).toContain('<script type="module" src="');
});

test('should not affect existing script tags in HTML template', async ({
build,
}) => {
const rsbuild = await build({
config: {
output: {
filenameHash: false,
},
html: {
template: './src/index.html',
scriptLoading: 'module',
},
},
});
const files = rsbuild.getDistFiles();
const html = getFileContent(files, 'index.html');
expect(html).toContain(
'<script async src="https://example.com/foo.js"></script>',
);
expect(html).toContain(
'<script type="module" src="/static/js/index.js"></script>',
);
});

test('should not affect tags generated by `html.tags`', async ({ build }) => {
const rsbuild = await build({
config: {
output: {
filenameHash: false,
},
html: {
scriptLoading: 'module',
tags: [
{
tag: 'script',
attrs: {
src: 'https://example.com/foo.js',
async: true,
},
},
],
},
},
});

const files = rsbuild.getDistFiles();
const html = getFileContent(files, 'index.html');
expect(html).toContain(
'<script src="https://example.com/foo.js" async></script>',
);
expect(html).toContain(
'<script type="module" src="/static/js/index.js"></script>',
);
});
7 changes: 7 additions & 0 deletions e2e/cases/html/script-loading/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!doctype html>
<html>
<head>
<script async src="https://example.com/foo.js"></script>
</head>
<body></body>
</html>
10 changes: 10 additions & 0 deletions website/docs/en/config/html/script-loading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ Specifies how `<script>` tags generated by Rsbuild are loaded.
If [output.module](/config/output/module) is enabled, the value is always `'module'`.
:::

## Note

The `scriptLoading` option only applies to `<script>` tags automatically generated by Rsbuild. It does not affect:

- `<script>` tags that already exist in the HTML template
- `<script>` tags added via [html.tags](/config/html/tags)
- `<script>` tags added via [api.modifyHtmlTags](/plugins/dev/hooks#modifyhtmltags)

## Optional values

### defer

By default, the `<script>` tag generated by Rsbuild will automatically set the [`defer` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script#defer) to avoid blocking the parsing and rendering of the page.
Expand Down
10 changes: 10 additions & 0 deletions website/docs/zh/config/html/script-loading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
如果启用了 [output.module](/config/output/module),该值将始终为 `'module'`。
:::

## 注意

`scriptLoading` 选项仅作用于 Rsbuild 自动生成的 `<script>` 标签,不会影响:

- HTML 模板中已经存在的 `<script>` 标签
- 通过 [html.tags](/config/html/tags) 添加的 `<script>` 标签
- 通过 [api.modifyHtmlTags](/plugins/dev/hooks#modifyhtmltags) 添加的 `<script>` 标签

## 可选值

### defer

默认情况下,Rsbuild 生成的 `<script>` 标签会自动设置 [`defer` 属性](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/script#defer),以避免阻塞页面的解析和渲染。
Expand Down
Loading