From d16e7a3dfa49af1239e3293dc64a7b053e3dab05 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sun, 31 Mar 2019 13:12:56 +0800 Subject: [PATCH] Generate manifest for cache issues --- app/Providers/AppServiceProvider.php | 1 + app/Services/Webpack.php | 24 ++++++++++++++++++++ app/helpers.php | 2 +- package.json | 1 + tests/ServicesTest/WebpackTest.php | 17 ++++++++++++++ webpack.config.js | 8 ++++--- yarn.lock | 34 ++++++++++++++++++++++++++-- 7 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 app/Services/Webpack.php create mode 100644 tests/ServicesTest/WebpackTest.php diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 359bddd3..d981a220 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -66,6 +66,7 @@ class AppServiceProvider extends ServiceProvider $this->app->singleton('cipher', 'App\Services\Cipher\\'.config('secure.cipher')); $this->app->singleton('options', \App\Services\Option::class); $this->app->singleton('parsedown', \Parsedown::class); + $this->app->singleton('webpack', \App\Services\Webpack::class); } /** diff --git a/app/Services/Webpack.php b/app/Services/Webpack.php new file mode 100644 index 00000000..a421ee53 --- /dev/null +++ b/app/Services/Webpack.php @@ -0,0 +1,24 @@ +manifest = json_decode(File::get($path), true); + } + } + + public function __get($path) + { + return url('app').'/'.Arr::get($this->manifest, $path, ''); + } +} diff --git a/app/helpers.php b/app/helpers.php index 9b976e7d..7d50e963 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -34,7 +34,7 @@ if (! function_exists('webpack_assets')) { // @codeCoverageIgnoreEnd } else { $cdn = option('cdn_address'); - return $cdn ? "$cdn/app/$relativeUri" : url("app/$relativeUri"); + return $cdn ? "$cdn/app/$relativeUri" : app('webpack')->$relativeUri; } } } diff --git a/package.json b/package.json index 840c3db4..c723624b 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,7 @@ "webpack": "^4.29.6", "webpack-cli": "^3.2.3", "webpack-dev-server": "^3.2.1", + "webpack-manifest-plugin": "^2.0.4", "webpackbar": "^3.1.5", "yaml-loader": "^0.5.0" }, diff --git a/tests/ServicesTest/WebpackTest.php b/tests/ServicesTest/WebpackTest.php new file mode 100644 index 00000000..cbad07cb --- /dev/null +++ b/tests/ServicesTest/WebpackTest.php @@ -0,0 +1,17 @@ +andReturn(true); + File::shouldReceive('get')->andReturn(json_encode(['a' => 'b'])); + $key = 'a'; + $this->assertEquals('http://localhost/app/b', app('webpack')->$key); + } +} diff --git a/webpack.config.js b/webpack.config.js index 9b5061f3..3e388731 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -4,6 +4,7 @@ const VueLoaderPlugin = require('vue-loader/lib/plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CopyWebpackPlugin = require('copy-webpack-plugin') const WebpackBar = require('webpackbar') +const ManifestPlugin = require('webpack-manifest-plugin') const devMode = !process.argv.includes('-p') @@ -26,7 +27,7 @@ const config = { }, output: { path: `${__dirname}/public/app`, - filename: '[name].js', + filename: '[name].[contenthash:7].js', chunkFilename: devMode ? '[id].js' : '[id].[contenthash:7].js', }, module: { @@ -102,8 +103,8 @@ const config = { plugins: [ new VueLoaderPlugin(), new MiniCssExtractPlugin({ - filename: '[name].css', - chunkFilename: '[id].css', + filename: '[name].[contenthash:7].css', + chunkFilename: '[id].[contenthash:7].css', }), new CopyWebpackPlugin([ { @@ -119,6 +120,7 @@ const config = { 'resources/assets/src/images/bg.jpg', 'resources/assets/src/images/favicon.ico', ]), + new ManifestPlugin(), ], resolve: { extensions: ['.js', '.ts', '.vue', '.json'], diff --git a/yarn.lock b/yarn.lock index 4c0b9f9d..3a952562 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4049,6 +4049,15 @@ from2@^2.1.0: inherits "^2.0.1" readable-stream "^2.0.0" +fs-extra@^7.0.0: + version "7.0.1" + resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9" + integrity sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw== + dependencies: + graceful-fs "^4.1.2" + jsonfile "^4.0.0" + universalify "^0.1.0" + fs-minipass@^1.2.5: version "1.2.5" resolved "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d" @@ -4263,7 +4272,7 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2: version "4.1.11" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" -graceful-fs@^4.1.15: +graceful-fs@^4.1.15, graceful-fs@^4.1.6: version "4.1.15" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00" @@ -5612,6 +5621,13 @@ json5@^1.0.1: dependencies: minimist "^1.2.0" +jsonfile@^4.0.0: + version "4.0.0" + resolved "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb" + integrity sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss= + optionalDependencies: + graceful-fs "^4.1.6" + jsonify@~0.0.0: version "0.0.0" resolved "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73" @@ -5824,7 +5840,7 @@ lodash.uniq@^4.5.0: version "4.5.0" resolved "https://registry.npmjs.org/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773" -lodash@^4.0.0, lodash@^4.17.11, lodash@~4.17.10: +"lodash@>=3.5 <5", lodash@^4.0.0, lodash@^4.17.11, lodash@~4.17.10: version "4.17.11" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" @@ -8973,6 +8989,11 @@ unique-slug@^2.0.0: dependencies: imurmurhash "^0.1.4" +universalify@^0.1.0: + version "0.1.2" + resolved "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66" + integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg== + unpipe@1.0.0, unpipe@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" @@ -9295,6 +9316,15 @@ webpack-log@^2.0.0: ansi-colors "^3.0.0" uuid "^3.3.2" +webpack-manifest-plugin@^2.0.4: + version "2.0.4" + resolved "https://registry.npmjs.org/webpack-manifest-plugin/-/webpack-manifest-plugin-2.0.4.tgz#e4ca2999b09557716b8ba4475fb79fab5986f0cd" + integrity sha512-nejhOHexXDBKQOj/5v5IZSfCeTO3x1Dt1RZEcGfBSul891X/eLIcIVH31gwxPDdsi2Z8LKKFGpM4w9+oTBOSCg== + dependencies: + fs-extra "^7.0.0" + lodash ">=3.5 <5" + tapable "^1.0.0" + webpack-sources@^1.1.0: version "1.2.0" resolved "https://registry.npmjs.org/webpack-sources/-/webpack-sources-1.2.0.tgz#18181e0d013fce096faf6f8e6d41eeffffdceac2"