@mpxjs/cli-shared-utils 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js ADDED
@@ -0,0 +1,45 @@
1
+
2
+ /**
3
+ * @typedef { 'wx' | 'ali' | 'swan' | 'qq' | 'tt' | 'dd' | 'web' } Mode
4
+ * @typedef { { mode: Mode, env: 'development' | 'production' } } Target
5
+ */
6
+
7
+ const { makeMap, runServiceCommand, removeArgv, normalizeCommandArgs } = require('./utils')
8
+ const {
9
+ SUPPORT_MODE,
10
+ MODE_CONFIG_FILES_MAP,
11
+ DEFAULT_MODE,
12
+ getCurrentTarget,
13
+ getTargets,
14
+ parseTarget,
15
+ rawTarget,
16
+ setTargetProcessEnv
17
+ } = require('./target')
18
+ const {
19
+ modifyMpxPluginConfig,
20
+ updateWebpackName,
21
+ modifyConfig,
22
+ getWebpackName
23
+ } = require('./webpack')
24
+ const { getMpxPluginOptions } = require('./mpx')
25
+ const { setServerBundle, getServerBundle } = require('./serverBundle')
26
+
27
+ module.exports.SUPPORT_MODE = SUPPORT_MODE
28
+ module.exports.MODE_CONFIG_FILES_MAP = MODE_CONFIG_FILES_MAP
29
+ module.exports.DEFAULT_MODE = DEFAULT_MODE
30
+ module.exports.runServiceCommand = runServiceCommand
31
+ module.exports.makeMap = makeMap
32
+ module.exports.removeArgv = removeArgv
33
+ module.exports.normalizeCommandArgs = normalizeCommandArgs
34
+ module.exports.getCurrentTarget = getCurrentTarget
35
+ module.exports.getTargets = getTargets
36
+ module.exports.parseTarget = parseTarget
37
+ module.exports.getMpxPluginOptions = getMpxPluginOptions
38
+ module.exports.setTargetProcessEnv = setTargetProcessEnv
39
+ module.exports.rawTarget = rawTarget
40
+ module.exports.modifyMpxPluginConfig = modifyMpxPluginConfig
41
+ module.exports.updateWebpackName = updateWebpackName
42
+ module.exports.modifyConfig = modifyConfig
43
+ module.exports.getWebpackName = getWebpackName
44
+ module.exports.setServerBundle = setServerBundle
45
+ module.exports.getServerBundle = getServerBundle
package/lib/mpx.js ADDED
@@ -0,0 +1,10 @@
1
+
2
+ /**
3
+ * 获取@mpxjs/webpack-plugin选项
4
+ * @param {*} options 选项
5
+ */
6
+ function getMpxPluginOptions (options = {}) {
7
+ return options.pluginOptions ? options.pluginOptions.mpx || {} : {}
8
+ }
9
+
10
+ module.exports.getMpxPluginOptions = getMpxPluginOptions
@@ -0,0 +1,12 @@
1
+ let serverBundle
2
+
3
+ const setServerBundle = (bundle) => {
4
+ serverBundle = bundle
5
+ }
6
+
7
+ const getServerBundle = () => {
8
+ return serverBundle
9
+ }
10
+
11
+ module.exports.setServerBundle = setServerBundle
12
+ module.exports.getServerBundle = getServerBundle
package/lib/target.js ADDED
@@ -0,0 +1,108 @@
1
+ const { makeMap } = require('./utils')
2
+
3
+ /**
4
+ * @typedef { import('.').Mode } Mode
5
+ * @typedef { import('.').Target } Target
6
+ */
7
+
8
+ /**
9
+ * @type { Mode[] }
10
+ */
11
+ const SUPPORT_MODE = ['wx', 'ali', 'swan', 'qq', 'tt', 'dd', 'web']
12
+
13
+ /**
14
+ * @type { Object.<Mode, string[]> }
15
+ */
16
+ const MODE_CONFIG_FILES_MAP = {
17
+ wx: ['project.config.json'],
18
+ ali: ['mini.project.json'],
19
+ swan: ['project.swan.json'],
20
+ qq: ['project.config.json'],
21
+ tt: ['project.config.json'],
22
+ dd: ['project.config.json'],
23
+ web: []
24
+ }
25
+
26
+ const DEFAULT_MODE = 'wx'
27
+
28
+ const supportedModeMap = makeMap(SUPPORT_MODE)
29
+
30
+ /**
31
+ *
32
+ * @param {Target} target
33
+ * @returns
34
+ */
35
+ function rawTarget (target) {
36
+ return [target.mode, target.env].filter(Boolean).join(':')
37
+ }
38
+
39
+ /**
40
+ * 根据args获取targets
41
+ * @param {*} args
42
+ * @returns { Target[] }
43
+ */
44
+ function getTargets (args) {
45
+ const defaultTargets = [{ mode: 'wx' || SUPPORT_MODE[0] }]
46
+ const inputTargets = args.targets
47
+ ? args.targets.split(/[,|]/)
48
+ : Object.keys(args)
49
+ const command = args._[0]
50
+ let targets = []
51
+ if (command === 'serve:ssr' || command === 'build:ssr') {
52
+ targets = [{ mode: 'web' }]
53
+ } else {
54
+ targets = inputTargets
55
+ .map((v) => parseTarget(v))
56
+ .filter((v) => supportedModeMap[v.mode])
57
+ }
58
+ return targets.length ? targets : defaultTargets
59
+ }
60
+
61
+ /**
62
+ * 根据mode:env解析target
63
+ * @param { string } target
64
+ * @returns { Target }
65
+ */
66
+ function parseTarget (target = '') {
67
+ let [mode, env] = target.split(':')
68
+ if (!mode) mode = 'wx' || SUPPORT_MODE[0]
69
+ return {
70
+ mode,
71
+ env
72
+ }
73
+ }
74
+
75
+ /**
76
+ * 设置target process env
77
+ * @param { Target } target
78
+ */
79
+ function setTargetProcessEnv (target) {
80
+ if (target.mode) {
81
+ process.env.MPX_CLI_MODE = target.mode === 'web' ? 'web' : 'mp'
82
+ process.env.MPX_CURRENT_TARGET_MODE = target.mode
83
+ }
84
+ if (target.env) {
85
+ process.env.MPX_CURRENT_TARGET_ENV = target.env
86
+ }
87
+ }
88
+
89
+ /**
90
+ * 获取当前构建的target
91
+ * @returns { Target }
92
+ */
93
+ function getCurrentTarget () {
94
+ const currentTarget = {
95
+ mode: process.env.MPX_CURRENT_TARGET_MODE,
96
+ env: process.env.MPX_CURRENT_TARGET_ENV
97
+ }
98
+ return currentTarget
99
+ }
100
+
101
+ module.exports.getCurrentTarget = getCurrentTarget
102
+ module.exports.setTargetProcessEnv = setTargetProcessEnv
103
+ module.exports.getTargets = getTargets
104
+ module.exports.parseTarget = parseTarget
105
+ module.exports.rawTarget = rawTarget
106
+ module.exports.SUPPORT_MODE = SUPPORT_MODE
107
+ module.exports.MODE_CONFIG_FILES_MAP = MODE_CONFIG_FILES_MAP
108
+ module.exports.DEFAULT_MODE = DEFAULT_MODE
package/lib/utils.js ADDED
@@ -0,0 +1,36 @@
1
+
2
+ /**
3
+ * makeMap
4
+ * @param {*} arr
5
+ * @returns
6
+ */
7
+ function makeMap (arr) {
8
+ const map = {}
9
+ arr.forEach((v) => (map[v] = true))
10
+ return map
11
+ }
12
+
13
+ function removeArgv (rawArgv, removeName) {
14
+ return rawArgv.filter((argv) => argv.indexOf(removeName) === -1)
15
+ }
16
+
17
+ function runServiceCommand (rawArgv, options = {}) {
18
+ const execa = require('execa')
19
+ const mpxCliServiceBinPath = require.resolve(
20
+ '@mpxjs/mpx-cli-service/bin/mpx-cli-service.js'
21
+ )
22
+ return execa.node(mpxCliServiceBinPath, [...rawArgv], options)
23
+ }
24
+
25
+ function normalizeCommandArgs (o1, o2) {
26
+ for (const key in o2) {
27
+ if (o1[key] == null) {
28
+ o1[key] = o2[key]
29
+ }
30
+ }
31
+ }
32
+
33
+ module.exports.runServiceCommand = runServiceCommand
34
+ module.exports.normalizeCommandArgs = normalizeCommandArgs
35
+ module.exports.removeArgv = removeArgv
36
+ module.exports.makeMap = makeMap
package/lib/webpack.js ADDED
@@ -0,0 +1,42 @@
1
+ const { getCurrentTarget } = require('./target')
2
+
3
+ function getWebpackName (api, target, pluginConfig) {
4
+ return [target.mode, pluginConfig.env, api.service.mode]
5
+ .filter((v) => v !== undefined)
6
+ .join('-')
7
+ }
8
+
9
+ function modifyConfig (config, fn) {
10
+ if (Array.isArray(config)) {
11
+ config.forEach((c) => fn(c))
12
+ } else {
13
+ fn(config)
14
+ }
15
+ }
16
+
17
+ function modifyMpxPluginConfig (api, config, pluginConfig) {
18
+ config.plugin('mpx-webpack-plugin').tap((args) => {
19
+ Object.assign(args[0], pluginConfig)
20
+ return args
21
+ })
22
+ updateWebpackName(api, config)
23
+ }
24
+
25
+ function updateWebpackName (api, config) {
26
+ const mpxWebpackPluginConfig = config
27
+ .plugin('mpx-webpack-plugin')
28
+ .get('args')[0]
29
+ const name = getWebpackName(api, getCurrentTarget(), mpxWebpackPluginConfig)
30
+ config.name(name)
31
+ config.plugin('webpackbar').tap((args) => {
32
+ const c = args[0]
33
+ c.name = name
34
+ c.reporter.name = c.name
35
+ return args
36
+ })
37
+ }
38
+
39
+ module.exports.getWebpackName = getWebpackName
40
+ module.exports.modifyMpxPluginConfig = modifyMpxPluginConfig
41
+ module.exports.updateWebpackName = updateWebpackName
42
+ module.exports.modifyConfig = modifyConfig
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@mpxjs/cli-shared-utils",
3
+ "version": "2.0.7",
4
+ "description": "share utils for mpx-cli",
5
+ "main": "lib/index.js",
6
+ "dependencies": {},
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [],
11
+ "author": "",
12
+ "license": "ISC",
13
+ "engines": {
14
+ "node": ">=8"
15
+ },
16
+ "publishConfig": {
17
+ "registry": "https://registry.npmjs.org",
18
+ "access": "public"
19
+ }
20
+ }