Skip to content

Commit

Permalink
fix: 修复 react-script 配置判断 (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
SunLxy committed Mar 30, 2022
1 parent 5b274a6 commit 7628e73
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 85 deletions.
28 changes: 6 additions & 22 deletions core/README.md
Expand Up @@ -63,8 +63,8 @@ npm install -g create-kkt-ssr
# Create project, Using the template method
create-kkt-ssr my-app -e react-router-rematch
cd my-app # Enter the directory
npm run watch # Watch file
npm run server # Start service
npm install # Watch file
npm run start # Start service
```

> ⚠️ A perfect example [`react-router-rematch`](example/react-router-rematch) is recommended for production environments, This example is similar to [`Next.js`](https://github.com/zeit/next.js).
Expand Down Expand Up @@ -252,31 +252,16 @@ export default {

```

**Rewrite watchOptions**

[watchOptions 参数](https://webpack.docschina.org/configuration/watch/#watchoptions)

```ts

export default {

watchOptions:{ }

}

```

### DefinePlugin

1. OUTPUT_PUBLIC_PATH: The default is `path.join(process.cwd(),"dist")`
2. KKT_PUBLIC_DIR: The default is `process.env.KKT_PUBLIC_DIR` or `OUTPUT_PUBLIC_PATH`
3. HOST: The default is `process.env.HOST` or `localhost`
4. PORT: The default is `process.env.PORT` or `3000`
5. Dev_Server: The default is `undefined`
6. HOSTAPI: The default is `undefined` , 当运行`start`命令时值为`http://${HOST}:${PORT}`
7. process.env.PORT: 默认值 `3000`
8. process.env.HOSTAPI: The default is `undefined` , 当运行`start`命令时值为`http://${HOST}:${PORT}`
9. process.env.HOST: The default is `localhost`
5. HOSTAPI: The default is `undefined` , 当运行`start`命令时值为`http://${HOST}:${PORT}`
6. process.env.PORT: 默认值 `3000`
7. process.env.HOSTAPI: The default is `undefined` , 当运行`start`命令时值为`http://${HOST}:${PORT}`
8. process.env.HOST: The default is `localhost`

## KKTSSR Config

Expand All @@ -294,7 +279,6 @@ The root directory creates the `.kktssrrc.js` file.
| isUseOriginalConfig| `boolean` | `false` | 是否使用原始 react-script 下的配置 |
| isUseServerConfig | `boolean` | `true` | 是否需要 server 配置 |
| paths | `Partial<Paths>` | `{}` | [paths 脚本中webpack配置 使用的地址](https://github.com/kktjs/ssr/blob/4376baf1b5c365709addb313c4dd3ee314734baa/core/src/interface.ts#L6-L30) |
| watchOptions| `webpack.Configuration["watchOptions"]`| `{}`| watch 配置 |
| proxySetup | `(app:Application)=>({path:stirng\|string[],options:MockerOption})` | `undefined`| [mock 代理配置](https://github.com/jaywcjlove/mocker-api) |

```js
Expand Down
13 changes: 3 additions & 10 deletions core/src/index.ts
Expand Up @@ -8,7 +8,7 @@ import { OptionsProps } from "./interface"

function help() {
const { version } = require('../package.json');
console.log('\n Usage: \x1b[34;1mkkt-ssr\x1b[0m [build|watch|start] [--help|h]');
console.log('\n Usage: \x1b[34;1mkkt-ssr\x1b[0m [build|start] [--help|h]');
console.log('\n Displays help information.');
console.log('\n Options:\n');
console.log(' --version, -v ', 'Show version number');
Expand All @@ -24,12 +24,11 @@ function help() {

console.log('\n Example:\n');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m build');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m watch');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m start');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m build --s-ne');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m watch --s-ne');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m start --s-ne');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m build --s-st');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m watch --s-st');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m start --s-st');
console.log(' $ \x1b[35mkkt-ssr\x1b[0m start -o');
console.log(`\n \x1b[34;1m@kkt/ssr\x1b[0m \x1b[32;1mv${version || ''}\x1b[0m\n`);
}
Expand Down Expand Up @@ -130,12 +129,6 @@ interface SSRNCCArgs extends BuildArgs {
const build = await import("./script/build")
await build.default(options)

} else if (scriptName === 'watch') {

setEnv("development")
const watch = await import("./script/watch")
await watch.default(options)

} else if (scriptName === 'start') {

setEnv("development")
Expand Down
2 changes: 0 additions & 2 deletions core/src/overrides/index.ts
Expand Up @@ -47,8 +47,6 @@ let overrides: OverridesProps = {
overridesCommonWebpack: undefined,
// 最终自定义配置设置
overridesWebpack: undefined,
// 监听配置
watchOptions: {},
// 代理配置
proxySetup: undefined
};
Expand Down
30 changes: 0 additions & 30 deletions core/src/script/watch.ts

This file was deleted.

13 changes: 6 additions & 7 deletions core/src/utils/ProcessingConfig.ts
Expand Up @@ -12,13 +12,12 @@ export type GetWebpackConfig = (
overrides: OverridesProps,
nodeExternals: boolean,
split: boolean,
env: "development" | "production",
isWebpackDevServer: boolean,
options: OptionsProps
) => webpack.Configuration


const getWebpackConfig: GetWebpackConfig = (newConfig, type, overrides, nodeExternals, split, env, isWebpackDevServer, options) => {
const getWebpackConfig: GetWebpackConfig = (newConfig, type, overrides, nodeExternals, split, options) => {
const isDev = process.env.NODE_ENV === "development"
/** 入口 */
newConfig.entry = overrides[`${type}_path`]
/** 加载 进度条 plugin */
Expand All @@ -41,7 +40,7 @@ const getWebpackConfig: GetWebpackConfig = (newConfig, type, overrides, nodeExte
const httpPath = `http://${HOST}:${PORT}`

/** start 命令时候 配置前缀 为 devServer 端口 */
if (isWebpackDevServer && env === "development") {
if (isDev) {
out.publicPath = `${httpPath}/`
} else {
out.publicPath = `/`
Expand All @@ -53,11 +52,11 @@ const getWebpackConfig: GetWebpackConfig = (newConfig, type, overrides, nodeExte
/** start 命令下 生成 client 端 asset 文件 **/
let isCreateAsset = false;

if (isWebpackDevServer && type === "client" && env === "development") {
if (type === "client" && isDev) {
isCreateAsset = true
}
/** start 命令下 生成 server.js文件和 自动启动 server.js 服务 */
if (isWebpackDevServer && type === "server" && env === "development") {
if (type === "server" && isDev) {
newConfig = addServerPlugins(newConfig, out)
}
/** 重置 asset-manifest.json 文件内容 */
Expand All @@ -67,7 +66,7 @@ const getWebpackConfig: GetWebpackConfig = (newConfig, type, overrides, nodeExte

newConfig.module.exprContextCritical = false;
// 添加 define plugin
newConfig = AddDefinePlugin(newConfig, overrides, isWebpackDevServer)
newConfig = AddDefinePlugin(newConfig, overrides, isDev)

newConfig.plugins.push(
new CreateTemporaryAsset(`${overrides.output_path}/asset-${type}-manifest.json`)
Expand Down
18 changes: 11 additions & 7 deletions core/src/utils/getWebpackConfig.ts
Expand Up @@ -16,11 +16,13 @@ const { choosePort } = require('react-dev-utils/WebpackDevServerUtils');
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
const HOST = process.env.HOST || 'localhost';

export default async (env: "development" | "production", options: OptionsProps, isWebpackDevServer: boolean = false) => {
export default async (env: "development" | "production", options: OptionsProps,) => {

const isDev = env === "development"

/** 端口处理 */
let PORT;
if (env === "development") {
if (isDev) {
PORT = await choosePort(HOST, DEFAULT_PORT)
}

Expand All @@ -29,6 +31,8 @@ export default async (env: "development" | "production", options: OptionsProps,

process.env.PORT = PORT || "3000"
process.env.HOST = HOST || "localhost";
/** 是否使用原始 react-script 下的配置, 📢注意:这个不控制 server 配置, **/
const original = options.original || overrides.isUseOriginalConfig

const { overridesClientWebpack, overridesServerWebpack, overridesWebpack, overridesCommonWebpack, ...rest } = overrides

Expand All @@ -43,11 +47,11 @@ export default async (env: "development" | "production", options: OptionsProps,
let newConfigClient = configClient

// 控制 client 是否使用 ssr,默认情况下使用
if (!options.original || overrides.isUseOriginalConfig) {
if (!original) {

newConfigClient = getWebpackConfig(configClient, "client", overrides, options.clientNodeExternals, options.clientIsChunk, env, isWebpackDevServer, options)
newConfigClient = getWebpackConfig(configClient, "client", overrides, options.clientNodeExternals, options.clientIsChunk, options)
}
if (isWebpackDevServer && (!options.original || !overrides.isUseOriginalConfig)) {
if (!original) {
// 去除 source-map-loader
newConfigClient = removeSourceMapLoader(newConfigClient)
}
Expand All @@ -65,7 +69,7 @@ export default async (env: "development" | "production", options: OptionsProps,

const configServer = configFactory(env);

let newConfigServer = getWebpackConfig(configServer, "server", overrides, options.serverNodeExternals, options.serverIsChunk, env, isWebpackDevServer, options)
let newConfigServer = getWebpackConfig(configServer, "server", overrides, options.serverNodeExternals, options.serverIsChunk, options)

newConfigServer.devtool = false
newConfigServer.target = "node14"
Expand Down Expand Up @@ -98,7 +102,7 @@ export default async (env: "development" | "production", options: OptionsProps,
}

return {
compiler: isWebpackDevServer ? undefined : webpack(configArr),
compiler: isDev ? undefined : webpack(configArr),
config: configArr,
overrides
}
Expand Down
1 change: 0 additions & 1 deletion core/src/utils/plugins/index.ts
Expand Up @@ -144,7 +144,6 @@ export const AddDefinePlugin = (conf: WebpackConfiguration, overrides: Overrides
/** 当前端口 **/
PORT: JSON.stringify(PORT),
/** 是否是 start 模式 **/
Dev_Server: JSON.stringify(isWebpackDevServer),
/** start 模式下 文件获取地址 */
HOSTAPI: JSON.stringify(undefined),
"process.env.PORT": JSON.stringify(PORT),
Expand Down
8 changes: 2 additions & 6 deletions example/react-router-rematch-old/src/serverIndex.js
Expand Up @@ -9,16 +9,12 @@ import { createStore } from './store';

const assetPath = `${OUTPUT_PUBLIC_PATH}/asset-client-manifest.json`

// require 方式 打包报错
let assetsMainifest = {}
// if (FS.existsSync(assetPath)) {
assetsMainifest = new Function(`return ${FS.readFileSync(`${assetPath}`, "utf-8")}`)()
// }
const assetsMainifest = new Function(`return ${FS.readFileSync(`${assetPath}`, "utf-8")}`)()

const appDirectory = FS.realpathSync(process.cwd());
const resolveApp = (relativePath) => Path.resolve(appDirectory, relativePath);

const isDev = process.env.NODE_ENV === "development" && Dev_Server
const isDev = process.env.NODE_ENV === "development"

// const target = `http://${process.env.HOST}:${process.env.PORT}`
const target = `http://${process.env.HOST || "localhost"}:${process.env.PORT || 3000}`
Expand Down

0 comments on commit 7628e73

Please sign in to comment.