Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support webpack #28

Merged
merged 7 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pnpm i unplugin-stylex --save-dev
<details>
<summary>Vite</summary><br>

```ts
```js
// vite.config.js
import { defineConfig } from 'vite'
import stylexPlugin from 'unplugin-stylex/vite'
Expand All @@ -45,7 +45,7 @@ export default defineConfig({
<details>
<summary>esbuild</summary><br>

```ts
```js
// esbuild.config.js
import { build } from 'esbuild'
import stylexPlugin from 'unplugin-stylex/esbuild'
Expand All @@ -62,7 +62,7 @@ export default {
<details>
<summary>rspack</summary><br>

```ts
```js
// rspack.config.js
import stylexPlugin from 'unplugin-stylex/rspack'

Expand All @@ -76,6 +76,44 @@ module.exports = {

</br></details>

<details>
<summary>rollup</summary><br>

```js
// rollup.config.js
import stylexRollupPlugin from 'unplugin-stylex/rollup'

export default {
// other rollup config
plugins: [
stylexRollupPlugin({ /* options */}),
],
}
```

</br></details>

<details>
<summary>webpack</summary><br>

```js
// webpack.config.js
import stylexWebpackPlugin from 'unplugin-stylex/webpack'

module.exports = {
// other webpack config
plugins: [
stylexWebpackPlugin({ /* options */}),
],
}
```

</br></details>

## Usage

More detail usage can check [examples](https://github.com/eryue0220/unplugin-stylex/tree/main/examples)

## Options

Current support argument, which may have change in the future
Expand Down
2 changes: 1 addition & 1 deletion examples/esbuild-example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Stylex with Esbuild!</title>
<title>Stylex with Esbuild</title>
<style>
@layer reset {
body {
Expand Down
2 changes: 1 addition & 1 deletion examples/esbuild-example/scripts/dev.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ await context
port,
})
.then(() => {
console.log(`[info]: server start at http:127.0.0.1:${port}.`)
console.log(`[info]: server start at http://127.0.0.1:${port}.`)
})
.catch((error) => {
console.error(error)
Expand Down
2 changes: 1 addition & 1 deletion examples/rollup-example/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default {
}),
serve({
contentBase: ['dist'],
host: 'localhost',
host: '127.0.0.1',
port: 8081,
}),
html({
Expand Down
3 changes: 3 additions & 0 deletions examples/vue-example/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import vue from '@vitejs/plugin-vue'
import stylexVitePlugin from 'unplugin-stylex/vite'

export default defineConfig({
build: {
outDir: 'dist'
},
plugins: [
vue(),
stylexVitePlugin({
Expand Down
28 changes: 28 additions & 0 deletions examples/webpack-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "webpack-example",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "NODE_ENV=development webpack serve",
"build": "webpack build"
},
"devDependencies": {
"@babel/core": "^7.23.6",
"@babel/preset-env": "^7.24.3",
"@babel/preset-react": "^7.24.1",
"@types/react": "^18.2.56",
"@types/react-dom": "^18.2.19",
"babel-loader": "^9.1.3",
"html-webpack-plugin": "^5.6.0",
"unplugin-stylex": "workspace:*",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
},
"dependencies": {
"@stylexjs/open-props": "^0.5.1",
"@stylexjs/stylex": "^0.5.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
25 changes: 25 additions & 0 deletions examples/webpack-example/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>StyleX with Webpack</title>
<style>
@layer reset {
body {
box-sizing: border-box;
padding: 0;
margin: 0;
}
}
</style>
<link rel="stylesheet" href="stylex.css">
</head>

<body>
<div id="root"></div>
</body>

</html>
39 changes: 39 additions & 0 deletions examples/webpack-example/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react'
import { createRoot } from 'react-dom/client'
import * as stylex from '@stylexjs/stylex'
import { colors } from '@stylexjs/open-props/lib/colors.stylex'
import { sizes } from '@stylexjs/open-props/lib/sizes.stylex'
import { fonts } from '@stylexjs/open-props/lib/fonts.stylex'

const styles = stylex.create({
main: {
width: '100vw',
height: '100vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: colors.pink7,
},
card: {
backgroundColor: colors.blue9,
padding: sizes.spacing5,
borderRadius: sizes.spacing2,
justifyContent: 'center',
display: 'flex',
alignItems: 'center',
color: colors.gray0,
fontFamily: fonts.mono,
},
})

function App() {
return (
<div {...stylex.props(styles.main)}>
<div {...stylex.props(styles.card)}>
<span>Blue rounded rectangle</span>
</div>
</div>
)
}

createRoot(document.getElementById('root')).render(<App />)
61 changes: 61 additions & 0 deletions examples/webpack-example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { default: stylexWebpackPlugin } = require('unplugin-stylex/webpack')

const isDev = process.env.NODE_ENV === 'development'

module.exports = {
context: __dirname,
mode: isDev ? 'development' : 'production',
target: 'web',
cache: true,
entry: {
main: './src/index.jsx',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react'
],
},
},
},
],
},
devServer: {
hot: true,
port: 8080,
},
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000
},
plugins: [
stylexWebpackPlugin({
dev: true,
stylex: {
useCSSLayers: true,
},
}),
new HtmlWebpackPlugin({
template: 'public/index.html',
}),
],
}
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eryue0220/unplugin-stylex",
"version": "0.2.4",
"version": "0.0.3",
"exports": {
"./index": "./src/index.ts",
"./esbuild": "./src/esbuild.ts",
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
"require": "./dist/vite.cjs",
"import": "./dist/vite.js"
},
"./webpack": {
"types": "./dist/webpack.d.ts",
"require": "./dist/webpack.cjs",
"import": "./dist/webpack.js"
},
"./types": {
"types": "./dist/types.d.ts",
"import": "./dist/types.js",
Expand Down Expand Up @@ -75,7 +80,7 @@
"@babel/plugin-syntax-typescript": "^7.23.3",
"@rollup/pluginutils": "^5.1.0",
"@stylexjs/babel-plugin": "^0.5.1",
"unplugin": "^1.7.1"
"unplugin": "^1.10.0"
},
"devDependencies": {
"@stylistic/eslint-plugin-ts": "^1.6.2",
Expand Down
9 changes: 5 additions & 4 deletions pnpm-lock.yaml

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

10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined>
return {
name: PLUGIN_NAME,

transformInclude(id) {
// webpack will contain these files, which will occur errors
const invalidExts = ['.json', '.html', '.jade', '.json5']
const extname = path.extname(id)
// for handle vite
const questionMarkIndex = extname.indexOf('?')
const validExtName = questionMarkIndex > -1 ? extname.slice(0, questionMarkIndex) : extname
return !invalidExts.includes(validExtName)
},

async transform(code, id) {
const dir = path.dirname(id)
const basename = path.basename(id)
Expand Down
26 changes: 18 additions & 8 deletions src/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
* @module
*/

import { isDevelopment } from './core/constants'
import type { UnpluginStylexOptions } from './types'
import { createWebpackPlugin } from 'unplugin'
import type { WebpackPluginInstance } from 'unplugin'
import { unpluginFactory } from './index'
import type { UnpluginStylexInstance } from './types'

const webpackPlugin: UnpluginStylexInstance<WebpackPluginInstance> = createWebpackPlugin(unpluginFactory)

/**
* Note: Current, please use @stylexjs/webpack-plugin
* Webpack plugin
*
* @example
*
* import stylexPlugin from 'unplugin-stylex/webpack'
*
* module.exports = {
* plugins: [
* stylexPlugin(),
* ],
* }
*/
export default (options?: UnpluginStylexOptions) => {
if (isDevelopment || options?.dev) {
throw new Error('If you want to use this plugin through webpack, please use "@stylexjs/webpack-plugin" instead')
}
}
export default webpackPlugin
5 changes: 0 additions & 5 deletions test/__snapshots__/rollup.test.ts.snap

This file was deleted.

7 changes: 0 additions & 7 deletions test/__snapshots__/webpack.test.ts.snap

This file was deleted.

7 changes: 0 additions & 7 deletions test/rollup.test.ts

This file was deleted.

7 changes: 0 additions & 7 deletions test/webpack.test.ts

This file was deleted.