Skip to content

Commit

Permalink
Chore: Improve default options for bundlers (#25)
Browse files Browse the repository at this point in the history
* Chore: Improve default options for bundlers

* Chore: Export more types from Babel plugin (#26)
  • Loading branch information
nmn authored Dec 4, 2023
1 parent be21b5b commit 9e5cc33
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module.name_mapper='^@stylexjs\/shared$' -> '<PROJECT_ROOT>/packages/shared/src/
module.name_mapper='^@stylexjs\/shared\/lib\/\([a-zA-Z0-9_\-]+\)$' -> '<PROJECT_ROOT>/packages/shared/src/\1'
module.name_mapper='^@stylexjs/stylex$' -> '<PROJECT_ROOT>/packages/stylex/src/stylex.js'
module.name_mapper='^@stylexjs/stylex\/lib\/\([a-zA-Z0-9_\-]+\)$' -> '<PROJECT_ROOT>/packages/stylex/src/\1'
module.name_mapper='^@stylexjs/babel-plugin$' -> '<PROJECT_ROOT>/packages/babel-plugin/src/index.js'
module.name_mapper='^@stylexjs/babel-plugin\/lib\/\([a-zA-Z0-9_\-]+\)$' -> '<PROJECT_ROOT>/packages/babel-plugin/src/\1'
; type-stubs
module.system.node.resolve_dirname=flow_modules
module.system.node.resolve_dirname=node_modules
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/docs/learn/05-theming/02-using-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ subject to change.

:::

Once [variables have been defined](../defining-variables), they can be imported
Once [variables have been defined](./01-defining-variables.mdx), they can be imported
and used to declare styles with `stylex.create`.

Assume the following variables have been defined in a file called
Expand Down
3 changes: 0 additions & 3 deletions apps/docs/docs/learn/05-theming/03-creating-themes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
sidebar_position: 3
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Creating themes

:::danger
Expand Down
3 changes: 2 additions & 1 deletion apps/nextjs-example/.babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module.exports = {
'@stylexjs/babel-plugin',
{
dev: process.env.NODE_ENV === 'development',
stylexSheetName: '<>',
runtimeInjection: false,
genConditionalClasses: true,
treeshakeCompensation: true,
unstable_moduleResolution: {
type: 'commonJS',
rootDir: path.join(__dirname, '../..'),
Expand Down
2 changes: 2 additions & 0 deletions apps/rollup-example/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/

import stylexPlugin from '@stylexjs/rollup-plugin';
Expand Down
11 changes: 10 additions & 1 deletion packages/babel-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import * as t from '@babel/types';
import type { NodePath } from '@babel/traverse';
import type { PluginObj } from '@babel/core';
import type { StyleXOptions } from './utils/state-manager';
import StateManager from './utils/state-manager';
import { readImportDeclarations, readRequires } from './visitors/imports';
import transformStyleXCreate from './visitors/stylex-create';
Expand All @@ -25,6 +26,8 @@ import { skipStylexPropsChildren } from './visitors/stylex-props';

const NAME = 'stylex';

export type Options = StyleXOptions;

/**
* Entry point for the StyleX babel plugin.
*/
Expand Down Expand Up @@ -139,6 +142,12 @@ export default function styleXTransform(): PluginObj<> {
};
}

styleXTransform.withOptions = function stylexPluginWithOptions(
options: StyleXOptions,
): [typeof styleXTransform, StyleXOptions] {
return [styleXTransform, options];
};

function isExported(path: null | NodePath<t.Node>): boolean {
if (path == null || pathUtils.isProgram(path)) {
return false;
Expand Down Expand Up @@ -167,7 +176,7 @@ function isExported(path: null | NodePath<t.Node>): boolean {
*
* End-users can choose to not use this function and use their own logic instead.
*/
type Rule = [string, { ltr: string, rtl?: null | string }, number];
export type Rule = [string, { ltr: string, rtl?: null | string }, number];
function processStylexRules(
rules: Array<Rule>,
useLayers: boolean = false,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin/src/utils/state-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type ModuleResolution =
themeFileExtension?: string,
};

type StyleXOptions = {
export type StyleXOptions = {
...RuntimeOptions,
importSources: Array<string>,
treeshakeCompensation?: boolean,
Expand Down
7 changes: 2 additions & 5 deletions packages/rollup-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ const IS_DEV_ENV =

module.exports = function stylexPlugin({
dev = IS_DEV_ENV,
runtimeInjection,
unstable_moduleResolution = { type: 'commonJS', rootDir: process.cwd() },
fileName = 'stylex.css',
babelConfig: { plugins = [], presets = [] } = {},
...options
} = {}) {
let stylexRules = {};

return {
name: 'rollup-plugin-stylex',
buildStart() {
Expand All @@ -36,7 +35,6 @@ module.exports = function stylexPlugin({
const rules = Object.values(stylexRules).flat();
if (rules.length > 0) {
const collectedCSS = stylexBabelPlugin.processStylexRules(rules, true);

this.emitFile({
fileName,
source: collectedCSS,
Expand All @@ -59,14 +57,13 @@ module.exports = function stylexPlugin({
? flowSyntaxPlugin
: typescriptSyntaxPlugin,
jsxSyntaxPlugin,
[stylexBabelPlugin, { dev, runtimeInjection, ...options }],
[stylexBabelPlugin, { dev, unstable_moduleResolution, ...options }],
],
});

if (!dev && metadata.stylex != null && metadata.stylex.length > 0) {
stylexRules[id] = metadata.stylex;
}

return { code, map, meta: metadata };
},
};
Expand Down
29 changes: 15 additions & 14 deletions packages/scripts/gen-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,30 @@ async function generateTypes(inputDir, outputDir, rootDir) {
.join('\n');
}

outputFlowContents = patchFlowModulePaths(
outputFullPath,
outputFlowContents,
flowModules,
);
const tsOutputName = dirent.name
.replace(/\.js$/, '.d.ts')
.replace(/\.js\.flow$/, '.d.ts');

await fsPromises.writeFile(
`${outputFullPath}.flow`,
outputFlowContents,
patchFlowModulePaths(
outputFullPath,
outputFlowContents,
flowModules,
),
);
const tsOutputName = dirent.name
.replace(/\.js$/, '.d.ts')
.replace(/\.js\.flow$/, '.d.ts');

if (dTsFiles.includes(tsOutputName)) {
continue;
}

const outputTSContents = await translate.translateFlowDefToTSDef(
outputFlowContents.replace(/\$ReadOnlyMap/g, 'ReadonlyMap'),
outputFlowContents
.replace(/\$ReadOnlyMap/g, 'ReadonlyMap')
.replace(/\$ReadOnlySet/g, 'ReadonlySet'),
monorepoPackage.prettier,
);

await fsPromises.writeFile(
outputFullPath.replace(/\.js$/, '.d.ts'),
// Typescript Prefers `NodePath` unlike `NodePath<>` in Flow
Expand Down Expand Up @@ -130,10 +134,7 @@ function preprocessFileContents(inputCode) {
}

function postProcessTSOutput(outputCode) {
const result = outputCode
.replace(/<>/g, '')
.replace(/\$ReadOnlyMap/g, 'ReadonlyMap')
.replace(/\$ReadOnlySet/g, 'ReadonlySet');
const result = outputCode.replace(/<>/g, '');

return result;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/webpack-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class StylexPlugin {

constructor({
dev = IS_DEV_ENV,
runtimeInjection,
appendTo,
filename = appendTo == null ? 'stylex.css' : undefined,
stylexImports = ['stylex', '@stylexjs/stylex'],
stylexImports = ['stylex', '@stylex/css'],
unstable_moduleResolution = { type: 'commonJS', rootDir: process.cwd() },
babelConfig: { plugins = [], presets = [], babelrc = false } = {},
...options
} /*: PluginOptions */ = {}) {
Expand All @@ -60,7 +60,7 @@ class StylexPlugin {
this.stylexImports = stylexImports;
this.babelPlugin = [
stylexBabelPlugin,
{ dev, runtimeInjection, ...options },
{ dev, unstable_moduleResolution, stylexImports, ...options },
];
}

Expand Down

0 comments on commit 9e5cc33

Please sign in to comment.