Skip to content

Commit

Permalink
Use postcss to extract styles in Expo example
Browse files Browse the repository at this point in the history
Extract styles to static CSS using postcss, as there is no existing
Metro integration.

Fix #34
Ref #236

Co-authored-by: Tsubasa Sakai <[email protected]>
  • Loading branch information
necolas and javascripter committed Nov 14, 2024
1 parent 98afb90 commit bd385f0
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 26 deletions.
6 changes: 3 additions & 3 deletions apps/examples/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"backgroundColor": "#ffffff"
},
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
}
},
"ios": {
"supportsTablet": true
},
"web": {
"bundler": "metro",
"favicon": "./assets/favicon.png"
Expand Down
7 changes: 2 additions & 5 deletions apps/examples/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ module.exports = function (api) {
//api.cache(true);

const platform = api.caller(getPlatform);
const isDev = api.caller(getIsDev);
const dev = api.caller(getIsDev);

const presets = ['babel-preset-expo'];
const plugins = [];

if (platform === 'web') {
presets.push([
reactStrictPreset,
{ debug: true, dev: isDev, rootDir: __dirname }
]);
presets.push([reactStrictPreset, { debug: true, dev, rootDir: __dirname }]);
}

return {
Expand Down
6 changes: 4 additions & 2 deletions apps/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"name": "examples",
"version": "0.0.0",
"main": "./src/index.js",
"main": "./src/app/index.js",
"scripts": {
"dev": "expo start --clear",
"dev:android": "expo start --android --clear",
Expand All @@ -14,10 +14,12 @@
"expo": "^52.0.3",
"expo-build-properties": "~0.13.1",
"expo-status-bar": "~2.0.0",
"postcss-react-strict-dom": "0.0.2",
"react": "~18.3.1",
"react-dom": "~18.3.1",
"react-native": "~0.76.1",
"react-native-web": "~0.19.13"
"react-native-web": "~0.19.13",
"react-strict-dom": "*"
},
"devDependencies": {
"@babel/core": "^7.25.2"
Expand Down
18 changes: 18 additions & 0 deletions apps/examples/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
plugins: {
'postcss-react-strict-dom': {
include: [
require.resolve('react-strict-dom'),
'src/**/*.{js,jsx,mjs,ts,tsx}'
]
},
autoprefixer: {}
}
};
9 changes: 6 additions & 3 deletions apps/examples/src/index.js → apps/examples/src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

import { LogBox } from 'react-native';
// Required to apply extracted on Expo Web
import './stylex.css';
// Required for Fast Refresh to work on Expo Web
import '@expo/metro-runtime';

import { LogBox } from 'react-native';
import { registerRootComponent } from 'expo';

import App from './App';
import App from '../components/App';

if (LogBox != null) {
LogBox.ignoreLogs([
Expand Down
13 changes: 13 additions & 0 deletions apps/examples/src/app/stylex.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* The @stylex directive is used by the react-strict-dom postcss plugin.
* It is automatically replaced with generated CSS during builds.
*/

@stylex;
File renamed without changes.
File renamed without changes.
34 changes: 33 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/react-strict-dom/babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function reactStrictPreset(_, options = {}) {
{
dev: opts.dev,
importSources: [{ from: 'react-strict-dom', as: 'css' }],
runtimeInjection: opts.dev, // temporary until Expo/Metro can support built-time
runtimeInjection: false,
styleResolution: 'property-specificity',
unstable_moduleResolution: {
type: 'commonJS',
Expand Down
4 changes: 0 additions & 4 deletions packages/react-strict-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
"./package.json": "./package.json"
},
"sideEffects": false,
"files": [
"*",
"!**/__tests__"
],
"scripts": {
"build": "rollup --config ./tools/rollup.config.mjs",
"clean": "del-cli \"./build/*\" \"./dist/*\"",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-strict-dom/src/dom/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const styles = stylex.create({
borderWidth: 1
},
codePre: {
fontFamily: 'monospace, monospace',
fontFamily: 'monospace, "monospace"',
fontSize: '1em',
overflow: 'auto'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ exports[`html "button" supports inline event handlers 1`] = `

exports[`html "code" ignores and warns about unsupported attributes 1`] = `
<code
className="html-code x1ghz6dp x1717udv x1hl2dhg x16tdsg8 x1vvkbs xelszwe xrv4cvt xysyzu8"
className="html-code x1ghz6dp x1717udv x1hl2dhg x16tdsg8 x1vvkbs x1lxnp44 xrv4cvt xysyzu8"
/>
`;

Expand Down Expand Up @@ -1231,7 +1231,7 @@ exports[`html "code" supports global attributes 1`] = `
aria-valuetext="Five"
autoCapitalize={true}
autoFocus={true}
className="html-code x1ghz6dp x1717udv x1hl2dhg x16tdsg8 x1vvkbs xelszwe xrv4cvt xysyzu8"
className="html-code x1ghz6dp x1717udv x1hl2dhg x16tdsg8 x1vvkbs x1lxnp44 xrv4cvt xysyzu8"
data-testid="some-test-id"
dir="ltr"
enterKeyHint="go"
Expand All @@ -1255,7 +1255,7 @@ exports[`html "code" supports global attributes 1`] = `

exports[`html "code" supports inline event handlers 1`] = `
<code
className="html-code x1ghz6dp x1717udv x1hl2dhg x16tdsg8 x1vvkbs xelszwe xrv4cvt xysyzu8"
className="html-code x1ghz6dp x1717udv x1hl2dhg x16tdsg8 x1vvkbs x1lxnp44 xrv4cvt xysyzu8"
onAuxClick={[Function]}
onBeforeInput={[Function]}
onBlur={[Function]}
Expand Down Expand Up @@ -4825,7 +4825,7 @@ exports[`html "p" supports inline event handlers 1`] = `

exports[`html "pre" ignores and warns about unsupported attributes 1`] = `
<pre
className="html-pre x1ghz6dp x1717udv xelszwe xrv4cvt xysyzu8"
className="html-pre x1ghz6dp x1717udv x1lxnp44 xrv4cvt xysyzu8"
/>
`;

Expand Down Expand Up @@ -4880,7 +4880,7 @@ exports[`html "pre" supports global attributes 1`] = `
aria-valuetext="Five"
autoCapitalize={true}
autoFocus={true}
className="html-pre x1ghz6dp x1717udv xelszwe xrv4cvt xysyzu8"
className="html-pre x1ghz6dp x1717udv x1lxnp44 xrv4cvt xysyzu8"
data-testid="some-test-id"
dir="ltr"
enterKeyHint="go"
Expand All @@ -4904,7 +4904,7 @@ exports[`html "pre" supports global attributes 1`] = `

exports[`html "pre" supports inline event handlers 1`] = `
<pre
className="html-pre x1ghz6dp x1717udv xelszwe xrv4cvt xysyzu8"
className="html-pre x1ghz6dp x1717udv x1lxnp44 xrv4cvt xysyzu8"
onAuxClick={[Function]}
onBeforeInput={[Function]}
onBlur={[Function]}
Expand Down

0 comments on commit bd385f0

Please sign in to comment.