Skip to content

Commit

Permalink
feat: enable msw for GitHub page and fix public folder miss files whe…
Browse files Browse the repository at this point in the history
…n bundle
  • Loading branch information
qinsong77 committed Jan 4, 2024
1 parent b886b3b commit 098c6b0
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:

env:
PUBLIC_PATH: /webpack5-react-template/
MSW_ENABLE: true

steps:
- name: Checkout
Expand Down
1 change: 1 addition & 0 deletions env/development.env → env/.env.development
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
APP_URL=url
PUBLIC_PATH=/
MSW_ENABLE=true
1 change: 1 addition & 0 deletions env/production.env → env/.env.production
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
## this should not commit actually
# PUBLIC_PATH=/webpack5-react-template/
#MSW_ENABLE=true
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"babel-jest": "^29.7.0",
"babel-loader": "^9.1.3",
"commitizen": "^4.3.0",
"copy-webpack-plugin": "^11.0.0",
"core-js": "^3.29.0",
"cross-env": "^7.0.3",
"css-loader": "^6.7.1",
Expand Down
36 changes: 35 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import App from './App'
import './style/index.css'

async function prepareApp() {
if (
process.env.NODE_ENV === 'development' ||
process.env.NODE_ENV === 'test'
) {
if (process.env.MSW_ENABLE) {
const { worker } = await import('./__mocks__/browser')
return worker.start()
}
Expand Down
1 change: 1 addition & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare namespace NodeJS {
readonly NODE_ENV: 'development' | 'production' | 'test'
readonly PUBLIC_PATH: string
readonly DEBUG: boolean
readonly MSW_ENABLE: boolean
}
}

Expand Down
4 changes: 2 additions & 2 deletions webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const NODE_ENV = process.env.NODE_ENV

export const IS_DEV = NODE_ENV === 'development'

const ENV_CONFIG_PATH = path.resolve(ROOT_PATH, 'env', `${NODE_ENV}.env`)
const ENV_CONFIG_PATH = path.resolve(ROOT_PATH, 'env', `.env.${NODE_ENV}`)

// node 读取env 配置
loadConfig({
Expand All @@ -17,7 +17,7 @@ loadConfig({
debug: process.env.DEBUG as unknown as boolean,
})

export const envKeys = ['PUBLIC_PATH'] // todo: coupled with env
export const envKeys = ['NODE_ENV', 'PUBLIC_PATH', 'MSW_ENABLE'] // todo: coupled with env

export const PUBLIC_PATH = process.env.PUBLIC_PATH ?? '/'

Expand Down
14 changes: 14 additions & 0 deletions webpack/webpack.production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import TerserPlugin from 'terser-webpack-plugin'
import type { Configuration } from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import merge from 'webpack-merge'
import CopyWebpackPlugin from 'copy-webpack-plugin'

// import InlineRuntimeChunkPlugin from './plugins/inline-runtime-chunk-html'
import commonConfig from './webpack.common'
Expand Down Expand Up @@ -40,6 +41,19 @@ const config: Configuration = {
},
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: 'public',
to: '',
globOptions: {
ignore: process.env.MSW_ENABLE
? ['**/index.html']
: ['**/mockServiceWorker.js', '**/index.html'],
},
},
],
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// all options are optional
Expand Down

0 comments on commit 098c6b0

Please sign in to comment.