Skip to content

Commit 797c151

Browse files
authored
Merge pull request #8 from principalstudio/Applelo/issue6
Fix #6
2 parents e38bbc0 + 52b23c9 commit 797c151

File tree

7 files changed

+1204
-4642
lines changed

7 files changed

+1204
-4642
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This plugin allows to add preload links anywhere you want.
55

66
# Installation
77

8-
You need to have HTMLWebpackPlugin v4 to make this plugin work.
8+
You need to have HTMLWebpackPlugin v5 to make this plugin work.
99

1010
```
1111
npm i -D @principalstudio/html-webpack-inject-preload

package-lock.json

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

package.json

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@principalstudio/html-webpack-inject-preload",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "A html webpack plugin for injecting <link rel='preload'>",
55
"main": "lib/main.js",
66
"types": "lib/main.d.ts",
@@ -31,21 +31,19 @@
3131
},
3232
"homepage": "https://github.com/principalstudio/html-webpack-inject-preload#readme",
3333
"devDependencies": {
34-
"@types/jest": "^26.0.14",
35-
"@types/mini-css-extract-plugin": "^0.9.1",
36-
"@types/node": "^14.11.5",
37-
"@types/webpack": "^4.41.22",
38-
"@typescript-eslint/parser": "^4.4.0",
39-
"css-loader": "^4.3.0",
40-
"eslint": "^7.10.0",
41-
"file-loader": "^6.1.0",
42-
"html-webpack-plugin": "^4.5.0",
43-
"jest": "^26.5.2",
44-
"mini-css-extract-plugin": "^0.12.0",
45-
"prettier": "^2.1.2",
46-
"ts-jest": "^26.4.1",
47-
"typescript": "^4.0.3",
48-
"url-loader": "^4.1.0",
49-
"webpack": "^4.44.2"
34+
"@types/jest": "^26.0.20",
35+
"@types/node": "^14.14.22",
36+
"@typescript-eslint/parser": "^4.14.0",
37+
"css-loader": "^5.0.1",
38+
"eslint": "^7.18.0",
39+
"file-loader": "^6.2.0",
40+
"html-webpack-plugin": "5.0.0-beta.6",
41+
"jest": "^26.6.3",
42+
"mini-css-extract-plugin": "^1.3.4",
43+
"prettier": "^2.2.1",
44+
"ts-jest": "^26.4.4",
45+
"typescript": "^4.1.3",
46+
"url-loader": "^4.1.1",
47+
"webpack": "^5.17.0"
5048
}
51-
}
49+
}

src/main.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {
22
default as HtmlWebpackPluginInstance,
33
HtmlTagObject,
44
} from 'html-webpack-plugin';
5-
import type {compilation, Compiler, Plugin} from 'webpack';
5+
import type {Compilation, Compiler, WebpackPluginInstance} from 'webpack';
66

77
declare namespace HtmlWebpackInjectPreload {
88
interface Options {
@@ -42,7 +42,7 @@ interface HtmlWebpackPluginData {
4242
*
4343
* @class InjectPreloadFiles
4444
*/
45-
class HtmlWebpackInjectPreload implements Plugin {
45+
class HtmlWebpackInjectPreload implements WebpackPluginInstance {
4646
private options: HtmlWebpackInjectPreload.Options = {
4747
files: [],
4848
};
@@ -78,7 +78,7 @@ class HtmlWebpackInjectPreload implements Plugin {
7878
};
7979

8080
private addLinks(
81-
compilation: compilation.Compilation,
81+
compilation: Compilation,
8282
htmlPluginData: HtmlWebpackPluginData,
8383
) {
8484
const assets = new Set(Object.keys(compilation.assets));
@@ -101,10 +101,13 @@ class HtmlWebpackInjectPreload implements Plugin {
101101
href = href[0] === '/' ? href : '/' + href;
102102

103103
if (file.match.test(asset)) {
104-
const preload = {
104+
const preload: HtmlTagObject = {
105105
tagName: 'link',
106106
attributes: Object.assign(file.attributes, {rel: 'preload', href}),
107107
voidTag: true,
108+
meta: {
109+
plugin: 'html-webpack-inject-preload'
110+
}
108111
};
109112

110113
if (linkIndex > -1) {

test/index.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import HtmlWebpackInjectPreload from '../src/main';
22
import HtmlWebpackPlugin from 'html-webpack-plugin';
33
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
4-
import webpack from 'webpack';
4+
import webpack, { WebpackPluginInstance } from 'webpack';
55
import path from 'path';
66
import fs from 'fs';
77

@@ -29,6 +29,8 @@ const options: HtmlWebpackInjectPreload.Options = {
2929
describe('HTMLWebpackInjectPreload', () => {
3030
it('webpack plugin', done => {
3131
const compiler = webpack({
32+
mode: 'production',
33+
context: path.resolve(__dirname),
3234
entry: path.join(__dirname, 'entry.js'),
3335
module: {
3436
rules: [
@@ -52,16 +54,22 @@ describe('HTMLWebpackInjectPreload', () => {
5254
},
5355
output: {
5456
path: path.join(__dirname, 'dist'),
57+
publicPath: '/',
5558
},
5659
plugins: [
57-
new MiniCssExtractPlugin(),
60+
new MiniCssExtractPlugin() as WebpackPluginInstance,
5861
new HtmlWebpackPlugin(),
5962
new HtmlWebpackInjectPreload(options),
6063
],
6164
});
6265
compiler.run((err, stats) => {
6366
if (err) expect(err).toBeNull();
64-
expect(stats.compilation.errors.length).toBe(0);
67+
68+
const statsErrors = stats ? stats.compilation.errors : [];
69+
if (statsErrors.length > 0) {
70+
console.error(statsErrors);
71+
}
72+
expect(statsErrors.length).toBe(0);
6573

6674
const result = fs.readFileSync(
6775
path.join(__dirname, 'dist/index.html'),

test/style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
body {
88
font-family: Roboto, sans-serif;
99
background-color: red;
10+
color: white;
11+
}
12+
13+
body::before {
14+
content: 'Hello world'
1015
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
/* Raise error on expressions and declarations with an implied 'any' type. */
1616
"esModuleInterop": true,
1717
/* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
18-
"typeRoots": ["node_modules/@types", "typings"],
1918
"lib": [
2019
"es2019"
2120
],

0 commit comments

Comments
 (0)