Skip to content

Commit df77c38

Browse files
committed
Merge branch 'release/1.8.0'
2 parents 03c2f01 + d5a5cb0 commit df77c38

File tree

7 files changed

+1029
-525
lines changed

7 files changed

+1029
-525
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-local",
3-
"version": "1.7.1",
3+
"version": "1.8.0",
44
"description": "Local Netlify service emulation",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -29,11 +29,13 @@
2929
"@types/node": "^10.11.7",
3030
"@types/serve-static": "^1.13.2",
3131
"@types/webpack": "^4.4.16",
32+
"@types/webpack-hot-middleware": "^2.16.4",
3233
"assert": "^1.4.1",
3334
"mocha": "^5.2.0",
3435
"ts-mocha": "^2.0.0",
3536
"tslint": "^5.12.0",
36-
"typescript": "^3.1.3"
37+
"typescript": "^3.1.3",
38+
"webpack-hot-middleware": "^2.24.3"
3739
},
3840
"dependencies": {
3941
"body-parser": "^1.18.3",
@@ -50,6 +52,6 @@
5052
"webpack": "^4.20.2"
5153
},
5254
"files": [
53-
".dist"
55+
"dist"
5456
]
5557
}

src/ts/cli.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as fs from "fs";
44
import * as path from "path";
5+
import * as webpack from "webpack";
56
import * as program from "commander";
67

78
import { Logger } from "./helper";
@@ -34,6 +35,7 @@ program
3435
const netlifyConfig = parseNetlifyConfig(program.netlify || "netlify.toml", {
3536
webpack: {
3637
config: program.webpack,
38+
hmr: program.hmr,
3739
},
3840
server: {
3941
static: program.static === undefined ? undefined : program.static === "false" ? false : true,
@@ -43,19 +45,23 @@ program
4345
},
4446
});
4547

48+
let compilers: Array<webpack.Compiler>;
49+
50+
if (netlifyConfig.plugins.local.webpack.config) {
51+
const webpackConfig = parseWebpackConfig(netlifyConfig.plugins.local.webpack.config);
52+
const webpackClient = new Webpack(webpackConfig);
53+
compilers = webpackClient.compilers;
54+
webpackClient.watch();
55+
}
56+
4657
const server = new Server({
4758
netlifyConfig: netlifyConfig,
4859
findAvailablePort: !program.hasOwnProperty("port"),
60+
compilers: compilers,
4961
});
5062

5163
await server.listen();
5264

53-
if (netlifyConfig.plugins.local.webpack.config) {
54-
const webpackConfig = parseWebpackConfig(netlifyConfig.plugins.local.webpack.config);
55-
const webpack = new Webpack(webpackConfig);
56-
webpack.watch();
57-
}
58-
5965
})()
6066
.catch((error) => {
6167
Logger.error(error);
@@ -75,6 +81,7 @@ program
7581
const netlifyConfig = parseNetlifyConfig(program.netlify || "netlify.toml", {
7682
webpack: {
7783
config: program.webpack,
84+
hmr: program.hmr,
7885
},
7986
});
8087

@@ -103,6 +110,7 @@ program
103110
const netlifyConfig = parseNetlifyConfig(program.netlify || "netlify.toml", {
104111
webpack: {
105112
config: program.webpack,
113+
hmr: program.hmr,
106114
},
107115
});
108116

src/ts/netlify.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export namespace Netlify {
2020
export interface Local {
2121
webpack?: {
2222
config: string;
23+
hmr: boolean;
2324
};
2425
server?: {
2526
static?: boolean;

src/ts/utility/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const composeWebpackEntry = (netlifyConfig: Netlify.Config) => {
2929
}, {});
3030
};
3131

32-
export const composeWebpackOutput = (netlifyConfig: Netlify.Config): { path: string, filename: string, libraryTarget: webpack.LibraryTarget} => {
32+
export const composeWebpackOutput = (netlifyConfig: Netlify.Config): webpack.Output => {
3333
return {
3434
path: path.join(process.cwd(), netlifyConfig.build.functions),
3535
filename: "[name].js",
@@ -93,6 +93,7 @@ export const parseNetlifyPluginLocalConfig = (netlifyConfig: Netlify.Config, ove
9393
const netlifyPluginLocalConfig: Netlify.Plugins.Local = {
9494
webpack: {
9595
config: undefined,
96+
hmr: false,
9697
},
9798
server: {
9899
static: true,
@@ -113,6 +114,9 @@ export const parseNetlifyPluginLocalConfig = (netlifyConfig: Netlify.Config, ove
113114
if (netlifyConfig.plugins.local.webpack.config !== undefined) {
114115
netlifyPluginLocalConfig.webpack.config = netlifyConfig.plugins.local.webpack.config;
115116
}
117+
if (netlifyConfig.plugins.local.webpack.hmr !== undefined) {
118+
netlifyPluginLocalConfig.webpack.hmr = netlifyConfig.plugins.local.webpack.hmr;
119+
}
116120
}
117121
if (netlifyConfig.plugins.local.server !== undefined) {
118122
if (netlifyConfig.plugins.local.server.static !== undefined) {

src/ts/utility/server.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "path";
22
import * as express from "express";
3+
import * as webpack from "webpack";
34
import * as bodyParser from "body-parser";
45
import * as serveStatic from "serve-static";
56
import * as queryString from "querystring";
@@ -8,9 +9,11 @@ import * as http from "http";
89
import * as https from "https";
910
import { URL } from "url";
1011
import * as UrlPattern from "url-pattern";
12+
// @ts-ignore
1113
import * as getPort from "get-port";
1214
// @ts-ignore
1315
import * as expressHttpProxy from "express-http-proxy";
16+
import * as webpackHotMiddleware from "webpack-hot-middleware";
1417

1518
import { Logger } from "../helper";
1619
import { Netlify } from "../netlify";
@@ -185,6 +188,10 @@ export class Server {
185188
this.express.use(bodyParser.raw({ limit: "6mb" }));
186189
this.express.use(bodyParser.text({ limit: "6mb", type: "*/*" }));
187190

191+
if(this.options.netlifyConfig.plugins.local.webpack.hmr) {
192+
this.webpackHotMiddleware(this.options.compilers);
193+
}
194+
188195
const hardRedirects = this.options.netlifyConfig.redirects.filter((redirect) => redirect.force === true);
189196
const softRedirects = this.options.netlifyConfig.redirects.filter((redirect) => redirect.force === false);
190197

@@ -195,6 +202,14 @@ export class Server {
195202
this.routeRedirects(softRedirects);
196203
}
197204

205+
public webpackHotMiddleware(compilers: Array<webpack.Compiler>): void {
206+
const clientCompiler = compilers.find(compiler => compiler.name === "client");
207+
const clientMiddleware = webpackHotMiddleware(clientCompiler, {
208+
log: false,
209+
});
210+
this.express.use(clientMiddleware);
211+
}
212+
198213
/**
199214
* Static Router
200215
*/
@@ -343,6 +358,7 @@ export namespace Server {
343358
export interface Options {
344359
netlifyConfig: Netlify.Config;
345360
findAvailablePort?: boolean;
361+
compilers?: Array<webpack.Compiler>;
346362
}
347363
export interface Certificates {
348364
key: string;

src/ts/utility/webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class Webpack {
1010
this.initialize();
1111
}
1212
private configs: Array<Webpack.Config>;
13-
private compilers: Array<webpack.Compiler>;
13+
public compilers: Array<webpack.Compiler>;
1414

1515
public initialize(): void {
1616
this.compilers = this.configs.map((config) => webpack(config));

0 commit comments

Comments
 (0)