Skip to content

Commit 80b0816

Browse files
committed
Better support for CSS and JS
1 parent bbdade3 commit 80b0816

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"presets": [
3-
"@babel/preset-env"
3+
"@babel/preset-env",
4+
"minify"
45
],
56
"plugins": [
67
"@babel/plugin-proposal-object-rest-spread"

app/Services/Helpers/AssetHashService.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,25 @@ public function __construct(Application $application, CacheRepository $cache, Fi
5858
public function url(string $resource): string
5959
{
6060
$file = last(explode('/', $resource));
61+
$data = array_get($this->manifest(), $file, $file);
6162

62-
return '/' . ltrim(str_replace($file, array_get($this->manifest(), $file, $file), $resource), '/');
63+
return '/' . ltrim(str_replace($file, array_get($data, 'src', $file), $resource), '/');
64+
}
65+
66+
/**
67+
* Return the data integrity hash for a resource.
68+
*
69+
* @param string $resource
70+
* @return string
71+
*
72+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
73+
*/
74+
public function integrity(string $resource): string
75+
{
76+
$file = last(explode('/', $resource));
77+
$data = array_get($this->manifest(), $file, $file);
78+
79+
return array_get($data, 'integrity', '');
6380
}
6481

6582
/**
@@ -72,7 +89,11 @@ public function url(string $resource): string
7289
*/
7390
public function css(string $resource): string
7491
{
75-
return '<link href="' . $this->url($resource) . '" rel="stylesheet preload" crossorigin="anonymous" referrerpolicy="no-referrer">';
92+
return '<link href="' . $this->url($resource) . '"
93+
rel="stylesheet preload"
94+
crossorigin="anonymous"
95+
integrity="' . $this->integrity($resource) . '"
96+
referrerpolicy="no-referrer">';
7697
}
7798

7899
/**
@@ -85,7 +106,9 @@ public function css(string $resource): string
85106
*/
86107
public function js(string $resource): string
87108
{
88-
return '<script src="' . $this->url($resource) . '" crossorigin="anonymous"></script>';
109+
return '<script src="' . $this->url($resource) . '"
110+
integrity="' . $this->integrity($resource) . '"
111+
crossorigin="anonymous"></script>';
89112
}
90113

91114
/**

resources/themes/pterodactyl/templates/wrapper.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@show
1111

1212
@section('assets')
13-
{!! $asset->css('assets/bundle.css') !!}
13+
{!! $asset->css('main.css') !!}
1414
@show
1515

1616
@include('layouts.scripts')
@@ -24,7 +24,7 @@
2424
@yield('below-container')
2525
@show
2626
@section('scripts')
27-
{!! $asset->js('assets/bundle.js') !!}
27+
{!! $asset->js('main.js') !!}
2828
@show
2929
</body>
3030
</html>

webpack.config.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const path = require('path');
2+
const AssetsManifestPlugin = require('webpack-assets-manifest');
23
const CleanPlugin = require('clean-webpack-plugin');
34
const ExtractTextPlugin = require('extract-text-webpack-plugin');
4-
const ManifestPlugin = require('webpack-manifest-plugin');
55
const ShellPlugin = require('webpack-shell-plugin');
6-
const UglifyJsPLugin = require('uglifyjs-webpack-plugin');
6+
const MinifyPlugin = require('babel-minify-webpack-plugin');
77

88
module.exports = {
99
mode: 'development',
@@ -36,19 +36,22 @@ module.exports = {
3636
{
3737
test: /\.js$/,
3838
include: [
39-
path.resolve(__dirname, 'resources/assets/scripts'),
39+
path.resolve(__dirname, 'resources'),
4040
],
4141
loader: 'babel-loader',
4242
},
4343
{
4444
test: /\.css$/,
4545
include: [
46-
path.resolve(__dirname, 'resources/assets/styles'),
46+
path.resolve(__dirname, 'resources'),
4747
],
4848
use: ExtractTextPlugin.extract({
4949
fallback: 'style-loader',
50-
use: ['css-loader', {
51-
loader: 'postcss-loader',
50+
use: [{
51+
loader: 'css-loader',
52+
options: {importLoaders: 1},
53+
}, {
54+
loader: 'postcss-loader',
5255
options: {
5356
ident: 'postcss',
5457
plugins: [
@@ -80,18 +83,19 @@ module.exports = {
8083
new ExtractTextPlugin('bundle-[chunkhash].css', {
8184
allChunks: true,
8285
}),
83-
new UglifyJsPLugin({
86+
new MinifyPlugin({
87+
mangle: {topLevel: true},
88+
}, {
8489
include: [
85-
path.resolve(__dirname, 'resources/assets/scripts'),
90+
path.resolve(__dirname, 'resources'),
91+
path.resolve(__dirname, 'node_modules'),
8692
],
87-
parallel: 2,
88-
sourceMap: false,
89-
uglifyOptions: {
90-
ecma: 5,
91-
toplevel: true,
92-
safari10: true,
93-
}
9493
}),
95-
new ManifestPlugin(),
94+
new AssetsManifestPlugin({
95+
writeToDisk: true,
96+
publicPath: true,
97+
integrity: true,
98+
integrityHashes: ['sha384'],
99+
}),
96100
]
97101
};

0 commit comments

Comments
 (0)