Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(plugin): test html plugin with basic cases of html-webpack-plugin #7571

Merged
merged 7 commits into from
Aug 14, 2024
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ tests/webpack-test/**/dev-defaults.webpack.lock
tests/plugin-test/css-extract/js
!tests/plugin-test/css-extract/**/node_modules

tests/plugin-test/html-plugin/js

/webpack-examples/**/dist

smoke-example
Expand Down
3 changes: 1 addition & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ packages/**/etc/**/*
packages/rspack-test-tools/template/**/*
packages/rspack-test-tools/src/helper/legacy/**/*
packages/rspack-test-tools/tests/**/*
packages/rspack-plugin-mini-css-extract/test/cases/**/*

crates/**/*
target/**/*
tests/**/*.*
tests/**/*
!tests/**/test.filter.js
1 change: 0 additions & 1 deletion biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"packages/rspack-test-tools/template/**/*",
"packages/rspack-test-tools/tests/**/*",
"packages/rspack-test-tools/src/helper/legacy/**/*",
"packages/rspack-plugin-mini-css-extract/test/cases/**/*",
"packages/playground/**/*",
// --- ignore runtime code in browser
"packages/rspack/hot",
Expand Down
10 changes: 6 additions & 4 deletions crates/rspack_plugin_html/src/visitors/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ impl HTMLPluginTag {
append_to: HtmlInject,
script_loading: &HtmlScriptLoading,
) -> HTMLPluginTag {
let mut attributes = vec![HtmlPluginAttribute {
attr_name: "src".to_string(),
attr_value: Some(src.to_string()),
}];
let mut attributes = vec![];
match script_loading {
HtmlScriptLoading::Defer => {
attributes.push(HtmlPluginAttribute {
Expand All @@ -65,6 +62,11 @@ impl HTMLPluginTag {
_ => {}
}

attributes.push(HtmlPluginAttribute {
attr_name: "src".to_string(),
attr_value: Some(src.to_string()),
});

HTMLPluginTag {
tag_name: "script".to_string(),
append_to,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"x": "zx x.mjs",
"dev": "pnpm --filter @rspack/cli run dev",
"clean": "pnpm --filter @rspack/cli run clean",
"check-dependency-version": "check-dependency-version-consistency . --ignore-dep typescript --ignore-dep @napi-rs/cli --ignore-dep chalk --ignore-package webpack-test --ignore-package webpack-examples",
"check-dependency-version": "check-dependency-version-consistency . --ignore-dep typescript --ignore-dep @napi-rs/cli --ignore-dep chalk --ignore-package webpack-test --ignore-package webpack-examples --ignore-package plugin-test",
"build:js": "pnpm --filter \"@rspack/core\" build:force && pnpm --filter \"@rspack/*\" --filter \"!@rspack/core\" build",
"build:cli:debug": "npm run build:binding:debug && npm run build:js",
"build:cli:release": "npm run build:binding:release && npm run build:js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>rspack</title>
<script src="/runtime.js" defer></script><script src="/main.js" defer></script><link href="/main.css" rel="stylesheet" /></head>
<script defer src="/runtime.js"></script><script defer src="/main.js"></script><link href="/main.css" rel="stylesheet" /></head>
<body>
LingyuCoder marked this conversation as resolved.
Show resolved Hide resolved

</body></html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8" />
<title>Rspack App</title>
<script src="/runtime.js" defer></script><script src="/chunk1.js" defer></script></head>
<script defer src="/runtime.js"></script><script defer src="/chunk1.js"></script></head>

<body>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>rspack</title>
<link rel="icon" href="/favicon.ico" /><script src="/runtime.js" defer></script><script src="/index.js" defer></script></head>
<link rel="icon" href="/favicon.ico" /><script defer src="/runtime.js"></script><script defer src="/index.js"></script></head>
<body>

</body></html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>rspack</title>
<script src="/runtime.js" defer></script><script src="/main.js" defer></script><link href="/main.css" rel="stylesheet" /></head>
<script defer src="/runtime.js"></script><script defer src="/main.js"></script><link href="/main.css" rel="stylesheet" /></head>
<body>

</body></html>
Expand All @@ -16,7 +16,7 @@
<head>
<meta charset="UTF-8" />
<title>Rspack App</title>
<script src="/runtime.js" defer></script><script src="/main.js" defer></script><link href="/main.css" rel="stylesheet" /></head>
<script defer src="/runtime.js"></script><script defer src="/main.js"></script><link href="/main.css" rel="stylesheet" /></head>

<body>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link href="/main.css" rel="stylesheet" /></head>
<body>

<script src="/runtime.js" defer></script><script src="/main.js" defer></script></body></html>
<script defer src="/runtime.js"></script><script defer src="/main.js"></script></body></html>
```

```html title=inject_false.html
Expand All @@ -28,7 +28,7 @@
<head>
<meta charset="utf-8">
<title>rspack</title>
<script src="/runtime.js" defer></script><script src="/main.js" defer></script><link href="/main.css" rel="stylesheet" /></head>
<script defer src="/runtime.js"></script><script defer src="/main.js"></script><link href="/main.css" rel="stylesheet" /></head>
<body>

</body></html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>rspack</title>
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport" /><script src="/runtime.js" defer></script><script src="/index.js" defer></script></head>
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport" /><script defer src="/runtime.js"></script><script defer src="/index.js"></script></head>
<body>

</body></html>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```html title=index.html
<!doctype html><meta charset=utf-8><title>rspack</title><script src=/runtime.js defer></script><script src=/index.js defer></script>
<!doctype html><meta charset=utf-8><title>rspack</title><script defer src=/runtime.js></script><script defer src=/index.js></script>
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8" />
<title>Rspack App</title>
<script src="/runtime.js" defer></script><script src="/chunk1.js" defer></script></head>
<script defer src="/runtime.js"></script><script defer src="/chunk1.js"></script></head>

<body>

Expand All @@ -19,7 +19,7 @@
<head>
<meta charset="UTF-8" />
<title>Rspack App</title>
<script src="/runtime.js" defer></script><script src="/chunk2.js" defer></script></head>
<script defer src="/runtime.js"></script><script defer src="/chunk2.js"></script></head>

<body>

Expand All @@ -34,7 +34,7 @@
<head>
<meta charset="UTF-8" />
<title>Rspack App</title>
<script src="/runtime.js" defer></script><script src="/chunk3.js" defer></script></head>
<script defer src="/runtime.js"></script><script defer src="/chunk3.js"></script></head>

<body>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>rspack</title>
<link rel="icon" href="/base/favicon.ico" /><script src="/base/runtime.js" defer></script><script src="/base/index.js" defer></script></head>
<link rel="icon" href="/base/favicon.ico" /><script defer src="/base/runtime.js"></script><script defer src="/base/index.js"></script></head>
<body>

</body></html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8" />
<title>bar</title>
<script src="/runtime.js" defer></script><script src="/index.js" defer></script></head>
<script defer src="/runtime.js"></script><script defer src="/index.js"></script></head>

<body>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```html title=index.html
<!DOCTYPE html>
<html>
<head><script src="/runtime.js" defer></script><script src="/index.js" defer></script></head><body><div>bar</div></body></html>
<head><script defer src="/runtime.js"></script><script defer src="/index.js"></script></head><body><div>bar</div></body></html>
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>Rspack title</title>
<script src="/runtime.js" defer></script><script src="/index.js" defer></script></head>
<script defer src="/runtime.js"></script><script defer src="/index.js"></script></head>
<body>

</body></html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ it("body-index.html inject", () => {
const htmlPath = path.join(__dirname, "./body-index.html");
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
expect(
htmlContent.includes('<script src="bundle0.js" defer></script></body>')
htmlContent.includes('<script defer src="bundle0.js"></script></body>')
).toBe(true);
});

it("head-index.html inject", () => {
const htmlPath = path.join(__dirname, "./head-index.html");
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
expect(
htmlContent.includes('<script src="bundle1.js" defer></script></head>')
htmlContent.includes('<script defer src="bundle1.js"></script></head>')
).toBe(true);
});

Expand All @@ -29,7 +29,7 @@ it("true-defer-index.html inject", () => {
const htmlPath = path.join(__dirname, "./true-defer-index.html");
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
expect(
htmlContent.includes('<script src="bundle3.js" defer></script></head>')
htmlContent.includes('<script defer src="bundle3.js"></script></head>')
).toBe(true);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const path = require("path");
it("html plugin should public path auto works", () => {
const htmlPath = path.join(__dirname, "./main_page/index.html");
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
expect(htmlContent.includes('<script src="../bundle0.js" defer>')).toBe(true);
expect(htmlContent.includes('<script defer src="../bundle0.js">')).toBe(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require("path");
it("html plugin should respect output.publicPath", () => {
const htmlPath = path.join(__dirname, "./index.html");
const htmlContent = fs.readFileSync(htmlPath, "utf-8");
expect(htmlContent.includes('<script src="/base/bundle0.js" defer>')).toBe(
expect(htmlContent.includes('<script defer src="/base/bundle0.js">')).toBe(
true
);
});
50 changes: 50 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/plugin-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
>
> - [mini-css-extract-plugin/test](https://github.com/webpack-contrib/mini-css-extract-plugin/tree/master/test)
> - [copy-webpack-plugin](https://github.com/webpack-contrib/copy-webpack-plugin/tree/master/test)
> - [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin/tree/main/spec)

## Credits

Thanks to:

- The [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) project created by [@sokra](https://github.com/sokra)
- The [copy-webpack-plugin](https://github.com/webpack-contrib/copy-webpack-plugin) project created by [@kevlened](https://github.com/kevlened)
- The [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) project created by [@jantimon](https://github.com/jantimon)
9 changes: 9 additions & 0 deletions tests/plugin-test/html-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The test code is modified based on
* https://github.com/jantimon/html-webpack-plugin/tree/main/spec
*
* MIT Licensed
* Author Jan Nicklas @jantimon
* Copyright JS Foundation and other contributors
* https://github.com/jantimon/html-webpack-plugin/blob/main/LICENSE
*/
Loading
Loading