Skip to content

Commit 440cc1d

Browse files
committed
feat: refactor for rspack
1 parent b91844c commit 440cc1d

35 files changed

+1704
-1389
lines changed

.vscode/tasks.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "npm",
6+
"script": "test",
7+
"group": {
8+
"kind": "test",
9+
"isDefault": true
10+
},
11+
"problemMatcher": [],
12+
"label": "npm: 运行测试",
13+
"detail": "npm run test"
14+
}
15+
]
16+
}

README.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# html-inline-css-webpack-plugin
1+
# html-inline-css-rspack-plugin
2+
3+
Forked the `html-inline-css-webpack-plugin` and modified it to be compatible with `rspack`
4+
5+
---
6+
27
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
38
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Runjuu/html-inline-css-webpack-plugin/pulls)
49
[![Total downloads](https://img.shields.io/npm/dm/html-inline-css-webpack-plugin.svg)](https://www.npmjs.com/package/html-inline-css-webpack-plugin)
@@ -14,37 +19,39 @@ Require [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-ex
1419
## Install
1520
#### NPM
1621
```bash
17-
npm i html-inline-css-webpack-plugin -D
22+
npm i html-inline-css-rspack-plugin -D
1823
```
1924
#### Yarn
2025
```bash
21-
yarn add html-inline-css-webpack-plugin -D
26+
yarn add html-inline-css-rspack-plugin -D
2227
```
2328

2429
## Minimal example
2530
```js
26-
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
27-
const HtmlWebpackPlugin = require('html-webpack-plugin');
28-
const HTMLInlineCSSWebpackPlugin = require("html-inline-css-webpack-plugin").default;
31+
// const MiniCssExtractPlugin = require("mini-css-extract-plugin");
32+
// const HtmlWebpackPlugin = require('html-webpack-plugin');
33+
// const HTMLInlineCSSWebpackPlugin = require("html-inline-css-rspack-plugin").default;
34+
import { rspack } from '@rspack/core';
35+
import { HTMLInlineRspackPlugin } from 'html-inline-css-webpack-plugin'
2936

3037
module.exports = {
3138
plugins: [
32-
new MiniCssExtractPlugin({
33-
filename: "[name].css",
34-
chunkFilename: "[id].css"
35-
}),
36-
new HtmlWebpackPlugin(),
37-
new HTMLInlineCSSWebpackPlugin(),
39+
// new MiniCssExtractPlugin({
40+
// filename: "[name].css",
41+
// chunkFilename: "[id].css"
42+
// }),
43+
// new HtmlWebpackPlugin(),
44+
new rspack.CssExtractRspackPlugin({}),
45+
new rspack.HtmlRspackPlugin(options);
46+
new HTMLInlineRspackPlugin(),
3847
],
3948
module: {
4049
rules: [
4150
{
42-
test: /\.css$/,
43-
use: [
44-
MiniCssExtractPlugin.loader,
45-
"css-loader"
46-
]
47-
}
51+
test: /\.css$/i,
52+
use: [rspack.CssExtractRspackPlugin.loader, 'css-loader'],
53+
type: 'javascript/auto',
54+
},
4855
]
4956
}
5057
}
@@ -72,7 +79,7 @@ Return `true` to make current file internal, otherwise ignore current file. Incl
7279
##### example
7380
```typescript
7481
...
75-
new HTMLInlineCSSWebpackPlugin({
82+
new HTMLInlineRspackPlugin({
7683
filter(fileName) {
7784
return fileName.includes('main');
7885
},
@@ -90,7 +97,7 @@ Used to customize the style tag.
9097
##### example
9198
```typescript
9299
...
93-
new HTMLInlineCSSWebpackPlugin({
100+
new HTMLInlineRspackPlugin({
94101
styleTagFactory({ style }) {
95102
return `<style type="text/css">${style}</style>`;
96103
},
@@ -129,7 +136,7 @@ if `true`, it will remove the `target` from the output HTML
129136

130137
```typescript
131138
...
132-
new HTMLInlineCSSWebpackPlugin({
139+
new HTMLInlineRspackPlugin({
133140
replace: {
134141
removeTarget: true,
135142
target: '<!-- inline_css_plugin -->',

dist/core/base-plugin.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Compilation } from '@rspack/core';
2+
import { Config, StyleTagFactory, FileCache } from '../types';
3+
export declare class BasePlugin {
4+
protected readonly config: Config;
5+
protected cssStyleCache: FileCache;
6+
protected get replaceConfig(): import("../types").ReplaceConfig;
7+
protected get styleTagFactory(): StyleTagFactory;
8+
constructor(config?: Config);
9+
protected prepare({ assets }: Compilation): void;
10+
protected getCSSStyle({ cssLink, publicPath, }: {
11+
cssLink: string;
12+
publicPath: string;
13+
}): string | undefined;
14+
protected isCurrentFileNeedsToBeInlined(fileName: string): boolean;
15+
protected addStyle({ html, htmlFileName, style, }: {
16+
html: string;
17+
htmlFileName: string;
18+
style: string;
19+
}): string;
20+
protected cleanUp(html: string): string;
21+
}

dist/core/base-plugin.js

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/base-plugin.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/rspack.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { type HtmlRspackPluginOptions, type Compiler } from '@rspack/core';
2+
import { BasePlugin } from './base-plugin';
3+
export type ExtraPluginHookData = {
4+
plugin: {
5+
options: HtmlRspackPluginOptions;
6+
};
7+
};
8+
export interface JsBeforeEmitData {
9+
html: string;
10+
outputName: string;
11+
}
12+
export interface JsHtmlPluginAssets {
13+
publicPath: string;
14+
js: Array<string>;
15+
css: Array<string>;
16+
favicon?: string;
17+
}
18+
export interface JsBeforeAssetTagGenerationData {
19+
assets: JsHtmlPluginAssets;
20+
outputName: string;
21+
}
22+
export declare class HTMLInlineRspackPlugin extends BasePlugin {
23+
private cssStyleMap;
24+
private prepareCSSStyle;
25+
private process;
26+
apply(compiler: Compiler): void;
27+
}

dist/core/rspack.js

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core/rspack.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { HTMLInlineRspackPlugin } from './core/rspack';

dist/index.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/types.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface ReplaceConfig {
2+
position?: 'before' | 'after';
3+
removeTarget?: boolean;
4+
target: string;
5+
}
6+
export type StyleTagFactory = (params: {
7+
style: string;
8+
}) => string;
9+
export declare const TAP_KEY_PREFIX = "html-inline-css-rspack-plugin";
10+
export declare const DEFAULT_REPLACE_CONFIG: ReplaceConfig;
11+
export interface Config {
12+
filter?(fileName: string): boolean;
13+
leaveCSSFile?: boolean;
14+
replace?: ReplaceConfig;
15+
styleTagFactory?: StyleTagFactory;
16+
}
17+
export interface FileCache {
18+
[fileName: string]: string;
19+
}

dist/types.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/types.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/utils.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare function is(filenameExtension: string): (fileName: string) => boolean;
2+
export declare const isCSS: (fileName: string) => boolean;
3+
export declare function escapeRegExp(string: string): string;

dist/utils.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/utils.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)