Skip to content

Commit 6b5e410

Browse files
committed
init
1 parent 1aedfda commit 6b5e410

File tree

7 files changed

+2346
-0
lines changed

7 files changed

+2346
-0
lines changed

build/index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Compiler } from 'webpack';
2+
interface Module {
3+
type?: string;
4+
content: string;
5+
}
6+
interface Compilation {
7+
modules: Module[];
8+
}
9+
interface PluginData {
10+
html: string;
11+
assets: {
12+
css: string[];
13+
};
14+
}
15+
export default class Plugin {
16+
static makeReg(fileName: string): RegExp;
17+
static getStyleString(modules: Module[]): string;
18+
static addStyleInToHTML(compilation: Compilation, pluginData: PluginData): void;
19+
static removeLinkTag(pluginData: PluginData): void;
20+
static replace(compilation: Compilation, pluginData: PluginData, callback: (...args: any[]) => void): void;
21+
apply(compiler: Compiler): void;
22+
}
23+
export {};

build/index.js

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

build/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.

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "html-inline-css-webpack-plugin",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "./build/index.js",
6+
"scripts": {
7+
"build": "rm -rf ./build && tsc"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Runjuu/html-inline-css-webpack-plugin.git"
12+
},
13+
"keywords": [],
14+
"author": "Runjuu",
15+
"license": "MIT",
16+
"bugs": {
17+
"url": "https://github.com/Runjuu/html-inline-css-webpack-plugin/issues"
18+
},
19+
"homepage": "https://github.com/Runjuu/html-inline-css-webpack-plugin#readme",
20+
"devDependencies": {
21+
"@types/webpack": "^4.4.0",
22+
"tslib": "^1.9.2",
23+
"typescript": "^2.9.1",
24+
"webpack": "^4.11.1"
25+
}
26+
}

src/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Compiler } from 'webpack';
2+
3+
interface Module {
4+
type?: string
5+
content: string
6+
}
7+
8+
interface Compilation {
9+
modules: Module[]
10+
}
11+
12+
interface PluginData {
13+
html: string
14+
assets: { css: string[] }
15+
}
16+
17+
export default class Plugin
18+
{
19+
20+
static makeReg(fileName: string) {
21+
return new RegExp(`<link[^>]+href=['"]${fileName}['"][^>]+(>|\\\\>|><\\/link>)`);
22+
}
23+
24+
static getStyleString(modules: Module[]): string {
25+
return modules
26+
.filter(({ type = '' }) => type.includes('mini-css-extract-plugin'))
27+
.reduce((result, { content = '' }) => {
28+
return result + content;
29+
}, '');
30+
}
31+
32+
static addStyleInToHTML(compilation: Compilation, pluginData: PluginData) {
33+
const style = this.getStyleString(compilation.modules);
34+
pluginData.html = pluginData.html
35+
.replace('</head>', `<style>\n${style}\n</style></head>`);
36+
}
37+
38+
static removeLinkTag(pluginData: PluginData) {
39+
pluginData.assets.css.forEach((fileName: string) => {
40+
pluginData.html = pluginData.html
41+
.replace(this.makeReg(fileName), '');
42+
});
43+
}
44+
45+
static replace(compilation: Compilation, pluginData: PluginData, callback: (...args: any[]) => void) {
46+
Plugin.removeLinkTag(pluginData);
47+
Plugin.addStyleInToHTML(compilation, pluginData);
48+
callback(null, pluginData);
49+
}
50+
51+
apply(compiler: Compiler) {
52+
compiler.hooks.compilation.tap('HtmlReplaceWebpackPlugin', (compilation: any) => {
53+
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing
54+
.tapAsync(
55+
'html-webpack-plugin-before-html-processing',
56+
Plugin.replace.bind(Plugin, compilation)
57+
);
58+
});
59+
}
60+
}

tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"target": "es5",
5+
"module": "commonjs",
6+
"outDir": "build/",
7+
"sourceMap": true,
8+
"experimentalDecorators": true,
9+
"lib": ["dom", "es2015", "es2016"],
10+
"allowSyntheticDefaultImports": true,
11+
"moduleResolution": "node",
12+
"noEmitHelpers": true,
13+
"importHelpers": true,
14+
"strict": true,
15+
"downlevelIteration": true,
16+
"noImplicitReturns": true
17+
},
18+
"exclude": [
19+
"node_modules"
20+
]
21+
}

0 commit comments

Comments
 (0)