Skip to content

Commit 7c607da

Browse files
committed
feat: add pri.devService.pipeConfig, all modify webpackDevServerConfig
1 parent 10ff9a9 commit 7c607da

4 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/node/dev-service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { plugin } from '../utils/plugins';
2+
import { IDevServerConfigPipe } from '../utils/define';
23

34
export const on = (name: string, callback: (data?: any, resolve?: any, reject?: any) => void) => {
45
plugin.devServices.socketListeners.push({
56
name,
67
callback,
78
});
89
};
10+
11+
/**
12+
* It is better not to change the host、port and https here when use DLL
13+
* Because of the dllScript'src,is read host、devPort、useHttps from priconfig.json
14+
*/
15+
export const pipeConfig = (pipe: IDevServerConfigPipe) => {
16+
plugin.devServerConfigPipes.push(pipe);
17+
};

src/utils/define.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from 'path';
22
import { Configuration } from 'webpack';
3+
import * as WebpackDevServer from 'webpack-dev-server';
34
import { Entry } from './create-entry';
45

56
export interface PackageJson {
@@ -398,6 +399,10 @@ export interface IPluginModule {
398399

399400
export type IDevDllList = (list: string[]) => string[];
400401

402+
export type IDevServerConfigPipe = (
403+
config: WebpackDevServer.Configuration,
404+
) => WebpackDevServer.Configuration | Promise<WebpackDevServer.Configuration>;
405+
401406
export type IJestConfigPipe = (options: any) => any;
402407

403408
export type IAfterTestRun = (result?: any) => any;

src/utils/plugins.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
CommandRegister,
2121
ProjectType,
2222
IDevDllList,
23+
IDevServerConfigPipe,
2324
IJestConfigPipe,
2425
IAfterTestRun,
2526
} from './define';
@@ -94,6 +95,8 @@ export class IPluginConfig {
9495

9596
public devDllPipes: IDevDllList[] = [];
9697

98+
public devServerConfigPipes: IDevServerConfigPipe[] = [];
99+
97100
public jestConfigPipes: IJestConfigPipe[] = [];
98101

99102
public afterTestRunCallbacks: IAfterTestRun[] = [];

src/utils/webpack-dev-server.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as WebpackDevServer from 'webpack-dev-server';
1313
import * as SpeedMeasurePlugin from 'speed-measure-webpack-plugin';
1414
import * as ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
1515
import { globalState } from './global-state';
16+
import { plugin } from './plugins';
1617
import { tempPath, srcPath, packagesPath } from './structor-config';
1718
import { logInfo } from './log';
1819
import { getWebpackConfig, IOptions } from './webpack-config';
@@ -91,7 +92,7 @@ export const runWebpackDevServer = async (
9192
);
9293
}
9394

94-
const webpackDevServerConfig: WebpackDevServer.Configuration = {
95+
const defaultWebpackDevServerConfig: WebpackDevServer.Configuration = {
9596
host: globalState.sourceConfig.host,
9697
hot: opts.hot,
9798
hotOnly: opts.hot,
@@ -130,6 +131,9 @@ export const runWebpackDevServer = async (
130131
port: opts.devServerPort,
131132
contentBase: opts.contentBase,
132133
} as any;
134+
const webpackDevServerConfig = (await plugin.devServerConfigPipes.reduce(async (newConfig, fn) => {
135+
return fn(await newConfig);
136+
}, Promise.resolve(defaultWebpackDevServerConfig))) as any;
133137

134138
WebpackDevServer.addDevServerEntrypoints(webpackConfig as any, webpackDevServerConfig);
135139

@@ -144,17 +148,13 @@ export const runWebpackDevServer = async (
144148
const compiler = webpack(webpackConfig);
145149

146150
const devServer = new WebpackDevServer(compiler as any, webpackDevServerConfig);
151+
const { port, host, https } = webpackDevServerConfig;
147152

148-
devServer.listen(opts.devServerPort, globalState.sourceConfig.host, () => {
153+
devServer.listen(port, host, () => {
149154
let devUrl: string = null;
150-
const localSuggestUrl = urlJoin(
151-
`${opts.https || globalState.sourceConfig.useHttps ? 'https' : 'http'}://${globalState.sourceConfig.host}:${
152-
opts.devServerPort
153-
}`,
154-
globalState.sourceConfig.baseHref,
155-
);
155+
const localSuggestUrl = urlJoin(`${https ? 'https' : 'http'}://${host}:${port}`, globalState.sourceConfig.baseHref);
156156

157-
if (opts.devUrl === globalState.sourceConfig.host) {
157+
if (opts.devUrl === host) {
158158
devUrl = localSuggestUrl;
159159
} else if (opts.devUrl !== undefined) {
160160
({ devUrl } = opts);

0 commit comments

Comments
 (0)