From 6b809485def6ebe09acca4272a90da1898b87a67 Mon Sep 17 00:00:00 2001 From: Mark Lawlor Date: Mon, 10 Oct 2022 00:23:45 +1000 Subject: [PATCH] feat: custom css import --- package-lock.json | 4 +++- packages/nativewind/package.json | 1 + packages/nativewind/src/metro/index.ts | 29 +++++++++++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0191b55..6326ad8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35968,11 +35968,12 @@ } }, "packages/nativewind": { - "version": "3.0.0-next.1-3-g91e5901", + "version": "3.0.0-next.1-10-g0123a38", "license": "MIT", "dependencies": { "@babel/helper-module-imports": "7.18.6", "@babel/types": "7.19.0", + "@expo/config": "^7.0.1", "css-tree": "^2.2.1", "find-cache-dir": "^3.3.2", "micromatch": "^4.0.5", @@ -56127,6 +56128,7 @@ "@babel/helper-module-imports": "7.18.6", "@babel/plugin-syntax-jsx": "7.18.6", "@babel/types": "7.19.0", + "@expo/config": "*", "@testing-library/react-hooks": "8.0.1", "@testing-library/react-native": "11.1.0", "@types/css-tree": "^1.0.7", diff --git a/packages/nativewind/package.json b/packages/nativewind/package.json index 8245372..8be5ea9 100644 --- a/packages/nativewind/package.json +++ b/packages/nativewind/package.json @@ -51,6 +51,7 @@ "dependencies": { "@babel/helper-module-imports": "7.18.6", "@babel/types": "7.19.0", + "@expo/config": "^7.0.1", "css-tree": "^2.2.1", "find-cache-dir": "^3.3.2", "micromatch": "^4.0.5", diff --git a/packages/nativewind/src/metro/index.ts b/packages/nativewind/src/metro/index.ts index 0895b4b..87bcbe6 100644 --- a/packages/nativewind/src/metro/index.ts +++ b/packages/nativewind/src/metro/index.ts @@ -1,8 +1,11 @@ -import { writeFileSync } from "node:fs"; +/* eslint-disable unicorn/prefer-module, @typescript-eslint/no-var-requires */ +import { readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; - -import findCacheDir from "find-cache-dir"; import { spawn, spawnSync } from "node:child_process"; + +import { resolveEntryPoint } from "@expo/config/paths"; +import findCacheDir from "find-cache-dir"; + import { getCreateOptions } from "../postcss/extract"; export interface WithNativeWindOptions { @@ -23,8 +26,24 @@ export default function withNativeWind( process.env.NATIVEWIND_OUTPUT = outputFile; if (!inputPath) { - inputPath = join(cacheDirectory, "input.css"); - writeFileSync(inputPath, "@tailwind components;@tailwind utilities;"); + try { + let { main } = require("package.json"); + + if (main && main === "node_modules/expo/AppEntry.js") { + main = resolveEntryPoint(__dirname, { platform: "ios" }); + } + + if (main) { + const cssImport = readFileSync(main, "utf8").match(/(\w+\.css)/); + + if (cssImport) { + inputPath = cssImport[0]; + } + } + } finally { + inputPath ??= join(cacheDirectory, "input.css"); + writeFileSync(inputPath, "@tailwind components;@tailwind utilities;"); + } } const spawnCommands = ["tailwind", "-i", inputPath];