Skip to content

Commit 2655f6d

Browse files
authored
Fix usage of "url" and "server" CLI arguments (#698)
1 parent 9d27b5f commit 2655f6d

File tree

5 files changed

+523
-303
lines changed

5 files changed

+523
-303
lines changed

.github/workflows/build.yml

+2-9
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

+6-6
Original file line numberDiff line numberDiff line change
@@ -169,29 +169,29 @@ npm run build
169169

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

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

175175
```sh
176-
yarn start --url=http://your.app
176+
yarn start --env url=http://your.app
177177
```
178178

179179
or
180180

181181
```sh
182-
npm start -- --url=http://your.app
182+
npm start --env url=http://your.app
183183
```
184184

185-
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.
185+
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.
186186

187187
```sh
188-
yarn start --server
188+
yarn start --env server
189189
```
190190

191191
or
192192

193193
```sh
194-
npm start -- --server
194+
npm start --env server
195195
```
196196

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

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-mpa-next",
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",
@@ -76,8 +76,7 @@
7676
"webpack": "5.64.4",
7777
"webpack-cli": "4.9.1",
7878
"webpack-shell-plugin-next": "2.2.2",
79-
"webpack-spritesmith": "1.1.0",
80-
"yargs": "17.3.0"
79+
"webpack-spritesmith": "1.1.0"
8180
},
8281
"browserslist": [
8382
"> 1%",

webpack.config.js

+50-47
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,13 @@ const { parse } = require('url');
55
const { resolve } = require('path');
66
const { readdirSync } = require('fs');
77

8-
const { argv } = require('yargs');
98
const { ProvidePlugin } = require('webpack');
109
const SpritesmithPlugin = require('webpack-spritesmith');
1110
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
1211
const WebpackShellPlugin = require('webpack-shell-plugin-next');
1312
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
1413
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
1514

16-
const { url, server, mode } = argv;
17-
const sourceMap = {
18-
sourceMap: mode === 'development'
19-
};
20-
21-
if (server) {
22-
exec('php index.php > index.html');
23-
}
24-
25-
const postcssOptions = {
26-
ident: 'postcss',
27-
plugins: [
28-
require('autoprefixer'),
29-
require('postcss-mixins'),
30-
require('postcss-easy-import'),
31-
require('postcss-url')({
32-
url: 'rebase'
33-
}),
34-
require('postcss-normalize')({
35-
forceImport: true
36-
}),
37-
require('postcss-color-mod-function'),
38-
require('postcss-each-variables'),
39-
require('postcss-each'),
40-
require('postcss-for'),
41-
require('postcss-nested'),
42-
require('postcss-extend'),
43-
require('postcss-utilities'),
44-
require('postcss-flexbugs-fixes'),
45-
require('postcss-merge-rules'),
46-
require('postcss-calc'),
47-
require('postcss-custom-media')
48-
],
49-
...sourceMap
50-
};
51-
5215
const babelConfig = [
5316
{
5417
loader: 'babel-loader',
@@ -60,7 +23,7 @@ const babelConfig = [
6023
}
6124
];
6225

63-
const browserSyncConfig = {
26+
const browserSyncConfig = server => ({
6427
host: 'localhost',
6528
port: 3000,
6629
open: 'external',
@@ -97,7 +60,7 @@ const browserSyncConfig = {
9760
}
9861
},
9962
proxy: 'localhost'
100-
};
63+
});
10164

10265
const extractTextConfig = {
10366
filename: 'app.css'
@@ -130,10 +93,48 @@ if (svgs.length) {
13093
shellScripts.push('spritesh -q -i assets/images/svg -o ./assets/dist/sprite.svg -p svg-');
13194
}
13295

133-
module.exports = () => {
96+
module.exports = (env, argv) => {
97+
const { url, server } = env;
98+
const { mode } = argv;
99+
134100
const isDevelopment = mode === 'development';
135101
const isProduction = mode === 'production';
136102

103+
if (server) {
104+
exec('php index.php > index.html');
105+
}
106+
107+
const sourceMap = {
108+
sourceMap: isDevelopment
109+
};
110+
111+
const postcssOptions = {
112+
ident: 'postcss',
113+
plugins: [
114+
require('autoprefixer'),
115+
require('postcss-mixins'),
116+
require('postcss-easy-import'),
117+
require('postcss-url')({
118+
url: 'rebase'
119+
}),
120+
require('postcss-normalize')({
121+
forceImport: true
122+
}),
123+
require('postcss-color-mod-function'),
124+
require('postcss-each-variables'),
125+
require('postcss-each'),
126+
require('postcss-for'),
127+
require('postcss-nested'),
128+
require('postcss-extend'),
129+
require('postcss-utilities'),
130+
require('postcss-flexbugs-fixes'),
131+
require('postcss-merge-rules'),
132+
require('postcss-calc'),
133+
require('postcss-custom-media')
134+
],
135+
...sourceMap
136+
};
137+
137138
if (isProduction) {
138139
postcssOptions.plugins.push(require('postcss-merge-rules'), require('cssnano')());
139140
}
@@ -148,7 +149,7 @@ module.exports = () => {
148149
}
149150

150151
const config = {
151-
mode: mode,
152+
mode,
152153
entry: ['./assets/styles/main.css', './assets/scripts/main.js'],
153154
output: {
154155
path: resolve(__dirname, './assets/dist'),
@@ -210,21 +211,23 @@ module.exports = () => {
210211
stats: 'errors-only'
211212
};
212213

214+
const bsConfig = browserSyncConfig(server);
215+
213216
if (isDevelopment) {
214217
if (url) {
215-
browserSyncConfig.host = parse(url).hostname;
216-
browserSyncConfig.proxy = url;
218+
bsConfig.host = parse(url).hostname;
219+
bsConfig.proxy = url;
217220
}
218221

219222
if (server) {
220-
delete browserSyncConfig.host;
221-
delete browserSyncConfig.proxy;
223+
delete bsConfig.host;
224+
delete bsConfig.proxy;
222225

223-
browserSyncConfig.server = true;
226+
bsConfig.server = true;
224227
}
225228

226229
config.plugins.push(
227-
new BrowserSyncPlugin(browserSyncConfig, {
230+
new BrowserSyncPlugin(bsConfig, {
228231
reload: false
229232
})
230233
);

0 commit comments

Comments
 (0)