@tamagui/next-plugin 1.0.1-beta.59 → 1.0.1-beta.60

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/next-plugin",
3
- "version": "1.0.1-beta.59",
3
+ "version": "1.0.1-beta.60",
4
4
  "source": "src/index.ts",
5
5
  "license": "MIT",
6
6
  "types": "./types/index.d.ts",
@@ -8,6 +8,7 @@
8
8
  "module": "dist/esm",
9
9
  "module:jsx": "dist/jsx",
10
10
  "files": [
11
+ "src",
11
12
  "types",
12
13
  "dist"
13
14
  ],
@@ -20,8 +21,8 @@
20
21
  "dependencies": {
21
22
  "@babel/preset-react": "^7.16.7",
22
23
  "@tamagui/expo-linear-gradient": "^11.2.8",
23
- "@tamagui/proxy-worm": "^1.0.1-beta.59",
24
- "@tamagui/static": "^1.0.1-beta.59",
24
+ "@tamagui/proxy-worm": "^1.0.1-beta.60",
25
+ "@tamagui/static": "^1.0.1-beta.60",
25
26
  "babel-loader": "^8.2.5",
26
27
  "browserslist": "^4.19.1",
27
28
  "css-loader": "^5.2.4",
@@ -30,11 +31,11 @@
30
31
  "mini-css-extract-plugin": "^2.6.0",
31
32
  "next-compose-plugins": "^2.2.1",
32
33
  "react-native-svg-web": "^1.0.9",
33
- "tamagui-loader": "^1.0.1-beta.59",
34
+ "tamagui-loader": "^1.0.1-beta.60",
34
35
  "thread-loader": "^3.0.4"
35
36
  },
36
37
  "devDependencies": {
37
- "@tamagui/build": "^1.0.1-beta.59",
38
+ "@tamagui/build": "^1.0.1-beta.60",
38
39
  "next": "^12.1.7-canary.16"
39
40
  },
40
41
  "peerDependencies": {
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './withTamagui'
2
+
3
+ // this applies automatically already!
4
+ // only exported necessary for weird/advanced use cases, where
5
+ // your react-native-web is in some other directory
6
+ export { patchReactNativeWeb } from '@tamagui/static'
@@ -0,0 +1,361 @@
1
+ import path from 'path'
2
+
3
+ import type { TamaguiOptions } from '@tamagui/static'
4
+ import browserslist from 'browserslist'
5
+ import CssMinimizerPlugin from 'css-minimizer-webpack-plugin'
6
+ import MiniCSSExtractPlugin from 'mini-css-extract-plugin'
7
+ import { lazyPostCSS } from 'next/dist/build/webpack/config/blocks/css'
8
+ import { getGlobalCssLoader } from 'next/dist/build/webpack/config/blocks/css/loaders'
9
+ import { shouldExclude as shouldExcludeDefault } from 'tamagui-loader'
10
+ import webpack from 'webpack'
11
+
12
+ export type WithTamaguiProps = TamaguiOptions & {
13
+ aliasReactPackages?: boolean
14
+ shouldExtract?: (path: string, projectRoot: string) => boolean | undefined
15
+ shouldExcludeFromServer?: (props: {
16
+ context: string
17
+ request: string
18
+ fullPath: string
19
+ }) => boolean | string | undefined
20
+ }
21
+
22
+ export const withTamagui = (tamaguiOptions: WithTamaguiProps) => {
23
+ // allows configuration
24
+ const shouldExclude = (path: string, projectRoot: string) => {
25
+ const res = tamaguiOptions.shouldExtract?.(path, projectRoot)
26
+ if (typeof res === 'boolean') {
27
+ return !res
28
+ }
29
+ return shouldExcludeDefault(path, projectRoot)
30
+ }
31
+
32
+ return (nextConfig: any = {}) => {
33
+ return {
34
+ ...nextConfig,
35
+ webpack: (webpackConfig: any, options: any) => {
36
+ const { dir, config, dev, isServer } = options
37
+
38
+ // @ts-ignore
39
+ if (typeof globalThis['__DEV__'] === 'undefined') {
40
+ // @ts-ignore
41
+ globalThis['__DEV__'] = dev
42
+ }
43
+
44
+ const isNext12 = typeof options.config?.swcMinify === 'boolean'
45
+ const prefix = `${isServer ? ' ssr ' : ' web '} |`
46
+
47
+ const safeResolves = (
48
+ ...resolves: ([string, string] | [string, string, [string, string]])[]
49
+ ) => {
50
+ let res: string[][] = []
51
+ for (const [out, mod, replace] of resolves) {
52
+ try {
53
+ const next = [out, require.resolve(mod)]
54
+ if (replace) {
55
+ next[1] = next[1].replace(...replace)
56
+ }
57
+ res.push(next)
58
+ } catch (err) {
59
+ // console.log(prefix, `withTamagui skipping resolving ${out}`)
60
+ }
61
+ }
62
+ return Object.fromEntries(res)
63
+ }
64
+
65
+ const alias = {
66
+ ...(webpackConfig.resolve.alias || {}),
67
+ ...safeResolves(
68
+ ['react-native-svg', 'react-native-svg-web'],
69
+ // fixes https://github.com/kentcdodds/mdx-bundler/issues/143
70
+ ['react/jsx-runtime.js', 'react/jsx-runtime'],
71
+ ['react/jsx-runtime', 'react/jsx-runtime'],
72
+ ['react/jsx-dev-runtime.js', 'react/jsx-dev-runtime'],
73
+ ['react/jsx-dev-runtime', 'react/jsx-dev-runtime'],
74
+ ['react-native$', 'react-native-web'],
75
+ ['react-native-reanimated', 'react-native-reanimated'],
76
+ ['expo-linear-gradient', '@tamagui/expo-linear-gradient'],
77
+ ['react-native-web$', 'react-native-web'],
78
+ ['@testing-library/react-native', '@tamagui/proxy-worm'],
79
+ ['@gorhom/bottom-sheet$', '@gorhom/bottom-sheet', ['commonjs', 'module']],
80
+ // fix reanimated 3
81
+ ['react-native/Libraries/Renderer/shims/ReactFabric', '@tamagui/proxy-worm'],
82
+ ...(tamaguiOptions.aliasReactPackages
83
+ ? ([
84
+ ['react', 'react'],
85
+ ['react-dom', 'react-dom'],
86
+ ] as any)
87
+ : [])
88
+ ),
89
+ // expo fix https://github.com/expo/expo/issues/9999
90
+ 'react-native-web/src': require.resolve('react-native-web/dist'),
91
+ // react: require.resolve('react'),
92
+ // 'react-dom': require.resolve('react-dom'),
93
+ }
94
+
95
+ webpackConfig.resolve.alias = alias
96
+
97
+ webpackConfig.plugins.push(
98
+ new webpack.DefinePlugin({
99
+ 'process.env.IS_STATIC': '""',
100
+ 'process.env.TAMAGUI_TARGET': '"web"',
101
+ __DEV__: JSON.stringify(dev),
102
+ })
103
+ )
104
+
105
+ const excludeExports = tamaguiOptions.excludeReactNativeWebExports
106
+ if (Array.isArray(excludeExports)) {
107
+ try {
108
+ const regexStr = `\/react-native-web\/.*(${excludeExports.join('|')}).*\/`
109
+ const regex = new RegExp(regexStr)
110
+ console.log(prefix, 'exclude', regexStr)
111
+ webpackConfig.plugins.push(
112
+ new webpack.NormalModuleReplacementPlugin(
113
+ regex,
114
+ require.resolve('@tamagui/proxy-worm/empty-react-native-view')
115
+ )
116
+ )
117
+ } catch (err) {
118
+ console.warn(
119
+ `Invalid names provided to excludeReactNativeWebExports: ${excludeExports.join(', ')}`
120
+ )
121
+ }
122
+ }
123
+
124
+ if (process.env.IGNORE_TS_CONFIG_PATHS) {
125
+ if (process.env.DEBUG) {
126
+ console.log(prefix, 'ignoring tsconfig paths')
127
+ }
128
+ if (webpackConfig.resolve.plugins[0]) {
129
+ delete webpackConfig.resolve.plugins[0].paths['@tamagui/*']
130
+ delete webpackConfig.resolve.plugins[0].paths['tamagui']
131
+ }
132
+ }
133
+
134
+ // TODO document and make configurable
135
+ // replace minifier with css-minimizer-webpack-plugin which handles deduping atomic styles
136
+ if (!dev) {
137
+ const cssMin = webpackConfig.optimization.minimizer.find((x) =>
138
+ x.toString().includes('css-minimizer-plugin')
139
+ )
140
+ if (cssMin) {
141
+ webpackConfig.optimization.minimizer = webpackConfig.optimization.minimizer.slice(
142
+ cssMin.index,
143
+ 1
144
+ )
145
+ }
146
+ webpackConfig.optimization.minimizer.push(new CssMinimizerPlugin())
147
+ }
148
+
149
+ if (!webpackConfig.resolve.extensions.includes('.web.js')) {
150
+ webpackConfig.resolve.extensions = ['.web.js', ...webpackConfig.resolve.extensions]
151
+ }
152
+
153
+ // look for compiled js with jsx intact as specified by module:jsx
154
+ webpackConfig.resolve.mainFields.unshift('module:jsx')
155
+
156
+ if (isServer) {
157
+ const externalize = (context: string, request: string) => {
158
+ const fullPath = request[0] === '.' ? path.join(context, request) : request
159
+
160
+ if (tamaguiOptions.shouldExcludeFromServer) {
161
+ const userRes = tamaguiOptions.shouldExcludeFromServer({
162
+ context,
163
+ request,
164
+ fullPath,
165
+ })
166
+ if (userRes !== undefined) {
167
+ return userRes
168
+ }
169
+ }
170
+
171
+ // must inline react-native so we can alias to react-native-web
172
+ if (fullPath === 'react-native' || fullPath.startsWith('react-native/')) {
173
+ return false
174
+ }
175
+
176
+ if (
177
+ // feather icons uses react-native-svg which needs to be aliased
178
+ // fullPath.includes('/feather-icons/') ||
179
+ fullPath.startsWith('react-native-web') ||
180
+ fullPath.includes('node_modules/react-native-web') ||
181
+ /^(react-dom|react)\/$/.test(fullPath)
182
+ ) {
183
+ return `commonjs ${fullPath}`
184
+ }
185
+ if (
186
+ fullPath.includes('/moti/') ||
187
+ fullPath.includes('/solito/') ||
188
+ fullPath === 'tamagui' ||
189
+ fullPath.startsWith('@tamagui/') ||
190
+ fullPath === 'react-native-safe-area-context' ||
191
+ fullPath.startsWith('@react-navigation') ||
192
+ fullPath === '@gorhom/bottom-sheet'
193
+ ) {
194
+ return
195
+ }
196
+ if (/^\@?react-native-/.test(request)) {
197
+ return false
198
+ }
199
+ return true
200
+ }
201
+
202
+ // externalize react native things from bundle
203
+ webpackConfig.externals = [
204
+ ...webpackConfig.externals.map((external) => {
205
+ if (typeof external !== 'function') {
206
+ return external
207
+ }
208
+ // only runs on server
209
+ return (ctx, cb) => {
210
+ const isCb = typeof cb === 'function'
211
+ const res = externalize(ctx.context, ctx.request)
212
+ if (isCb) {
213
+ if (typeof res === 'string') {
214
+ return cb(null, res)
215
+ }
216
+ if (res) {
217
+ return external(ctx, cb)
218
+ }
219
+ return cb()
220
+ }
221
+ return !res
222
+ ? Promise.resolve(undefined)
223
+ : typeof res === 'string'
224
+ ? Promise.resolve(res)
225
+ : external(ctx)
226
+ }
227
+ }),
228
+ ]
229
+ }
230
+
231
+ const oneOfRule = webpackConfig.module.rules.find((x) => !!x.oneOf)
232
+
233
+ const cssLoader = getGlobalCssLoader(
234
+ // @ts-ignore
235
+ {
236
+ assetPrefix: options.config.assetPrefix || config.assetPrefix,
237
+ future: nextConfig.future,
238
+ experimental: nextConfig.experimental || {},
239
+ isClient: !isServer,
240
+ isServer,
241
+ isDevelopment: dev,
242
+ },
243
+ // @ts-ignore
244
+ () => lazyPostCSS(dir, getSupportedBrowsers(dir, dev)),
245
+ []
246
+ )
247
+
248
+ if (oneOfRule) {
249
+ if (!dev) {
250
+ const postCSSLoader = cssLoader[cssLoader.length - 1]
251
+ // replace nextjs picky style rules with simple minicssextract
252
+ oneOfRule.oneOf.unshift({
253
+ test: /\.css$/i,
254
+ use: [MiniCSSExtractPlugin.loader, 'css-loader', postCSSLoader],
255
+ sideEffects: true,
256
+ })
257
+ webpackConfig.plugins.push(
258
+ new MiniCSSExtractPlugin({
259
+ filename: 'static/css/[name].[contenthash].css',
260
+ ignoreOrder: true,
261
+ runtime: false,
262
+ })
263
+ )
264
+ } else {
265
+ oneOfRule.oneOf.unshift({
266
+ test: /\.css$/i,
267
+ sideEffects: true,
268
+ use: cssLoader,
269
+ })
270
+ }
271
+ }
272
+
273
+ if (isNext12) {
274
+ const firstOneOfRule = webpackConfig.module.rules.findIndex((x) => x && !!x.oneOf)
275
+ const oneOfJSRules: any[] = webpackConfig.module.rules[firstOneOfRule].oneOf
276
+ const afterSWCLoaderIndex =
277
+ oneOfJSRules.findIndex(
278
+ (x) => x && x.use && x.use.loader === 'next-swc-loader' && x.issuerLayer !== 'api'
279
+ ) + 1
280
+ const swcLoader = oneOfJSRules[afterSWCLoaderIndex]
281
+
282
+ // put an earlier loader where we just do tamagui stuff before regular swc
283
+ oneOfJSRules.splice(
284
+ afterSWCLoaderIndex,
285
+ 0,
286
+ {
287
+ test: /(bottom-sheet).*\.[tj]sx?$/,
288
+ use: [
289
+ {
290
+ loader: 'babel-loader',
291
+ options: {
292
+ presets: ['@babel/preset-react'],
293
+ plugins: ['react-native-reanimated/plugin'],
294
+ },
295
+ },
296
+ ],
297
+ },
298
+ {
299
+ test: /\.(jsx?|tsx?)$/,
300
+ exclude: (path: string) => shouldExclude(path, options.dir),
301
+ use: [
302
+ ...[].concat(swcLoader.use),
303
+ {
304
+ loader: 'tamagui-loader',
305
+ options: tamaguiOptions,
306
+ },
307
+ ],
308
+ }
309
+ )
310
+ } else {
311
+ // next 11 modify loader
312
+ const [first, second, ...rest] = webpackConfig.module.rules
313
+ webpackConfig.module.rules = [
314
+ first,
315
+ second,
316
+ {
317
+ test: /\.(tsx|jsx)$/,
318
+ exclude: (path: string) => shouldExclude(path, options.dir),
319
+ use: [
320
+ 'thread-loader',
321
+ {
322
+ loader: 'esbuild-loader',
323
+ options: {
324
+ loader: 'tsx',
325
+ minify: false,
326
+ },
327
+ },
328
+ {
329
+ loader: 'tamagui-loader',
330
+ options: {
331
+ prefixLogs: prefix,
332
+ ...tamaguiOptions,
333
+ },
334
+ },
335
+ ],
336
+ },
337
+ ...rest,
338
+ ]
339
+ }
340
+
341
+ if (typeof nextConfig.webpack === 'function') {
342
+ return nextConfig.webpack(webpackConfig, options)
343
+ }
344
+
345
+ return webpackConfig
346
+ },
347
+ }
348
+ }
349
+ }
350
+
351
+ function getSupportedBrowsers(dir, isDevelopment) {
352
+ let browsers
353
+ try {
354
+ browsers = browserslist.loadConfig({
355
+ path: dir,
356
+ env: isDevelopment ? 'development' : 'production',
357
+ })
358
+ } catch {}
359
+
360
+ return browsers
361
+ }