@tarojs/plugin-framework-solid 4.0.0-beta.83 → 4.0.0-beta.84
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/LICENSE +7 -0
- package/README.md +2 -2
- package/package.json +25 -26
- package/dist/api-loader.js +0 -84
- package/dist/api-loader.js.map +0 -1
- package/dist/index.js +0 -821
- package/dist/index.js.map +0 -1
- package/dist/reconciler.js +0 -347
- package/dist/reconciler.js.map +0 -1
- package/dist/runtime.js +0 -1165
- package/dist/runtime.js.map +0 -1
package/dist/index.js
DELETED
|
@@ -1,821 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var helper = require('@tarojs/helper');
|
|
6
|
-
var shared = require('@tarojs/shared');
|
|
7
|
-
var tslib = require('tslib');
|
|
8
|
-
var lodash = require('lodash');
|
|
9
|
-
var acorn = require('acorn');
|
|
10
|
-
var walk = require('acorn-walk');
|
|
11
|
-
var path = require('node:path');
|
|
12
|
-
|
|
13
|
-
function _interopNamespaceDefault(e) {
|
|
14
|
-
var n = Object.create(null);
|
|
15
|
-
if (e) {
|
|
16
|
-
Object.keys(e).forEach(function (k) {
|
|
17
|
-
if (k !== 'default') {
|
|
18
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
get: function () { return e[k]; }
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
n.default = e;
|
|
27
|
-
return Object.freeze(n);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
var acorn__namespace = /*#__PURE__*/_interopNamespaceDefault(acorn);
|
|
31
|
-
var walk__namespace = /*#__PURE__*/_interopNamespaceDefault(walk);
|
|
32
|
-
|
|
33
|
-
function apiLoader (str) {
|
|
34
|
-
return `import {
|
|
35
|
-
useAddToFavorites,
|
|
36
|
-
useDidHide,
|
|
37
|
-
useDidShow,
|
|
38
|
-
useError,
|
|
39
|
-
useLaunch,
|
|
40
|
-
useLoad,
|
|
41
|
-
useOptionMenuClick,
|
|
42
|
-
usePageNotFound,
|
|
43
|
-
usePageScroll,
|
|
44
|
-
usePullDownRefresh,
|
|
45
|
-
usePullIntercept,
|
|
46
|
-
useReachBottom,
|
|
47
|
-
useReady,
|
|
48
|
-
useResize,
|
|
49
|
-
useRouter,
|
|
50
|
-
useSaveExitState,
|
|
51
|
-
useShareAppMessage,
|
|
52
|
-
useShareTimeline,
|
|
53
|
-
useTabItemTap,
|
|
54
|
-
useTitleClick,
|
|
55
|
-
useScope,
|
|
56
|
-
useUnhandledRejection,
|
|
57
|
-
useUnload
|
|
58
|
-
} from '@tarojs/plugin-framework-react/dist/runtime'
|
|
59
|
-
${str}
|
|
60
|
-
|
|
61
|
-
taro.useAddToFavorites = useAddToFavorites
|
|
62
|
-
taro.useDidHide = useDidHide
|
|
63
|
-
taro.useDidShow = useDidShow
|
|
64
|
-
taro.useError = useError
|
|
65
|
-
taro.useLaunch = useLaunch
|
|
66
|
-
taro.useLoad = useLoad
|
|
67
|
-
taro.useOptionMenuClick = useOptionMenuClick
|
|
68
|
-
taro.usePageNotFound = usePageNotFound
|
|
69
|
-
taro.usePageScroll = usePageScroll
|
|
70
|
-
taro.usePullDownRefresh = usePullDownRefresh
|
|
71
|
-
taro.usePullIntercept = usePullIntercept
|
|
72
|
-
taro.useReachBottom = useReachBottom
|
|
73
|
-
taro.useReady = useReady
|
|
74
|
-
taro.useResize = useResize
|
|
75
|
-
taro.useRouter = useRouter
|
|
76
|
-
taro.useSaveExitState = useSaveExitState
|
|
77
|
-
taro.useShareAppMessage = useShareAppMessage
|
|
78
|
-
taro.useShareTimeline = useShareTimeline
|
|
79
|
-
taro.useTabItemTap = useTabItemTap
|
|
80
|
-
taro.useTitleClick = useTitleClick
|
|
81
|
-
taro.useScope = useScope
|
|
82
|
-
taro.useUnhandledRejection = useUnhandledRejection
|
|
83
|
-
taro.useUnload = useUnload
|
|
84
|
-
|
|
85
|
-
export {
|
|
86
|
-
useAddToFavorites,
|
|
87
|
-
useDidHide,
|
|
88
|
-
useDidShow,
|
|
89
|
-
useError,
|
|
90
|
-
useLaunch,
|
|
91
|
-
useLoad,
|
|
92
|
-
useOptionMenuClick,
|
|
93
|
-
usePageNotFound,
|
|
94
|
-
usePageScroll,
|
|
95
|
-
usePullDownRefresh,
|
|
96
|
-
usePullIntercept,
|
|
97
|
-
useReachBottom,
|
|
98
|
-
useReady,
|
|
99
|
-
useResize,
|
|
100
|
-
useRouter,
|
|
101
|
-
useSaveExitState,
|
|
102
|
-
useShareAppMessage,
|
|
103
|
-
useShareTimeline,
|
|
104
|
-
useTabItemTap,
|
|
105
|
-
useTitleClick,
|
|
106
|
-
useScope,
|
|
107
|
-
useUnhandledRejection,
|
|
108
|
-
useUnload
|
|
109
|
-
}
|
|
110
|
-
`;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function addConfig(source) {
|
|
114
|
-
const configsMap = {
|
|
115
|
-
enableShareAppMessage: ['onShareAppMessage', 'useShareAppMessage'],
|
|
116
|
-
enableShareTimeline: ['onShareTimeline', 'useShareTimeline']
|
|
117
|
-
};
|
|
118
|
-
const ast = acorn__namespace.parse(source, {
|
|
119
|
-
ecmaVersion: 'latest',
|
|
120
|
-
sourceType: 'module'
|
|
121
|
-
});
|
|
122
|
-
const additionConfig = {};
|
|
123
|
-
function check(name) {
|
|
124
|
-
Object.keys(configsMap).forEach(configName => {
|
|
125
|
-
const apis = configsMap[configName];
|
|
126
|
-
if (apis.includes(name)) {
|
|
127
|
-
additionConfig[configName] = true;
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
walk__namespace.simple(ast, {
|
|
132
|
-
FunctionExpression(node) {
|
|
133
|
-
if (!node.id || !node.id.name)
|
|
134
|
-
return;
|
|
135
|
-
check(node.id.name);
|
|
136
|
-
},
|
|
137
|
-
FunctionDeclaration(node) {
|
|
138
|
-
if (!node.id || !node.id.name)
|
|
139
|
-
return;
|
|
140
|
-
check(node.id.name);
|
|
141
|
-
},
|
|
142
|
-
CallExpression(node) {
|
|
143
|
-
const { callee } = node;
|
|
144
|
-
if (callee.type === 'Identifier') {
|
|
145
|
-
check(callee.name);
|
|
146
|
-
}
|
|
147
|
-
else if (callee.type === 'MemberExpression') {
|
|
148
|
-
if (callee.property.type === 'Identifier') {
|
|
149
|
-
check(callee.property.name);
|
|
150
|
-
}
|
|
151
|
-
else if (callee.property.type === 'Literal') {
|
|
152
|
-
check(callee.property.value);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
node.arguments.forEach(item => {
|
|
156
|
-
if (item.type === 'Literal' && item.value) {
|
|
157
|
-
check(item.value);
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
return additionConfig;
|
|
163
|
-
}
|
|
164
|
-
function getLoaderMeta(framework) {
|
|
165
|
-
const loaderMeta = {
|
|
166
|
-
importFrameworkStatement: `
|
|
167
|
-
import * as React from 'react'
|
|
168
|
-
import ReactDOM from 'react-dom'
|
|
169
|
-
`,
|
|
170
|
-
mockAppStatement: `
|
|
171
|
-
class App extends React.Component {
|
|
172
|
-
render () {
|
|
173
|
-
return this.props.children
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
`,
|
|
177
|
-
frameworkArgs: 'React, ReactDOM, config',
|
|
178
|
-
creator: 'createReactApp',
|
|
179
|
-
creatorLocation: '@tarojs/plugin-framework-react/dist/runtime',
|
|
180
|
-
importFrameworkName: 'React',
|
|
181
|
-
extraImportForWeb: '',
|
|
182
|
-
execBeforeCreateWebApp: '',
|
|
183
|
-
modifyConfig(config, source) {
|
|
184
|
-
Object.assign(config, addConfig(source));
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
if (framework === 'solid') {
|
|
188
|
-
Object.assign(loaderMeta, {
|
|
189
|
-
creator: 'createSolidApp',
|
|
190
|
-
frameworkArgs: 'config',
|
|
191
|
-
importFrameworkStatement: '',
|
|
192
|
-
importFrameworkName: '',
|
|
193
|
-
mockAppStatement: `
|
|
194
|
-
function App(props) {
|
|
195
|
-
return null
|
|
196
|
-
}
|
|
197
|
-
`,
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
if (process.env.TARO_PLATFORM === 'web') {
|
|
201
|
-
if (framework === 'react') {
|
|
202
|
-
const react = require('react');
|
|
203
|
-
const majorVersion = Number((react.version || '18').split('.')[0]);
|
|
204
|
-
if (majorVersion >= 18) {
|
|
205
|
-
// Note: In react 18 or above, should using react-dom/client
|
|
206
|
-
loaderMeta.importFrameworkStatement = loaderMeta.importFrameworkStatement.replace('\'react-dom\'', '\'react-dom/client\'');
|
|
207
|
-
loaderMeta.extraImportForWeb += `import { findDOMNode, render, unstable_batchedUpdates } from 'react-dom'\n`;
|
|
208
|
-
loaderMeta.execBeforeCreateWebApp += `Object.assign(ReactDOM, { findDOMNode, render, unstable_batchedUpdates })\n`;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
return loaderMeta;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function h5iVitePlugin(ctx, framework) {
|
|
216
|
-
return [
|
|
217
|
-
injectLoaderMeta$2(ctx, framework),
|
|
218
|
-
setTaroApi(),
|
|
219
|
-
esbuildExclude(framework)
|
|
220
|
-
];
|
|
221
|
-
}
|
|
222
|
-
function injectLoaderMeta$2(ctx, framework) {
|
|
223
|
-
function customizer(object = '', sources = '') {
|
|
224
|
-
if ([object, sources].every(e => typeof e === 'string'))
|
|
225
|
-
return object + sources;
|
|
226
|
-
}
|
|
227
|
-
const { runnerUtils } = ctx;
|
|
228
|
-
const { getViteH5CompilerContext } = runnerUtils;
|
|
229
|
-
return {
|
|
230
|
-
name: 'taro-react:loader-meta',
|
|
231
|
-
buildStart() {
|
|
232
|
-
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
233
|
-
const viteCompilerContext = yield getViteH5CompilerContext(this);
|
|
234
|
-
if (viteCompilerContext) {
|
|
235
|
-
viteCompilerContext.loaderMeta = lodash.mergeWith(getLoaderMeta(framework), viteCompilerContext.loaderMeta, customizer);
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
function setTaroApi() {
|
|
242
|
-
// dev 环境通过 esbuild 来做; pro 环境通过 rollup load 钩子来做;因为生产环境不会走 esbuild
|
|
243
|
-
return {
|
|
244
|
-
name: 'taro-react:process-import-taro',
|
|
245
|
-
enforce: 'pre',
|
|
246
|
-
config: () => ({
|
|
247
|
-
optimizeDeps: {
|
|
248
|
-
esbuildOptions: {
|
|
249
|
-
plugins: [
|
|
250
|
-
{
|
|
251
|
-
name: 'taro:react-api',
|
|
252
|
-
setup(build) {
|
|
253
|
-
build.onLoad({ filter: helper.REG_TARO_H5_RUNTIME_API }, (args) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
254
|
-
const input = yield helper.fs.readFile(args.path, 'utf8');
|
|
255
|
-
return {
|
|
256
|
-
contents: apiLoader(input + '\n' + 'const taro = Taro__default\n')
|
|
257
|
-
};
|
|
258
|
-
}));
|
|
259
|
-
},
|
|
260
|
-
}
|
|
261
|
-
]
|
|
262
|
-
}
|
|
263
|
-
},
|
|
264
|
-
}),
|
|
265
|
-
load(id) {
|
|
266
|
-
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
267
|
-
if (process.env.NODE_ENV === 'production' && helper.REG_TARO_H5_RUNTIME_API.test(id)) {
|
|
268
|
-
try {
|
|
269
|
-
const input = yield helper.fs.readFile(id, 'utf8');
|
|
270
|
-
return apiLoader(input + '\n' + 'const taro = Taro__default\n');
|
|
271
|
-
}
|
|
272
|
-
catch (_) {
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
}
|
|
280
|
-
// todo 后面看看能否把 preact 改为虚拟模块
|
|
281
|
-
function esbuildExclude(framework) {
|
|
282
|
-
if (framework !== 'preact')
|
|
283
|
-
return null;
|
|
284
|
-
return {
|
|
285
|
-
name: 'taro-react:esvuild-exclude',
|
|
286
|
-
enforce: 'pre',
|
|
287
|
-
config: () => ({
|
|
288
|
-
optimizeDeps: {
|
|
289
|
-
exclude: ['react', 'preact', 'solid-js']
|
|
290
|
-
}
|
|
291
|
-
})
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function harmonyVitePlugin(ctx, framework) {
|
|
296
|
-
return [
|
|
297
|
-
injectLoaderMeta$1(ctx, framework),
|
|
298
|
-
aliasPlugin$1(ctx, framework),
|
|
299
|
-
];
|
|
300
|
-
}
|
|
301
|
-
function injectLoaderMeta$1(ctx, framework) {
|
|
302
|
-
return {
|
|
303
|
-
name: 'taro-react:loader-meta',
|
|
304
|
-
buildStart() {
|
|
305
|
-
const { runnerUtils } = ctx;
|
|
306
|
-
const { getViteHarmonyCompilerContext } = runnerUtils;
|
|
307
|
-
const viteCompilerContext = getViteHarmonyCompilerContext(this);
|
|
308
|
-
if (viteCompilerContext) {
|
|
309
|
-
viteCompilerContext.loaderMeta = getLoaderMeta(framework);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
function aliasPlugin$1(ctx, framework) {
|
|
315
|
-
if (framework === 'react') {
|
|
316
|
-
return {
|
|
317
|
-
name: 'taro-react:alias',
|
|
318
|
-
config(config) {
|
|
319
|
-
var _a;
|
|
320
|
-
const alias = [
|
|
321
|
-
{ find: /react-dom$/, replacement: '@tarojs/react' },
|
|
322
|
-
];
|
|
323
|
-
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
324
|
-
const resolveOptions = {
|
|
325
|
-
basedir: process.cwd(),
|
|
326
|
-
mainFields,
|
|
327
|
-
};
|
|
328
|
-
const isProd = config.mode === 'production';
|
|
329
|
-
// TODO:harmony 目前会导致部分包用 production 版本,部分用 development 版本,导致许多 api 报错
|
|
330
|
-
const isHarmony = ctx.runOpts.options.platform === 'harmony';
|
|
331
|
-
if (!isProd && ((_a = ctx.initialConfig.harmony) === null || _a === void 0 ? void 0 : _a.debugReact) !== true && !isHarmony) {
|
|
332
|
-
// 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
|
|
333
|
-
alias.push({ find: /react-reconciler$/, replacement: 'react-reconciler/cjs/react-reconciler.production.min.js' });
|
|
334
|
-
alias.push({ find: /react$/, replacement: 'react/cjs/react.production.min.js' });
|
|
335
|
-
alias.push({ find: /scheduler$/, replacement: 'scheduler/cjs/scheduler.production.min.js' });
|
|
336
|
-
alias.push({ find: /react\/jsx-runtime$/, replacement: 'react/cjs/react-jsx-runtime.production.min.js' });
|
|
337
|
-
// 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
|
|
338
|
-
// Why writeJson? prebundle will load package.json via readFile to check exports property.
|
|
339
|
-
const reactPkgPath = helper.resolveSync('react/package.json', resolveOptions);
|
|
340
|
-
if (reactPkgPath) {
|
|
341
|
-
const reactPkg = require('react/package.json');
|
|
342
|
-
const reactVersion = (reactPkg.version || '');
|
|
343
|
-
if ((/^[~^]?18/).test(reactVersion) && reactPkg.exports) {
|
|
344
|
-
reactPkg.exports = Object.assign(reactPkg.exports, {
|
|
345
|
-
'./cjs/': './cjs/'
|
|
346
|
-
});
|
|
347
|
-
helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
return {
|
|
352
|
-
resolve: {
|
|
353
|
-
alias
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
else if (framework === 'solid') {
|
|
360
|
-
return {
|
|
361
|
-
name: 'taro-solid:alias',
|
|
362
|
-
config() {
|
|
363
|
-
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
364
|
-
const alias = [
|
|
365
|
-
{ find: 'solid-js/web', replacement: reconcilerName },
|
|
366
|
-
{ find: 'react/jsx-runtime', replacement: reconcilerName },
|
|
367
|
-
];
|
|
368
|
-
return {
|
|
369
|
-
resolve: {
|
|
370
|
-
alias
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
return [];
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
function miniVitePlugin(ctx, framework) {
|
|
380
|
-
return [
|
|
381
|
-
injectLoaderMeta(ctx, framework),
|
|
382
|
-
aliasPlugin(ctx, framework),
|
|
383
|
-
];
|
|
384
|
-
}
|
|
385
|
-
function injectLoaderMeta(ctx, framework) {
|
|
386
|
-
return {
|
|
387
|
-
name: 'taro-react:loader-meta',
|
|
388
|
-
buildStart() {
|
|
389
|
-
const { runnerUtils } = ctx;
|
|
390
|
-
const { getViteMiniCompilerContext } = runnerUtils;
|
|
391
|
-
const viteCompilerContext = getViteMiniCompilerContext(this);
|
|
392
|
-
if (viteCompilerContext) {
|
|
393
|
-
viteCompilerContext.loaderMeta = getLoaderMeta(framework);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
function aliasPlugin(ctx, framework) {
|
|
399
|
-
if (framework === 'react') {
|
|
400
|
-
return {
|
|
401
|
-
name: 'taro-react:alias',
|
|
402
|
-
config(config) {
|
|
403
|
-
var _a;
|
|
404
|
-
const alias = [
|
|
405
|
-
{ find: /react-dom$/, replacement: '@tarojs/react' }
|
|
406
|
-
];
|
|
407
|
-
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
408
|
-
const resolveOptions = {
|
|
409
|
-
basedir: process.cwd(),
|
|
410
|
-
mainFields,
|
|
411
|
-
};
|
|
412
|
-
const isProd = config.mode === 'production';
|
|
413
|
-
// TODO:harmony 目前会导致部分包用 production 版本,部分用 development 版本,导致许多 api 报错
|
|
414
|
-
const isHarmony = ctx.runOpts.options.platform === 'harmony';
|
|
415
|
-
if (!isProd && ((_a = ctx.initialConfig.mini) === null || _a === void 0 ? void 0 : _a.debugReact) !== true && !isHarmony) {
|
|
416
|
-
// 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
|
|
417
|
-
alias.push({ find: /react-reconciler$/, replacement: 'react-reconciler/cjs/react-reconciler.production.min.js' });
|
|
418
|
-
alias.push({ find: /^(?!.*mobx-react$).*react$/, replacement: 'react/cjs/react.production.min.js' });
|
|
419
|
-
alias.push({ find: /scheduler$/, replacement: 'scheduler/cjs/scheduler.production.min.js' });
|
|
420
|
-
alias.push({ find: /react\/jsx-runtime$/, replacement: 'react/cjs/react-jsx-runtime.production.min.js' });
|
|
421
|
-
// 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
|
|
422
|
-
// Why writeJson? prebundle will load package.json via readFile to check exports property.
|
|
423
|
-
const reactPkgPath = helper.resolveSync('react/package.json', resolveOptions);
|
|
424
|
-
if (reactPkgPath) {
|
|
425
|
-
const reactPkg = require('react/package.json');
|
|
426
|
-
const reactVersion = (reactPkg.version || '');
|
|
427
|
-
if ((/^[~^]?18/).test(reactVersion) && reactPkg.exports) {
|
|
428
|
-
reactPkg.exports = Object.assign(reactPkg.exports, {
|
|
429
|
-
'./cjs/': './cjs/'
|
|
430
|
-
});
|
|
431
|
-
helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
return {
|
|
436
|
-
resolve: {
|
|
437
|
-
alias
|
|
438
|
-
}
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
else if (framework === 'solid') {
|
|
444
|
-
return {
|
|
445
|
-
name: 'taro-solid:alias',
|
|
446
|
-
config() {
|
|
447
|
-
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
448
|
-
const alias = [
|
|
449
|
-
{ find: 'solid-js/web', replacement: reconcilerName },
|
|
450
|
-
{ find: 'react/jsx-runtime', replacement: reconcilerName },
|
|
451
|
-
];
|
|
452
|
-
return {
|
|
453
|
-
resolve: {
|
|
454
|
-
alias
|
|
455
|
-
}
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
};
|
|
459
|
-
}
|
|
460
|
-
return [];
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
function modifyH5WebpackChain(ctx, framework, chain) {
|
|
464
|
-
var _a;
|
|
465
|
-
setLoader$2(framework, chain);
|
|
466
|
-
setPlugin(ctx, framework, chain);
|
|
467
|
-
const { isBuildNativeComp = false } = ((_a = ctx.runOpts) === null || _a === void 0 ? void 0 : _a.options) || {};
|
|
468
|
-
const externals = {};
|
|
469
|
-
if (isBuildNativeComp) {
|
|
470
|
-
// Note: 该模式不支持 prebundle 优化,不必再处理
|
|
471
|
-
externals.react = {
|
|
472
|
-
commonjs: 'react',
|
|
473
|
-
commonjs2: 'react',
|
|
474
|
-
amd: 'react',
|
|
475
|
-
root: 'React'
|
|
476
|
-
};
|
|
477
|
-
externals['react-dom'] = {
|
|
478
|
-
commonjs: 'react-dom',
|
|
479
|
-
commonjs2: 'react-dom',
|
|
480
|
-
amd: 'react-dom',
|
|
481
|
-
root: 'ReactDOM'
|
|
482
|
-
};
|
|
483
|
-
if (framework === 'preact') {
|
|
484
|
-
externals.preact = 'preact';
|
|
485
|
-
}
|
|
486
|
-
chain.merge({
|
|
487
|
-
externalsType: 'umd'
|
|
488
|
-
});
|
|
489
|
-
}
|
|
490
|
-
chain.merge({
|
|
491
|
-
externals,
|
|
492
|
-
module: {
|
|
493
|
-
rule: {
|
|
494
|
-
'process-import-taro-h5': {
|
|
495
|
-
test: helper.REG_TARO_H5,
|
|
496
|
-
loader: require.resolve('./api-loader'),
|
|
497
|
-
},
|
|
498
|
-
},
|
|
499
|
-
},
|
|
500
|
-
});
|
|
501
|
-
chain.merge({
|
|
502
|
-
externals,
|
|
503
|
-
module: {
|
|
504
|
-
rule: {
|
|
505
|
-
'process-import-taro-harmony-hybrid': {
|
|
506
|
-
test: /taro-platform-harmony-hybrid[\\/]dist[\\/]api[\\/]apis[\\/]taro/,
|
|
507
|
-
loader: require.resolve('./api-loader')
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
},
|
|
511
|
-
});
|
|
512
|
-
chain.merge({
|
|
513
|
-
externals,
|
|
514
|
-
module: {
|
|
515
|
-
rule: {
|
|
516
|
-
'process-import-taro-harmony-hybrid': {
|
|
517
|
-
test: /taro-platform-harmony-hybrid[\\/]dist[\\/]api[\\/]apis[\\/]taro/,
|
|
518
|
-
loader: require.resolve('./api-loader')
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
},
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
|
-
function setLoader$2(framework, chain) {
|
|
525
|
-
function customizer(object = '', sources = '') {
|
|
526
|
-
if ([object, sources].every((e) => typeof e === 'string'))
|
|
527
|
-
return object + sources;
|
|
528
|
-
}
|
|
529
|
-
chain.plugin('mainPlugin').tap((args) => {
|
|
530
|
-
args[0].loaderMeta = lodash.mergeWith(getLoaderMeta(framework), args[0].loaderMeta, customizer);
|
|
531
|
-
return args;
|
|
532
|
-
});
|
|
533
|
-
}
|
|
534
|
-
function setPlugin(ctx, framework, chain) {
|
|
535
|
-
var _a, _b;
|
|
536
|
-
const config = ctx.initialConfig;
|
|
537
|
-
const webpackConfig = chain.toConfig();
|
|
538
|
-
const isProd = webpackConfig.mode === 'production';
|
|
539
|
-
if (!isProd && ((_b = (_a = config.h5) === null || _a === void 0 ? void 0 : _a.devServer) === null || _b === void 0 ? void 0 : _b.hot) !== false) {
|
|
540
|
-
// 默认开启 fast-refresh
|
|
541
|
-
if (framework === 'react') {
|
|
542
|
-
chain.plugin('fastRefreshPlugin').use(require('@pmmmwh/react-refresh-webpack-plugin'));
|
|
543
|
-
}
|
|
544
|
-
else if (framework === 'preact') {
|
|
545
|
-
chain.plugin('hotModuleReplacementPlugin').use(require('webpack').HotModuleReplacementPlugin);
|
|
546
|
-
chain.plugin('fastRefreshPlugin').use(require('@prefresh/webpack'));
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
550
|
-
const resolveOptions = {
|
|
551
|
-
basedir: process.cwd(),
|
|
552
|
-
mainFields,
|
|
553
|
-
};
|
|
554
|
-
if (framework === 'solid') {
|
|
555
|
-
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
556
|
-
const alias = chain.resolve.alias;
|
|
557
|
-
alias.set(reconcilerName, helper.resolveSync('solid-js/web', resolveOptions));
|
|
558
|
-
// Note: 本地 link 调试时,避免 solid 重复打包
|
|
559
|
-
alias.set('solid-js$', helper.resolveSync('solid-js', resolveOptions));
|
|
560
|
-
}
|
|
561
|
-
else if (framework === 'react') {
|
|
562
|
-
const alias = chain.resolve.alias;
|
|
563
|
-
// Note: 本地 link 调试时,避免 react 重复打包
|
|
564
|
-
alias.set('react$', helper.resolveSync('react', resolveOptions));
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
function modifyHarmonyWebpackChain(ctx, framework, chain) {
|
|
569
|
-
setAlias$2(ctx, framework, chain);
|
|
570
|
-
setLoader$1(framework, chain);
|
|
571
|
-
}
|
|
572
|
-
function setAlias$2(ctx, framework, chain) {
|
|
573
|
-
var _a;
|
|
574
|
-
const config = ctx.initialConfig;
|
|
575
|
-
const alias = chain.resolve.alias;
|
|
576
|
-
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
577
|
-
const resolveOptions = {
|
|
578
|
-
basedir: process.cwd(),
|
|
579
|
-
mainFields,
|
|
580
|
-
};
|
|
581
|
-
if (framework === 'react') {
|
|
582
|
-
alias.set('react-dom$', '@tarojs/react');
|
|
583
|
-
alias.set('react-dom/client$', '@tarojs/react');
|
|
584
|
-
const webpackConfig = chain.toConfig();
|
|
585
|
-
const isProd = webpackConfig.mode === 'production';
|
|
586
|
-
if (!isProd && ((_a = config.harmony) === null || _a === void 0 ? void 0 : _a.debugReact) !== true) {
|
|
587
|
-
// 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
|
|
588
|
-
// 兼容pnpm workspace
|
|
589
|
-
const reactModulePath = helper.resolveSync('react', resolveOptions);
|
|
590
|
-
const newFilePath = path.join(path.dirname(reactModulePath), 'cjs', 'react.production.min.js');
|
|
591
|
-
alias.set('react-reconciler$', 'react-reconciler/cjs/react-reconciler.production.min.js');
|
|
592
|
-
alias.set('react$', newFilePath);
|
|
593
|
-
alias.set('react/jsx-runtime$', 'react/cjs/react-jsx-runtime.production.min.js');
|
|
594
|
-
// 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
|
|
595
|
-
// Why writeJson? prebundle will load package.json via readFile to check exports property.
|
|
596
|
-
const reactPkgPath = helper.resolveSync('react/package.json', resolveOptions);
|
|
597
|
-
if (reactPkgPath) {
|
|
598
|
-
const reactPkg = require('react/package.json');
|
|
599
|
-
const reactVersion = reactPkg.version || '';
|
|
600
|
-
if (/^[~^]?18/.test(reactVersion) && reactPkg.exports) {
|
|
601
|
-
reactPkg.exports = Object.assign(reactPkg.exports, {
|
|
602
|
-
'./cjs/': './cjs/',
|
|
603
|
-
});
|
|
604
|
-
helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
else if (framework === 'solid') {
|
|
610
|
-
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
611
|
-
alias.set('solid-js/web', reconcilerName);
|
|
612
|
-
alias.set('react/jsx-runtime', reconcilerName);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
function setLoader$1(framework, chain) {
|
|
616
|
-
chain.plugin('mainPlugin').tap((args) => {
|
|
617
|
-
args[0].loaderMeta = getLoaderMeta(framework);
|
|
618
|
-
return args;
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
function modifyMiniWebpackChain(ctx, framework, chain) {
|
|
623
|
-
setAlias$1(ctx, framework, chain);
|
|
624
|
-
setLoader(framework, chain);
|
|
625
|
-
}
|
|
626
|
-
function setAlias$1(ctx, framework, chain) {
|
|
627
|
-
var _a;
|
|
628
|
-
const config = ctx.initialConfig;
|
|
629
|
-
const alias = chain.resolve.alias;
|
|
630
|
-
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
631
|
-
const resolveOptions = {
|
|
632
|
-
basedir: process.cwd(),
|
|
633
|
-
mainFields,
|
|
634
|
-
};
|
|
635
|
-
if (framework === 'react') {
|
|
636
|
-
alias.set('react-dom$', '@tarojs/react');
|
|
637
|
-
alias.set('react-dom/client$', '@tarojs/react');
|
|
638
|
-
const webpackConfig = chain.toConfig();
|
|
639
|
-
const isProd = webpackConfig.mode === 'production';
|
|
640
|
-
if (!isProd && ((_a = config.mini) === null || _a === void 0 ? void 0 : _a.debugReact) !== true) {
|
|
641
|
-
// 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
|
|
642
|
-
// 兼容pnpm workspace
|
|
643
|
-
const reactModulePath = helper.resolveSync('react', resolveOptions);
|
|
644
|
-
const newFilePath = path.join(path.dirname(reactModulePath), 'cjs', 'react.production.min.js');
|
|
645
|
-
alias.set('react-reconciler$', 'react-reconciler/cjs/react-reconciler.production.min.js');
|
|
646
|
-
alias.set(/^(?!.*mobx-react$).*react$/, newFilePath);
|
|
647
|
-
alias.set('react/jsx-runtime$', 'react/cjs/react-jsx-runtime.production.min.js');
|
|
648
|
-
// 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
|
|
649
|
-
// Why writeJson? prebundle will load package.json via readFile to check exports property.
|
|
650
|
-
const reactPkgPath = helper.resolveSync('react/package.json', resolveOptions);
|
|
651
|
-
if (reactPkgPath) {
|
|
652
|
-
const reactPkg = require('react/package.json');
|
|
653
|
-
const reactVersion = reactPkg.version || '';
|
|
654
|
-
if (/^[~^]?18/.test(reactVersion) && reactPkg.exports) {
|
|
655
|
-
reactPkg.exports = Object.assign(reactPkg.exports, {
|
|
656
|
-
'./cjs/': './cjs/',
|
|
657
|
-
});
|
|
658
|
-
helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
else if (framework === 'solid') {
|
|
664
|
-
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
665
|
-
alias.set('solid-js/web', reconcilerName);
|
|
666
|
-
// Note: 本地 link 调试时,避免 solid 重复打包
|
|
667
|
-
alias.set('solid-js$', helper.resolveSync('solid-js', resolveOptions));
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
function setLoader(framework, chain) {
|
|
671
|
-
chain.plugin('miniPlugin').tap((args) => {
|
|
672
|
-
args[0].loaderMeta = getLoaderMeta(framework);
|
|
673
|
-
return args;
|
|
674
|
-
});
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
function isReactLike(framework = 'react') {
|
|
678
|
-
return ['react', 'preact', 'solid'].includes(framework);
|
|
679
|
-
}
|
|
680
|
-
var index = (ctx) => {
|
|
681
|
-
const { framework = 'react' } = ctx.initialConfig;
|
|
682
|
-
if (!isReactLike(framework))
|
|
683
|
-
return;
|
|
684
|
-
ctx.modifyWebpackChain(({ chain }) => {
|
|
685
|
-
// 通用
|
|
686
|
-
setAlias(framework, chain);
|
|
687
|
-
if (process.env.TARO_PLATFORM === 'web') {
|
|
688
|
-
// H5
|
|
689
|
-
modifyH5WebpackChain(ctx, framework, chain);
|
|
690
|
-
}
|
|
691
|
-
else if (process.env.TARO_PLATFORM === 'harmony' || process.env.TARO_ENV === 'harmony') {
|
|
692
|
-
// 鸿蒙
|
|
693
|
-
modifyHarmonyWebpackChain(ctx, framework, chain);
|
|
694
|
-
}
|
|
695
|
-
else {
|
|
696
|
-
// 小程序
|
|
697
|
-
modifyMiniWebpackChain(ctx, framework, chain);
|
|
698
|
-
}
|
|
699
|
-
});
|
|
700
|
-
ctx.modifyRunnerOpts(({ opts }) => {
|
|
701
|
-
if (!(opts === null || opts === void 0 ? void 0 : opts.compiler))
|
|
702
|
-
return;
|
|
703
|
-
if (shared.isString(opts.compiler)) {
|
|
704
|
-
opts.compiler = {
|
|
705
|
-
type: opts.compiler,
|
|
706
|
-
};
|
|
707
|
-
}
|
|
708
|
-
const { compiler } = opts;
|
|
709
|
-
if (compiler.type === 'webpack5') {
|
|
710
|
-
// 提供给 webpack5 依赖预编译收集器的第三方依赖
|
|
711
|
-
const deps = ['react', 'react-dom', 'react/jsx-runtime', '@tarojs/plugin-framework-react/dist/runtime'];
|
|
712
|
-
compiler.prebundle || (compiler.prebundle = {});
|
|
713
|
-
const prebundleOptions = compiler.prebundle;
|
|
714
|
-
prebundleOptions.include || (prebundleOptions.include = []);
|
|
715
|
-
prebundleOptions.include = prebundleOptions.include.concat(deps);
|
|
716
|
-
prebundleOptions.exclude || (prebundleOptions.exclude = []);
|
|
717
|
-
prebundleOptions.exclude.push(/mobx/); // 依赖会对 webpack 修改,默认排除
|
|
718
|
-
if (prebundleOptions.enable === false)
|
|
719
|
-
return;
|
|
720
|
-
const taroReactPlugin = {
|
|
721
|
-
name: 'taroReactPlugin',
|
|
722
|
-
setup(build) {
|
|
723
|
-
build.onLoad({ filter: helper.REG_TARO_H5 }, ({ path }) => {
|
|
724
|
-
const content = helper.fs.readFileSync(path).toString();
|
|
725
|
-
return {
|
|
726
|
-
contents: require('./api-loader')(content),
|
|
727
|
-
};
|
|
728
|
-
});
|
|
729
|
-
build.onLoad({ filter: /taro-platform-harmony-hybrid[\\/]dist[\\/]api[\\/]apis[\\/]taro/ }, ({ path }) => {
|
|
730
|
-
const content = helper.fs.readFileSync(path).toString();
|
|
731
|
-
return {
|
|
732
|
-
contents: require('./api-loader')(content)
|
|
733
|
-
};
|
|
734
|
-
});
|
|
735
|
-
},
|
|
736
|
-
};
|
|
737
|
-
prebundleOptions.esbuild || (prebundleOptions.esbuild = {});
|
|
738
|
-
const esbuildConfig = prebundleOptions.esbuild;
|
|
739
|
-
esbuildConfig.plugins || (esbuildConfig.plugins = []);
|
|
740
|
-
esbuildConfig.plugins.push(taroReactPlugin);
|
|
741
|
-
}
|
|
742
|
-
else if (compiler.type === 'vite') {
|
|
743
|
-
compiler.vitePlugins || (compiler.vitePlugins = []);
|
|
744
|
-
compiler.vitePlugins.push(viteCommonPlugin(framework));
|
|
745
|
-
compiler.vitePlugins.push(VitePresetPlugin(framework));
|
|
746
|
-
if (process.env.TARO_PLATFORM === 'web') {
|
|
747
|
-
// H5
|
|
748
|
-
compiler.vitePlugins.push(h5iVitePlugin(ctx, framework));
|
|
749
|
-
}
|
|
750
|
-
else if (process.env.TARO_PLATFORM === 'harmony' || process.env.TARO_ENV === 'harmony') {
|
|
751
|
-
// 鸿蒙
|
|
752
|
-
compiler.vitePlugins.push(harmonyVitePlugin(ctx, framework));
|
|
753
|
-
}
|
|
754
|
-
else {
|
|
755
|
-
// 小程序
|
|
756
|
-
compiler.vitePlugins.push(miniVitePlugin(ctx, framework));
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
});
|
|
760
|
-
};
|
|
761
|
-
function setAlias(framework, chain) {
|
|
762
|
-
const alias = chain.resolve.alias;
|
|
763
|
-
switch (framework) {
|
|
764
|
-
case 'preact':
|
|
765
|
-
alias.set('react', 'preact/compat');
|
|
766
|
-
alias.set('react-dom/test-utils', 'preact/test-utils');
|
|
767
|
-
alias.set('react-dom', 'preact/compat');
|
|
768
|
-
alias.set('react/jsx-runtime', 'preact/jsx-runtime');
|
|
769
|
-
break;
|
|
770
|
-
case 'solid':
|
|
771
|
-
alias.set('react/jsx-runtime', 'solid-js/h/jsx-runtime');
|
|
772
|
-
break;
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
function VitePresetPlugin(framework) {
|
|
776
|
-
return framework === 'preact'
|
|
777
|
-
? require('@preact/preset-vite').preact({
|
|
778
|
-
babel: {
|
|
779
|
-
plugins: [
|
|
780
|
-
['@babel/plugin-proposal-decorators', { legacy: true }],
|
|
781
|
-
['@babel/plugin-proposal-class-properties', { loose: true }],
|
|
782
|
-
],
|
|
783
|
-
},
|
|
784
|
-
})
|
|
785
|
-
: require('@vitejs/plugin-react').default({
|
|
786
|
-
babel: {
|
|
787
|
-
plugins: [
|
|
788
|
-
['@babel/plugin-proposal-decorators', { legacy: true }],
|
|
789
|
-
['@babel/plugin-proposal-class-properties', { loose: true }],
|
|
790
|
-
],
|
|
791
|
-
},
|
|
792
|
-
});
|
|
793
|
-
}
|
|
794
|
-
function viteCommonPlugin(framework) {
|
|
795
|
-
return {
|
|
796
|
-
name: 'taro-react:common',
|
|
797
|
-
config() {
|
|
798
|
-
const alias = framework === 'preact'
|
|
799
|
-
? [
|
|
800
|
-
{ find: 'react', replacement: 'preact/compat' },
|
|
801
|
-
{ find: 'react-dom/test-utils', replacement: 'preact/test-utils' },
|
|
802
|
-
{ find: 'react-dom', replacement: 'preact/compat' },
|
|
803
|
-
{ find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' },
|
|
804
|
-
]
|
|
805
|
-
: [];
|
|
806
|
-
if (framework === 'solid') {
|
|
807
|
-
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
808
|
-
alias.push({ find: 'react/jsx-runtime', replacement: reconcilerName });
|
|
809
|
-
}
|
|
810
|
-
return {
|
|
811
|
-
resolve: {
|
|
812
|
-
alias,
|
|
813
|
-
},
|
|
814
|
-
};
|
|
815
|
-
},
|
|
816
|
-
};
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
exports.default = index;
|
|
820
|
-
exports.isReactLike = isReactLike;
|
|
821
|
-
//# sourceMappingURL=index.js.map
|