@tarojs/plugin-framework-react 3.5.0-beta.2 → 3.5.0-beta.5

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.
@@ -1,62 +1,80 @@
1
1
  'use strict';
2
2
 
3
- function apiLoader (str) {
3
+ function apiLoader (str) {
4
4
  return `import {
5
- useDidShow,
6
- useDidHide,
7
- usePullDownRefresh,
8
- useReachBottom,
9
- usePageScroll,
10
- useResize,
11
- useShareAppMessage,
12
- useTabItemTap,
13
- useTitleClick,
14
- useOptionMenuClick,
15
- usePullIntercept,
16
- useShareTimeline,
17
- useAddToFavorites,
18
- useReady,
19
- useRouter,
20
- useScope
21
- } from '@tarojs/plugin-framework-react/dist/runtime'
22
- ${str}
5
+ useAddToFavorites,
6
+ useDidHide,
7
+ useDidShow,
8
+ useError,
9
+ useLaunch,
10
+ useLoad,
11
+ useOptionMenuClick,
12
+ usePageNotFound,
13
+ usePageScroll,
14
+ usePullDownRefresh,
15
+ usePullIntercept,
16
+ useReachBottom,
17
+ useReady,
18
+ useResize,
19
+ useRouter,
20
+ useSaveExitState,
21
+ useShareAppMessage,
22
+ useShareTimeline,
23
+ useTabItemTap,
24
+ useTitleClick,
25
+ useScope,
26
+ useUnload
27
+ } from '@tarojs/plugin-framework-react/dist/runtime'
28
+ ${str}
23
29
 
24
- taro.useDidShow = useDidShow
25
- taro.useDidHide = useDidHide
26
- taro.usePullDownRefresh = usePullDownRefresh
27
- taro.useReachBottom = useReachBottom
28
- taro.usePageScroll = usePageScroll
29
- taro.useResize = useResize
30
- taro.useShareAppMessage = useShareAppMessage
31
- taro.useTabItemTap = useTabItemTap
32
- taro.useTitleClick = useTitleClick
33
- taro.useOptionMenuClick = useOptionMenuClick
34
- taro.usePullIntercept = usePullIntercept
35
- taro.useShareTimeline = useShareTimeline
36
- taro.useAddToFavorites = useAddToFavorites
37
- taro.useReady = useReady
38
- taro.useRouter = useRouter
39
- taro.useScope = useScope
30
+ taro.useAddToFavorites = useAddToFavorites
31
+ taro.useDidHide = useDidHide
32
+ taro.useDidShow = useDidShow
33
+ taro.useError = useError
34
+ taro.useLaunch = useLaunch
35
+ taro.useLoad = useLoad
36
+ taro.useOptionMenuClick = useOptionMenuClick
37
+ taro.usePageNotFound = usePageNotFound
38
+ taro.usePageScroll = usePageScroll
39
+ taro.usePullDownRefresh = usePullDownRefresh
40
+ taro.usePullIntercept = usePullIntercept
41
+ taro.useReachBottom = useReachBottom
42
+ taro.useReady = useReady
43
+ taro.useResize = useResize
44
+ taro.useRouter = useRouter
45
+ taro.useSaveExitState = useSaveExitState
46
+ taro.useShareAppMessage = useShareAppMessage
47
+ taro.useShareTimeline = useShareTimeline
48
+ taro.useTabItemTap = useTabItemTap
49
+ taro.useTitleClick = useTitleClick
50
+ taro.useScope = useScope
51
+ taro.useUnload = useUnload
40
52
 
41
- export {
42
- useDidShow,
43
- useDidHide,
44
- usePullDownRefresh,
45
- useReachBottom,
46
- usePageScroll,
47
- useResize,
48
- useShareAppMessage,
49
- useTabItemTap,
50
- useTitleClick,
51
- useOptionMenuClick,
52
- usePullIntercept,
53
- useShareTimeline,
54
- useAddToFavorites,
55
- useReady,
56
- useRouter,
57
- useScope
58
- }
59
- `;
53
+ export {
54
+ useAddToFavorites,
55
+ useDidHide,
56
+ useDidShow,
57
+ useError,
58
+ useLaunch,
59
+ useLoad,
60
+ useOptionMenuClick,
61
+ usePageNotFound,
62
+ usePageScroll,
63
+ usePullDownRefresh,
64
+ usePullIntercept,
65
+ useReachBottom,
66
+ useReady,
67
+ useResize,
68
+ useRouter,
69
+ useSaveExitState,
70
+ useShareAppMessage,
71
+ useShareTimeline,
72
+ useTabItemTap,
73
+ useTitleClick,
74
+ useScope,
75
+ useUnload
76
+ }
77
+ `;
60
78
  }
61
79
 
62
80
  module.exports = apiLoader;
@@ -1 +1 @@
1
- {"version":3,"file":"api-loader.js","sources":["../src/api-loader.ts"],"sourcesContent":["export default function (str) {\n return `import {\n useDidShow,\n useDidHide,\n usePullDownRefresh,\n useReachBottom,\n usePageScroll,\n useResize,\n useShareAppMessage,\n useTabItemTap,\n useTitleClick,\n useOptionMenuClick,\n usePullIntercept,\n useShareTimeline,\n useAddToFavorites,\n useReady,\n useRouter,\n useScope\n } from '@tarojs/plugin-framework-react/dist/runtime'\n ${str}\n\n taro.useDidShow = useDidShow\n taro.useDidHide = useDidHide\n taro.usePullDownRefresh = usePullDownRefresh\n taro.useReachBottom = useReachBottom\n taro.usePageScroll = usePageScroll\n taro.useResize = useResize\n taro.useShareAppMessage = useShareAppMessage\n taro.useTabItemTap = useTabItemTap\n taro.useTitleClick = useTitleClick\n taro.useOptionMenuClick = useOptionMenuClick\n taro.usePullIntercept = usePullIntercept\n taro.useShareTimeline = useShareTimeline\n taro.useAddToFavorites = useAddToFavorites\n taro.useReady = useReady\n taro.useRouter = useRouter\n taro.useScope = useScope\n\n export {\n useDidShow,\n useDidHide,\n usePullDownRefresh,\n useReachBottom,\n usePageScroll,\n useResize,\n useShareAppMessage,\n useTabItemTap,\n useTitleClick,\n useOptionMenuClick,\n usePullIntercept,\n useShareTimeline,\n useAddToFavorites,\n useReady,\n useRouter,\n useScope\n }\n `\n}\n"],"names":[],"mappings":";;AAAc,kBAAA,EAAW,GAAG,EAAA;IAC1B,OAAO,CAAA;;;;;;;;;;;;;;;;;;IAkBL,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCJ,CAAA;AACH;;;;"}
1
+ {"version":3,"file":"api-loader.js","sources":["../src/api-loader.ts"],"sourcesContent":["export default function (str) {\n return `import {\n useAddToFavorites,\n useDidHide,\n useDidShow,\n useError,\n useLaunch,\n useLoad,\n useOptionMenuClick,\n usePageNotFound,\n usePageScroll,\n usePullDownRefresh,\n usePullIntercept,\n useReachBottom,\n useReady,\n useResize,\n useRouter,\n useSaveExitState,\n useShareAppMessage,\n useShareTimeline,\n useTabItemTap,\n useTitleClick,\n useScope,\n useUnload\n} from '@tarojs/plugin-framework-react/dist/runtime'\n${str}\n\ntaro.useAddToFavorites = useAddToFavorites\ntaro.useDidHide = useDidHide\ntaro.useDidShow = useDidShow\ntaro.useError = useError\ntaro.useLaunch = useLaunch\ntaro.useLoad = useLoad\ntaro.useOptionMenuClick = useOptionMenuClick\ntaro.usePageNotFound = usePageNotFound\ntaro.usePageScroll = usePageScroll\ntaro.usePullDownRefresh = usePullDownRefresh\ntaro.usePullIntercept = usePullIntercept\ntaro.useReachBottom = useReachBottom\ntaro.useReady = useReady\ntaro.useResize = useResize\ntaro.useRouter = useRouter\ntaro.useSaveExitState = useSaveExitState\ntaro.useShareAppMessage = useShareAppMessage\ntaro.useShareTimeline = useShareTimeline\ntaro.useTabItemTap = useTabItemTap\ntaro.useTitleClick = useTitleClick\ntaro.useScope = useScope\ntaro.useUnload = useUnload\n\nexport {\n useAddToFavorites,\n useDidHide,\n useDidShow,\n useError,\n useLaunch,\n useLoad,\n useOptionMenuClick,\n usePageNotFound,\n usePageScroll,\n usePullDownRefresh,\n usePullIntercept,\n useReachBottom,\n useReady,\n useResize,\n useRouter,\n useSaveExitState,\n useShareAppMessage,\n useShareTimeline,\n useTabItemTap,\n useTitleClick,\n useScope,\n useUnload\n}\n`\n}\n"],"names":[],"mappings":";;AAAc,kBAAA,EAAW,GAAG,EAAA;IACxB,OAAO,CAAA;;;;;;;;;;;;;;;;;;;;;;;;EAwBT,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiDJ,CAAC;AACF;;;;"}
package/dist/index.js CHANGED
@@ -28,260 +28,273 @@ function _interopNamespace(e) {
28
28
  var acorn__namespace = /*#__PURE__*/_interopNamespace(acorn);
29
29
  var walk__namespace = /*#__PURE__*/_interopNamespace(walk);
30
30
 
31
- function addConfig(source) {
32
- const configsMap = {
33
- enableShareAppMessage: ['onShareAppMessage', 'useShareAppMessage'],
34
- enableShareTimeline: ['onShareTimeline', 'useShareTimeline']
35
- };
36
- const ast = acorn__namespace.parse(source, {
37
- ecmaVersion: 'latest',
38
- sourceType: 'module'
39
- });
40
- const additionConfig = {};
41
- function check(name) {
42
- Object.keys(configsMap).forEach(configName => {
43
- const apis = configsMap[configName];
44
- if (apis.includes(name)) {
45
- additionConfig[configName] = true;
46
- }
47
- });
48
- }
49
- walk__namespace.simple(ast, {
50
- FunctionExpression(node) {
51
- if (!node.id || !node.id.name)
52
- return;
53
- check(node.id.name);
54
- },
55
- FunctionDeclaration(node) {
56
- if (!node.id || !node.id.name)
57
- return;
58
- check(node.id.name);
59
- },
60
- CallExpression(node) {
61
- const { callee } = node;
62
- if (callee.type === 'Identifier') {
63
- check(callee.name);
64
- }
65
- else if (callee.type === 'MemberExpression') {
66
- if (callee.property.type === 'Identifier') {
67
- check(callee.property.name);
68
- }
69
- else if (callee.property.type === 'Literal') {
70
- check(callee.property.value);
71
- }
72
- }
73
- }
74
- });
75
- return additionConfig;
76
- }
77
- const frameworkMeta = {
78
- nerv: {
31
+ function addConfig(source) {
32
+ const configsMap = {
33
+ enableShareAppMessage: ['onShareAppMessage', 'useShareAppMessage'],
34
+ enableShareTimeline: ['onShareTimeline', 'useShareTimeline']
35
+ };
36
+ const ast = acorn__namespace.parse(source, {
37
+ ecmaVersion: 'latest',
38
+ sourceType: 'module'
39
+ });
40
+ const additionConfig = {};
41
+ function check(name) {
42
+ Object.keys(configsMap).forEach(configName => {
43
+ const apis = configsMap[configName];
44
+ if (apis.includes(name)) {
45
+ additionConfig[configName] = true;
46
+ }
47
+ });
48
+ }
49
+ walk__namespace.simple(ast, {
50
+ FunctionExpression(node) {
51
+ if (!node.id || !node.id.name)
52
+ return;
53
+ check(node.id.name);
54
+ },
55
+ FunctionDeclaration(node) {
56
+ if (!node.id || !node.id.name)
57
+ return;
58
+ check(node.id.name);
59
+ },
60
+ CallExpression(node) {
61
+ const { callee } = node;
62
+ if (callee.type === 'Identifier') {
63
+ check(callee.name);
64
+ }
65
+ else if (callee.type === 'MemberExpression') {
66
+ if (callee.property.type === 'Identifier') {
67
+ check(callee.property.name);
68
+ }
69
+ else if (callee.property.type === 'Literal') {
70
+ check(callee.property.value);
71
+ }
72
+ }
73
+ }
74
+ });
75
+ return additionConfig;
76
+ }
77
+ const frameworkMeta = {
78
+ nerv: {
79
79
  importFrameworkStatement: `
80
80
  import Nerv from 'nervjs';
81
- `,
81
+ `,
82
82
  mockAppStatement: `
83
83
  class App extends Nerv.Component {
84
84
  render () {
85
85
  return this.props.children
86
86
  }
87
87
  }
88
- `,
89
- frameworkArgs: 'Nerv, Nerv, config',
90
- creator: 'createReactApp',
91
- creatorLocation: '@tarojs/plugin-framework-react/dist/runtime',
92
- importFrameworkName: 'Nerv',
93
- modifyConfig(config, source) {
94
- Object.assign(config, addConfig(source));
95
- }
96
- },
97
- react: {
88
+ `,
89
+ frameworkArgs: 'Nerv, Nerv, config',
90
+ creator: 'createReactApp',
91
+ creatorLocation: '@tarojs/plugin-framework-react/dist/runtime',
92
+ importFrameworkName: 'Nerv',
93
+ modifyConfig(config, source) {
94
+ Object.assign(config, addConfig(source));
95
+ }
96
+ },
97
+ react: {
98
98
  importFrameworkStatement: `
99
99
  import * as React from 'react'
100
100
  import ReactDOM from 'react-dom'
101
- `,
101
+ `,
102
102
  mockAppStatement: `
103
103
  class App extends React.Component {
104
104
  render () {
105
105
  return this.props.children
106
106
  }
107
107
  }
108
- `,
109
- frameworkArgs: 'React, ReactDOM, config',
110
- creator: 'createReactApp',
111
- creatorLocation: '@tarojs/plugin-framework-react/dist/runtime',
112
- importFrameworkName: 'React',
113
- compatComponentImport: 'import { PullDownRefresh } from "@tarojs/components"',
114
- compatComponentExtra: 'config.PullDownRefresh = PullDownRefresh',
115
- modifyConfig(config, source) {
116
- Object.assign(config, addConfig(source));
117
- }
118
- }
119
- };
120
- function getLoaderMeta(framework) {
121
- if (framework === 'preact')
122
- framework = 'react';
123
- return frameworkMeta[framework];
108
+ `,
109
+ frameworkArgs: 'React, ReactDOM, config',
110
+ creator: 'createReactApp',
111
+ creatorLocation: '@tarojs/plugin-framework-react/dist/runtime',
112
+ importFrameworkName: 'React',
113
+ compatComponentImport: 'import { PullDownRefresh } from "@tarojs/components"',
114
+ compatComponentExtra: 'config.PullDownRefresh = PullDownRefresh',
115
+ modifyConfig(config, source) {
116
+ Object.assign(config, addConfig(source));
117
+ }
118
+ }
119
+ };
120
+ function getLoaderMeta(framework) {
121
+ if (framework === 'preact')
122
+ framework = 'react';
123
+ return frameworkMeta[framework];
124
124
  }
125
125
 
126
- function modifyH5WebpackChain(ctx, framework, chain) {
127
- setAlias$2(ctx, chain);
128
- setLoader$1(framework, chain);
129
- setPlugin(ctx, framework, chain);
130
- chain.merge({
131
- module: {
132
- rule: {
133
- 'process-import-taro': {
134
- test: /taro-h5[\\/]dist[\\/]index/,
135
- loader: require.resolve('./api-loader')
136
- }
137
- }
138
- }
139
- });
140
- }
141
- function setAlias$2(ctx, chain) {
142
- var _a;
143
- const config = ctx.initialConfig;
144
- const alias = chain.resolve.alias;
145
- if ((_a = config.h5) === null || _a === void 0 ? void 0 : _a.useHtmlComponents) {
146
- alias.set('@tarojs/components$', '@tarojs/components-react/index');
147
- }
148
- else {
149
- alias.set('@tarojs/components$', '@tarojs/components/dist-h5/react');
150
- }
151
- }
152
- function setLoader$1(framework, chain) {
153
- chain.plugin('mainPlugin')
154
- .tap(args => {
155
- args[0].loaderMeta = getLoaderMeta(framework);
156
- return args;
157
- });
158
- }
159
- function setPlugin(ctx, framework, chain) {
160
- var _a, _b;
161
- const config = ctx.initialConfig;
162
- if (process.env.NODE_ENV !== 'production' &&
163
- ((_b = (_a = config.h5) === null || _a === void 0 ? void 0 : _a.devServer) === null || _b === void 0 ? void 0 : _b.hot) !== false) {
164
- // 默认开启 fast-refresh
165
- if (framework === 'react') {
166
- chain
167
- .plugin('fastRefreshPlugin')
168
- .use(require('@pmmmwh/react-refresh-webpack-plugin'));
169
- }
170
- else if (framework === 'preact') {
171
- chain
172
- .plugin('fastRefreshPlugin')
173
- .use(require('@prefresh/webpack'));
174
- }
175
- }
126
+ function modifyH5WebpackChain(ctx, framework, chain) {
127
+ setAlias$2(ctx, chain);
128
+ setLoader$1(framework, chain);
129
+ setPlugin(ctx, framework, chain);
130
+ chain.merge({
131
+ module: {
132
+ rule: {
133
+ 'process-import-taro': {
134
+ test: /taro-h5[\\/]dist[\\/]index/,
135
+ loader: require.resolve('./api-loader')
136
+ }
137
+ }
138
+ }
139
+ });
140
+ }
141
+ function setAlias$2(ctx, chain) {
142
+ var _a;
143
+ const config = ctx.initialConfig;
144
+ const alias = chain.resolve.alias;
145
+ if ((_a = config.h5) === null || _a === void 0 ? void 0 : _a.useHtmlComponents) {
146
+ alias.set('@tarojs/components$', '@tarojs/components-react/index');
147
+ }
148
+ else {
149
+ alias.set('@tarojs/components$', '@tarojs/components/dist-h5/react');
150
+ }
151
+ }
152
+ function setLoader$1(framework, chain) {
153
+ chain.plugin('mainPlugin')
154
+ .tap(args => {
155
+ args[0].loaderMeta = getLoaderMeta(framework);
156
+ return args;
157
+ });
158
+ }
159
+ function setPlugin(ctx, framework, chain) {
160
+ var _a, _b;
161
+ const config = ctx.initialConfig;
162
+ if (process.env.NODE_ENV !== 'production' &&
163
+ ((_b = (_a = config.h5) === null || _a === void 0 ? void 0 : _a.devServer) === null || _b === void 0 ? void 0 : _b.hot) !== false) {
164
+ // 默认开启 fast-refresh
165
+ if (framework === 'react') {
166
+ chain
167
+ .plugin('fastRefreshPlugin')
168
+ .use(require('@pmmmwh/react-refresh-webpack-plugin'));
169
+ }
170
+ else if (framework === 'preact') {
171
+ chain
172
+ .plugin('fastRefreshPlugin')
173
+ .use(require('@prefresh/webpack'));
174
+ }
175
+ }
176
176
  }
177
177
 
178
- function modifyMiniWebpackChain(ctx, framework, chain) {
179
- setAlias$1(ctx, framework, chain);
180
- setLoader(framework, chain);
181
- }
182
- function setAlias$1(ctx, framework, chain) {
183
- var _a;
184
- const config = ctx.initialConfig;
185
- const alias = chain.resolve.alias;
186
- if (framework === 'react') {
187
- alias.set('react-dom$', '@tarojs/react');
188
- if (process.env.NODE_ENV !== 'production' && ((_a = config.mini) === null || _a === void 0 ? void 0 : _a.debugReact) !== true) {
189
- // 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
190
- alias.set('react-reconciler$', 'react-reconciler/cjs/react-reconciler.production.min.js');
191
- alias.set('react$', 'react/cjs/react.production.min.js');
192
- alias.set('scheduler$', 'scheduler/cjs/scheduler.production.min.js');
193
- alias.set('react/jsx-runtime$', 'react/cjs/react-jsx-runtime.production.min.js');
194
- }
195
- }
196
- }
197
- function setLoader(framework, chain) {
198
- chain.plugin('miniPlugin')
199
- .tap(args => {
200
- args[0].loaderMeta = getLoaderMeta(framework);
201
- return args;
202
- });
178
+ function modifyMiniWebpackChain(ctx, framework, chain) {
179
+ setAlias$1(ctx, framework, chain);
180
+ setLoader(framework, chain);
181
+ }
182
+ function setAlias$1(ctx, framework, chain) {
183
+ var _a;
184
+ const config = ctx.initialConfig;
185
+ const alias = chain.resolve.alias;
186
+ if (framework === 'react') {
187
+ alias.set('react-dom$', '@tarojs/react');
188
+ if (process.env.NODE_ENV !== 'production' && ((_a = config.mini) === null || _a === void 0 ? void 0 : _a.debugReact) !== true) {
189
+ // 不是生产环境,且没有设置 debugReact,则使用压缩版本的 react 依赖,减少体积
190
+ alias.set('react-reconciler$', 'react-reconciler/cjs/react-reconciler.production.min.js');
191
+ alias.set('react$', 'react/cjs/react.production.min.js');
192
+ alias.set('scheduler$', 'scheduler/cjs/scheduler.production.min.js');
193
+ alias.set('react/jsx-runtime$', 'react/cjs/react-jsx-runtime.production.min.js');
194
+ // 在React18中,使用了exports字段约定了模块暴露路径,其中并未暴露 ./cjs/ 。这将使上面的alias在编译时报错。相当的tricky。
195
+ // Why wiriteJson? prebundle will load package.json via readFile to check exports property.
196
+ const reactPkgPath = require.resolve('react/package.json');
197
+ if (reactPkgPath) {
198
+ const reactPkg = require('react/package.json');
199
+ const reactVersion = (reactPkg.version || '');
200
+ if ((/^[~^]?18/).test(reactVersion) && reactPkg.exports) {
201
+ reactPkg.exports = Object.assign(reactPkg.exports, {
202
+ './cjs/': './cjs/'
203
+ });
204
+ helper.fs.writeJsonSync(reactPkgPath, reactPkg, { spaces: 2 });
205
+ }
206
+ }
207
+ }
208
+ }
209
+ }
210
+ function setLoader(framework, chain) {
211
+ chain.plugin('miniPlugin')
212
+ .tap(args => {
213
+ args[0].loaderMeta = getLoaderMeta(framework);
214
+ return args;
215
+ });
203
216
  }
204
217
 
205
- var index = (ctx, config = {}) => {
206
- const { framework } = ctx.initialConfig;
207
- const { reactMode = 'legacy' } = config; // will change default value in the future
208
- if (framework !== 'react' && framework !== 'nerv' && framework !== 'preact')
209
- return;
210
- ctx.modifyWebpackChain(({ chain }) => {
211
- // 通用
212
- setAlias(framework, chain);
213
- chain
214
- .plugin('definePlugin')
215
- .tap(args => {
216
- const config = args[0];
217
- config.__TARO_FRAMEWORK__ = `"${framework}"`;
218
- config.__TARO_FRAMEWORK_REACT_MODE__ = `"${reactMode}"`;
219
- return args;
220
- });
221
- if (process.env.TARO_ENV === 'h5') {
222
- // H5
223
- modifyH5WebpackChain(ctx, framework, chain);
224
- }
225
- else {
226
- // 小程序
227
- modifyMiniWebpackChain(ctx, framework, chain);
228
- }
229
- });
230
- ctx.modifyRunnerOpts(({ opts }) => {
231
- if (!(opts === null || opts === void 0 ? void 0 : opts.compiler))
232
- return;
233
- const { compiler } = opts;
234
- // 提供给 webpack5 依赖预编译收集器的第三方依赖
235
- const deps = [
236
- 'react',
237
- 'react-dom',
238
- 'react/jsx-runtime',
239
- '@tarojs/plugin-framework-react/dist/runtime'
240
- ];
241
- if (shared.isString(opts.compiler)) {
242
- opts.compiler = {
243
- type: opts.compiler
244
- };
245
- }
246
- if (compiler.type === 'webpack5') {
247
- compiler.prebundle || (compiler.prebundle = {});
248
- const prebundleOptions = compiler.prebundle;
249
- prebundleOptions.include || (prebundleOptions.include = []);
250
- prebundleOptions.include = prebundleOptions.include.concat(deps);
251
- if (prebundleOptions.enable === false)
252
- return;
253
- const taroReactPlugin = {
254
- name: 'taroReactPlugin',
255
- setup(build) {
256
- build.onLoad({ filter: /taro-h5[\\/]dist[\\/]index/ }, ({ path }) => {
257
- const content = helper.fs.readFileSync(path).toString();
258
- return {
259
- contents: require('./api-loader')(content)
260
- };
261
- });
262
- }
263
- };
264
- prebundleOptions.esbuild || (prebundleOptions.esbuild = {});
265
- const esbuildConfig = prebundleOptions.esbuild;
266
- esbuildConfig.plugins || (esbuildConfig.plugins = []);
267
- esbuildConfig.plugins.push(taroReactPlugin);
268
- }
269
- });
270
- };
271
- function setAlias(framework, chain) {
272
- const alias = chain.resolve.alias;
273
- switch (framework) {
274
- case 'preact':
275
- alias.set('react', 'preact/compat');
276
- alias.set('react-dom/test-utils', 'preact/test-utils');
277
- alias.set('react-dom', 'preact/compat');
278
- alias.set('react/jsx-runtime', 'preact/jsx-runtime');
279
- break;
280
- case 'nerv':
281
- alias.set('react$', 'nervjs');
282
- alias.set('react-dom$', 'nervjs');
283
- break;
284
- }
218
+ var index = (ctx, config = {}) => {
219
+ const { framework } = ctx.initialConfig;
220
+ const { reactMode = 'legacy' } = config; // will change default value in the future
221
+ if (framework !== 'react' && framework !== 'nerv' && framework !== 'preact')
222
+ return;
223
+ ctx.modifyWebpackChain(({ chain }) => {
224
+ // 通用
225
+ setAlias(framework, chain);
226
+ chain
227
+ .plugin('definePlugin')
228
+ .tap(args => {
229
+ const config = args[0];
230
+ config.__TARO_FRAMEWORK__ = `"${framework}"`;
231
+ config.__TARO_FRAMEWORK_REACT_MODE__ = `"${reactMode}"`;
232
+ return args;
233
+ });
234
+ if (process.env.TARO_ENV === 'h5') {
235
+ // H5
236
+ modifyH5WebpackChain(ctx, framework, chain);
237
+ }
238
+ else {
239
+ // 小程序
240
+ modifyMiniWebpackChain(ctx, framework, chain);
241
+ }
242
+ });
243
+ ctx.modifyRunnerOpts(({ opts }) => {
244
+ if (!(opts === null || opts === void 0 ? void 0 : opts.compiler))
245
+ return;
246
+ // 提供给 webpack5 依赖预编译收集器的第三方依赖
247
+ const deps = [
248
+ 'react',
249
+ 'react-dom',
250
+ 'react/jsx-runtime',
251
+ '@tarojs/plugin-framework-react/dist/runtime'
252
+ ];
253
+ if (shared.isString(opts.compiler)) {
254
+ opts.compiler = {
255
+ type: opts.compiler
256
+ };
257
+ }
258
+ const { compiler } = opts;
259
+ if (compiler.type === 'webpack5') {
260
+ compiler.prebundle || (compiler.prebundle = {});
261
+ const prebundleOptions = compiler.prebundle;
262
+ prebundleOptions.include || (prebundleOptions.include = []);
263
+ prebundleOptions.include = prebundleOptions.include.concat(deps);
264
+ if (prebundleOptions.enable === false)
265
+ return;
266
+ const taroReactPlugin = {
267
+ name: 'taroReactPlugin',
268
+ setup(build) {
269
+ build.onLoad({ filter: /taro-h5[\\/]dist[\\/]index/ }, ({ path }) => {
270
+ const content = helper.fs.readFileSync(path).toString();
271
+ return {
272
+ contents: require('./api-loader')(content)
273
+ };
274
+ });
275
+ }
276
+ };
277
+ prebundleOptions.esbuild || (prebundleOptions.esbuild = {});
278
+ const esbuildConfig = prebundleOptions.esbuild;
279
+ esbuildConfig.plugins || (esbuildConfig.plugins = []);
280
+ esbuildConfig.plugins.push(taroReactPlugin);
281
+ }
282
+ });
283
+ };
284
+ function setAlias(framework, chain) {
285
+ const alias = chain.resolve.alias;
286
+ switch (framework) {
287
+ case 'preact':
288
+ alias.set('react', 'preact/compat');
289
+ alias.set('react-dom/test-utils', 'preact/test-utils');
290
+ alias.set('react-dom', 'preact/compat');
291
+ alias.set('react/jsx-runtime', 'preact/jsx-runtime');
292
+ break;
293
+ case 'nerv':
294
+ alias.set('react$', 'nervjs');
295
+ alias.set('react-dom$', 'nervjs');
296
+ break;
297
+ }
285
298
  }
286
299
 
287
300
  exports["default"] = index;