Skip to content

Add option to leave CSS file #5

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

Closed
wants to merge 4 commits into from
Closed
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ typings/
.next
/.idea
/.rpt2_cache
/build
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Fork of: https://github.com/Runjuu/html-inline-css-webpack-plugin

# html-inline-css-webpack-plugin
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Runjuu/html-inline-css-webpack-plugin/pulls)
Expand Down Expand Up @@ -57,6 +59,7 @@ interface Config {
target: string
position?: 'before' | 'after'
removeTarget?: boolean
leaveCssFile?: boolean
}
}
```
Expand All @@ -83,6 +86,7 @@ replace?: {
target: string
position?: 'before' | 'after' // default is 'before'
removeTarget?: boolean // default is false
leaveCssFile?: boolean // default is false
}
```
A config for customizing the location of injection, default will add internal style sheet before the `</head>`
Expand All @@ -92,6 +96,8 @@ A target for adding the internal style sheet
Add internal style sheet `before`/`after` the `target`
#### removeTarget(optional)
if `true`, it will remove the `target` from the output HTML
#### leaveCssFile(optional)
if `true`, it will leave CSS files where they are when inlining

##### example
```html
Expand Down
25 changes: 25 additions & 0 deletions build/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Compiler } from 'webpack';
interface ReplaceConfig {
position?: 'before' | 'after';
removeTarget?: boolean;
target: string;
leaveCssFile?: boolean;
}
interface Config {
filter?(fileName: string): boolean;
replace?: ReplaceConfig;
}
export default class Plugin {
private readonly config;
static addStyle(html: string, style: string, replaceConfig: ReplaceConfig): string;
static removeLinkTag(html: string, cssFileName: string): string;
static cleanUp(html: string, replaceConfig: ReplaceConfig): string;
private css;
private html;
constructor(config?: Config);
private filter;
private prepare;
private process;
apply(compiler: Compiler): void;
}
export {};
102 changes: 102 additions & 0 deletions build/index.js

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

1 change: 1 addition & 0 deletions build/index.js.map

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "html-inline-css-webpack-plugin",
"name": "html-inline-css-webpack-plugin-wdd",
"version": "1.3.2",
"description": "☄️ A webpack plugin for convert external stylesheet to embedded stylesheet, aka document stylesheet",
"main": "./build/index.js",
Expand All @@ -9,7 +9,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/Runjuu/html-inline-css-webpack-plugin.git"
"url": "git+https://github.com/wilddogdesign/html-inline-css-webpack-plugin.git"
},
"keywords": [
"webpack",
Expand All @@ -26,9 +26,9 @@
"author": "Runjuu",
"license": "MIT",
"bugs": {
"url": "https://github.com/Runjuu/html-inline-css-webpack-plugin/issues"
"url": "https://github.com/wilddogdesign/html-inline-css-webpack-plugin/issues"
},
"homepage": "https://github.com/Runjuu/html-inline-css-webpack-plugin#readme",
"homepage": "https://github.com/wilddogdesign/html-inline-css-webpack-plugin#readme",
"devDependencies": {
"@types/webpack": "^4.4.0",
"tslib": "^1.9.2",
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ interface Compilation {
interface ReplaceConfig {
position?: 'before' | 'after'
removeTarget?: boolean
target: string
target: string,
leaveCssFile?: boolean
}

interface Config {
Expand Down Expand Up @@ -71,13 +72,16 @@ export default class Plugin
private prepare({ assets }: Compilation) {
const isCSS = is('css');
const isHTML = is('html');
const { replace: replaceConfig = DEFAULT_REPLACE_CONFIG } = this.config;

Object.keys(assets).forEach((fileName) => {
if (isCSS(fileName)) {
const isCurrentFileNeedsToBeInlined = this.filter(fileName);
if (isCurrentFileNeedsToBeInlined) {
this.css[fileName] = assets[fileName].source();
delete assets[fileName];
if (!replaceConfig.leaveCssFile) {
delete assets[fileName]
}
}
} else if (isHTML(fileName)) {
this.html[fileName] = assets[fileName].source();
Expand Down