Skip to content

Commit 86814b9

Browse files
authored
Fix usage of "url" and "server" CLI arguments (#1046)
1 parent aaf281d commit 86814b9

File tree

5 files changed

+536
-327
lines changed

5 files changed

+536
-327
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
@@ -166,29 +166,29 @@ npm run build
166166

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

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

172172
```sh
173-
yarn start --url=http://your.app
173+
yarn start --env url=http://your.app
174174
```
175175

176176
or
177177

178178
```sh
179-
npm start -- --url=http://your.app
179+
npm start --env url=http://your.app
180180
```
181181

182-
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.
182+
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.
183183

184184
```sh
185-
yarn start --server
185+
yarn start --env server
186186
```
187187

188188
or
189189

190190
```sh
191-
npm start -- --server
191+
npm start --env server
192192
```
193193

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

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-ts",
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",
@@ -32,7 +32,7 @@
3232
"itscss": "2.0.1"
3333
},
3434
"devDependencies": {
35-
"@three11/optisize": "1.3.0",
35+
"@three11/optisize": "2.0.0",
3636
"@types/node": "16.11.11",
3737
"@typescript-eslint/eslint-plugin": "5.5.0",
3838
"@typescript-eslint/parser": "5.5.0",
@@ -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

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

8-
const { argv } = require('yargs');
9-
const webpack = require('webpack');
108
const magicImporter = require('node-sass-magic-importer');
9+
const { ProvidePlugin } = require('webpack');
1110
const SpritesmithPlugin = require('webpack-spritesmith');
1211
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
1312
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 cssnano = require('cssnano');
18-
const postcssURL = require('postcss-url');
19-
const autoprefixer = require('autoprefixer');
20-
const postcssUtilities = require('postcss-utilities');
21-
const postcssEasyImport = require('postcss-easy-import');
22-
const postcssMergeRules = require('postcss-merge-rules');
23-
const postcssWatchFolder = require('postcss-watch-folder');
24-
const postcssFlexbugsFixed = require('postcss-flexbugs-fixes');
25-
26-
const { url, server, mode } = argv;
27-
const sourceMap = {
28-
sourceMap: mode === 'development'
29-
};
30-
31-
if (server) {
32-
exec('php index.php > index.html');
33-
}
34-
35-
const postcssOptions = {
36-
plugins: [postcssURL({ url: 'rebase' }), autoprefixer(), postcssUtilities, postcssEasyImport, postcssFlexbugsFixed],
37-
...sourceMap
38-
};
39-
40-
const browserSyncConfig = {
16+
const browserSyncConfig = server => ({
4117
host: 'localhost',
4218
port: 3000,
4319
open: 'external',
@@ -74,7 +50,7 @@ const browserSyncConfig = {
7450
}
7551
},
7652
proxy: 'localhost'
77-
};
53+
});
7854

7955
const extractTextConfig = {
8056
filename: 'app.css'
@@ -108,25 +84,49 @@ if (svgs.length) {
10884
shellScripts.push('spritesh -q -i assets/images/svg -o ./assets/dist/sprite.svg -p svg-');
10985
}
11086

111-
module.exports = () => {
87+
module.exports = (env, argv) => {
88+
const { url, server } = env;
89+
const { mode } = argv;
90+
11291
const isDevelopment = mode === 'development';
11392
const isProduction = mode === 'production';
11493

94+
if (server) {
95+
exec('php index.php > index.html');
96+
}
97+
98+
const sourceMap = {
99+
sourceMap: mode === 'development'
100+
};
101+
102+
const postcssOptions = {
103+
plugins: [
104+
require('postcss-easy-import'),
105+
require('postcss-url')({
106+
url: 'rebase'
107+
}),
108+
require('postcss-utilities'),
109+
require('postcss-flexbugs-fixes'),
110+
require('autoprefixer')()
111+
],
112+
...sourceMap
113+
};
114+
115115
if (isProduction) {
116-
postcssOptions.plugins.push(postcssMergeRules, cssnano());
116+
postcssOptions.plugins.push(require('postcss-merge-rules'), require('cssnano')());
117117
}
118118

119119
if (isDevelopment) {
120120
postcssOptions.plugins.push(
121-
postcssWatchFolder({
121+
require('postcss-watch-folder')({
122122
folder: './assets/styles',
123123
main: './assets/styles/main.scss'
124124
})
125125
);
126126
}
127127

128128
const config = {
129-
mode: mode,
129+
mode,
130130
entry: ['./assets/styles/main.scss', './assets/scripts/main.ts'],
131131
output: {
132132
path: resolve(__dirname, './assets/dist'),
@@ -178,7 +178,7 @@ module.exports = () => {
178178
]
179179
},
180180
plugins: [
181-
new webpack.ProvidePlugin({
181+
new ProvidePlugin({
182182
$: 'jquery',
183183
jQuery: 'jquery',
184184
'window.jQuery': 'jquery'
@@ -201,21 +201,23 @@ module.exports = () => {
201201
stats: 'errors-only'
202202
};
203203

204+
const bsConfig = browserSyncConfig(server);
205+
204206
if (isDevelopment) {
205207
if (url) {
206-
browserSyncConfig.host = parse(url).hostname;
207-
browserSyncConfig.proxy = url;
208+
bsConfig.host = parse(url).hostname;
209+
bsConfig.proxy = url;
208210
}
209211

210212
if (server) {
211-
delete browserSyncConfig.host;
212-
delete browserSyncConfig.proxy;
213+
delete bsConfig.host;
214+
delete bsConfig.proxy;
213215

214-
browserSyncConfig.server = true;
216+
bsConfig.server = true;
215217
}
216218

217219
config.plugins.push(
218-
new BrowserSyncPlugin(browserSyncConfig, {
220+
new BrowserSyncPlugin(bsConfig, {
219221
reload: false
220222
})
221223
);

0 commit comments

Comments
 (0)