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

SWC plugin #190

Merged
merged 33 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
197722c
Initial commit
guyca Oct 24, 2024
622fffd
Cleanup .gitignore file
guyca Oct 24, 2024
9bbfe5a
Commit before trying out writing the plugin in TS like Claude instruc…
guyca Oct 24, 2024
3d7b8d4
f
guyca Oct 28, 2024
2770d2a
Add demo app
guyca Nov 7, 2024
ad4a5c1
Move the demo app to the SWC plugin folder
guyca Nov 8, 2024
8dae0d6
Commit before fixing compilation errors
guyca Nov 8, 2024
b8fac04
Plugin compiles - doesn't work
guyca Nov 11, 2024
c88daa4
Added logs + commit before switching to unplugin-swc
guyca Nov 12, 2024
87ce345
Provider decorator works!
guyca Nov 12, 2024
afce3ec
@Inject decorator works
guyca Nov 13, 2024
a6712ef
Update lock file
guyca Nov 13, 2024
f6f0726
format package.json + update keywords
guyca Nov 13, 2024
cebb03a
Tests pass locally
guyca Nov 14, 2024
fe516c0
Fix test script
guyca Nov 14, 2024
bca901c
Test inject
guyca Nov 14, 2024
d15ca52
Remove prettier + update title
guyca Nov 14, 2024
424f603
Update demo app readme
guyca Nov 14, 2024
fab257f
Clean up
guyca Nov 15, 2024
327a1f7
Remove debug logs from plugin
guyca Nov 15, 2024
fec073b
f
guyca Nov 15, 2024
d240fc1
Preparations for publish - cleanup dependencies + introduce main file
guyca Nov 16, 2024
43b7e79
version bump
guyca Nov 16, 2024
b81171e
Add index.d.ts + fix prepack script
guyca Nov 16, 2024
aded536
version bump
guyca Nov 16, 2024
320d373
version bump
guyca Nov 16, 2024
4b9c17b
version bump
guyca Nov 16, 2024
07cca33
Switch from ESM syntax to CJS + version bump
guyca Nov 16, 2024
d21b3a7
Maybe fix missing file + version bump
guyca Nov 16, 2024
1e33fc3
Remove prerelease tag after testing locally
guyca Nov 17, 2024
1b27af8
Move @swc/core to peer dependencies
guyca Nov 17, 2024
22bda37
Merge branch 'master' into swcPlugin + version bump
guyca Nov 17, 2024
1d06a66
Documentation
guyca Nov 21, 2024
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
84 changes: 2 additions & 82 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,94 +6,21 @@ node_modules

# Lock files
package-lock.json
# This package-lock is used as cache key and needs to be comitted
!documentation/package-lock.json

############
# iOS
############

# Build generated
dist
ReactNativeExample/ios/build/
ReactNativeExample/ios/DerivedData/

# Various settings
ReactNativeExample/ios/xcuserdata/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
ReactNativeExample/ios/Pods/

############
# Android
############
# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
ReactNativeExample/android/bin/
ReactNativeExample/android/gen/
ReactNativeExample/android/out/

# Gradle files
ReactNativeExample/android/.gradle/
ReactNativeExample/android/build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
ReactNativeExample/android/proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
ReactNativeExample/android/.navigation/

# Android Studio captures folder
ReactNativeExample/android/captures/

# Intellij
*.iml

##################
# React-Native
##################
# OSX
#
.DS_Store

# Xcode
#
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# Android/IJ
#
build/
Expand All @@ -109,15 +36,6 @@ node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore
!debug.keystore

main.jsbundle.map
index.android.bundle.map

.jest_cache
.eslintcache
**/.docusaurus
Expand All @@ -128,3 +46,5 @@ packages/react-obsidian/coverage/
documentation/.yarn/cache
documentation/.yarn/install-state.gz
.vscode/yw_helper_history.txt
packages/swc-plugin-obsidian/target
packages/swc-plugin-obsidian/src/swc_plugin_obsidian.wasm
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"TSES",
"unimported",
"unmagler",
"unplugin",
"unsubscribers"
],
"eslint.ignoreUntitled": true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"scripts": {
"test": "yarn workspaces foreach run test --colors",
"test": "yarn workspaces foreach --exclude swc-plugin-obsidian run test --colors && yarn workspace swc-plugin-obsidian test",
"lint": "yarn workspaces foreach run lint",
"build": "yarn workspace react-obsidian build && yarn workspace eslint-plugin-obsidian build"
},
Expand Down
147 changes: 79 additions & 68 deletions packages/documentation/docs/documentation/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,51 @@ import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Installation
Installing Obsidian is a straightforward process that involves adding the package to your project, enabling TypeScript decorators, and adding Obsidian's transformer to your build system, either Babel or SWC. Let's go through the steps.

Like most Dependency Injection frameworks, Obsidian uses automatic code generation to create the bindings necessary for resolving dependencies. This approach helps reduce the amount of boilerplate code required by developers. Obsidian relies on Babel for code generation, so you'll need to have Babel configured in your project.
## 1. Install Obsidian
Install the package using your preferred package manager:

:::important
If your project uses either **Vite** or **Create React App**, please refer to the [Integration with third-party front-end environments](#integration-with-third-party-front-end-environments) section before following the steps below.
:::
<Tabs>
<TabItem value="yarn" label="Yarn" default>

## 1. Install Obsidian
```shell
yarn add react-obsidian
```

```bash
npm install react-obsidian
```
</TabItem>
<TabItem value="npm" label="NPM" default>

```shell
npm install react-obsidian
```

</TabItem>

</Tabs>

## 2. Install Reflect-metadata
First, install and enable the reflect-metadata polyfill.
```bash
npm install reflect-metadata
```
First, install the `reflect-metadata` package:

<Tabs>
<TabItem value="yarn" label="Yarn" default>

```shell
yarn add reflect-metadata
```

</TabItem>
<TabItem value="npm" label="NPM" default>

```shell
npm install reflect-metadata
```
</TabItem>

</Tabs>

Then, enable the `reflect-metadata` polyfill by adding the following line to the top of your application's entry point (usually index.js or index.ts):

Then, add the following line to the top of your application's entry point (usually index.js or index.ts):
```js
import 'reflect-metadata';
```
Expand All @@ -44,55 +69,50 @@ Add the following options to your tsconfig.json file.
}
```

## 4. Add the required Babel plugins
[BabelJS](https://babeljs.io/) is a JavaScript compiler that is used to transpile modern JavaScript code into code that is compatible with older browsers. Obsidian uses a Babel transformer to generate code that is used to resolve dependencies.
## 4. Add Obsidian's transformer
Like most Dependency Injection frameworks, Obsidian uses automatic code generation to create the bindings necessary for resolving dependencies. This approach helps reduce the amount of boilerplate code required by developers.

<Tabs>
<TabItem value="babel" label="Babel (React Native or Vite + Babel)" default>

### 4.1. Install the required Babel plugins
### Install the required Babel plugins

You will need to install these plugins if you don't have them already:
<Tabs>
<TabItem value="yarn" label="Yarn" default>


```shell
yarn add @babel/plugin-proposal-decorators @babel/plugin-transform-class-properties babel-plugin-parameter-decorator @babel/core @babel/preset-env @babel/preset-typescript
```


</TabItem>
<TabItem value="npm" label="NPM" default>


```shell
npm install @babel/plugin-proposal-decorators @babel/plugin-transform-class-properties babel-plugin-parameter-decorator @babel/core @babel/preset-env
```


</TabItem>

</Tabs>

If this is your first time using Babel, you will also need to install Babel's core packages:

<Tabs>
<TabItem value="yarn" label="Yarn" default>


```shell
yarn add @babel/core @babel/preset-env @babel/preset-typescript
```
</TabItem>
<TabItem value="npm" label="NPM" default>


```shell
npm install @babel/core @babel/preset-env @babel/preset-typescript
```
</TabItem>

</Tabs>

### 4.2. Update your Babel configuration
### Update your Babel configuration

Add the transformer and the required plugins to the list of plugins in your `babel.config.js` file or `.babelrc` file:

Expand All @@ -111,64 +131,55 @@ module.exports = {
};
```

## 5. Optional - Add Obsidian's ESLint plugin
Obsidian provides an ESLint plugin that can help you find errors in your code related to dependency injection. See the [ESLint plugin](/docs/documentation/meta/eslint) documentation for more information.
</TabItem>
<TabItem value="swc" label="SWC (Vite + SWC)">

### Install the required dependencies
<Tabs>
<TabItem value="yarn" label="Yarn" default>

```shell
yarn add -D unplugin-swc @swc/core
```
</TabItem>
<TabItem value="npm" label="NPM" default>

## Integration with third-party front-end environments
### Vite
[Vite](https://vitejs.dev/) provides a modern development environment for React application. It boasts an extremely fast development server that uses Hot Module Replacement (HMR) to enable near-instant startup time.
```shell
npm install -D unplugin-swc @swc/core
```
</TabItem>
</Tabs>

When integrating Obsidian in a Vite application, follow steps **1-3 and step 4.1 that are listed above**. Instead of step 4.2, we'll configure Obsidian't Babel transformer in Vite's `vite.config.js` file:
### Update your Vite configuration
Add the transformer to the list of plugins in your `vite.config.js` file:

```js title="vite.config.js"
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import swc from 'unplugin-swc';
import obsidian from 'swc-plugin-obsidian';

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react({
babel: {
plugins: [
'react-obsidian/dist/transformers/babel-plugin-obsidian',
['@babel/plugin-proposal-decorators', { legacy: true }],
],
swc.vite({
jsc: {
parser: {
syntax: "typescript",
decorators: true,
},
experimental: {
runPluginFirst: true,
plugins: [obsidian()],
},
},
}),
],
});
```

### Create React App
Create React App (CRA) is a popular tool for getting started with React. It provides a pre-configured development environment that is ready to use out of the box.

When integrating Obsidian in a CRA application, follow steps **1-4 that are listed above**. Since CRA doesn't allow you to configure Babel directly, we'll need to install two additional packages:
1. [react-app-rewired](https://github.com/timarney/react-app-rewired). This package allows you to customize CRA scripts without ejecting.
2. [customize-cra](https://github.com/arackaf/customize-cra). This package provides a set of utilities that can be used to customize CRA configurations. It has to be used instead of the default `react-scripts` package.

#### Install the required packages and modify the default scripts
```diff title="package.json"
{
"devDependencies": {
+ "customize-cra": "1.0.0",
+ "react-app-rewired": "2.2.1"
},
"scripts": {
+ "start": "react-app-rewired start",
+ "build": "react-app-rewired build",
+ "test": "npx jest",
- "start": "react-scripts start",
- "build": "react-scripts build",
- "test": "react-scripts test --env=jsdom",
}
}
```
</TabItem>
</Tabs>

#### Configure Babel
Create a `config-overrides.js` file in the root of your project and add the following code to it:
## 5. Optional - Add Obsidian's ESLint plugin
Obsidian provides an ESLint plugin that can help you find errors in your code related to dependency injection. See the [ESLint plugin](/docs/documentation/meta/eslint) documentation for more information.

```js title="config-overrides.js"
const { useBabelRc, override } = require('customize-cra');
module.exports = override(useBabelRc());
```
2 changes: 1 addition & 1 deletion packages/eslint-plugin-obsidian/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "eslint-plugin-obsidian",
"description": "ESLint rules for Obsidian",
"main": "dist/index.js",
"version": "2.12.1",
"version": "2.13.0",
"scripts": {
"build": "npx tsc --project tsconfig.prod.json",
"test": "npx jest",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-obsidian/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-obsidian",
"version": "2.12.1",
"version": "2.13.0",
"description": "Dependency injection framework for React and React Native applications",
"scripts": {
"prepack": "npm run lint && tsc --project tsconfig.prod.json",
Expand Down Expand Up @@ -47,7 +47,7 @@
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-import-newlines": "^1.1.5",
"eslint-plugin-jest-formatting": "^3.1.0",
"eslint-plugin-obsidian": "2.12.1",
"eslint-plugin-obsidian": "2.13.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-unused-imports": "3.1.x",
Expand Down
8 changes: 8 additions & 0 deletions packages/swc-plugin-obsidian/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cSpell.words": [
"debuginfo",
"RUSTFLAGS"
],
"editor.bracketPairColorization.independentColorPoolPerBracketType": false,
"editor.tabSize": 2
}
Loading
Loading