Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
feat: autodetect hostname from .env
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Jan 20, 2022
1 parent 2c92cea commit 435452d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 72 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Install the extension:
npm install laravel-mix-valet --save-dev
```

Next require the extension in your `webpack.mix.js` and call `valet(hostname)`:
Next require the extension in your `webpack.mix.js` and call `valet()`:

```js
const mix = require('laravel-mix');
require('laravel-mix-valet');

mix.js('resources/js/app.js', 'public/js')
.valet('mysite.test'); // replace with your hostname
.valet();
```

Alternatively, you can pass a config object instead, which will be merged with the defaults below:
Expand All @@ -29,17 +29,15 @@ require('laravel-mix-valet');

mix.js('resources/js/app.js', 'public/js')
.valet({
host: 'mysite.test', // replace with your hostname
port: 8080,
...
host: 'othersite.test',
port: 12345,
});
```

## Options

| Option | Default |
| -------------------------- | ------- |
| **host** | null |
| **port** | 8080 |
| **https** | true |
| **removeHotTrailingSlash** | true |
| Option | Default |
| --------- | ----------------------- |
| **host** | Hostname from `APP_URL` |
| **port** | 8080 |
| **https** | true |
112 changes: 53 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,33 @@
const path = require('path');
const fs = require('fs');
const mix = require('laravel-mix');
const Log = require('laravel-mix/src/Log');
const merge = require('lodash/merge');

class LaravelMixValet {
constructor() {
this.config = {};
this.config = {
host: this.getAppHost(),
port: 8080,
https: true,
};
}

name() {
return 'valet';
}

register(config = {}) {
const defaults = {
host: null,
port: 8080,
https: true,
removeHotTrailingSlash: true,
};

if (typeof config === 'string') {
config = { host: config };
}

if (!config.host) {
Log.error(
'Valet host not configured. Disabling laravel-mix-valet...'
);
if (!this.isHot()) {
return;
}

let key = this.getCertPath('key', config.host);
if (!fs.existsSync(key)) {
Log.message({
text: `Could not find key at ${key}. Disabling laravel-mix-valet...`,
type: 'warn',
});
return;
}

let crt = this.getCertPath('crt', config.host);
if (!fs.existsSync(crt)) {
Log.message({
text: `Could not find certificate at ${crt}. Disabling laravel-mix-valet...`,
type: 'warn',
});
return;
if (typeof config === 'string') {
config = { host: config };
}

this.config = { ...defaults, ...config };
merge(this.config, config);

Config.merge({
mix.options({
hmrOptions: {
https: this.config.https,
host: this.config.host,
Expand All @@ -61,53 +37,71 @@ class LaravelMixValet {
}

boot() {
if (!Config.hmr || !this.config.removeHotTrailingSlash) {
return;
}

const hotFile = path.resolve('public', 'hot');

if (!fs.existsSync(hotFile)) {
if (!this.isHot()) {
return;
}

let url = fs.readFileSync(hotFile, 'utf8');

fs.writeFileSync(hotFile, url.replace(/\/$/gm, ''));
this.updateHotFile();
}

webpackConfig(config) {
if (!Config.hmr) {
if (!this.isHot()) {
return;
}

config.devServer.hot = true;

config.output.publicPath =
(this.config.https ? 'https' : 'http') +
`://${this.config.host}:${this.config.port}/`;
config.output.publicPath = this.hotUrl();

if (this.config.https) {
config.devServer.https = {
key: fs.readFileSync(this.getCertPath('key')),
cert: fs.readFileSync(this.getCertPath('crt')),
key: this.loadCert('key'),
cert: this.loadCert('crt'),
};
}
}

getCertPath(ext, host) {
if (!['key', 'crt'].includes(ext)) {
return;
loadCert(ext) {
const cert = path.resolve(
process.env.HOME,
`.config/valet/Certificates/${this.config.host}.${ext}`
);

if (!fs.existsSync(cert)) {
throw new Error(`Could not find ${cert}`);
}

if (!host) {
host = this.config.host;
return fs.readFileSync(cert);
}

updateHotFile() {
const hotFile = path.resolve('public', 'hot');

if (!fs.existsSync(hotFile)) {
return;
}

return path.resolve(
process.env.HOME,
`.config/valet/Certificates/${host}.${ext}`
fs.writeFileSync(hotFile, this.hotUrl());
}

hotUrl() {
return (
(this.config.https ? 'https' : 'http') +
`://${this.config.host}:${this.config.port}`
);
}

isHot() {
return process.argv.includes('--hot');
}

getAppHost() {
try {
return new URL(process.env.APP_URL).hostname;
} catch (error) {
return null;
}
}
}

mix.extend('valet', new LaravelMixValet());
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"prepare": "husky install"
},
"peerDependencies": {
"laravel-mix": "^6.0.0"
"laravel-mix": "^6.0.0",
"lodash": "^4.17.20"
},
"devDependencies": {
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"husky": "^7.0.1"
"husky": "^7.0.0"
},
"config": {
"commitizen": {
Expand Down

0 comments on commit 435452d

Please sign in to comment.