@tarojs/plugin-framework-react 3.8.0-canary.0 → 4.0.0-alpha.10
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 +11 -4
- package/dist/api-loader.js.map +1 -1
- package/dist/index.js +528 -63
- package/dist/index.js.map +1 -1
- package/dist/runtime.js +81 -47
- package/dist/runtime.js.map +1 -1
- package/package.json +45 -26
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var helper = require('@tarojs/helper');
|
|
6
6
|
var shared = require('@tarojs/shared');
|
|
7
|
+
var tslib = require('tslib');
|
|
7
8
|
var lodash = require('lodash');
|
|
8
9
|
var acorn = require('acorn');
|
|
9
10
|
var walk = require('acorn-walk');
|
|
@@ -29,6 +30,86 @@ function _interopNamespaceDefault(e) {
|
|
|
29
30
|
var acorn__namespace = /*#__PURE__*/_interopNamespaceDefault(acorn);
|
|
30
31
|
var walk__namespace = /*#__PURE__*/_interopNamespaceDefault(walk);
|
|
31
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
|
+
|
|
32
113
|
function addConfig(source) {
|
|
33
114
|
const configsMap = {
|
|
34
115
|
enableShareAppMessage: ['onShareAppMessage', 'useShareAppMessage'],
|
|
@@ -80,20 +161,6 @@ function addConfig(source) {
|
|
|
80
161
|
});
|
|
81
162
|
return additionConfig;
|
|
82
163
|
}
|
|
83
|
-
const nervMeta = {
|
|
84
|
-
importFrameworkStatement: `
|
|
85
|
-
import Nerv from 'nervjs';
|
|
86
|
-
`,
|
|
87
|
-
mockAppStatement: `
|
|
88
|
-
class App extends Nerv.Component {
|
|
89
|
-
render () {
|
|
90
|
-
return this.props.children
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
`,
|
|
94
|
-
frameworkArgs: 'Nerv, Nerv, config',
|
|
95
|
-
importFrameworkName: 'Nerv'
|
|
96
|
-
};
|
|
97
164
|
function getLoaderMeta(framework) {
|
|
98
165
|
const loaderMeta = {
|
|
99
166
|
importFrameworkStatement: `
|
|
@@ -117,10 +184,20 @@ class App extends React.Component {
|
|
|
117
184
|
Object.assign(config, addConfig(source));
|
|
118
185
|
}
|
|
119
186
|
};
|
|
120
|
-
if (framework === '
|
|
121
|
-
Object.assign(loaderMeta,
|
|
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
|
+
});
|
|
122
199
|
}
|
|
123
|
-
if (
|
|
200
|
+
if (process.env.TARO_PLATFORM === 'web') {
|
|
124
201
|
if (framework === 'react') {
|
|
125
202
|
const react = require('react');
|
|
126
203
|
const majorVersion = Number((react.version || '18').split('.')[0]);
|
|
@@ -135,39 +212,294 @@ class App extends React.Component {
|
|
|
135
212
|
return loaderMeta;
|
|
136
213
|
}
|
|
137
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
|
+
// 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
|
|
333
|
+
// Why writeJson? prebundle will load package.json via readFile to check exports property.
|
|
334
|
+
const reactPkgPath = helper.resolveSync('react/package.json', resolveOptions);
|
|
335
|
+
if (reactPkgPath) {
|
|
336
|
+
const reactPkg = require('react/package.json');
|
|
337
|
+
const reactVersion = (reactPkg.version || '');
|
|
338
|
+
if ((/^[~^]?18/).test(reactVersion) && reactPkg.exports) {
|
|
339
|
+
reactPkg.exports = Object.assign(reactPkg.exports, {
|
|
340
|
+
'./cjs/': './cjs/'
|
|
341
|
+
});
|
|
342
|
+
helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
resolve: {
|
|
348
|
+
alias
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
else if (framework === 'solid') {
|
|
355
|
+
return {
|
|
356
|
+
name: 'taro-solid:alias',
|
|
357
|
+
config() {
|
|
358
|
+
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
359
|
+
const alias = [
|
|
360
|
+
{ find: 'solid-js/web', replacement: reconcilerName },
|
|
361
|
+
{ find: 'react/jsx-runtime', replacement: reconcilerName },
|
|
362
|
+
];
|
|
363
|
+
return {
|
|
364
|
+
resolve: {
|
|
365
|
+
alias
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
return [];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function miniVitePlugin(ctx, framework) {
|
|
375
|
+
return [
|
|
376
|
+
injectLoaderMeta(ctx, framework),
|
|
377
|
+
aliasPlugin(ctx, framework),
|
|
378
|
+
];
|
|
379
|
+
}
|
|
380
|
+
function injectLoaderMeta(ctx, framework) {
|
|
381
|
+
return {
|
|
382
|
+
name: 'taro-react:loader-meta',
|
|
383
|
+
buildStart() {
|
|
384
|
+
const { runnerUtils } = ctx;
|
|
385
|
+
const { getViteMiniCompilerContext } = runnerUtils;
|
|
386
|
+
const viteCompilerContext = getViteMiniCompilerContext(this);
|
|
387
|
+
if (viteCompilerContext) {
|
|
388
|
+
viteCompilerContext.loaderMeta = getLoaderMeta(framework);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function aliasPlugin(ctx, framework) {
|
|
394
|
+
if (framework === 'react') {
|
|
395
|
+
return {
|
|
396
|
+
name: 'taro-react:alias',
|
|
397
|
+
config(config) {
|
|
398
|
+
var _a;
|
|
399
|
+
const alias = [
|
|
400
|
+
{ find: /react-dom$/, replacement: '@tarojs/react' }
|
|
401
|
+
];
|
|
402
|
+
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
403
|
+
const resolveOptions = {
|
|
404
|
+
basedir: process.cwd(),
|
|
405
|
+
mainFields,
|
|
406
|
+
};
|
|
407
|
+
const isProd = config.mode === 'production';
|
|
408
|
+
// TODO:harmony 目前会导致部分包用 production 版本,部分用 development 版本,导致许多 api 报错
|
|
409
|
+
const isHarmony = ctx.runOpts.options.platform === 'harmony';
|
|
410
|
+
if (!isProd && ((_a = ctx.initialConfig.mini) === null || _a === void 0 ? void 0 : _a.debugReact) !== true && !isHarmony) {
|
|
411
|
+
// 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
|
|
412
|
+
alias.push({ find: /react-reconciler$/, replacement: 'react-reconciler/cjs/react-reconciler.production.min.js' });
|
|
413
|
+
alias.push({ find: /^(?!.*mobx-react$).*react$/, replacement: 'react/cjs/react.production.min.js' });
|
|
414
|
+
alias.push({ find: /scheduler$/, replacement: 'scheduler/cjs/scheduler.production.min.js' });
|
|
415
|
+
alias.push({ find: /react\/jsx-runtime$/, replacement: 'react/cjs/react-jsx-runtime.production.min.js' });
|
|
416
|
+
// 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
|
|
417
|
+
// Why writeJson? prebundle will load package.json via readFile to check exports property.
|
|
418
|
+
const reactPkgPath = helper.resolveSync('react/package.json', resolveOptions);
|
|
419
|
+
if (reactPkgPath) {
|
|
420
|
+
const reactPkg = require('react/package.json');
|
|
421
|
+
const reactVersion = (reactPkg.version || '');
|
|
422
|
+
if ((/^[~^]?18/).test(reactVersion) && reactPkg.exports) {
|
|
423
|
+
reactPkg.exports = Object.assign(reactPkg.exports, {
|
|
424
|
+
'./cjs/': './cjs/'
|
|
425
|
+
});
|
|
426
|
+
helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
resolve: {
|
|
432
|
+
alias
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
else if (framework === 'solid') {
|
|
439
|
+
return {
|
|
440
|
+
name: 'taro-solid:alias',
|
|
441
|
+
config() {
|
|
442
|
+
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
443
|
+
const alias = [
|
|
444
|
+
{ find: 'solid-js/web', replacement: reconcilerName },
|
|
445
|
+
{ find: 'react/jsx-runtime', replacement: reconcilerName },
|
|
446
|
+
];
|
|
447
|
+
return {
|
|
448
|
+
resolve: {
|
|
449
|
+
alias
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
return [];
|
|
456
|
+
}
|
|
457
|
+
|
|
138
458
|
function modifyH5WebpackChain(ctx, framework, chain) {
|
|
139
459
|
var _a;
|
|
140
|
-
setLoader$
|
|
460
|
+
setLoader$2(framework, chain);
|
|
141
461
|
setPlugin(ctx, framework, chain);
|
|
142
462
|
const { isBuildNativeComp = false } = ((_a = ctx.runOpts) === null || _a === void 0 ? void 0 : _a.options) || {};
|
|
143
463
|
const externals = {};
|
|
144
464
|
if (isBuildNativeComp) {
|
|
145
465
|
// Note: 该模式不支持 prebundle 优化,不必再处理
|
|
146
|
-
externals.react =
|
|
147
|
-
|
|
466
|
+
externals.react = {
|
|
467
|
+
commonjs: 'react',
|
|
468
|
+
commonjs2: 'react',
|
|
469
|
+
amd: 'react',
|
|
470
|
+
root: 'React'
|
|
471
|
+
};
|
|
472
|
+
externals['react-dom'] = {
|
|
473
|
+
commonjs: 'react-dom',
|
|
474
|
+
commonjs2: 'react-dom',
|
|
475
|
+
amd: 'react-dom',
|
|
476
|
+
root: 'ReactDOM'
|
|
477
|
+
};
|
|
148
478
|
if (framework === 'preact') {
|
|
149
479
|
externals.preact = 'preact';
|
|
150
480
|
}
|
|
481
|
+
chain.merge({
|
|
482
|
+
externalsType: 'umd'
|
|
483
|
+
});
|
|
151
484
|
}
|
|
152
485
|
chain.merge({
|
|
153
486
|
externals,
|
|
154
487
|
module: {
|
|
155
488
|
rule: {
|
|
156
|
-
'process-import-taro': {
|
|
157
|
-
test:
|
|
158
|
-
loader: require.resolve('./api-loader')
|
|
159
|
-
}
|
|
160
|
-
}
|
|
489
|
+
'process-import-taro-h5': {
|
|
490
|
+
test: helper.REG_TARO_H5,
|
|
491
|
+
loader: require.resolve('./api-loader'),
|
|
492
|
+
},
|
|
493
|
+
},
|
|
161
494
|
},
|
|
162
495
|
});
|
|
163
496
|
}
|
|
164
|
-
function setLoader$
|
|
497
|
+
function setLoader$2(framework, chain) {
|
|
165
498
|
function customizer(object = '', sources = '') {
|
|
166
|
-
if ([object, sources].every(e => typeof e === 'string'))
|
|
499
|
+
if ([object, sources].every((e) => typeof e === 'string'))
|
|
167
500
|
return object + sources;
|
|
168
501
|
}
|
|
169
|
-
chain.plugin('mainPlugin')
|
|
170
|
-
.tap(args => {
|
|
502
|
+
chain.plugin('mainPlugin').tap((args) => {
|
|
171
503
|
args[0].loaderMeta = lodash.mergeWith(getLoaderMeta(framework), args[0].loaderMeta, customizer);
|
|
172
504
|
return args;
|
|
173
505
|
});
|
|
@@ -180,19 +512,85 @@ function setPlugin(ctx, framework, chain) {
|
|
|
180
512
|
if (!isProd && ((_b = (_a = config.h5) === null || _a === void 0 ? void 0 : _a.devServer) === null || _b === void 0 ? void 0 : _b.hot) !== false) {
|
|
181
513
|
// 默认开启 fast-refresh
|
|
182
514
|
if (framework === 'react') {
|
|
183
|
-
chain
|
|
184
|
-
.plugin('fastRefreshPlugin')
|
|
185
|
-
.use(require('@pmmmwh/react-refresh-webpack-plugin'));
|
|
515
|
+
chain.plugin('fastRefreshPlugin').use(require('@pmmmwh/react-refresh-webpack-plugin'));
|
|
186
516
|
}
|
|
187
517
|
else if (framework === 'preact') {
|
|
188
|
-
chain
|
|
189
|
-
|
|
190
|
-
.use(require('webpack').HotModuleReplacementPlugin);
|
|
191
|
-
chain
|
|
192
|
-
.plugin('fastRefreshPlugin')
|
|
193
|
-
.use(require('@prefresh/webpack'));
|
|
518
|
+
chain.plugin('hotModuleReplacementPlugin').use(require('webpack').HotModuleReplacementPlugin);
|
|
519
|
+
chain.plugin('fastRefreshPlugin').use(require('@prefresh/webpack'));
|
|
194
520
|
}
|
|
195
521
|
}
|
|
522
|
+
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
523
|
+
const resolveOptions = {
|
|
524
|
+
basedir: process.cwd(),
|
|
525
|
+
mainFields,
|
|
526
|
+
};
|
|
527
|
+
if (framework === 'solid') {
|
|
528
|
+
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
529
|
+
const alias = chain.resolve.alias;
|
|
530
|
+
alias.set(reconcilerName, helper.resolveSync('solid-js/web', resolveOptions));
|
|
531
|
+
// Note: 本地 link 调试时,避免 solid 重复打包
|
|
532
|
+
alias.set('solid-js$', helper.resolveSync('solid-js', resolveOptions));
|
|
533
|
+
}
|
|
534
|
+
else if (framework === 'react') {
|
|
535
|
+
const alias = chain.resolve.alias;
|
|
536
|
+
// Note: 本地 link 调试时,避免 react 重复打包
|
|
537
|
+
alias.set('react$', helper.resolveSync('react', resolveOptions));
|
|
538
|
+
alias.set('react-dom$', helper.resolveSync('react-dom', resolveOptions));
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function modifyHarmonyWebpackChain(ctx, framework, chain) {
|
|
543
|
+
setAlias$2(ctx, framework, chain);
|
|
544
|
+
setLoader$1(framework, chain);
|
|
545
|
+
}
|
|
546
|
+
function setAlias$2(ctx, framework, chain) {
|
|
547
|
+
var _a;
|
|
548
|
+
const config = ctx.initialConfig;
|
|
549
|
+
const alias = chain.resolve.alias;
|
|
550
|
+
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
551
|
+
const resolveOptions = {
|
|
552
|
+
basedir: process.cwd(),
|
|
553
|
+
mainFields,
|
|
554
|
+
};
|
|
555
|
+
if (framework === 'react') {
|
|
556
|
+
alias.set('react-dom$', '@tarojs/react');
|
|
557
|
+
alias.set('react-dom/client$', '@tarojs/react');
|
|
558
|
+
const webpackConfig = chain.toConfig();
|
|
559
|
+
const isProd = webpackConfig.mode === 'production';
|
|
560
|
+
if (!isProd && ((_a = config.harmony) === null || _a === void 0 ? void 0 : _a.debugReact) !== true) {
|
|
561
|
+
// 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
|
|
562
|
+
// 兼容pnpm workspace
|
|
563
|
+
const reactModulePath = helper.resolveSync('react', resolveOptions);
|
|
564
|
+
const newFilePath = path.join(path.dirname(reactModulePath), 'cjs', 'react.production.min.js');
|
|
565
|
+
alias.set('react-reconciler$', 'react-reconciler/cjs/react-reconciler.production.min.js');
|
|
566
|
+
alias.set('react$', newFilePath);
|
|
567
|
+
alias.set('react/jsx-runtime$', 'react/cjs/react-jsx-runtime.production.min.js');
|
|
568
|
+
// 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
|
|
569
|
+
// Why writeJson? prebundle will load package.json via readFile to check exports property.
|
|
570
|
+
const reactPkgPath = helper.resolveSync('react/package.json', resolveOptions);
|
|
571
|
+
if (reactPkgPath) {
|
|
572
|
+
const reactPkg = require('react/package.json');
|
|
573
|
+
const reactVersion = reactPkg.version || '';
|
|
574
|
+
if (/^[~^]?18/.test(reactVersion) && reactPkg.exports) {
|
|
575
|
+
reactPkg.exports = Object.assign(reactPkg.exports, {
|
|
576
|
+
'./cjs/': './cjs/',
|
|
577
|
+
});
|
|
578
|
+
helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
else if (framework === 'solid') {
|
|
584
|
+
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
585
|
+
alias.set('solid-js/web', reconcilerName);
|
|
586
|
+
alias.set('react/jsx-runtime', reconcilerName);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function setLoader$1(framework, chain) {
|
|
590
|
+
chain.plugin('mainPlugin').tap((args) => {
|
|
591
|
+
args[0].loaderMeta = getLoaderMeta(framework);
|
|
592
|
+
return args;
|
|
593
|
+
});
|
|
196
594
|
}
|
|
197
595
|
|
|
198
596
|
function modifyMiniWebpackChain(ctx, framework, chain) {
|
|
@@ -203,21 +601,27 @@ function setAlias$1(ctx, framework, chain) {
|
|
|
203
601
|
var _a;
|
|
204
602
|
const config = ctx.initialConfig;
|
|
205
603
|
const alias = chain.resolve.alias;
|
|
604
|
+
const mainFields = ['unpkg', ...helper.defaultMainFields];
|
|
605
|
+
const resolveOptions = {
|
|
606
|
+
basedir: process.cwd(),
|
|
607
|
+
mainFields,
|
|
608
|
+
};
|
|
206
609
|
if (framework === 'react') {
|
|
207
610
|
alias.set('react-dom$', '@tarojs/react');
|
|
611
|
+
alias.set('react-dom/client$', '@tarojs/react');
|
|
208
612
|
const webpackConfig = chain.toConfig();
|
|
209
613
|
const isProd = webpackConfig.mode === 'production';
|
|
210
614
|
if (!isProd && ((_a = config.mini) === null || _a === void 0 ? void 0 : _a.debugReact) !== true) {
|
|
211
615
|
// 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
|
|
212
616
|
// 兼容pnpm workspace
|
|
213
|
-
const reactModulePath =
|
|
617
|
+
const reactModulePath = helper.resolveSync('react', resolveOptions);
|
|
214
618
|
const newFilePath = path.join(path.dirname(reactModulePath), 'cjs', 'react.production.min.js');
|
|
215
619
|
alias.set('react-reconciler$', 'react-reconciler/cjs/react-reconciler.production.min.js');
|
|
216
|
-
alias.set(
|
|
620
|
+
alias.set(/^(?!.*mobx-react$).*react$/, newFilePath);
|
|
217
621
|
alias.set('react/jsx-runtime$', 'react/cjs/react-jsx-runtime.production.min.js');
|
|
218
622
|
// 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
|
|
219
623
|
// Why writeJson? prebundle will load package.json via readFile to check exports property.
|
|
220
|
-
const reactPkgPath =
|
|
624
|
+
const reactPkgPath = helper.resolveSync('react/package.json', resolveOptions);
|
|
221
625
|
if (reactPkgPath) {
|
|
222
626
|
const reactPkg = require('react/package.json');
|
|
223
627
|
const reactVersion = reactPkg.version || '';
|
|
@@ -230,6 +634,12 @@ function setAlias$1(ctx, framework, chain) {
|
|
|
230
634
|
}
|
|
231
635
|
}
|
|
232
636
|
}
|
|
637
|
+
else if (framework === 'solid') {
|
|
638
|
+
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
639
|
+
alias.set('solid-js/web', reconcilerName);
|
|
640
|
+
// Note: 本地 link 调试时,避免 solid 重复打包
|
|
641
|
+
alias.set('solid-js$', helper.resolveSync('solid-js', resolveOptions));
|
|
642
|
+
}
|
|
233
643
|
}
|
|
234
644
|
function setLoader(framework, chain) {
|
|
235
645
|
chain.plugin('miniPlugin').tap((args) => {
|
|
@@ -238,24 +648,24 @@ function setLoader(framework, chain) {
|
|
|
238
648
|
});
|
|
239
649
|
}
|
|
240
650
|
|
|
651
|
+
function isReactLike(framework = 'react') {
|
|
652
|
+
return ['react', 'preact', 'solid'].includes(framework);
|
|
653
|
+
}
|
|
241
654
|
var index = (ctx) => {
|
|
242
|
-
const { framework } = ctx.initialConfig;
|
|
243
|
-
if (framework
|
|
655
|
+
const { framework = 'react' } = ctx.initialConfig;
|
|
656
|
+
if (!isReactLike(framework))
|
|
244
657
|
return;
|
|
245
658
|
ctx.modifyWebpackChain(({ chain }) => {
|
|
246
659
|
// 通用
|
|
247
660
|
setAlias(framework, chain);
|
|
248
|
-
|
|
249
|
-
.plugin('definePlugin')
|
|
250
|
-
.tap(args => {
|
|
251
|
-
const config = args[0];
|
|
252
|
-
config.__TARO_FRAMEWORK__ = `"${framework}"`;
|
|
253
|
-
return args;
|
|
254
|
-
});
|
|
255
|
-
if (shared.isWebPlatform()) {
|
|
661
|
+
if (process.env.TARO_PLATFORM === 'web') {
|
|
256
662
|
// H5
|
|
257
663
|
modifyH5WebpackChain(ctx, framework, chain);
|
|
258
664
|
}
|
|
665
|
+
else if (process.env.TARO_PLATFORM === 'harmony' || process.env.TARO_ENV === 'harmony') {
|
|
666
|
+
// 鸿蒙
|
|
667
|
+
modifyHarmonyWebpackChain(ctx, framework, chain);
|
|
668
|
+
}
|
|
259
669
|
else {
|
|
260
670
|
// 小程序
|
|
261
671
|
modifyMiniWebpackChain(ctx, framework, chain);
|
|
@@ -266,18 +676,13 @@ var index = (ctx) => {
|
|
|
266
676
|
return;
|
|
267
677
|
if (shared.isString(opts.compiler)) {
|
|
268
678
|
opts.compiler = {
|
|
269
|
-
type: opts.compiler
|
|
679
|
+
type: opts.compiler,
|
|
270
680
|
};
|
|
271
681
|
}
|
|
272
682
|
const { compiler } = opts;
|
|
273
683
|
if (compiler.type === 'webpack5') {
|
|
274
684
|
// 提供给 webpack5 依赖预编译收集器的第三方依赖
|
|
275
|
-
const deps = [
|
|
276
|
-
'react',
|
|
277
|
-
'react-dom',
|
|
278
|
-
'react/jsx-runtime',
|
|
279
|
-
'@tarojs/plugin-framework-react/dist/runtime'
|
|
280
|
-
];
|
|
685
|
+
const deps = ['react', 'react-dom', 'react/jsx-runtime', '@tarojs/plugin-framework-react/dist/runtime'];
|
|
281
686
|
compiler.prebundle || (compiler.prebundle = {});
|
|
282
687
|
const prebundleOptions = compiler.prebundle;
|
|
283
688
|
prebundleOptions.include || (prebundleOptions.include = []);
|
|
@@ -289,10 +694,10 @@ var index = (ctx) => {
|
|
|
289
694
|
const taroReactPlugin = {
|
|
290
695
|
name: 'taroReactPlugin',
|
|
291
696
|
setup(build) {
|
|
292
|
-
build.onLoad({ filter:
|
|
697
|
+
build.onLoad({ filter: helper.REG_TARO_H5 }, ({ path }) => {
|
|
293
698
|
const content = helper.fs.readFileSync(path).toString();
|
|
294
699
|
return {
|
|
295
|
-
contents: require('./api-loader')(content)
|
|
700
|
+
contents: require('./api-loader')(content),
|
|
296
701
|
};
|
|
297
702
|
});
|
|
298
703
|
}
|
|
@@ -302,6 +707,23 @@ var index = (ctx) => {
|
|
|
302
707
|
esbuildConfig.plugins || (esbuildConfig.plugins = []);
|
|
303
708
|
esbuildConfig.plugins.push(taroReactPlugin);
|
|
304
709
|
}
|
|
710
|
+
else if (compiler.type === 'vite') {
|
|
711
|
+
compiler.vitePlugins || (compiler.vitePlugins = []);
|
|
712
|
+
compiler.vitePlugins.push(viteCommonPlugin(framework));
|
|
713
|
+
compiler.vitePlugins.push(VitePresetPlugin(framework));
|
|
714
|
+
if (process.env.TARO_PLATFORM === 'web') {
|
|
715
|
+
// H5
|
|
716
|
+
compiler.vitePlugins.push(h5iVitePlugin(ctx, framework));
|
|
717
|
+
}
|
|
718
|
+
else if (process.env.TARO_PLATFORM === 'harmony' || process.env.TARO_ENV === 'harmony') {
|
|
719
|
+
// 鸿蒙
|
|
720
|
+
compiler.vitePlugins.push(harmonyVitePlugin(ctx, framework));
|
|
721
|
+
}
|
|
722
|
+
else {
|
|
723
|
+
// 小程序
|
|
724
|
+
compiler.vitePlugins.push(miniVitePlugin(ctx, framework));
|
|
725
|
+
}
|
|
726
|
+
}
|
|
305
727
|
});
|
|
306
728
|
};
|
|
307
729
|
function setAlias(framework, chain) {
|
|
@@ -313,12 +735,55 @@ function setAlias(framework, chain) {
|
|
|
313
735
|
alias.set('react-dom', 'preact/compat');
|
|
314
736
|
alias.set('react/jsx-runtime', 'preact/jsx-runtime');
|
|
315
737
|
break;
|
|
316
|
-
case '
|
|
317
|
-
alias.set('react
|
|
318
|
-
alias.set('react-dom$', 'nervjs');
|
|
738
|
+
case 'solid':
|
|
739
|
+
alias.set('react/jsx-runtime', 'solid-js/h/jsx-runtime');
|
|
319
740
|
break;
|
|
320
741
|
}
|
|
321
742
|
}
|
|
743
|
+
function VitePresetPlugin(framework) {
|
|
744
|
+
return framework === 'preact'
|
|
745
|
+
? require('@preact/preset-vite').preact({
|
|
746
|
+
babel: {
|
|
747
|
+
plugins: [
|
|
748
|
+
['@babel/plugin-proposal-decorators', { legacy: true }],
|
|
749
|
+
['@babel/plugin-proposal-class-properties', { loose: true }],
|
|
750
|
+
],
|
|
751
|
+
},
|
|
752
|
+
})
|
|
753
|
+
: require('@vitejs/plugin-react').default({
|
|
754
|
+
babel: {
|
|
755
|
+
plugins: [
|
|
756
|
+
['@babel/plugin-proposal-decorators', { legacy: true }],
|
|
757
|
+
['@babel/plugin-proposal-class-properties', { loose: true }],
|
|
758
|
+
],
|
|
759
|
+
},
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
function viteCommonPlugin(framework) {
|
|
763
|
+
return {
|
|
764
|
+
name: 'taro-react:common',
|
|
765
|
+
config() {
|
|
766
|
+
const alias = framework === 'preact'
|
|
767
|
+
? [
|
|
768
|
+
{ find: 'react', replacement: 'preact/compat' },
|
|
769
|
+
{ find: 'react-dom/test-utils', replacement: 'preact/test-utils' },
|
|
770
|
+
{ find: 'react-dom', replacement: 'preact/compat' },
|
|
771
|
+
{ find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' },
|
|
772
|
+
]
|
|
773
|
+
: [];
|
|
774
|
+
if (framework === 'solid') {
|
|
775
|
+
const reconcilerName = '@tarojs/plugin-framework-react/dist/reconciler';
|
|
776
|
+
alias.push({ find: 'react/jsx-runtime', replacement: reconcilerName });
|
|
777
|
+
}
|
|
778
|
+
return {
|
|
779
|
+
resolve: {
|
|
780
|
+
alias,
|
|
781
|
+
},
|
|
782
|
+
};
|
|
783
|
+
},
|
|
784
|
+
};
|
|
785
|
+
}
|
|
322
786
|
|
|
323
787
|
exports.default = index;
|
|
788
|
+
exports.isReactLike = isReactLike;
|
|
324
789
|
//# sourceMappingURL=index.js.map
|