@tarojs/plugin-framework-solid 4.0.0-beta.83 → 4.0.0-beta.85

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/dist/index.js CHANGED
@@ -4,32 +4,64 @@ 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');
8
7
  var lodash = require('lodash');
9
8
  var acorn = require('acorn');
10
9
  var walk = require('acorn-walk');
11
- var path = require('node:path');
12
10
 
13
11
  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]; }
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
22
  });
23
- }
24
- });
25
- }
26
- n.default = e;
27
- return Object.freeze(n);
23
+ }
24
+ n.default = e;
25
+ return Object.freeze(n);
28
26
  }
29
27
 
30
28
  var acorn__namespace = /*#__PURE__*/_interopNamespaceDefault(acorn);
31
29
  var walk__namespace = /*#__PURE__*/_interopNamespaceDefault(walk);
32
30
 
31
+ const RECONCILER_NAME = '@tarojs/plugin-framework-solid/dist/reconciler';
32
+
33
+ /******************************************************************************
34
+ Copyright (c) Microsoft Corporation.
35
+
36
+ Permission to use, copy, modify, and/or distribute this software for any
37
+ purpose with or without fee is hereby granted.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
40
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
41
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
42
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
43
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
44
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
45
+ PERFORMANCE OF THIS SOFTWARE.
46
+ ***************************************************************************** */
47
+ /* global Reflect, Promise, SuppressedError, Symbol */
48
+
49
+
50
+ function __awaiter(thisArg, _arguments, P, generator) {
51
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
52
+ return new (P || (P = Promise))(function (resolve, reject) {
53
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
54
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
55
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
56
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
57
+ });
58
+ }
59
+
60
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
61
+ var e = new Error(message);
62
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
63
+ };
64
+
33
65
  function apiLoader (str) {
34
66
  return `import {
35
67
  useAddToFavorites,
@@ -55,7 +87,7 @@ function apiLoader (str) {
55
87
  useScope,
56
88
  useUnhandledRejection,
57
89
  useUnload
58
- } from '@tarojs/plugin-framework-react/dist/runtime'
90
+ } from '@tarojs/plugin-framework-solid/dist/runtime'
59
91
  ${str}
60
92
 
61
93
  taro.useAddToFavorites = useAddToFavorites
@@ -161,65 +193,34 @@ function addConfig(source) {
161
193
  });
162
194
  return additionConfig;
163
195
  }
164
- function getLoaderMeta(framework) {
196
+ function getLoaderMeta() {
165
197
  const loaderMeta = {
166
- importFrameworkStatement: `
167
- import * as React from 'react'
168
- import ReactDOM from 'react-dom'
169
- `,
198
+ importFrameworkStatement: '',
170
199
  mockAppStatement: `
171
- class App extends React.Component {
172
- render () {
173
- return this.props.children
174
- }
200
+ function App(props) {
201
+ return null
175
202
  }
176
203
  `,
177
- frameworkArgs: 'React, ReactDOM, config',
178
- creator: 'createReactApp',
179
- creatorLocation: '@tarojs/plugin-framework-react/dist/runtime',
180
- importFrameworkName: 'React',
204
+ frameworkArgs: 'config',
205
+ creator: 'createSolidApp',
206
+ creatorLocation: '@tarojs/plugin-framework-solid/dist/runtime',
207
+ importFrameworkName: '',
181
208
  extraImportForWeb: '',
182
209
  execBeforeCreateWebApp: '',
183
210
  modifyConfig(config, source) {
184
211
  Object.assign(config, addConfig(source));
185
- }
212
+ },
186
213
  };
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
214
  return loaderMeta;
213
215
  }
214
216
 
215
- function h5iVitePlugin(ctx, framework) {
217
+ function h5iVitePlugin(ctx) {
216
218
  return [
217
- injectLoaderMeta$2(ctx, framework),
219
+ injectLoaderMeta$2(ctx),
218
220
  setTaroApi(),
219
- esbuildExclude(framework)
220
221
  ];
221
222
  }
222
- function injectLoaderMeta$2(ctx, framework) {
223
+ function injectLoaderMeta$2(ctx) {
223
224
  function customizer(object = '', sources = '') {
224
225
  if ([object, sources].every(e => typeof e === 'string'))
225
226
  return object + sources;
@@ -227,12 +228,12 @@ function injectLoaderMeta$2(ctx, framework) {
227
228
  const { runnerUtils } = ctx;
228
229
  const { getViteH5CompilerContext } = runnerUtils;
229
230
  return {
230
- name: 'taro-react:loader-meta',
231
+ name: 'taro-solid:loader-meta',
231
232
  buildStart() {
232
- return tslib.__awaiter(this, void 0, void 0, function* () {
233
+ return __awaiter(this, void 0, void 0, function* () {
233
234
  const viteCompilerContext = yield getViteH5CompilerContext(this);
234
235
  if (viteCompilerContext) {
235
- viteCompilerContext.loaderMeta = lodash.mergeWith(getLoaderMeta(framework), viteCompilerContext.loaderMeta, customizer);
236
+ viteCompilerContext.loaderMeta = lodash.mergeWith(getLoaderMeta(), viteCompilerContext.loaderMeta, customizer);
236
237
  }
237
238
  });
238
239
  }
@@ -241,16 +242,16 @@ function injectLoaderMeta$2(ctx, framework) {
241
242
  function setTaroApi() {
242
243
  // dev 环境通过 esbuild 来做; pro 环境通过 rollup load 钩子来做;因为生产环境不会走 esbuild
243
244
  return {
244
- name: 'taro-react:process-import-taro',
245
+ name: 'taro-solid:process-import-taro',
245
246
  enforce: 'pre',
246
247
  config: () => ({
247
248
  optimizeDeps: {
248
249
  esbuildOptions: {
249
250
  plugins: [
250
251
  {
251
- name: 'taro:react-api',
252
+ name: 'taro:solid-api',
252
253
  setup(build) {
253
- build.onLoad({ filter: helper.REG_TARO_H5_RUNTIME_API }, (args) => tslib.__awaiter(this, void 0, void 0, function* () {
254
+ build.onLoad({ filter: helper.REG_TARO_H5_RUNTIME_API }, (args) => __awaiter(this, void 0, void 0, function* () {
254
255
  const input = yield helper.fs.readFile(args.path, 'utf8');
255
256
  return {
256
257
  contents: apiLoader(input + '\n' + 'const taro = Taro__default\n')
@@ -263,7 +264,7 @@ function setTaroApi() {
263
264
  },
264
265
  }),
265
266
  load(id) {
266
- return tslib.__awaiter(this, void 0, void 0, function* () {
267
+ return __awaiter(this, void 0, void 0, function* () {
267
268
  if (process.env.NODE_ENV === 'production' && helper.REG_TARO_H5_RUNTIME_API.test(id)) {
268
269
  try {
269
270
  const input = yield helper.fs.readFile(id, 'utf8');
@@ -277,212 +278,85 @@ function setTaroApi() {
277
278
  }
278
279
  };
279
280
  }
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
281
 
295
- function harmonyVitePlugin(ctx, framework) {
282
+ function harmonyVitePlugin(ctx) {
296
283
  return [
297
- injectLoaderMeta$1(ctx, framework),
298
- aliasPlugin$1(ctx, framework),
284
+ injectLoaderMeta$1(ctx),
285
+ aliasPlugin$1(),
299
286
  ];
300
287
  }
301
- function injectLoaderMeta$1(ctx, framework) {
288
+ function injectLoaderMeta$1(ctx) {
302
289
  return {
303
- name: 'taro-react:loader-meta',
290
+ name: 'taro-solid:loader-meta',
304
291
  buildStart() {
305
292
  const { runnerUtils } = ctx;
306
293
  const { getViteHarmonyCompilerContext } = runnerUtils;
307
294
  const viteCompilerContext = getViteHarmonyCompilerContext(this);
308
295
  if (viteCompilerContext) {
309
- viteCompilerContext.loaderMeta = getLoaderMeta(framework);
296
+ viteCompilerContext.loaderMeta = getLoaderMeta();
310
297
  }
311
298
  }
312
299
  };
313
300
  }
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
- }
301
+ function aliasPlugin$1() {
302
+ return {
303
+ name: 'taro-solid:alias',
304
+ config() {
305
+ const alias = [
306
+ { find: 'solid-js/web$', replacement: RECONCILER_NAME },
307
+ { find: 'react/jsx-runtime', replacement: RECONCILER_NAME },
308
+ ];
309
+ return {
310
+ resolve: {
311
+ alias
350
312
  }
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 [];
313
+ };
314
+ }
315
+ };
377
316
  }
378
317
 
379
- function miniVitePlugin(ctx, framework) {
318
+ function miniVitePlugin(ctx) {
380
319
  return [
381
- injectLoaderMeta(ctx, framework),
382
- aliasPlugin(ctx, framework),
320
+ injectLoaderMeta(ctx),
321
+ aliasPlugin(),
383
322
  ];
384
323
  }
385
- function injectLoaderMeta(ctx, framework) {
324
+ function injectLoaderMeta(ctx) {
386
325
  return {
387
- name: 'taro-react:loader-meta',
326
+ name: 'taro-solid:loader-meta',
388
327
  buildStart() {
389
328
  const { runnerUtils } = ctx;
390
329
  const { getViteMiniCompilerContext } = runnerUtils;
391
330
  const viteCompilerContext = getViteMiniCompilerContext(this);
392
331
  if (viteCompilerContext) {
393
- viteCompilerContext.loaderMeta = getLoaderMeta(framework);
332
+ viteCompilerContext.loaderMeta = getLoaderMeta();
394
333
  }
395
334
  }
396
335
  };
397
336
  }
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
- }
337
+ function aliasPlugin() {
338
+ return {
339
+ name: 'taro-solid:alias',
340
+ config() {
341
+ const alias = [
342
+ { find: 'solid-js/web$', replacement: RECONCILER_NAME },
343
+ ];
344
+ return {
345
+ resolve: {
346
+ alias
434
347
  }
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 [];
348
+ };
349
+ }
350
+ };
461
351
  }
462
352
 
463
- function modifyH5WebpackChain(ctx, framework, chain) {
353
+ function modifyH5WebpackChain(ctx, chain) {
464
354
  var _a;
465
- setLoader$2(framework, chain);
466
- setPlugin(ctx, framework, chain);
355
+ setLoader$2(chain);
356
+ setPlugin(chain);
467
357
  const { isBuildNativeComp = false } = ((_a = ctx.runOpts) === null || _a === void 0 ? void 0 : _a.options) || {};
468
358
  const externals = {};
469
359
  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
360
  chain.merge({
487
361
  externalsType: 'umd'
488
362
  });
@@ -509,192 +383,87 @@ function modifyH5WebpackChain(ctx, framework, chain) {
509
383
  }
510
384
  },
511
385
  });
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
386
  }
524
- function setLoader$2(framework, chain) {
387
+ function setLoader$2(chain) {
525
388
  function customizer(object = '', sources = '') {
526
389
  if ([object, sources].every((e) => typeof e === 'string'))
527
390
  return object + sources;
528
391
  }
529
392
  chain.plugin('mainPlugin').tap((args) => {
530
- args[0].loaderMeta = lodash.mergeWith(getLoaderMeta(framework), args[0].loaderMeta, customizer);
393
+ args[0].loaderMeta = lodash.mergeWith(getLoaderMeta(), args[0].loaderMeta, customizer);
531
394
  return args;
532
395
  });
533
396
  }
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
- }
397
+ function setPlugin(chain) {
549
398
  const mainFields = ['unpkg', ...helper.defaultMainFields];
550
399
  const resolveOptions = {
551
400
  basedir: process.cwd(),
552
401
  mainFields,
553
402
  };
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
- }
403
+ const alias = chain.resolve.alias;
404
+ // Note: 本地 link 调试时,避免 solid 重复打包
405
+ alias.set('solid-js$', helper.resolveSync('solid-js', resolveOptions));
566
406
  }
567
407
 
568
- function modifyHarmonyWebpackChain(ctx, framework, chain) {
569
- setAlias$2(ctx, framework, chain);
570
- setLoader$1(framework, chain);
408
+ function modifyHarmonyWebpackChain(chain) {
409
+ setAlias$1(chain);
410
+ setLoader$1(chain);
571
411
  }
572
- function setAlias$2(ctx, framework, chain) {
573
- var _a;
574
- const config = ctx.initialConfig;
412
+ function setAlias$1(chain) {
575
413
  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
- }
414
+ alias.set('solid-js/web$', RECONCILER_NAME);
614
415
  }
615
- function setLoader$1(framework, chain) {
416
+ function setLoader$1(chain) {
616
417
  chain.plugin('mainPlugin').tap((args) => {
617
- args[0].loaderMeta = getLoaderMeta(framework);
418
+ args[0].loaderMeta = getLoaderMeta();
618
419
  return args;
619
420
  });
620
421
  }
621
422
 
622
- function modifyMiniWebpackChain(ctx, framework, chain) {
623
- setAlias$1(ctx, framework, chain);
624
- setLoader(framework, chain);
423
+ function modifyMiniWebpackChain(chain) {
424
+ setAlias(chain);
425
+ setLoader(chain);
625
426
  }
626
- function setAlias$1(ctx, framework, chain) {
627
- var _a;
628
- const config = ctx.initialConfig;
427
+ function setAlias(chain) {
629
428
  const alias = chain.resolve.alias;
630
429
  const mainFields = ['unpkg', ...helper.defaultMainFields];
631
430
  const resolveOptions = {
632
431
  basedir: process.cwd(),
633
432
  mainFields,
634
433
  };
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
- }
434
+ alias.set('solid-js/web$', RECONCILER_NAME);
435
+ // Note: 本地 link 调试时,避免 solid 重复打包
436
+ alias.set('solid-js$', helper.resolveSync('solid-js', resolveOptions));
669
437
  }
670
- function setLoader(framework, chain) {
671
- chain.plugin('miniPlugin').tap((args) => {
672
- args[0].loaderMeta = getLoaderMeta(framework);
438
+ function setLoader(chain) {
439
+ chain.plugin('miniPlugin')
440
+ .tap(args => {
441
+ args[0].loaderMeta = getLoaderMeta();
673
442
  return args;
674
443
  });
675
444
  }
676
445
 
677
- function isReactLike(framework = 'react') {
678
- return ['react', 'preact', 'solid'].includes(framework);
679
- }
680
446
  var index = (ctx) => {
681
- const { framework = 'react' } = ctx.initialConfig;
682
- if (!isReactLike(framework))
447
+ const { framework } = ctx.initialConfig;
448
+ if (framework !== 'solid')
683
449
  return;
684
450
  ctx.modifyWebpackChain(({ chain }) => {
685
- // 通用
686
- setAlias(framework, chain);
451
+ chain.plugin('definePlugin').tap((args) => {
452
+ const config = args[0];
453
+ config.__TARO_FRAMEWORK__ = `"${framework}"`;
454
+ return args;
455
+ });
687
456
  if (process.env.TARO_PLATFORM === 'web') {
688
457
  // H5
689
- modifyH5WebpackChain(ctx, framework, chain);
458
+ modifyH5WebpackChain(ctx, chain);
690
459
  }
691
460
  else if (process.env.TARO_PLATFORM === 'harmony' || process.env.TARO_ENV === 'harmony') {
692
461
  // 鸿蒙
693
- modifyHarmonyWebpackChain(ctx, framework, chain);
462
+ modifyHarmonyWebpackChain(chain);
694
463
  }
695
464
  else {
696
465
  // 小程序
697
- modifyMiniWebpackChain(ctx, framework, chain);
466
+ modifyMiniWebpackChain(chain);
698
467
  }
699
468
  });
700
469
  ctx.modifyRunnerOpts(({ opts }) => {
@@ -708,17 +477,13 @@ var index = (ctx) => {
708
477
  const { compiler } = opts;
709
478
  if (compiler.type === 'webpack5') {
710
479
  // 提供给 webpack5 依赖预编译收集器的第三方依赖
711
- const deps = ['react', 'react-dom', 'react/jsx-runtime', '@tarojs/plugin-framework-react/dist/runtime'];
480
+ const deps = ['@tarojs/plugin-framework-solid/dist/runtime'];
712
481
  compiler.prebundle || (compiler.prebundle = {});
713
482
  const prebundleOptions = compiler.prebundle;
714
483
  prebundleOptions.include || (prebundleOptions.include = []);
715
484
  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',
485
+ const taroSolidPlugin = {
486
+ name: 'taroSolidPlugin',
722
487
  setup(build) {
723
488
  build.onLoad({ filter: helper.REG_TARO_H5 }, ({ path }) => {
724
489
  const content = helper.fs.readFileSync(path).toString();
@@ -737,85 +502,49 @@ var index = (ctx) => {
737
502
  prebundleOptions.esbuild || (prebundleOptions.esbuild = {});
738
503
  const esbuildConfig = prebundleOptions.esbuild;
739
504
  esbuildConfig.plugins || (esbuildConfig.plugins = []);
740
- esbuildConfig.plugins.push(taroReactPlugin);
505
+ esbuildConfig.plugins.push(taroSolidPlugin);
741
506
  }
742
507
  else if (compiler.type === 'vite') {
508
+ const solidPlugin = require('vite-plugin-solid');
743
509
  compiler.vitePlugins || (compiler.vitePlugins = []);
744
- compiler.vitePlugins.push(viteCommonPlugin(framework));
745
- compiler.vitePlugins.push(VitePresetPlugin(framework));
510
+ const solidOptions = {};
746
511
  if (process.env.TARO_PLATFORM === 'web') {
747
512
  // H5
748
- compiler.vitePlugins.push(h5iVitePlugin(ctx, framework));
513
+ compiler.vitePlugins.push(...h5iVitePlugin(ctx));
749
514
  }
750
515
  else if (process.env.TARO_PLATFORM === 'harmony' || process.env.TARO_ENV === 'harmony') {
751
516
  // 鸿蒙
752
- compiler.vitePlugins.push(harmonyVitePlugin(ctx, framework));
517
+ compiler.vitePlugins.push(harmonyVitePlugin(ctx));
753
518
  }
754
519
  else {
520
+ Object.assign(solidOptions, {
521
+ moduleName: RECONCILER_NAME,
522
+ generate: 'universal',
523
+ uniqueTransform: true,
524
+ });
755
525
  // 小程序
756
- compiler.vitePlugins.push(miniVitePlugin(ctx, framework));
526
+ compiler.vitePlugins.push(miniVitePlugin(ctx));
757
527
  }
528
+ // @TODO vite的插件需要内部删除babel-preset-solid
529
+ compiler.vitePlugins.unshift(solidPlugin({
530
+ babel: {
531
+ presets: [
532
+ [
533
+ require('babel-plugin-transform-solid-jsx'),
534
+ solidOptions
535
+ ],
536
+ ],
537
+ },
538
+ }));
539
+ }
540
+ });
541
+ // 映射、收集使用到的小程序组件
542
+ ctx.onParseCreateElement(({ nodeName, componentConfig }) => {
543
+ if (shared.capitalize(shared.toCamelCase(nodeName)) in shared.internalComponents) {
544
+ componentConfig.includes.add(nodeName);
758
545
  }
759
546
  });
760
547
  };
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
548
 
819
549
  exports.default = index;
820
- exports.isReactLike = isReactLike;
821
550
  //# sourceMappingURL=index.js.map