Skip to content

Commit 3f9b047

Browse files
authored
Merge pull request #30 from Elhebert/typescript-rewrite
Typescript rewrite
2 parents e0101e0 + a2891b5 commit 3f9b047

File tree

10 files changed

+1020
-1244
lines changed

10 files changed

+1020
-1244
lines changed

.github/workflows/publish.yml

Whitespace-only changes.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules
22
npm-debug.log
3+
./build

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
44

5-
A laravel mix extension to generate integrity hashes on build for your assets.
5+
A laravel mix 6.0 extension to generate integrity hashes on build for your assets.
6+
7+
For older version of mix, see [v0.0.7](https://github.com/Elhebert/laravel-mix-sri/tree/0.0.7)
68

79
## Installation
810

package-lock.json

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

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "laravel-mix-sri",
3-
"version": "0.0.6",
3+
"version": "1.0.0-alpha",
44
"description": "subresources integrity for laravel mix",
5-
"main": "src/index.js",
5+
"main": "build/index.js",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/Elhebert/laravel-mix-sri"
99
},
1010
"scripts": {
11-
"lint": "prettier --write \"**/*.js\""
11+
"lint": "prettier --write '**/*.ts'",
12+
"build": "tsc --project ./tsconfig.json"
1213
},
1314
"homepage": "https://github.com/Elhebert/laravel-mix-sri",
1415
"keywords": [
@@ -20,13 +21,14 @@
2021
"author": "Dieter Stinglhamber",
2122
"license": "MIT",
2223
"peerDependencies": {
23-
"laravel-mix": "^2.1.2 || ^3.0 || ^4.0 || ^5.0 || ^6.0"
24+
"laravel-mix": "^6.0"
2425
},
2526
"engines": {
26-
"node": ">=12.0.0"
27+
"node": ">=15.0.0"
2728
},
2829
"devDependencies": {
29-
"laravel-mix": "^2.1.2 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
30-
"prettier": "^1.18.2"
30+
"laravel-mix": "^6.0",
31+
"prettier": "^2.2.1",
32+
"typescript": "^4.2.4"
3133
}
3234
}

src/SriPlugin.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/SriPlugin.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import collect from 'collect.js'
2+
import webpack from 'webpack'
3+
import crypto from 'crypto'
4+
import fs from 'fs'
5+
import path from 'path'
6+
import { cwd } from 'process'
7+
8+
export default class SriPlugin implements webpack.WebpackPluginInstance {
9+
constructor(private algorithm: 'sha256' | 'sha384' | 'sha512') {
10+
this.algorithm = algorithm
11+
}
12+
13+
apply(compiler: webpack.Compiler): void {
14+
const process = (stats: webpack.Stats) => {
15+
let assets: Record<string, string[]> = stats.toJson().assetsByChunkName
16+
let hashes: Record<string, string> = {}
17+
18+
// If there's a temporary mix.js chunk, we can safely remove it.
19+
if (assets.mix) {
20+
assets.mix = collect(assets.mix).except(['mix.js']).all()
21+
}
22+
23+
collect<string>(assets)
24+
.flatten()
25+
// Don't add hot updates to manifest
26+
.filter((name: string) => name.indexOf('hot-update') === -1)
27+
.all()
28+
.forEach((filePath: string) => {
29+
if (!filePath.startsWith('/')) {
30+
filePath = `/${filePath}`
31+
}
32+
33+
filePath = filePath.replace(/\?id=\w{20}/, '')
34+
35+
hashes[filePath] = crypto
36+
.createHash(this.algorithm)
37+
.update(
38+
fs.readFileSync(
39+
// @ts-ignore TS2304
40+
path.join(cwd(), Config.publicPath, 'mix-sri.json')
41+
)
42+
)
43+
.digest('base64')
44+
})
45+
46+
fs.writeFileSync(
47+
// @ts-ignore TS2304
48+
path.join(cwd(), Config.publicPath, 'mix-sri.json'),
49+
JSON.stringify(hashes, null, 4)
50+
)
51+
}
52+
53+
compiler.hooks.done.tapAsync('SriPlugin', process)
54+
}
55+
}

src/index.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import mix from 'laravel-mix'
2+
import SriPlugin from './SriPlugin'
3+
import webpack from 'webpack'
4+
import { ClassComponent } from 'laravel-mix/types/component'
5+
6+
export type Options = {
7+
algorithm?: 'sha256' | 'sha384' | 'sha512'
8+
enabled?: boolean
9+
}
10+
11+
class IntegrityHash implements ClassComponent {
12+
private config: Options = {}
13+
14+
name(): string {
15+
return 'generateIntegrityHash'
16+
}
17+
18+
register(options: Options = {}): void {
19+
this.config = {
20+
algorithm: 'sha256',
21+
enabled: options.enabled || mix.inProduction(),
22+
}
23+
24+
if (['sha256', 'sha384', 'sha512'].includes(options.algorithm)) {
25+
this.config.algorithm = options.algorithm
26+
}
27+
}
28+
29+
webpackPlugins(): webpack.WebpackPluginInstance[] {
30+
if (this.config.enabled) {
31+
return [new SriPlugin(this.config.algorithm)]
32+
}
33+
}
34+
}
35+
36+
mix.extend('generateIntegrityHash', new IntegrityHash())

tsconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"types": ["laravel-mix"],
4+
"alwaysStrict": true,
5+
"module": "commonjs",
6+
"target": "es2018",
7+
"lib": [
8+
"es6",
9+
"esnext",
10+
"es2015",
11+
"es2016",
12+
"es2017",
13+
"es2018"
14+
],
15+
"esModuleInterop": true,
16+
"moduleResolution": "node",
17+
"sourceMap": true,
18+
"outDir": "./build",
19+
"rootDir": "./src"
20+
},
21+
"include": [
22+
"src/**/*"
23+
]
24+
}

0 commit comments

Comments
 (0)