@dcloudio/uni-cli-shared 2.0.2-4020920240930001 → 2.0.2-4040420241231001

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.
@@ -3,27 +3,38 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.parseUTSModuleDeps = exports.capitalize = exports.camelize = exports.parseInjects = exports.parseUniExtApis = exports.getUniExtApiProviderRegisters = exports.getUniExtApiProviders = void 0;
6
+ exports.parseUTSModuleDeps = exports.capitalize = exports.camelize = exports.parseInjects = exports.parseUniExtApi = exports.parseUniExtApis = exports.getUniExtApiProviderRegisters = exports.formatExtApiProviderName = exports.getUniExtApiPlugins = exports.getUniExtApiProviders = void 0;
7
7
  // 重要:此文件编译后的js,需同步至 vue2 编译器中 uni-cli-shared/lib/uts/uni_modules.js
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const fs_extra_1 = __importDefault(require("fs-extra"));
10
10
  const extApiProviders = [];
11
+ const extApiPlugins = new Set();
11
12
  function getUniExtApiProviders() {
12
13
  return extApiProviders;
13
14
  }
14
15
  exports.getUniExtApiProviders = getUniExtApiProviders;
16
+ function getUniExtApiPlugins() {
17
+ return [...extApiPlugins].map((plugin) => {
18
+ return { plugin };
19
+ });
20
+ }
21
+ exports.getUniExtApiPlugins = getUniExtApiPlugins;
22
+ function formatExtApiProviderName(service, name) {
23
+ if (service === 'oauth') {
24
+ service = 'OAuth';
25
+ }
26
+ return `Uni${(0, exports.capitalize)((0, exports.camelize)(service))}${(0, exports.capitalize)((0, exports.camelize)(name))}ProviderImpl`;
27
+ }
28
+ exports.formatExtApiProviderName = formatExtApiProviderName;
15
29
  function getUniExtApiProviderRegisters() {
16
30
  const result = [];
17
31
  extApiProviders.forEach((provider) => {
18
32
  if (provider.name && provider.service) {
19
33
  result.push({
20
34
  name: provider.name,
35
+ plugin: provider.plugin,
21
36
  service: provider.service,
22
- class: `uts.sdk.modules.${(0, exports.camelize)(provider.plugin)}.${(0, exports.capitalize)((0, exports.camelize)('uni-ext-api-' +
23
- provider.service +
24
- '-' +
25
- provider.name +
26
- '-provider'))}`,
37
+ class: `uts.sdk.modules.${(0, exports.camelize)(provider.plugin)}.${formatExtApiProviderName(provider.service, provider.name)}`,
27
38
  });
28
39
  }
29
40
  });
@@ -40,6 +51,7 @@ function parseUniExtApis(vite = true, platform, language = 'javascript') {
40
51
  }
41
52
  const injects = {};
42
53
  extApiProviders.length = 0;
54
+ extApiPlugins.clear();
43
55
  fs_extra_1.default.readdirSync(uniModulesDir).forEach((uniModuleDir) => {
44
56
  // 必须以 uni- 开头
45
57
  if (!uniModuleDir.startsWith('uni-')) {
@@ -62,6 +74,7 @@ function parseUniExtApis(vite = true, platform, language = 'javascript') {
62
74
  provider.plugin = uniModuleDir;
63
75
  extApiProviders.push(provider);
64
76
  }
77
+ extApiPlugins.add(uniModuleDir);
65
78
  const curInjects = parseInjects(vite, platform, language, `@/uni_modules/${uniModuleDir}`, uniModuleRootDir, exports);
66
79
  Object.assign(injects, curInjects);
67
80
  }
@@ -71,6 +84,21 @@ function parseUniExtApis(vite = true, platform, language = 'javascript') {
71
84
  return injects;
72
85
  }
73
86
  exports.parseUniExtApis = parseUniExtApis;
87
+ function parseUniExtApi(pluginDir, pluginId, vite = true, platform, language = 'javascript') {
88
+ const pkgPath = path_1.default.resolve(pluginDir, 'package.json');
89
+ if (!fs_extra_1.default.existsSync(pkgPath)) {
90
+ return;
91
+ }
92
+ let exports;
93
+ const pkg = JSON.parse(fs_extra_1.default.readFileSync(pkgPath, 'utf8'));
94
+ if (pkg && pkg.uni_modules && pkg.uni_modules['uni-ext-api']) {
95
+ exports = pkg.uni_modules['uni-ext-api'];
96
+ }
97
+ if (exports) {
98
+ return parseInjects(vite, platform, language, `@/uni_modules/${pluginId}`, pluginDir, exports);
99
+ }
100
+ }
101
+ exports.parseUniExtApi = parseUniExtApi;
74
102
  /**
75
103
  * uni:'getBatteryInfo'
76
104
  * import getBatteryInfo from '..'
@@ -113,13 +141,15 @@ function parseInjects(vite = true, platform, language, source, uniModuleRootDir,
113
141
  if (platform === 'app') {
114
142
  hasPlatformFile =
115
143
  fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-android')) ||
116
- fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-ios'));
144
+ fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-ios')) ||
145
+ fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-harmony'));
117
146
  }
118
147
  }
119
148
  // 其他平台修改source,直接指向目标文件,否则 uts2js 找不到类型信息
120
149
  if (platform !== 'app' &&
121
150
  platform !== 'app-android' &&
122
- platform !== 'app-ios') {
151
+ platform !== 'app-ios' &&
152
+ platform !== 'app-harmony') {
123
153
  if (fs_extra_1.default.existsSync(platformIndexFileName)) {
124
154
  source = `${source}/utssdk/${platform}/index.uts`;
125
155
  }
@@ -127,6 +157,11 @@ function parseInjects(vite = true, platform, language, source, uniModuleRootDir,
127
157
  source = `${source}/utssdk/index.uts`;
128
158
  }
129
159
  }
160
+ else if (process.env.UNI_APP_X_UVUE_SCRIPT_ENGINE === 'js') {
161
+ if (fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-js', 'index.uts'))) {
162
+ source = `${source}/utssdk/app-js/index.uts`;
163
+ }
164
+ }
130
165
  for (const key in rootDefines) {
131
166
  Object.assign(injects, parseInject(vite, platform, language, source, 'uni', rootDefines[key], hasPlatformFile));
132
167
  }
@@ -162,7 +197,9 @@ function parseInject(vite = true, platform, language, source, globalObject, defi
162
197
  }
163
198
  else {
164
199
  const defineOptions = define[d];
165
- const p = platform === 'app-android' || platform === 'app-ios'
200
+ const p = platform === 'app-android' ||
201
+ platform === 'app-ios' ||
202
+ platform === 'app-harmony'
166
203
  ? 'app'
167
204
  : platform;
168
205
  if (!(p in defineOptions)) {
@@ -175,18 +212,12 @@ function parseInject(vite = true, platform, language, source, globalObject, defi
175
212
  if (p === 'app') {
176
213
  const appOptions = defineOptions.app;
177
214
  if (isPlainObject(appOptions)) {
178
- if (language === 'javascript') {
179
- if (appOptions.js === false) {
180
- return;
181
- }
182
- }
183
- else if (language === 'kotlin') {
184
- if (appOptions.kotlin === false) {
185
- return;
186
- }
187
- }
188
- else if (language === 'swift') {
189
- if (appOptions.swift === false) {
215
+ // js engine 下且存在 app-js,不检查
216
+ const skipCheck = process.env.UNI_APP_X_UVUE_SCRIPT_ENGINE === 'js' &&
217
+ source.includes('app-js');
218
+ if (!skipCheck) {
219
+ const targetLanguage = language === 'javascript' ? 'js' : language;
220
+ if (targetLanguage && appOptions[targetLanguage] === false) {
190
221
  return;
191
222
  }
192
223
  }
@@ -6,10 +6,159 @@ const {
6
6
  const {
7
7
  parseUTSModuleDeps
8
8
  } = require('./uni_modules')
9
+ const {
10
+ dataToEsm
11
+ } = require('./dataToEsm')
12
+ const {
13
+ getUniXKotlinCompiler,
14
+ getUniXSwiftCompiler,
15
+ getChangedFiles,
16
+ getUTSPlugins
17
+ } = require('./uts-webpack-plugin')
18
+
19
+ function createUniModulesSyncFilePreprocessor (
20
+ platform,
21
+ utsPlatform
22
+ ) {
23
+ const initPreprocessContext = require('../preprocess')
24
+ const {
25
+ vueContext: preContext
26
+ } = initPreprocessContext(
27
+ platform,
28
+ global.uniPlugin.platforms
29
+ )
30
+ if (utsPlatform === 'app-android') {
31
+ preContext.APP_ANDROID = true
32
+ }
33
+ if (utsPlatform === 'app-ios') {
34
+ preContext.APP_IOS = true
35
+ }
36
+ const {
37
+ preprocess
38
+ } = require('@dcloudio/vue-cli-plugin-uni/packages/webpack-preprocess-loader/preprocess/lib/preprocess')
39
+
40
+ function preJs (jsCode) {
41
+ return preprocess(jsCode, preContext, {
42
+ type: 'js'
43
+ })
44
+ }
45
+
46
+ function preHtml (htmlCode) {
47
+ return preprocess(htmlCode, preContext, {
48
+ type: 'html'
49
+ })
50
+ }
51
+
52
+ return async (content, fileName) => {
53
+ const extname = path.extname(fileName)
54
+ if (extname === '.json') {
55
+ return dataToEsm(JSON.parse(preJs(content)), {
56
+ namedExports: true,
57
+ preferConst: true
58
+ })
59
+ } else if (extname === '.uts' || extname === '.ts') {
60
+ return preJs(content)
61
+ } else if (extname === '.uvue' || extname === '.vue') {
62
+ return preJs(preHtml(content))
63
+ }
64
+ return content
65
+ }
66
+ }
67
+
68
+ function once (fn, ctx = null) {
69
+ let res
70
+ return (...args) => {
71
+ if (fn) {
72
+ res = fn.apply(ctx, args)
73
+ fn = null
74
+ }
75
+ return res
76
+ }
77
+ }
78
+
79
+ const createAppAndroidUniModulesSyncFilePreprocessorOnce = once(
80
+ () => {
81
+ return createUniModulesSyncFilePreprocessor('app', 'app-android')
82
+ }
83
+ )
84
+
85
+ const createAppIosUniModulesSyncFilePreprocessorOnce = once(
86
+ () => {
87
+ return createUniModulesSyncFilePreprocessor('app', 'app-ios')
88
+ }
89
+ )
90
+
9
91
  module.exports = async function (content) {
10
92
  const callback = this.async()
11
93
 
94
+ const uniXKotlinCompiler = getUniXKotlinCompiler()
95
+ const uniXSwiftCompiler = getUniXSwiftCompiler()
96
+
97
+ const {
98
+ syncUniModuleFilesByCompiler,
99
+ resolveTscUniModuleIndexFileName
100
+ } = resolveUTSCompiler()
101
+
12
102
  const compilePlugin = async (pluginDir) => {
103
+ const pluginId = path.basename(pluginDir)
104
+
105
+ const utsPlugins = getUTSPlugins()
106
+ const changedFiles = getChangedFiles()
107
+ if (uniXKotlinCompiler) {
108
+ await uniXKotlinCompiler.init()
109
+ await syncUniModuleFilesByCompiler(
110
+ 'app-android',
111
+ uniXKotlinCompiler,
112
+ pluginDir,
113
+ createAppAndroidUniModulesSyncFilePreprocessorOnce()
114
+ )
115
+ }
116
+
117
+ if (uniXSwiftCompiler) {
118
+ await uniXSwiftCompiler.init()
119
+ await syncUniModuleFilesByCompiler(
120
+ 'app-ios',
121
+ uniXSwiftCompiler,
122
+ pluginDir,
123
+ createAppIosUniModulesSyncFilePreprocessorOnce()
124
+ )
125
+ }
126
+
127
+ if (!utsPlugins.has(pluginId)) {
128
+ utsPlugins.add(pluginId)
129
+ if (uniXKotlinCompiler) {
130
+ const indexFileName = resolveTscUniModuleIndexFileName(
131
+ 'app-android',
132
+ pluginDir
133
+ )
134
+ if (indexFileName) {
135
+ await uniXKotlinCompiler.addRootFile(indexFileName)
136
+ }
137
+ }
138
+ if (uniXSwiftCompiler) {
139
+ const indexFileName = resolveTscUniModuleIndexFileName(
140
+ 'app-ios',
141
+ pluginDir
142
+ )
143
+ if (indexFileName) {
144
+ await uniXSwiftCompiler.addRootFile(indexFileName)
145
+ }
146
+ }
147
+ }
148
+
149
+ // 处理uni_modules中的文件变更
150
+ const files = changedFiles.get(pluginId)
151
+ if (files) {
152
+ // 仅限watch模式是会生效
153
+ changedFiles.delete(pluginId)
154
+ if (uniXKotlinCompiler) {
155
+ await uniXKotlinCompiler.invalidate(files)
156
+ }
157
+ if (uniXSwiftCompiler) {
158
+ await uniXSwiftCompiler.invalidate(files)
159
+ }
160
+ }
161
+
13
162
  const pkgJson = require(path.join(pluginDir, 'package.json'))
14
163
  const compiler = resolveUTSCompiler()
15
164
  // 处理依赖的 uts 插件
@@ -33,6 +182,9 @@ module.exports = async function (content) {
33
182
  ),
34
183
  sourceMap: process.env.NODE_ENV === 'development',
35
184
  uni_modules: deps
185
+ // 暂不提供
186
+ // async kotlinAutoImports() {}
187
+ // async swiftAutoImports() {}
36
188
  })
37
189
  }
38
190
 
@@ -0,0 +1,138 @@
1
+ const path = require('path')
2
+ const fs = require('fs-extra')
3
+ const {
4
+ resolveUTSCompiler
5
+ } = require('./uts')
6
+ const {
7
+ normalizePath
8
+ } = require('../util')
9
+ const {
10
+ runByHBuilderX
11
+ } = require('../platform')
12
+
13
+ class WebpackUTSPlugin {
14
+ apply (compiler) {
15
+ compiler.hooks.watchRun.tapAsync('WatchRun', (comp, done) => {
16
+ let changedFileNames = []
17
+ if (comp.modifiedFiles) { // webpack5
18
+ changedFileNames = Array.from(comp.modifiedFiles)
19
+ } else if (comp.watchFileSystem?.watcher?.mtimes) { // webpack4
20
+ changedFileNames = Object.keys(comp.watchFileSystem.watcher.mtimes)
21
+ }
22
+ if (changedFileNames.length) {
23
+ // console.log('====================================')
24
+ // console.log('NEW BUILD FILES CHANGED:', changedFileNames)
25
+ // console.log('====================================')
26
+ const uniModulesDir = normalizePath(path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules'))
27
+ changedFileNames.forEach(fileName => {
28
+ fileName = normalizePath(fileName)
29
+ if (fileName.startsWith(uniModulesDir)) {
30
+ // 仅处理uni_modules中的文件
31
+ const plugin = fileName.slice(uniModulesDir.length + 1).split('/')[0]
32
+ if (utsPlugins.has(plugin)) {
33
+ const changeFile = {
34
+ fileName,
35
+ event: 'update' // 写死 update
36
+ }
37
+ if (!changedFiles.has(plugin)) {
38
+ changedFiles.set(plugin, [changeFile])
39
+ } else {
40
+ changedFiles.get(plugin).push(changeFile)
41
+ }
42
+ }
43
+ }
44
+ })
45
+ }
46
+ return done()
47
+ })
48
+ compiler.hooks.done.tapPromise('webpack-uts-done', async () => {
49
+ if (process.env.NODE_ENV !== 'development') {
50
+ if (uniXKotlinCompiler) {
51
+ await uniXKotlinCompiler.close()
52
+ }
53
+ if (uniXSwiftCompiler) {
54
+ await uniXSwiftCompiler.close()
55
+ }
56
+ }
57
+ })
58
+ }
59
+ }
60
+ let uniXKotlinCompiler = null
61
+ let uniXSwiftCompiler = null
62
+ const utsPlugins = new Set()
63
+ const changedFiles = new Map()
64
+
65
+ function getUTSPlugins () {
66
+ return utsPlugins
67
+ }
68
+
69
+ function getChangedFiles () {
70
+ return changedFiles
71
+ }
72
+
73
+ function getUniXKotlinCompiler () {
74
+ if (uniXKotlinCompiler) {
75
+ return uniXKotlinCompiler
76
+ }
77
+ const {
78
+ createUniXKotlinCompilerOnce
79
+ } = resolveUTSCompiler()
80
+
81
+ uniXKotlinCompiler = runByHBuilderX && process.env.UNI_APP_X_TSC === 'true' &&
82
+ (process.env.UNI_UTS_PLATFORM === 'app-android' ||
83
+ process.env.UNI_UTS_PLATFORM === 'app' ||
84
+ process.env.UNI_UTS_PLATFORM === 'app-plus')
85
+ ? createUniXKotlinCompilerOnce()
86
+ : null
87
+ if (uniXKotlinCompiler) {
88
+ const tscDir = path.resolve(process.env.UNI_OUTPUT_DIR, '../.tsc/app-android')
89
+ const uvueDir = path.resolve(process.env.UNI_OUTPUT_DIR, '../.uvue/app-android')
90
+ try {
91
+ if (fs.existsSync(tscDir)) {
92
+ fs.emptyDirSync(tscDir)
93
+ }
94
+ if (fs.existsSync(uvueDir)) {
95
+ fs.emptyDirSync(uvueDir)
96
+ }
97
+ } catch (e) {}
98
+ }
99
+ return uniXKotlinCompiler
100
+ }
101
+
102
+ function getUniXSwiftCompiler () {
103
+ if (uniXSwiftCompiler) {
104
+ return uniXSwiftCompiler
105
+ }
106
+ const {
107
+ createUniXSwiftCompilerOnce
108
+ } = resolveUTSCompiler()
109
+
110
+ uniXSwiftCompiler =
111
+ runByHBuilderX && process.env.UNI_APP_X_TSC === 'true' &&
112
+ (process.env.UNI_UTS_PLATFORM === 'app-ios' ||
113
+ process.env.UNI_UTS_PLATFORM === 'app' ||
114
+ process.env.UNI_UTS_PLATFORM === 'app-plus')
115
+ ? createUniXSwiftCompilerOnce()
116
+ : null
117
+
118
+ if (uniXSwiftCompiler) {
119
+ const tscDir = path.resolve(process.env.UNI_OUTPUT_DIR, '../.tsc/app-ios')
120
+ const uvueDir = path.resolve(process.env.UNI_OUTPUT_DIR, '../.uvue/app-ios')
121
+ try {
122
+ if (fs.existsSync(tscDir)) {
123
+ fs.emptyDirSync(tscDir)
124
+ }
125
+ if (fs.existsSync(uvueDir)) {
126
+ fs.emptyDirSync(uvueDir)
127
+ }
128
+ } catch (e) {}
129
+ }
130
+ return uniXSwiftCompiler
131
+ }
132
+ module.exports = {
133
+ getUTSPlugins,
134
+ getChangedFiles,
135
+ getUniXKotlinCompiler,
136
+ getUniXSwiftCompiler,
137
+ WebpackUTSPlugin
138
+ }