@tarojs/plugin-framework-react 3.7.0-canary.6 → 3.8.0-canary.1

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 CHANGED
@@ -158,3 +158,17 @@ MIT (stencil-vue2-output-target):
158
158
  The following files embed [stencil-vue2-output-target](https://github.com/diondree/stencil-vue2-output-target) MIT:
159
159
  `/packages/taro-components-library-vue2/src/vue-component-lib/utils.ts`
160
160
  See `/LICENSE` for details of the license.
161
+
162
+ ==================
163
+
164
+ MIT (weui):
165
+ The following files embed [stencil-vue2-output-target](https://github.com/Tencent/weui) MIT:
166
+ `/packages/taro-components/src/components/*.scss`
167
+ See `/LICENSE.txt` for details of the license.
168
+
169
+ ==================
170
+
171
+ Apache-2.0 (intersection-observer):
172
+ The following files embed [intersection-observer](https://github.com/GoogleChromeLabs/intersection-observer) Apache-2.0:
173
+ `/packages/taro-api/src/polyfill/intersection-observer.ts`
174
+ See `/LICENSE.txt` for details of the license.
package/dist/index.js CHANGED
@@ -10,132 +10,25 @@ var walk = require('acorn-walk');
10
10
  var path = require('node:path');
11
11
 
12
12
  function _interopNamespaceDefault(e) {
13
- var n = Object.create(null);
14
- if (e) {
15
- Object.keys(e).forEach(function (k) {
16
- if (k !== 'default') {
17
- var d = Object.getOwnPropertyDescriptor(e, k);
18
- Object.defineProperty(n, k, d.get ? d : {
19
- enumerable: true,
20
- get: function () { return e[k]; }
21
- });
22
- }
13
+ var n = Object.create(null);
14
+ if (e) {
15
+ Object.keys(e).forEach(function (k) {
16
+ if (k !== 'default') {
17
+ var d = Object.getOwnPropertyDescriptor(e, k);
18
+ Object.defineProperty(n, k, d.get ? d : {
19
+ enumerable: true,
20
+ get: function () { return e[k]; }
23
21
  });
24
- }
25
- n.default = e;
26
- return Object.freeze(n);
22
+ }
23
+ });
24
+ }
25
+ n.default = e;
26
+ return Object.freeze(n);
27
27
  }
28
28
 
29
29
  var acorn__namespace = /*#__PURE__*/_interopNamespaceDefault(acorn);
30
30
  var walk__namespace = /*#__PURE__*/_interopNamespaceDefault(walk);
31
31
 
32
- /******************************************************************************
33
- Copyright (c) Microsoft Corporation.
34
-
35
- Permission to use, copy, modify, and/or distribute this software for any
36
- purpose with or without fee is hereby granted.
37
-
38
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
39
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
40
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
41
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
42
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
43
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
44
- PERFORMANCE OF THIS SOFTWARE.
45
- ***************************************************************************** */
46
- /* global Reflect, Promise */
47
-
48
-
49
- function __awaiter(thisArg, _arguments, P, generator) {
50
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
51
- return new (P || (P = Promise))(function (resolve, reject) {
52
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
53
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
54
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
55
- step((generator = generator.apply(thisArg, _arguments || [])).next());
56
- });
57
- }
58
-
59
- function apiLoader (str) {
60
- return `import {
61
- useAddToFavorites,
62
- useDidHide,
63
- useDidShow,
64
- useError,
65
- useLaunch,
66
- useLoad,
67
- useOptionMenuClick,
68
- usePageNotFound,
69
- usePageScroll,
70
- usePullDownRefresh,
71
- usePullIntercept,
72
- useReachBottom,
73
- useReady,
74
- useResize,
75
- useRouter,
76
- useSaveExitState,
77
- useShareAppMessage,
78
- useShareTimeline,
79
- useTabItemTap,
80
- useTitleClick,
81
- useScope,
82
- useUnhandledRejection,
83
- useUnload
84
- } from '@tarojs/plugin-framework-react/dist/runtime'
85
- ${str}
86
-
87
- taro.useAddToFavorites = useAddToFavorites
88
- taro.useDidHide = useDidHide
89
- taro.useDidShow = useDidShow
90
- taro.useError = useError
91
- taro.useLaunch = useLaunch
92
- taro.useLoad = useLoad
93
- taro.useOptionMenuClick = useOptionMenuClick
94
- taro.usePageNotFound = usePageNotFound
95
- taro.usePageScroll = usePageScroll
96
- taro.usePullDownRefresh = usePullDownRefresh
97
- taro.usePullIntercept = usePullIntercept
98
- taro.useReachBottom = useReachBottom
99
- taro.useReady = useReady
100
- taro.useResize = useResize
101
- taro.useRouter = useRouter
102
- taro.useSaveExitState = useSaveExitState
103
- taro.useShareAppMessage = useShareAppMessage
104
- taro.useShareTimeline = useShareTimeline
105
- taro.useTabItemTap = useTabItemTap
106
- taro.useTitleClick = useTitleClick
107
- taro.useScope = useScope
108
- taro.useUnhandledRejection = useUnhandledRejection
109
- taro.useUnload = useUnload
110
-
111
- export {
112
- useAddToFavorites,
113
- useDidHide,
114
- useDidShow,
115
- useError,
116
- useLaunch,
117
- useLoad,
118
- useOptionMenuClick,
119
- usePageNotFound,
120
- usePageScroll,
121
- usePullDownRefresh,
122
- usePullIntercept,
123
- useReachBottom,
124
- useReady,
125
- useResize,
126
- useRouter,
127
- useSaveExitState,
128
- useShareAppMessage,
129
- useShareTimeline,
130
- useTabItemTap,
131
- useTitleClick,
132
- useScope,
133
- useUnhandledRejection,
134
- useUnload
135
- }
136
- `;
137
- }
138
-
139
32
  function addConfig(source) {
140
33
  const configsMap = {
141
34
  enableShareAppMessage: ['onShareAppMessage', 'useShareAppMessage'],
@@ -227,7 +120,7 @@ class App extends React.Component {
227
120
  if (framework === 'nerv') {
228
121
  Object.assign(loaderMeta, nervMeta);
229
122
  }
230
- if (shared.isWebPlatform()) {
123
+ if (process.env.TARO_PLATFORM === 'web') {
231
124
  if (framework === 'react') {
232
125
  const react = require('react');
233
126
  const majorVersion = Number((react.version || '18').split('.')[0]);
@@ -242,147 +135,6 @@ class App extends React.Component {
242
135
  return loaderMeta;
243
136
  }
244
137
 
245
- function h5iVitePlugin(ctx, framework) {
246
- return [
247
- injectLoaderMeta$1(ctx, framework),
248
- setTaroApi(),
249
- esbuildExclude(framework)
250
- ];
251
- }
252
- function injectLoaderMeta$1(ctx, framework) {
253
- function customizer(object = '', sources = '') {
254
- if ([object, sources].every(e => typeof e === 'string'))
255
- return object + sources;
256
- }
257
- const { runnerUtils } = ctx;
258
- const { getViteH5CompilerContext } = runnerUtils;
259
- return {
260
- name: 'taro-react:loader-meta',
261
- buildStart() {
262
- return __awaiter(this, void 0, void 0, function* () {
263
- const viteCompilerContext = yield getViteH5CompilerContext(this);
264
- if (viteCompilerContext) {
265
- viteCompilerContext.loaderMeta = lodash.mergeWith(getLoaderMeta(framework), viteCompilerContext.loaderMeta, customizer);
266
- }
267
- });
268
- }
269
- };
270
- }
271
- function setTaroApi() {
272
- // dev 环境通过 esbuild 来做; pro 环境通过 rollup load 钩子来做;因为生产环境不会走 esbuild
273
- return {
274
- name: 'taro-react:process-import-taro',
275
- enforce: 'pre',
276
- config: () => ({
277
- optimizeDeps: {
278
- force: true,
279
- esbuildOptions: {
280
- plugins: [
281
- {
282
- name: 'taro:react-api',
283
- setup(build) {
284
- build.onLoad({ filter: helper.REG_TARO_H5_RUNTIME_API }, (args) => __awaiter(this, void 0, void 0, function* () {
285
- const input = yield helper.fs.readFile(args.path, 'utf8');
286
- return {
287
- contents: apiLoader(input + '\n' + 'const taro = Taro__default\n')
288
- };
289
- }));
290
- },
291
- }
292
- ]
293
- }
294
- },
295
- }),
296
- load(id) {
297
- return __awaiter(this, void 0, void 0, function* () {
298
- if (process.env.NODE_ENV === 'production' && helper.REG_TARO_H5_RUNTIME_API.test(id)) {
299
- try {
300
- const input = yield helper.fs.readFile(id, 'utf8');
301
- return apiLoader(input + '\n' + 'const taro = Taro__default\n');
302
- }
303
- catch (_) {
304
- return null;
305
- }
306
- }
307
- });
308
- }
309
- };
310
- }
311
- // todo 后面看看能否把 preact 改为虚拟模块
312
- function esbuildExclude(framework) {
313
- if (framework !== 'preact')
314
- return null;
315
- return {
316
- name: 'taro-react:esvuild-exclude',
317
- enforce: 'pre',
318
- config: () => ({
319
- optimizeDeps: {
320
- exclude: ['react', 'preact']
321
- }
322
- })
323
- };
324
- }
325
-
326
- function miniVitePlugin(ctx, framework) {
327
- return [
328
- injectLoaderMeta(ctx, framework),
329
- aliasPlugin(ctx, framework),
330
- ];
331
- }
332
- function injectLoaderMeta(ctx, framework) {
333
- return {
334
- name: 'taro-react:loader-meta',
335
- buildStart() {
336
- const { runnerUtils } = ctx;
337
- const { getViteMiniCompilerContext } = runnerUtils;
338
- const viteCompilerContext = getViteMiniCompilerContext(this);
339
- if (viteCompilerContext) {
340
- viteCompilerContext.loaderMeta = getLoaderMeta(framework);
341
- }
342
- }
343
- };
344
- }
345
- function aliasPlugin(ctx, framework) {
346
- if (framework === 'react') {
347
- return {
348
- name: 'taro-react:alias',
349
- config(config) {
350
- var _a;
351
- const alias = [
352
- { find: /react-dom$/, replacement: '@tarojs/react' }
353
- ];
354
- const isProd = config.mode === 'production';
355
- if (!isProd && ((_a = ctx.initialConfig.mini) === null || _a === void 0 ? void 0 : _a.debugReact) !== true) {
356
- // 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
357
- alias.push({ find: /react-reconciler$/, replacement: 'react-reconciler/cjs/react-reconciler.production.min.js' });
358
- alias.push({ find: /^(?!.*mobx-react$).*react$/, replacement: 'react/cjs/react.production.min.js' });
359
- alias.push({ find: /scheduler$/, replacement: 'scheduler/cjs/scheduler.production.min.js' });
360
- alias.push({ find: /react\/jsx-runtime$/, replacement: 'react/cjs/react-jsx-runtime.production.min.js' });
361
- // 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
362
- // Why writeJson? prebundle will load package.json via readFile to check exports property.
363
- const reactPkgPath = require.resolve('react/package.json', { paths: [process.cwd()] });
364
- if (reactPkgPath) {
365
- const reactPkg = require('react/package.json');
366
- const reactVersion = (reactPkg.version || '');
367
- if ((/^[~^]?18/).test(reactVersion) && reactPkg.exports) {
368
- reactPkg.exports = Object.assign(reactPkg.exports, {
369
- './cjs/': './cjs/'
370
- });
371
- helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
372
- }
373
- }
374
- }
375
- return {
376
- resolve: {
377
- alias
378
- }
379
- };
380
- }
381
- };
382
- }
383
- return [];
384
- }
385
-
386
138
  function modifyH5WebpackChain(ctx, framework, chain) {
387
139
  var _a;
388
140
  setLoader$1(framework, chain);
@@ -391,30 +143,44 @@ function modifyH5WebpackChain(ctx, framework, chain) {
391
143
  const externals = {};
392
144
  if (isBuildNativeComp) {
393
145
  // Note: 该模式不支持 prebundle 优化,不必再处理
394
- externals.react = 'React';
395
- externals['react-dom'] = 'ReactDOM';
146
+ externals.react = {
147
+ commonjs: 'react',
148
+ commonjs2: 'react',
149
+ amd: 'react',
150
+ root: 'React'
151
+ };
152
+ externals['react-dom'] = {
153
+ commonjs: 'react-dom',
154
+ commonjs2: 'react-dom',
155
+ amd: 'react-dom',
156
+ root: 'ReactDOM'
157
+ };
396
158
  if (framework === 'preact') {
397
159
  externals.preact = 'preact';
398
160
  }
161
+ chain.merge({
162
+ externalsType: 'umd'
163
+ });
399
164
  }
400
165
  chain.merge({
401
166
  externals,
402
167
  module: {
403
168
  rule: {
404
- 'process-import-taro': {
405
- test: helper.REG_TARO_H5,
406
- loader: require.resolve('./api-loader'),
407
- },
408
- },
169
+ 'process-import-taro-h5': {
170
+ test: /taro-h5[\\/]dist[\\/]api[\\/]taro/,
171
+ loader: require.resolve('./api-loader')
172
+ }
173
+ }
409
174
  },
410
175
  });
411
176
  }
412
177
  function setLoader$1(framework, chain) {
413
178
  function customizer(object = '', sources = '') {
414
- if ([object, sources].every((e) => typeof e === 'string'))
179
+ if ([object, sources].every(e => typeof e === 'string'))
415
180
  return object + sources;
416
181
  }
417
- chain.plugin('mainPlugin').tap((args) => {
182
+ chain.plugin('mainPlugin')
183
+ .tap(args => {
418
184
  args[0].loaderMeta = lodash.mergeWith(getLoaderMeta(framework), args[0].loaderMeta, customizer);
419
185
  return args;
420
186
  });
@@ -427,11 +193,17 @@ function setPlugin(ctx, framework, chain) {
427
193
  if (!isProd && ((_b = (_a = config.h5) === null || _a === void 0 ? void 0 : _a.devServer) === null || _b === void 0 ? void 0 : _b.hot) !== false) {
428
194
  // 默认开启 fast-refresh
429
195
  if (framework === 'react') {
430
- chain.plugin('fastRefreshPlugin').use(require('@pmmmwh/react-refresh-webpack-plugin'));
196
+ chain
197
+ .plugin('fastRefreshPlugin')
198
+ .use(require('@pmmmwh/react-refresh-webpack-plugin'));
431
199
  }
432
200
  else if (framework === 'preact') {
433
- chain.plugin('hotModuleReplacementPlugin').use(require('webpack').HotModuleReplacementPlugin);
434
- chain.plugin('fastRefreshPlugin').use(require('@prefresh/webpack'));
201
+ chain
202
+ .plugin('hotModuleReplacementPlugin')
203
+ .use(require('webpack').HotModuleReplacementPlugin);
204
+ chain
205
+ .plugin('fastRefreshPlugin')
206
+ .use(require('@prefresh/webpack'));
435
207
  }
436
208
  }
437
209
  }
@@ -455,7 +227,7 @@ function setAlias$1(ctx, framework, chain) {
455
227
  const reactModulePath = require.resolve('react', { paths: [process.cwd()] });
456
228
  const newFilePath = path.join(path.dirname(reactModulePath), 'cjs', 'react.production.min.js');
457
229
  alias.set('react-reconciler$', 'react-reconciler/cjs/react-reconciler.production.min.js');
458
- alias.set(/^(?!.*mobx-react$).*react$/, newFilePath);
230
+ alias.set('react$', newFilePath);
459
231
  alias.set('react/jsx-runtime$', 'react/cjs/react-jsx-runtime.production.min.js');
460
232
  // 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
461
233
  // Why writeJson? prebundle will load package.json via readFile to check exports property.
@@ -487,12 +259,14 @@ var index = (ctx) => {
487
259
  ctx.modifyWebpackChain(({ chain }) => {
488
260
  // 通用
489
261
  setAlias(framework, chain);
490
- chain.plugin('definePlugin').tap((args) => {
262
+ chain
263
+ .plugin('definePlugin')
264
+ .tap(args => {
491
265
  const config = args[0];
492
266
  config.__TARO_FRAMEWORK__ = `"${framework}"`;
493
267
  return args;
494
268
  });
495
- if (shared.isWebPlatform()) {
269
+ if (process.env.TARO_PLATFORM === 'web') {
496
270
  // H5
497
271
  modifyH5WebpackChain(ctx, framework, chain);
498
272
  }
@@ -506,13 +280,18 @@ var index = (ctx) => {
506
280
  return;
507
281
  if (shared.isString(opts.compiler)) {
508
282
  opts.compiler = {
509
- type: opts.compiler,
283
+ type: opts.compiler
510
284
  };
511
285
  }
512
286
  const { compiler } = opts;
513
287
  if (compiler.type === 'webpack5') {
514
288
  // 提供给 webpack5 依赖预编译收集器的第三方依赖
515
- const deps = ['react', 'react-dom', 'react/jsx-runtime', '@tarojs/plugin-framework-react/dist/runtime'];
289
+ const deps = [
290
+ 'react',
291
+ 'react-dom',
292
+ 'react/jsx-runtime',
293
+ '@tarojs/plugin-framework-react/dist/runtime'
294
+ ];
516
295
  compiler.prebundle || (compiler.prebundle = {});
517
296
  const prebundleOptions = compiler.prebundle;
518
297
  prebundleOptions.include || (prebundleOptions.include = []);
@@ -524,32 +303,19 @@ var index = (ctx) => {
524
303
  const taroReactPlugin = {
525
304
  name: 'taroReactPlugin',
526
305
  setup(build) {
527
- build.onLoad({ filter: helper.REG_TARO_H5 }, ({ path }) => {
306
+ build.onLoad({ filter: /taro-h5[\\/]dist[\\/]api[\\/]taro/ }, ({ path }) => {
528
307
  const content = helper.fs.readFileSync(path).toString();
529
308
  return {
530
- contents: require('./api-loader')(content),
309
+ contents: require('./api-loader')(content)
531
310
  };
532
311
  });
533
- },
312
+ }
534
313
  };
535
314
  prebundleOptions.esbuild || (prebundleOptions.esbuild = {});
536
315
  const esbuildConfig = prebundleOptions.esbuild;
537
316
  esbuildConfig.plugins || (esbuildConfig.plugins = []);
538
317
  esbuildConfig.plugins.push(taroReactPlugin);
539
318
  }
540
- else if (compiler.type === 'vite') {
541
- compiler.vitePlugins || (compiler.vitePlugins = []);
542
- compiler.vitePlugins.push(viteCommonPlugin(framework));
543
- compiler.vitePlugins.push(VitePresetPlugin(framework));
544
- if (shared.isWebPlatform()) {
545
- // H5
546
- compiler.vitePlugins.push(h5iVitePlugin(ctx, framework));
547
- }
548
- else {
549
- // 小程序
550
- compiler.vitePlugins.push(miniVitePlugin(ctx, framework));
551
- }
552
- }
553
319
  });
554
320
  };
555
321
  function setAlias(framework, chain) {
@@ -567,55 +333,6 @@ function setAlias(framework, chain) {
567
333
  break;
568
334
  }
569
335
  }
570
- function VitePresetPlugin(framework) {
571
- return framework === 'preact'
572
- ? require('@preact/preset-vite').preact({
573
- babel: {
574
- plugins: [
575
- ['@babel/plugin-proposal-decorators', { legacy: true }],
576
- ['@babel/plugin-proposal-class-properties', { loose: true }],
577
- ],
578
- },
579
- })
580
- : require('@vitejs/plugin-react').default({
581
- babel: {
582
- plugins: [
583
- ['@babel/plugin-proposal-decorators', { legacy: true }],
584
- ['@babel/plugin-proposal-class-properties', { loose: true }],
585
- ],
586
- },
587
- });
588
- }
589
- function viteCommonPlugin(framework) {
590
- return {
591
- name: 'taro-react:common',
592
- config() {
593
- const alias = framework === 'preact'
594
- ? [
595
- { find: 'react', replacement: 'preact/compat' },
596
- { find: 'react-dom/test-utils', replacement: 'preact/test-utils' },
597
- { find: 'react-dom', replacement: 'preact/compat' },
598
- { find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' },
599
- ]
600
- : [];
601
- return {
602
- optimizeDeps: {
603
- esbuildOptions: {
604
- define: {
605
- __TARO_FRAMEWORK__: `"${framework}"`
606
- }
607
- }
608
- },
609
- define: {
610
- __TARO_FRAMEWORK__: `"${framework}"`,
611
- },
612
- resolve: {
613
- alias,
614
- },
615
- };
616
- },
617
- };
618
- }
619
336
 
620
337
  exports.default = index;
621
338
  //# sourceMappingURL=index.js.map