Skip to content

Commit ec9ed03

Browse files
committed
feat: support syncPackage, support emit source-map on production build
1 parent d1aa242 commit ec9ed03

File tree

16 files changed

+258
-510
lines changed

16 files changed

+258
-510
lines changed

packages/react-imvc-v2/build/createWebpackConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ module.exports = async function createWebpackConfig(options, isServer = false) {
179179
parallel: true,
180180
// Enable file caching
181181
cache: true,
182-
sourceMap: false,
182+
sourceMap: config.devtool ? true : false,
183183
}),
184184
],
185185
})

packages/react-imvc-v2/config/config.defaults.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,10 @@ module.exports = {
298298
* 默认为 false
299299
*/
300300
useContentHash: false,
301+
302+
/**
303+
* 是否同步 {root}/package.json 到 {root}/{publish}/package.json
304+
* 默认为 true
305+
*/
306+
syncPackage: true,
301307
}

packages/react-imvc-v2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-imvc",
3-
"version": "2.10.27",
3+
"version": "2.10.28",
44
"description": "An Isomorphic MVC Framework",
55
"main": "./index",
66
"bin": {

packages/react-imvc-v2/project/build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const config = {
88
routes: 'routes', // 服务端路由目录
99
layout: 'Layout', // 自定义 Layout
1010
staticPath: '/my_static', // 静态资源目录
11+
// devtool: 'source-map',
1112
// bundleAnalyzer: true,
1213
// staticEntry: 'index.html',
1314
publish: '../project_publish', // 打包输出目录

packages/react-imvc-v2/project/preview.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const config = {
88
basename: ['/a', '/b'],
99
routes: 'routes', // 服务端路由目录
1010
staticPath: '/my_static', // 静态资源目录
11+
publish: '../project_publish', // 打包输出目录
1112
layout: 'Layout', // 自定义 Layout
1213
// bundleAnalyzer: true,
1314
// useTypeCheck: true

packages/react-imvc-v2/project/start.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const config = {
1010
layout: 'Layout', // 自定义 Layout
1111
staticPath: '/my_static', // 静态资源目录
1212
useFileLoader: true,
13+
publish: '../project_publish', // 打包输出目录
1314
// bundleAnalyzer: true,
1415
// useTypeCheck: true
1516
}

packages/react-imvc-v2/scripts/start.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ if (process.env.NODE_ENV === 'development') {
1111
})
1212
}
1313

14-
require('../start/index')(options)
14+
require('../start/index')({
15+
...options,
16+
fromScript: true,
17+
})

packages/react-imvc-v2/start/index.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if (!process.env.NODE_ENV) {
88
require('core-js/stable')
99
require('regenerator-runtime/runtime')
1010

11+
let fs = require('fs').promises
1112
let path = require('path')
1213
let http = require('http')
1314
let fetch = require('node-fetch')
@@ -20,8 +21,83 @@ createExpressApp = createExpressApp.default || createExpressApp
2021
getConfig = getConfig.default || getConfig
2122
createPageRouter = createPageRouter.default || createPageRouter
2223

24+
const isExist = async (path) => {
25+
try {
26+
await fs.access(path)
27+
return true
28+
} catch (e) {
29+
return false
30+
}
31+
}
32+
33+
const findUpClosestTwoPackages = async (startDir) => {
34+
let currentDir = startDir
35+
let packages = []
36+
37+
while (true) {
38+
let packageFile = path.join(currentDir, 'package.json')
39+
if (await isExist(packageFile)) {
40+
packages.unshift({
41+
filename: packageFile,
42+
package: require(packageFile),
43+
})
44+
45+
if (packages.length >= 2) {
46+
break
47+
}
48+
}
49+
50+
let parentDir = path.dirname(currentDir)
51+
52+
if (parentDir === currentDir) {
53+
break
54+
}
55+
56+
currentDir = parentDir
57+
}
58+
59+
return packages
60+
}
61+
62+
const syncPackage = async () => {
63+
const mainFilename = path.normalize(require.main?.filename ?? '')
64+
65+
if (!mainFilename) {
66+
return
67+
}
68+
69+
const packages = await findUpClosestTwoPackages(path.dirname(mainFilename))
70+
71+
if (packages.length < 2) {
72+
return
73+
}
74+
75+
const [rootPackage, publishPackage] = packages
76+
77+
const isPackageNameEqual = rootPackage.package.name === publishPackage.package.name
78+
const isPackageContentEqual = JSON.stringify(rootPackage.package) === JSON.stringify(publishPackage.package)
79+
80+
if (!isPackageNameEqual || isPackageContentEqual) {
81+
return
82+
}
83+
84+
console.error(`${publishPackage.filename}\nis not equal to\n${rootPackage.filename}\nSyncing...`)
85+
await fs.writeFile(publishPackage.filename, await fs.readFile(rootPackage.filename))
86+
console.error('Syncing done. Please restart the server to apply changes')
87+
process.exit(1)
88+
}
89+
2390
module.exports = async function start(options) {
2491
let config = getConfig(options)
92+
93+
/**
94+
* sync package.json in root dir and publish dir
95+
* if they are not equal when server starts not from script
96+
*/
97+
if (config.syncPackage && options.fromScript !== true) {
98+
await syncPackage()
99+
}
100+
25101
let [app, pageRouter] = await Promise.all([createExpressApp(config), createPageRouter(config)])
26102
let port = normalizePort(config.port)
27103

packages/react-imvc-v3/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-imvc",
3-
"version": "3.5.0",
3+
"version": "3.5.1",
44
"description": "An Isomorphic MVC Framework",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -160,7 +160,7 @@
160160
"@types/loader-utils": "^1.1.9",
161161
"@types/memory-fs": "^0.3.2",
162162
"@types/morgan": "^1.9.0",
163-
"@types/node": "^10.17.17",
163+
"@types/node": "^22.13.9",
164164
"@types/node-fetch": "^2.5.5",
165165
"@types/node-notifier": "^6.0.0",
166166
"@types/puppeteer": "^2.0.1",

packages/react-imvc-v3/project/imvc.config.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const config: Config = {
1212
routes: 'routes', // 服务端路由目录
1313
layout: 'Layout', // 自定义 Layout
1414
publish: '../publish',
15+
devtool: 'source-map',
1516
useContentHash: true,
1617
useFileLoader: true,
1718
useSass: true,

0 commit comments

Comments
 (0)