@tamagui/vite-plugin 1.0.1-beta.141 → 1.0.1-beta.142

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.
@@ -0,0 +1,164 @@
1
+ import path from "path";
2
+ import {
3
+ createExtractor,
4
+ extractToClassNames,
5
+ getPragmaOptions
6
+ } from "@tamagui/static";
7
+ import outdent from "outdent";
8
+ import { normalizePath } from "vite";
9
+ const styleUpdateEvent = (fileId) => `tamagui-style-update:${fileId}`;
10
+ const GLOBAL_CSS_VIRTUAL_PATH = "__tamagui_global_css__.css";
11
+ function tamaguiExtractPlugin(options) {
12
+ const disableStatic = options.disable || options.disableDebugAttr && options.disableExtraction;
13
+ if (disableStatic) {
14
+ return {
15
+ name: "tamagui-extract"
16
+ };
17
+ }
18
+ let extractor = null;
19
+ const cssMap = /* @__PURE__ */ new Map();
20
+ let config;
21
+ let server;
22
+ let shouldReturnCSS = true;
23
+ let virtualExt;
24
+ const getAbsoluteVirtualFileId = (filePath) => {
25
+ if (filePath.startsWith(config.root)) {
26
+ return filePath;
27
+ }
28
+ return normalizePath(path.join(config.root, filePath));
29
+ };
30
+ return {
31
+ name: "tamagui-extract",
32
+ enforce: "pre",
33
+ configureServer(_server) {
34
+ server = _server;
35
+ },
36
+ buildEnd() {
37
+ extractor.cleanupBeforeExit();
38
+ },
39
+ writeBundle(options2, bundle) {
40
+ setTimeout(() => {
41
+ console.warn("some sort of dangling process or osmethign, exit for now...");
42
+ process.exit(0);
43
+ }, 100);
44
+ },
45
+ config(_userConfig, env) {
46
+ const include = env.command === "serve" ? ["@tamagui/core/injectStyles"] : [];
47
+ return {
48
+ optimizeDeps: { include }
49
+ };
50
+ },
51
+ async configResolved(resolvedConfig) {
52
+ config = resolvedConfig;
53
+ extractor = createExtractor({
54
+ logger: resolvedConfig.logger
55
+ });
56
+ shouldReturnCSS = true;
57
+ virtualExt = `.tamagui.${shouldReturnCSS ? "css" : "js"}`;
58
+ },
59
+ async resolveId(source) {
60
+ if (source === "tamagui.css") {
61
+ await extractor.loadTamagui(options);
62
+ return GLOBAL_CSS_VIRTUAL_PATH;
63
+ }
64
+ const [validId, query] = source.split("?");
65
+ if (!validId.endsWith(virtualExt)) {
66
+ return;
67
+ }
68
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(validId);
69
+ if (cssMap.has(absoluteId)) {
70
+ return absoluteId + (query ? `?${query}` : "");
71
+ }
72
+ },
73
+ load(id, options2) {
74
+ const [validId] = id.split("?");
75
+ if (validId === GLOBAL_CSS_VIRTUAL_PATH) {
76
+ return extractor.getTamagui().getCSS();
77
+ }
78
+ if (!cssMap.has(validId)) {
79
+ return;
80
+ }
81
+ const css = cssMap.get(validId);
82
+ if (typeof css !== "string") {
83
+ return;
84
+ }
85
+ if (shouldReturnCSS || !server || server.config.isProduction) {
86
+ return css;
87
+ }
88
+ return outdent`
89
+ import { injectStyles } from '@tamagui/core/injectStyles';
90
+
91
+ const inject = (css) => injectStyles({
92
+ filePath: "${validId}",
93
+ css
94
+ });
95
+
96
+ inject(${JSON.stringify(css)});
97
+
98
+ if (import.meta.hot) {
99
+ import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {
100
+ inject(css);
101
+ });
102
+ }
103
+ `;
104
+ },
105
+ async transform(code, id, ssrParam) {
106
+ const [validId] = id.split("?");
107
+ if (!validId.endsWith(".tsx")) {
108
+ return;
109
+ }
110
+ let ssr;
111
+ if (typeof ssrParam === "boolean") {
112
+ ssr = ssrParam;
113
+ } else {
114
+ ssr = ssrParam?.ssr;
115
+ }
116
+ const { shouldDisable, shouldPrintDebug } = getPragmaOptions({
117
+ source: code,
118
+ path: validId
119
+ });
120
+ if (shouldDisable) {
121
+ return;
122
+ }
123
+ const extracted = await extractToClassNames({
124
+ extractor,
125
+ source: code,
126
+ sourcePath: validId,
127
+ options,
128
+ shouldPrintDebug
129
+ });
130
+ if (!extracted) {
131
+ return;
132
+ }
133
+ const rootRelativeId = `${validId}${virtualExt}`;
134
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
135
+ let source = extracted.js;
136
+ if (extracted.styles) {
137
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== extracted.styles) {
138
+ const { moduleGraph } = server;
139
+ const [module] = Array.from(moduleGraph.getModulesByFile(absoluteId) || []);
140
+ if (module) {
141
+ moduleGraph.invalidateModule(module);
142
+ module.lastHMRTimestamp = module.lastInvalidationTimestamp || Date.now();
143
+ }
144
+ server.ws.send({
145
+ type: "custom",
146
+ event: styleUpdateEvent(absoluteId),
147
+ data: extracted.styles
148
+ });
149
+ }
150
+ source = `${source}
151
+ import "${rootRelativeId}";`;
152
+ cssMap.set(absoluteId, extracted.styles);
153
+ }
154
+ return {
155
+ code: source.toString(),
156
+ map: extracted.map
157
+ };
158
+ }
159
+ };
160
+ }
161
+ export {
162
+ tamaguiExtractPlugin
163
+ };
164
+ //# sourceMappingURL=extract.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/extract.ts"],
4
+ "sourcesContent": ["// fork from https://github.com/seek-oss/vanilla-extract\n\nimport path from 'path'\n\nimport {\n TamaguiOptions,\n createExtractor,\n extractToClassNames,\n getPragmaOptions,\n} from '@tamagui/static'\nimport outdent from 'outdent'\nimport type { Plugin, ResolvedConfig, ViteDevServer } from 'vite'\nimport { normalizePath } from 'vite'\n\nconst styleUpdateEvent = (fileId: string) => `tamagui-style-update:${fileId}`\nconst GLOBAL_CSS_VIRTUAL_PATH = '__tamagui_global_css__.css'\n\nexport function tamaguiExtractPlugin(options: TamaguiOptions): Plugin {\n const disableStatic = options.disable || (options.disableDebugAttr && options.disableExtraction)\n\n if (disableStatic) {\n return {\n name: 'tamagui-extract',\n }\n }\n\n let extractor: ReturnType<typeof createExtractor> | null = null\n const cssMap = new Map<string, string>()\n\n let config: ResolvedConfig\n let server: ViteDevServer\n let shouldReturnCSS = true //config.command === 'serve'\n let virtualExt: string\n\n const getAbsoluteVirtualFileId = (filePath: string) => {\n if (filePath.startsWith(config.root)) {\n return filePath\n }\n return normalizePath(path.join(config.root, filePath))\n }\n\n return {\n name: 'tamagui-extract',\n enforce: 'pre',\n\n configureServer(_server) {\n server = _server\n },\n\n buildEnd() {\n extractor!.cleanupBeforeExit()\n },\n\n writeBundle(this, options, bundle) {\n setTimeout(() => {\n // eslint-disable-next-line no-console\n console.warn('some sort of dangling process or osmethign, exit for now...')\n process.exit(0)\n }, 100)\n },\n\n config(_userConfig, env) {\n const include = env.command === 'serve' ? ['@tamagui/core/injectStyles'] : []\n return {\n optimizeDeps: { include },\n }\n },\n\n async configResolved(resolvedConfig) {\n config = resolvedConfig\n extractor = createExtractor({\n logger: resolvedConfig.logger,\n })\n shouldReturnCSS = true\n // TODO postcss work with postcss.config.js\n // packageName = getPackageInfo(config.root).name;\n // if (config.command === 'serve') {\n // postCssConfig = await resolvePostcssConfig(config);\n // }\n virtualExt = `.tamagui.${shouldReturnCSS ? 'css' : 'js'}`\n },\n\n async resolveId(source) {\n if (source === 'tamagui.css') {\n await extractor!.loadTamagui(options)\n return GLOBAL_CSS_VIRTUAL_PATH\n }\n\n const [validId, query] = source.split('?')\n\n if (!validId.endsWith(virtualExt)) {\n return\n }\n\n // Absolute paths seem to occur often in monorepos, where files are\n // imported from outside the config root.\n const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(validId)\n\n // There should always be an entry in the `cssMap` here.\n // The only valid scenario for a missing one is if someone had written\n // a file in their app using the .tamagui.js/.tamagui.css extension\n if (cssMap.has(absoluteId)) {\n // Keep the original query string for HMR.\n return absoluteId + (query ? `?${query}` : '')\n }\n },\n\n /**\n * TODO\n *\n * mainFields module:jsx breaks, so lets just have a mapping here\n * where we load() and map it to the jsx path before transform\n *\n */\n\n load(id, options) {\n const [validId] = id.split('?')\n\n if (validId === GLOBAL_CSS_VIRTUAL_PATH) {\n return extractor!.getTamagui()!.getCSS()\n }\n\n if (!cssMap.has(validId)) {\n return\n }\n\n const css = cssMap.get(validId)\n\n if (typeof css !== 'string') {\n return\n }\n\n if (shouldReturnCSS || !server || server.config.isProduction) {\n return css\n }\n\n return outdent`\n import { injectStyles } from '@tamagui/core/injectStyles';\n\n const inject = (css) => injectStyles({\n filePath: \"${validId}\",\n css\n });\n\n inject(${JSON.stringify(css)});\n\n if (import.meta.hot) {\n import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {\n inject(css);\n });\n }\n `\n },\n\n async transform(code, id, ssrParam) {\n const [validId] = id.split('?')\n\n if (!validId.endsWith('.tsx')) {\n return\n }\n\n let ssr: boolean | undefined\n if (typeof ssrParam === 'boolean') {\n ssr = ssrParam\n } else {\n ssr = ssrParam?.ssr\n }\n\n const { shouldDisable, shouldPrintDebug } = getPragmaOptions({\n source: code,\n path: validId,\n })\n\n if (shouldDisable) {\n return\n }\n\n const extracted = await extractToClassNames({\n extractor: extractor!,\n source: code,\n sourcePath: validId,\n options,\n shouldPrintDebug,\n })\n\n if (!extracted) {\n return\n }\n\n const rootRelativeId = `${validId}${virtualExt}`\n const absoluteId = getAbsoluteVirtualFileId(rootRelativeId)\n\n let source = extracted.js\n\n if (extracted.styles) {\n if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== extracted.styles) {\n const { moduleGraph } = server\n const [module] = Array.from(moduleGraph.getModulesByFile(absoluteId) || [])\n\n if (module) {\n moduleGraph.invalidateModule(module)\n\n // Vite uses this timestamp to add `?t=` query string automatically for HMR.\n module.lastHMRTimestamp = (module as any).lastInvalidationTimestamp || Date.now()\n }\n\n server.ws.send({\n type: 'custom',\n event: styleUpdateEvent(absoluteId),\n data: extracted.styles,\n })\n }\n\n source = `${source}\\nimport \"${rootRelativeId}\";`\n cssMap.set(absoluteId, extracted.styles)\n }\n\n return {\n code: source.toString(),\n map: extracted.map,\n }\n\n // if (ssr && !process.env.VITE_RSC_BUILD) {\n // return addFileScope({\n // source: code,\n // filePath: normalizePath(validId),\n // rootPath: config.root,\n // packageName,\n // })\n // }\n\n // const { source, watchFiles } = await compile({\n // filePath: validId,\n // cwd: config.root,\n // esbuildOptions,\n // })\n\n // for (const file of watchFiles) {\n // // In start mode, we need to prevent the file from rewatching itself.\n // // If it's a `build --watch`, it needs to watch everything.\n // if (config.command === 'build' || file !== validId) {\n // this.addWatchFile(file)\n // }\n // }\n },\n }\n}\n"],
5
+ "mappings": "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",
6
+ "names": []
7
+ }
package/dist/jsx/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./plugin";
2
+ export * from "./extract";
2
3
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
- "sourcesContent": ["export * from './plugin'\n"],
5
- "mappings": "AAAA;",
4
+ "sourcesContent": ["export * from './plugin'\nexport * from './extract'\n"],
5
+ "mappings": "AAAA;AACA;",
6
6
  "names": []
7
7
  }
@@ -1,13 +1,10 @@
1
- import envPlugin from "vite-plugin-environment";
2
1
  function tamaguiPlugin(options) {
3
2
  const plugin = {
4
- name: "tamagui",
3
+ name: "tamagui-base",
5
4
  enforce: "pre",
6
5
  config(userConfig, env) {
7
6
  return {
8
- plugins: [
9
- envPlugin(["NODE_ENV", "TAMAGUI_TARGET"])
10
- ],
7
+ plugins: [],
11
8
  esbuild: {
12
9
  loader: "tsx"
13
10
  },
@@ -16,20 +13,20 @@ function tamaguiPlugin(options) {
16
13
  _frameTimestamp: void 0,
17
14
  _WORKLET: false,
18
15
  ...process.env.NODE_ENV !== "test" && {
19
- process: {
20
- env: {
21
- TAMAGUI_TARGET: process.env.TAMAGUI_TARGET || "web",
22
- NODE_ENV: process.env.NODE_ENV || env.mode
23
- }
24
- }
16
+ "process.env.TAMAGUI_TARGET": JSON.stringify(process.env.TAMAGUI_TARGET || "web"),
17
+ "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || env.mode),
18
+ "process.env.ENABLE_RSC": JSON.stringify(process.env.ENABLE_RSC || ""),
19
+ "process.env.ENABLE_STEPS": JSON.stringify(process.env.ENABLE_STEPS || ""),
20
+ "process.env.IS_STATIC": JSON.stringify(false)
25
21
  }
26
22
  },
27
23
  ssr: {
28
- noExternal: /tamagui|react-native/,
29
- optimizeDeps: {}
24
+ noExternal: /tamagui|react-native|expo-linear-gradient/
30
25
  },
31
26
  optimizeDeps: {
27
+ include: ["styleq", "copy-to-clipboard", "react-native-reanimated"],
32
28
  esbuildOptions: {
29
+ jsx: "transform",
33
30
  resolveExtensions: [
34
31
  ".web.js",
35
32
  ".web.ts",
@@ -61,7 +58,13 @@ function tamaguiPlugin(options) {
61
58
  alias: {
62
59
  "react-native/Libraries/Renderer/shims/ReactFabric": "@tamagui/proxy-worm",
63
60
  "react-native/Libraries/Utilities/codegenNativeComponent": "@tamagui/proxy-worm",
64
- "react-native": "react-native-web"
61
+ "react-native-svg": "react-native-svg-web",
62
+ "react-native": "react-native-web",
63
+ ...options.useReactNativeWebLite && {
64
+ "@tamagui/rnw": "@tamagui/rnw-lite",
65
+ "react-native": "react-native-web-lite",
66
+ "react-native-web": "react-native-web-lite"
67
+ }
65
68
  }
66
69
  }
67
70
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/plugin.ts"],
4
- "sourcesContent": ["import { esbuildCommonjs, viteCommonjs } from '@originjs/vite-plugin-commonjs'\nimport type { TamaguiOptions } from '@tamagui/static'\nimport type { Plugin } from 'vite'\nimport envPlugin from 'vite-plugin-environment'\n\nimport { tamaguiExtractPlugin } from './extractPlugin'\n\n/**\n * For some reason envPlugin doesnt work for vitest, but process: { env: {} } breaks vitest\n */\n\nexport function tamaguiPlugin(options: TamaguiOptions): Plugin {\n const plugin: Plugin = {\n name: 'tamagui',\n enforce: 'pre',\n\n config(userConfig, env) {\n return {\n plugins: [\n // viteCommonjs(),\n envPlugin(['NODE_ENV', 'TAMAGUI_TARGET']),\n // ...(options.disable || (options.disableDebugAttr && options.disableExtraction)\n // ? []\n // : [tamaguiExtractPlugin(options)]),\n ],\n esbuild: {\n loader: 'tsx',\n },\n define: {\n // reanimated support\n 'global.__x': {},\n _frameTimestamp: undefined,\n _WORKLET: false,\n ...(process.env.NODE_ENV !== 'test' && {\n process: {\n env: {\n TAMAGUI_TARGET: process.env.TAMAGUI_TARGET || 'web',\n NODE_ENV: process.env.NODE_ENV || env.mode,\n },\n },\n }),\n },\n // build: {\n // commonjsOptions: {\n // transformMixedEsModules: true,\n // },\n // },\n ssr: {\n noExternal: /tamagui|react-native/,\n optimizeDeps: {\n // disabled: true,\n },\n },\n optimizeDeps: {\n // include: [/node_modules/],\n esbuildOptions: {\n // plugins: [esbuildCommonjs(['fbjs'])],\n resolveExtensions: [\n '.web.js',\n '.web.ts',\n '.web.tsx',\n '.js',\n '.jsx',\n '.json',\n '.ts',\n '.tsx',\n '.mjs',\n ],\n loader: {\n '.js': 'jsx',\n },\n },\n },\n resolve: {\n // for once it extracts\n // mainFields: ['module:jsx', 'module', 'jsnext:main', 'jsnext'],\n extensions: [\n '.web.js',\n '.web.ts',\n '.web.tsx',\n '.js',\n '.jsx',\n '.json',\n '.ts',\n '.tsx',\n '.mjs',\n ],\n alias: {\n 'react-native/Libraries/Renderer/shims/ReactFabric': '@tamagui/proxy-worm',\n 'react-native/Libraries/Utilities/codegenNativeComponent': '@tamagui/proxy-worm',\n // 'react-native': 'react-native-web-lite',\n 'react-native': 'react-native-web',\n },\n },\n }\n },\n }\n\n return plugin\n}\n"],
5
- "mappings": "AAGA;AAQO,uBAAuB,SAAiC;AAC7D,QAAM,SAAiB;AAAA,IACrB,MAAM;AAAA,IACN,SAAS;AAAA,IAET,OAAO,YAAY,KAAK;AACtB,aAAO;AAAA,QACL,SAAS;AAAA,UAEP,UAAU,CAAC,YAAY,gBAAgB,CAAC;AAAA,QAI1C;AAAA,QACA,SAAS;AAAA,UACP,QAAQ;AAAA,QACV;AAAA,QACA,QAAQ;AAAA,UAEN,cAAc,CAAC;AAAA,UACf,iBAAiB;AAAA,UACjB,UAAU;AAAA,UACV,GAAI,QAAQ,IAAI,aAAa,UAAU;AAAA,YACrC,SAAS;AAAA,cACP,KAAK;AAAA,gBACH,gBAAgB,QAAQ,IAAI,kBAAkB;AAAA,gBAC9C,UAAU,QAAQ,IAAI,YAAY,IAAI;AAAA,cACxC;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,QAMA,KAAK;AAAA,UACH,YAAY;AAAA,UACZ,cAAc,CAEd;AAAA,QACF;AAAA,QACA,cAAc;AAAA,UAEZ,gBAAgB;AAAA,YAEd,mBAAmB;AAAA,cACjB;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YACF;AAAA,YACA,QAAQ;AAAA,cACN,OAAO;AAAA,YACT;AAAA,UACF;AAAA,QACF;AAAA,QACA,SAAS;AAAA,UAGP,YAAY;AAAA,YACV;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,UACA,OAAO;AAAA,YACL,qDAAqD;AAAA,YACrD,2DAA2D;AAAA,YAE3D,gBAAgB;AAAA,UAClB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;",
4
+ "sourcesContent": ["import { esbuildCommonjs, viteCommonjs } from '@originjs/vite-plugin-commonjs'\nimport type { TamaguiOptions } from '@tamagui/static'\nimport type { Plugin } from 'vite'\nimport envPlugin from 'vite-plugin-environment'\n\n/**\n * For some reason envPlugin doesnt work for vitest, but process: { env: {} } breaks vitest\n */\n\nexport function tamaguiPlugin(\n options: TamaguiOptions & {\n useReactNativeWebLite?: boolean\n }\n): Plugin {\n const plugin: Plugin = {\n name: 'tamagui-base',\n enforce: 'pre',\n\n config(userConfig, env) {\n return {\n plugins: [\n //\n // envPlugin(['NODE_ENV', 'TAMAGUI_TARGET', 'ENABLE_RSC']),\n // viteCommonjs(),\n ],\n esbuild: {\n loader: 'tsx',\n },\n define: {\n // reanimated support\n 'global.__x': {},\n _frameTimestamp: undefined,\n _WORKLET: false,\n ...(process.env.NODE_ENV !== 'test' && {\n 'process.env.TAMAGUI_TARGET': JSON.stringify(process.env.TAMAGUI_TARGET || 'web'),\n 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || env.mode),\n 'process.env.ENABLE_RSC': JSON.stringify(process.env.ENABLE_RSC || ''),\n 'process.env.ENABLE_STEPS': JSON.stringify(process.env.ENABLE_STEPS || ''),\n 'process.env.IS_STATIC': JSON.stringify(false),\n }),\n },\n // build: {\n // commonjsOptions: {\n // transformMixedEsModules: true,\n // },\n // },\n ssr: {\n // noExternal: /^(tamagui|react-native|expo-linear-gradient)$/,\n noExternal: /tamagui|react-native|expo-linear-gradient/,\n },\n optimizeDeps: {\n // disabled: false,\n include: ['styleq', 'copy-to-clipboard', 'react-native-reanimated'],\n esbuildOptions: {\n jsx: 'transform',\n // plugins: [\n // esbuildCommonjs([\n // 'styleq',\n // 'inline-style-prefixer',\n // 'create-react-class',\n // 'copy-to-clipboard',\n // ]),\n // ],\n resolveExtensions: [\n '.web.js',\n '.web.ts',\n '.web.tsx',\n '.js',\n '.jsx',\n '.json',\n '.ts',\n '.tsx',\n '.mjs',\n ],\n loader: {\n '.js': 'jsx',\n },\n },\n },\n resolve: {\n // for once it extracts\n // mainFields: ['module:jsx', 'module', 'jsnext:main', 'jsnext', 'main'],\n extensions: [\n '.web.js',\n '.web.ts',\n '.web.tsx',\n '.js',\n '.jsx',\n '.json',\n '.ts',\n '.tsx',\n '.mjs',\n ],\n alias: {\n 'react-native/Libraries/Renderer/shims/ReactFabric': '@tamagui/proxy-worm',\n 'react-native/Libraries/Utilities/codegenNativeComponent': '@tamagui/proxy-worm',\n 'react-native-svg': 'react-native-svg-web',\n 'react-native': 'react-native-web',\n ...(options.useReactNativeWebLite && {\n '@tamagui/rnw': '@tamagui/rnw-lite',\n 'react-native': 'react-native-web-lite',\n 'react-native-web': 'react-native-web-lite',\n }),\n },\n },\n }\n },\n }\n\n return plugin\n}\n"],
5
+ "mappings": "AASO,uBACL,SAGQ;AACR,QAAM,SAAiB;AAAA,IACrB,MAAM;AAAA,IACN,SAAS;AAAA,IAET,OAAO,YAAY,KAAK;AACtB,aAAO;AAAA,QACL,SAAS,CAIT;AAAA,QACA,SAAS;AAAA,UACP,QAAQ;AAAA,QACV;AAAA,QACA,QAAQ;AAAA,UAEN,cAAc,CAAC;AAAA,UACf,iBAAiB;AAAA,UACjB,UAAU;AAAA,UACV,GAAI,QAAQ,IAAI,aAAa,UAAU;AAAA,YACrC,8BAA8B,KAAK,UAAU,QAAQ,IAAI,kBAAkB,KAAK;AAAA,YAChF,wBAAwB,KAAK,UAAU,QAAQ,IAAI,YAAY,IAAI,IAAI;AAAA,YACvE,0BAA0B,KAAK,UAAU,QAAQ,IAAI,cAAc,EAAE;AAAA,YACrE,4BAA4B,KAAK,UAAU,QAAQ,IAAI,gBAAgB,EAAE;AAAA,YACzE,yBAAyB,KAAK,UAAU,KAAK;AAAA,UAC/C;AAAA,QACF;AAAA,QAMA,KAAK;AAAA,UAEH,YAAY;AAAA,QACd;AAAA,QACA,cAAc;AAAA,UAEZ,SAAS,CAAC,UAAU,qBAAqB,yBAAyB;AAAA,UAClE,gBAAgB;AAAA,YACd,KAAK;AAAA,YASL,mBAAmB;AAAA,cACjB;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YACF;AAAA,YACA,QAAQ;AAAA,cACN,OAAO;AAAA,YACT;AAAA,UACF;AAAA,QACF;AAAA,QACA,SAAS;AAAA,UAGP,YAAY;AAAA,YACV;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,UACA,OAAO;AAAA,YACL,qDAAqD;AAAA,YACrD,2DAA2D;AAAA,YAC3D,oBAAoB;AAAA,YACpB,gBAAgB;AAAA,YAChB,GAAI,QAAQ,yBAAyB;AAAA,cACnC,gBAAgB;AAAA,cAChB,gBAAgB;AAAA,cAChB,oBAAoB;AAAA,YACtB;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/vite-plugin",
3
- "version": "1.0.1-beta.141",
3
+ "version": "1.0.1-beta.142",
4
4
  "types": "./types/index.d.ts",
5
5
  "main": "dist/cjs",
6
6
  "module": "dist/esm",
@@ -18,16 +18,30 @@
18
18
  "clean:build": "tamagui-build clean:build"
19
19
  },
20
20
  "dependencies": {
21
- "@tamagui/proxy-worm": "^1.0.1-beta.141",
22
- "@tamagui/static": "^1.0.1-beta.141",
21
+ "@originjs/vite-plugin-commonjs": "^1.0.3",
22
+ "@tamagui/fake-react-native": "^1.0.1-beta.142",
23
+ "@tamagui/proxy-worm": "^1.0.1-beta.142",
24
+ "@tamagui/static": "^1.0.1-beta.142",
23
25
  "fs-extra": "^10.1.0",
24
26
  "lodash": "^4.17.21",
25
- "outdent": "^0.8.0"
27
+ "outdent": "^0.8.0",
28
+ "react-native-svg-web": "^1.0.1-beta.142"
26
29
  },
27
30
  "devDependencies": {
28
- "@tamagui/build": "^1.0.1-beta.141",
31
+ "@tamagui/build": "^1.0.1-beta.142",
29
32
  "vite-plugin-environment": "^1.1.2"
30
33
  },
34
+ "exports": {
35
+ "./package.json": "./package.json",
36
+ ".": {
37
+ "import": "./dist/esm/index.js",
38
+ "require": "./dist/cjs/index.js"
39
+ },
40
+ "./extract": {
41
+ "import": "./dist/esm/extract.js",
42
+ "require": "./dist/cjs/extract.js"
43
+ }
44
+ },
31
45
  "publishConfig": {
32
46
  "access": "public"
33
47
  }
package/src/extract.ts ADDED
@@ -0,0 +1,247 @@
1
+ // fork from https://github.com/seek-oss/vanilla-extract
2
+
3
+ import path from 'path'
4
+
5
+ import {
6
+ TamaguiOptions,
7
+ createExtractor,
8
+ extractToClassNames,
9
+ getPragmaOptions,
10
+ } from '@tamagui/static'
11
+ import outdent from 'outdent'
12
+ import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite'
13
+ import { normalizePath } from 'vite'
14
+
15
+ const styleUpdateEvent = (fileId: string) => `tamagui-style-update:${fileId}`
16
+ const GLOBAL_CSS_VIRTUAL_PATH = '__tamagui_global_css__.css'
17
+
18
+ export function tamaguiExtractPlugin(options: TamaguiOptions): Plugin {
19
+ const disableStatic = options.disable || (options.disableDebugAttr && options.disableExtraction)
20
+
21
+ if (disableStatic) {
22
+ return {
23
+ name: 'tamagui-extract',
24
+ }
25
+ }
26
+
27
+ let extractor: ReturnType<typeof createExtractor> | null = null
28
+ const cssMap = new Map<string, string>()
29
+
30
+ let config: ResolvedConfig
31
+ let server: ViteDevServer
32
+ let shouldReturnCSS = true //config.command === 'serve'
33
+ let virtualExt: string
34
+
35
+ const getAbsoluteVirtualFileId = (filePath: string) => {
36
+ if (filePath.startsWith(config.root)) {
37
+ return filePath
38
+ }
39
+ return normalizePath(path.join(config.root, filePath))
40
+ }
41
+
42
+ return {
43
+ name: 'tamagui-extract',
44
+ enforce: 'pre',
45
+
46
+ configureServer(_server) {
47
+ server = _server
48
+ },
49
+
50
+ buildEnd() {
51
+ extractor!.cleanupBeforeExit()
52
+ },
53
+
54
+ writeBundle(this, options, bundle) {
55
+ setTimeout(() => {
56
+ // eslint-disable-next-line no-console
57
+ console.warn('some sort of dangling process or osmethign, exit for now...')
58
+ process.exit(0)
59
+ }, 100)
60
+ },
61
+
62
+ config(_userConfig, env) {
63
+ const include = env.command === 'serve' ? ['@tamagui/core/injectStyles'] : []
64
+ return {
65
+ optimizeDeps: { include },
66
+ }
67
+ },
68
+
69
+ async configResolved(resolvedConfig) {
70
+ config = resolvedConfig
71
+ extractor = createExtractor({
72
+ logger: resolvedConfig.logger,
73
+ })
74
+ shouldReturnCSS = true
75
+ // TODO postcss work with postcss.config.js
76
+ // packageName = getPackageInfo(config.root).name;
77
+ // if (config.command === 'serve') {
78
+ // postCssConfig = await resolvePostcssConfig(config);
79
+ // }
80
+ virtualExt = `.tamagui.${shouldReturnCSS ? 'css' : 'js'}`
81
+ },
82
+
83
+ async resolveId(source) {
84
+ if (source === 'tamagui.css') {
85
+ await extractor!.loadTamagui(options)
86
+ return GLOBAL_CSS_VIRTUAL_PATH
87
+ }
88
+
89
+ const [validId, query] = source.split('?')
90
+
91
+ if (!validId.endsWith(virtualExt)) {
92
+ return
93
+ }
94
+
95
+ // Absolute paths seem to occur often in monorepos, where files are
96
+ // imported from outside the config root.
97
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(validId)
98
+
99
+ // There should always be an entry in the `cssMap` here.
100
+ // The only valid scenario for a missing one is if someone had written
101
+ // a file in their app using the .tamagui.js/.tamagui.css extension
102
+ if (cssMap.has(absoluteId)) {
103
+ // Keep the original query string for HMR.
104
+ return absoluteId + (query ? `?${query}` : '')
105
+ }
106
+ },
107
+
108
+ /**
109
+ * TODO
110
+ *
111
+ * mainFields module:jsx breaks, so lets just have a mapping here
112
+ * where we load() and map it to the jsx path before transform
113
+ *
114
+ */
115
+
116
+ load(id, options) {
117
+ const [validId] = id.split('?')
118
+
119
+ if (validId === GLOBAL_CSS_VIRTUAL_PATH) {
120
+ return extractor!.getTamagui()!.getCSS()
121
+ }
122
+
123
+ if (!cssMap.has(validId)) {
124
+ return
125
+ }
126
+
127
+ const css = cssMap.get(validId)
128
+
129
+ if (typeof css !== 'string') {
130
+ return
131
+ }
132
+
133
+ if (shouldReturnCSS || !server || server.config.isProduction) {
134
+ return css
135
+ }
136
+
137
+ return outdent`
138
+ import { injectStyles } from '@tamagui/core/injectStyles';
139
+
140
+ const inject = (css) => injectStyles({
141
+ filePath: "${validId}",
142
+ css
143
+ });
144
+
145
+ inject(${JSON.stringify(css)});
146
+
147
+ if (import.meta.hot) {
148
+ import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {
149
+ inject(css);
150
+ });
151
+ }
152
+ `
153
+ },
154
+
155
+ async transform(code, id, ssrParam) {
156
+ const [validId] = id.split('?')
157
+
158
+ if (!validId.endsWith('.tsx')) {
159
+ return
160
+ }
161
+
162
+ let ssr: boolean | undefined
163
+ if (typeof ssrParam === 'boolean') {
164
+ ssr = ssrParam
165
+ } else {
166
+ ssr = ssrParam?.ssr
167
+ }
168
+
169
+ const { shouldDisable, shouldPrintDebug } = getPragmaOptions({
170
+ source: code,
171
+ path: validId,
172
+ })
173
+
174
+ if (shouldDisable) {
175
+ return
176
+ }
177
+
178
+ const extracted = await extractToClassNames({
179
+ extractor: extractor!,
180
+ source: code,
181
+ sourcePath: validId,
182
+ options,
183
+ shouldPrintDebug,
184
+ })
185
+
186
+ if (!extracted) {
187
+ return
188
+ }
189
+
190
+ const rootRelativeId = `${validId}${virtualExt}`
191
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId)
192
+
193
+ let source = extracted.js
194
+
195
+ if (extracted.styles) {
196
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== extracted.styles) {
197
+ const { moduleGraph } = server
198
+ const [module] = Array.from(moduleGraph.getModulesByFile(absoluteId) || [])
199
+
200
+ if (module) {
201
+ moduleGraph.invalidateModule(module)
202
+
203
+ // Vite uses this timestamp to add `?t=` query string automatically for HMR.
204
+ module.lastHMRTimestamp = (module as any).lastInvalidationTimestamp || Date.now()
205
+ }
206
+
207
+ server.ws.send({
208
+ type: 'custom',
209
+ event: styleUpdateEvent(absoluteId),
210
+ data: extracted.styles,
211
+ })
212
+ }
213
+
214
+ source = `${source}\nimport "${rootRelativeId}";`
215
+ cssMap.set(absoluteId, extracted.styles)
216
+ }
217
+
218
+ return {
219
+ code: source.toString(),
220
+ map: extracted.map,
221
+ }
222
+
223
+ // if (ssr && !process.env.VITE_RSC_BUILD) {
224
+ // return addFileScope({
225
+ // source: code,
226
+ // filePath: normalizePath(validId),
227
+ // rootPath: config.root,
228
+ // packageName,
229
+ // })
230
+ // }
231
+
232
+ // const { source, watchFiles } = await compile({
233
+ // filePath: validId,
234
+ // cwd: config.root,
235
+ // esbuildOptions,
236
+ // })
237
+
238
+ // for (const file of watchFiles) {
239
+ // // In start mode, we need to prevent the file from rewatching itself.
240
+ // // If it's a `build --watch`, it needs to watch everything.
241
+ // if (config.command === 'build' || file !== validId) {
242
+ // this.addWatchFile(file)
243
+ // }
244
+ // }
245
+ },
246
+ }
247
+ }
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './plugin'
2
+ export * from './extract'