Skip to content

Commit a5a6929

Browse files
authored
Fix usage of "url" and "server" CLI arguments (#809)
1 parent 44d2746 commit a5a6929

File tree

7 files changed

+542
-398
lines changed

7 files changed

+542
-398
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
41
name: Build
52

63
on:
@@ -13,16 +10,12 @@ jobs:
1310
build:
1411
runs-on: ubuntu-latest
1512

16-
strategy:
17-
matrix:
18-
node-version: [12.x, 14.x, 16.x]
19-
2013
steps:
2114
- uses: actions/checkout@v2
22-
- name: Use Node.js ${{ matrix.node-version }}
15+
- name: Use Node.js LTS
2316
uses: actions/setup-node@v2
2417
with:
25-
node-version: ${{ matrix.node-version }}
18+
node-version: lts/*
2619
- run: yarn
2720
- run: yarn lint
2821
- run: yarn pwa

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,29 +168,29 @@ npm run build
168168

169169
5. Automatic browser reload using [BrowserSync](https://browsersync.io/)
170170

171-
- The setup assumes that you have a web server installed.
172-
- If you wish to use a proxy in browsersync you can do it using the `--url` CLI argument like this:
171+
- The setup assumes that you have a web server (MAMP, XAMPP, etc) installed.
172+
- If you wish to use a proxy in browsersync you can do it using the `url` CLI argument like this:
173173

174174
```sh
175-
yarn start --url=http://your.app
175+
yarn start --env url=http://your.app
176176
```
177177

178178
or
179179

180180
```sh
181-
npm start -- --url=http://your.app
181+
npm start --env url=http://your.app
182182
```
183183

184-
If you do not have a web server installed, then the files can be served via the browser-sync built-in server. In order to use this you need to pass a new CLI argument `--server` like this.
184+
If you do not have a web server installed, then the files can be served via the browser-sync built-in server. In order to use this you need to pass a new CLI argument `server` like this.
185185

186186
```sh
187-
yarn start --server
187+
yarn start --env server
188188
```
189189

190190
or
191191

192192
```sh
193-
npm start -- --server
193+
npm start --env server
194194
```
195195

196196
6) Images optimization using [Optisize](https://github.com/three11/optisize)

admin/data.json

Lines changed: 0 additions & 28 deletions
This file was deleted.

admin/index.html

Lines changed: 0 additions & 24 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-mpa",
3-
"version": "3.10.0",
3+
"version": "3.11.0",
44
"description": "Multi page app setup with webpack",
55
"scripts": {
66
"build": "webpack --mode=production",
@@ -34,7 +34,7 @@
3434
"devDependencies": {
3535
"@babel/core": "7.16.0",
3636
"@babel/preset-env": "7.16.4",
37-
"@three11/optisize": "1.3.0",
37+
"@three11/optisize": "2.0.0",
3838
"autoprefixer": "10.4.0",
3939
"babel-loader": "8.2.3",
4040
"browser-sync": "2.27.7",
@@ -72,8 +72,7 @@
7272
"webpack": "5.64.4",
7373
"webpack-cli": "4.9.1",
7474
"webpack-shell-plugin-next": "2.2.2",
75-
"webpack-spritesmith": "1.1.0",
76-
"yargs": "17.3.0"
75+
"webpack-spritesmith": "1.1.0"
7776
},
7877
"browserslist": [
7978
"> 1%",

webpack.config.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const { parse } = require('url');
55
const { resolve } = require('path');
66
const { readdirSync } = require('fs');
77

8-
const { argv } = require('yargs');
98
const magicImporter = require('node-sass-magic-importer');
109
const { ProvidePlugin } = require('webpack');
1110
const SpritesmithPlugin = require('webpack-spritesmith');
@@ -14,28 +13,6 @@ const WebpackShellPlugin = require('webpack-shell-plugin-next');
1413
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
1514
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
1615

17-
const { url, server, mode } = argv;
18-
const sourceMap = {
19-
sourceMap: mode === 'development'
20-
};
21-
22-
if (server) {
23-
exec('php index.php > index.html');
24-
}
25-
26-
const postcssOptions = {
27-
plugins: [
28-
require('postcss-easy-import'),
29-
require('postcss-url')({
30-
url: 'rebase'
31-
}),
32-
require('postcss-utilities'),
33-
require('postcss-flexbugs-fixes'),
34-
require('autoprefixer')()
35-
],
36-
...sourceMap
37-
};
38-
3916
const babelConfig = [
4017
{
4118
loader: 'babel-loader',
@@ -47,7 +24,7 @@ const babelConfig = [
4724
}
4825
];
4926

50-
const browserSyncConfig = {
27+
const browserSyncConfig = server => ({
5128
host: 'localhost',
5229
port: 3000,
5330
open: 'external',
@@ -84,7 +61,7 @@ const browserSyncConfig = {
8461
}
8562
},
8663
proxy: 'localhost'
87-
};
64+
});
8865

8966
const extractTextConfig = {
9067
filename: 'app.css'
@@ -117,10 +94,34 @@ if (svgs.length) {
11794
shellScripts.push('spritesh -q -i assets/images/svg -o ./assets/dist/sprite.svg -p svg-');
11895
}
11996

120-
module.exports = () => {
97+
module.exports = (env, argv) => {
98+
const { url, server } = env;
99+
const { mode } = argv;
100+
121101
const isDevelopment = mode === 'development';
122102
const isProduction = mode === 'production';
123103

104+
if (server) {
105+
exec('php index.php > index.html');
106+
}
107+
108+
const sourceMap = {
109+
sourceMap: isDevelopment
110+
};
111+
112+
const postcssOptions = {
113+
plugins: [
114+
require('postcss-easy-import'),
115+
require('postcss-url')({
116+
url: 'rebase'
117+
}),
118+
require('postcss-utilities'),
119+
require('postcss-flexbugs-fixes'),
120+
require('autoprefixer')()
121+
],
122+
...sourceMap
123+
};
124+
124125
if (isProduction) {
125126
postcssOptions.plugins.push(require('postcss-merge-rules'), require('cssnano')());
126127
}
@@ -135,7 +136,7 @@ module.exports = () => {
135136
}
136137

137138
const config = {
138-
mode: mode,
139+
mode,
139140
entry: ['./assets/styles/main.scss', './assets/scripts/main.js'],
140141
output: {
141142
path: resolve(__dirname, './assets/dist'),
@@ -206,21 +207,23 @@ module.exports = () => {
206207
stats: 'errors-only'
207208
};
208209

210+
const bsConfig = browserSyncConfig(server);
211+
209212
if (isDevelopment) {
210213
if (url) {
211-
browserSyncConfig.host = parse(url).hostname;
212-
browserSyncConfig.proxy = url;
214+
bsConfig.host = parse(url).hostname;
215+
bsConfig.proxy = url;
213216
}
214217

215218
if (server) {
216-
delete browserSyncConfig.host;
217-
delete browserSyncConfig.proxy;
219+
delete bsConfig.host;
220+
delete bsConfig.proxy;
218221

219-
browserSyncConfig.server = true;
222+
bsConfig.server = true;
220223
}
221224

222225
config.plugins.push(
223-
new BrowserSyncPlugin(browserSyncConfig, {
226+
new BrowserSyncPlugin(bsConfig, {
224227
reload: false
225228
})
226229
);

0 commit comments

Comments
 (0)