Skip to content

Commit 91a3c28

Browse files
authored
feat: 打印 dev server 可访问 url (#178)
1 parent 1b345db commit 91a3c28

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

npm-shrinkwrap.json

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "fec-builder",
3-
"version": "2.7.1",
3+
"version": "2.7.2",
44
"bin": {
55
"fec-builder": "./lib/bin.js"
66
},
77
"scripts": {
88
"dev": "ts-node ./src/bin.ts -r ./samples/typescript-react",
99
"build": "rm -rf lib && tsc && chmod +x lib/bin.js",
1010
"build-config-doc": "ts-node src/schema2md.ts",
11-
"prepublishOnly": "npm run build"
11+
"prepublishOnly": "npm run build",
12+
"lint": "tsc --noEmit"
1213
},
1314
"engines": {
1415
"node": ">=12"
@@ -44,6 +45,7 @@
4445
"log4js": "^6.3.0",
4546
"mini-css-extract-plugin": "^1.3.9",
4647
"mustache": "^4.2.0",
48+
"picocolors": "^1.1.1",
4749
"postcss-loader": "^4.3.0",
4850
"postcss-preset-env": "~7.2.3",
4951
"qiniu": "^7.3.2",
@@ -78,6 +80,6 @@
7880
"license": "Apache-2.0",
7981
"repository": {
8082
"type": "git",
81-
"url": "https://github.com/Front-End-Engineering-Cloud/builder.git"
83+
"url": "https://github.com/qiniu/builder.git"
8284
}
8385
}

src/bin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { Env, setEnv } from './utils/build-env'
88
import logger from './utils/logger'
99
import { setNeedAnalyze } from './utils/build-conf'
1010

11+
const packageInfo = require('../package.json')
12+
1113
// 禁掉 auto freeze,否则有的插件改数据时会异常,
1214
// 比如 postcss-loader 会去 delete options 上的 plugins 字段;
1315
// 详情见 https://immerjs.github.io/immer/docs/freezing
@@ -155,8 +157,8 @@ Object.entries(commands).forEach(([name, { desc, handler, isDefault }]) => {
155157
})
156158
})
157159

158-
// enable --version
159-
parser.version()
160+
parser
161+
.version(packageInfo.version).alias('v', 'version') // enable --version
160162
.help('h').alias('h', 'help')
161163
.locale('en')
162164
.argv

src/serve.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getPageFilename, getPathFromUrl, logLifecycle, watchFile } from './util
1313
import { getConfigForDevServer } from './webpack'
1414
import { BuildConfig, DevProxy, findBuildConfig, watchBuildConfig } from './utils/build-conf'
1515
import { entries, mapValues } from 'lodash'
16+
import colors from 'picocolors'
1617
import { abs } from './utils/paths'
1718

1819
// 业务项目的配置文件,变更时需要重启 server
@@ -55,6 +56,7 @@ async function runDevServer(port: number) {
5556
const webpackConfig = await getConfigForDevServer()
5657
logger.debug('webpack config:', webpackConfig)
5758

59+
const host = '0.0.0.0'
5860
const devServerConfig: WebpackDevServer.Configuration = {
5961
hotOnly: true,
6062
// 方便开发调试
@@ -64,7 +66,7 @@ async function runDevServer(port: number) {
6466
// 从而正确地建立 hot module replace 依赖的 ws 链接及其它请求,逻辑见:
6567
// 这里之所以要求使用页面的 window.location 信息,是因为 builder 在容器中 serve 时端口会被转发,
6668
// 即可能配置 port 为 80,在(宿主机)浏览器中通过 8080 端口访问
67-
public: '0.0.0.0:0',
69+
public: `${host}:0`,
6870
publicPath: getPathFromUrl(buildConfig.publicUrl),
6971
stats: 'errors-only',
7072
proxy: getProxyConfig(buildConfig.devProxy),
@@ -81,9 +83,8 @@ async function runDevServer(port: number) {
8183
})
8284
})
8385

84-
const host = '0.0.0.0'
8586
server.listen(port, host, () => {
86-
logger.info(`Server started on ${host}:${port}`)
87+
logger.info(`Server started on ${colors.cyan(`http://localhost:${port}`)}`)
8788
})
8889

8990
return () => new Promise<void>(resolve => {

src/webpack/swc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { mergeWith } from 'lodash'
77
import { parse } from 'jsonc-parser'
88

99
import { shouldAddGlobalPolyfill, AddPolyfill } from '../utils/build-conf'
10-
import { getBuildRoot } from '../utils/paths'
10+
import { abs, getBuildRoot } from '../utils/paths'
1111

1212
/** 读取 tsconfig.json 文件获取 compilerOptions 配置 */
1313
function getTsCompilerOptions(): CompilerOptions | null {
@@ -78,7 +78,7 @@ function transformTsCompilerOptions(options: CompilerOptions | null): SwcOptions
7878
},
7979
},
8080
paths,
81-
baseUrl
81+
...(baseUrl && { baseUrl: abs(baseUrl) })
8282
}
8383
}
8484
}

0 commit comments

Comments
 (0)