Better support for CSS and JS

This commit is contained in:
Dane Everitt 2018-06-03 19:35:50 -07:00
parent bbdade398a
commit 80b0816718
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 50 additions and 22 deletions

View file

@ -1,6 +1,7 @@
{ {
"presets": [ "presets": [
"@babel/preset-env" "@babel/preset-env",
"minify"
], ],
"plugins": [ "plugins": [
"@babel/plugin-proposal-object-rest-spread" "@babel/plugin-proposal-object-rest-spread"

View file

@ -58,8 +58,25 @@ class AssetHashService
public function url(string $resource): string public function url(string $resource): string
{ {
$file = last(explode('/', $resource)); $file = last(explode('/', $resource));
$data = array_get($this->manifest(), $file, $file);
return '/' . ltrim(str_replace($file, array_get($this->manifest(), $file, $file), $resource), '/'); return '/' . ltrim(str_replace($file, array_get($data, 'src', $file), $resource), '/');
}
/**
* Return the data integrity hash for a resource.
*
* @param string $resource
* @return string
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function integrity(string $resource): string
{
$file = last(explode('/', $resource));
$data = array_get($this->manifest(), $file, $file);
return array_get($data, 'integrity', '');
} }
/** /**
@ -72,7 +89,11 @@ class AssetHashService
*/ */
public function css(string $resource): string public function css(string $resource): string
{ {
return '<link href="' . $this->url($resource) . '" rel="stylesheet preload" crossorigin="anonymous" referrerpolicy="no-referrer">'; return '<link href="' . $this->url($resource) . '"
rel="stylesheet preload"
crossorigin="anonymous"
integrity="' . $this->integrity($resource) . '"
referrerpolicy="no-referrer">';
} }
/** /**
@ -85,7 +106,9 @@ class AssetHashService
*/ */
public function js(string $resource): string public function js(string $resource): string
{ {
return '<script src="' . $this->url($resource) . '" crossorigin="anonymous"></script>'; return '<script src="' . $this->url($resource) . '"
integrity="' . $this->integrity($resource) . '"
crossorigin="anonymous"></script>';
} }
/** /**

View file

@ -10,7 +10,7 @@
@show @show
@section('assets') @section('assets')
{!! $asset->css('assets/bundle.css') !!} {!! $asset->css('main.css') !!}
@show @show
@include('layouts.scripts') @include('layouts.scripts')
@ -24,7 +24,7 @@
@yield('below-container') @yield('below-container')
@show @show
@section('scripts') @section('scripts')
{!! $asset->js('assets/bundle.js') !!} {!! $asset->js('main.js') !!}
@show @show
</body> </body>
</html> </html>

View file

@ -1,9 +1,9 @@
const path = require('path'); const path = require('path');
const AssetsManifestPlugin = require('webpack-assets-manifest');
const CleanPlugin = require('clean-webpack-plugin'); const CleanPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin'); const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const ShellPlugin = require('webpack-shell-plugin'); const ShellPlugin = require('webpack-shell-plugin');
const UglifyJsPLugin = require('uglifyjs-webpack-plugin'); const MinifyPlugin = require('babel-minify-webpack-plugin');
module.exports = { module.exports = {
mode: 'development', mode: 'development',
@ -36,18 +36,21 @@ module.exports = {
{ {
test: /\.js$/, test: /\.js$/,
include: [ include: [
path.resolve(__dirname, 'resources/assets/scripts'), path.resolve(__dirname, 'resources'),
], ],
loader: 'babel-loader', loader: 'babel-loader',
}, },
{ {
test: /\.css$/, test: /\.css$/,
include: [ include: [
path.resolve(__dirname, 'resources/assets/styles'), path.resolve(__dirname, 'resources'),
], ],
use: ExtractTextPlugin.extract({ use: ExtractTextPlugin.extract({
fallback: 'style-loader', fallback: 'style-loader',
use: ['css-loader', { use: [{
loader: 'css-loader',
options: {importLoaders: 1},
}, {
loader: 'postcss-loader', loader: 'postcss-loader',
options: { options: {
ident: 'postcss', ident: 'postcss',
@ -80,18 +83,19 @@ module.exports = {
new ExtractTextPlugin('bundle-[chunkhash].css', { new ExtractTextPlugin('bundle-[chunkhash].css', {
allChunks: true, allChunks: true,
}), }),
new UglifyJsPLugin({ new MinifyPlugin({
mangle: {topLevel: true},
}, {
include: [ include: [
path.resolve(__dirname, 'resources/assets/scripts'), path.resolve(__dirname, 'resources'),
path.resolve(__dirname, 'node_modules'),
], ],
parallel: 2,
sourceMap: false,
uglifyOptions: {
ecma: 5,
toplevel: true,
safari10: true,
}
}), }),
new ManifestPlugin(), new AssetsManifestPlugin({
writeToDisk: true,
publicPath: true,
integrity: true,
integrityHashes: ['sha384'],
}),
] ]
}; };