在代客安全站点 SSL 错误中使用 laravel-mix 进行 Webpack 热重载

Sal*_*301 3 laravel webpack webpack-hmr laravel-mix

我正在运行Laravel Valet以在本地托管站点,并运行Laravel Mix来编译资产并使用 Webpack 开发服务器执行 HMR

.dev通过以下方式在本地保护了该站点

valet secure
Run Code Online (Sandbox Code Playgroud)

{{ mix('js/app.js') }} 运行时调用没问题npm run watch

但是每当我想通过运行hotnpm 脚本来利用热重载时,我都会得到这个

GET https://localhost:8080//css/app.css net::ERR_CERT_AUTHORITY_INVALID

GitHub问题建议添加--https 标志,我试过了,也--http

我什至通过--disable-host-check标志禁用了主机检查并清除了所有可能的缓存,甚至尝试了新的git clone但无济于事

这是我的 package.json

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --https --disable-host-check --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "@kazupon/vue-i18n-loader": "^0.3.0",
        "cross-env": "^5.1",
        "eslint-plugin-vue": "^5.2.3",
        "laravel-mix": "^4.0.7",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.21.0",
        "sass-loader": "^7.1.0",
        "vue-template-compiler": "^2.6.10"
    },
    "dependencies": {
        "algoliasearch": "^3.33.0",
        "axios": "^0.19.0",
        "font-awesome": "^4.7.0",
        "jquery": "^2.1.1",
        "lato-webfont": "^2.15.1",
        "modernizr": "^3.7.1",
        "raleway-webfont": "^3.0.1",
        "raphael": "^2.1.4",
        "vlightbox": "^2.0.2",
        "vue": "^2.5.17",
        "vue-i18n": "^8.12.0",
        "vue-instantsearch": "^2.2.1"
    }
}
Run Code Online (Sandbox Code Playgroud)

webpack.mix.js如果它是有帮助

const mix = require('laravel-mix');

// Extend Mix with the "i18n" method, that loads the vue-i18n-loader
mix.extend('i18n', new class {
    webpackRules() {
        return [{
            resourceQuery: /blockType=i18n/,
            type: 'javascript/auto',
            loader: '@kazupon/vue-i18n-loader',
        }, ];
    }
}(), );

// Call the .i18n() (to load the loader) before .js(..., ...)
mix.i18n()
   .js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css');
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?这个可以复现吗?我也应该找到一种方法来确保安全localhost:8080吗?

Sal*_*301 7

So to get this working, instruct laravel mix to use specific domain and port for HMR in the options object

webpack.mix.js

// Get the APP_URL from .env and remove protocol
let url = process.env.APP_URL.replace(/(^\w+:|^)\/\//, '');
mix.options({
   hmrOptions: {
       host: url,
       port: 8080 // Can't use 443 here because address already in use
   }
});
Run Code Online (Sandbox Code Playgroud)

Keep the --https flag to instruct webpack-dev-server as to what protocol to use, however, remove --disable-host-check because it's redundant (Google Chrome has strict SSL policy for .dev domains anyway)

Now given that valet secure generates SSL key and certificate for each domain, instruct webpack-dev-server to use them as well in the hot script of package.json

"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js
--inline --hot --https
--key /home/caddy/.valet/Certificates/laravel.dev.key
--cert /home/caddy/.valet/Certificates/laravel.dev.crt --config=node_modules/laravel-mix/setup/webpack.config.js",
Run Code Online (Sandbox Code Playgroud)

replace /home/caddy/ by your own absolute path

run

"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js
--inline --hot --https
--key /home/caddy/.valet/Certificates/laravel.dev.key
--cert /home/caddy/.valet/Certificates/laravel.dev.crt --config=node_modules/laravel-mix/setup/webpack.config.js",
Run Code Online (Sandbox Code Playgroud)

Now hot reloading is working fine with secured valet sites

Source