Skip to content

Commit

Permalink
feat: add rsbuild import asset as string example (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Jan 27, 2025
1 parent 407004c commit 2c5eb02
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 5 deletions.
35 changes: 30 additions & 5 deletions pnpm-lock.yaml

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

13 changes: 13 additions & 0 deletions rsbuild/query-raw/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Local
.DS_Store
*.local
*.log*

# Dist
node_modules
dist/

# IDE
.vscode/*
!.vscode/extensions.json
.idea
29 changes: 29 additions & 0 deletions rsbuild/query-raw/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Rsbuild Project

## Setup

Install the dependencies:

```bash
pnpm install
```

## Get Started

Start the dev server:

```bash
pnpm dev
```

Build the app for production:

```bash
pnpm build
```

Preview the production build locally:

```bash
pnpm preview
```
21 changes: 21 additions & 0 deletions rsbuild/query-raw/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "rsbuild-query-raw",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "rsbuild build",
"dev": "rsbuild dev --open",
"preview": "rsbuild preview"
},
"dependencies": {
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"@rsbuild/core": "1.2.3",
"@rsbuild/plugin-react": "^1.1.0",
"@types/react": "^19.0.8",
"@types/react-dom": "^19.0.3",
"typescript": "^5.7.3"
}
}
19 changes: 19 additions & 0 deletions rsbuild/query-raw/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
plugins: [pluginReact()],
tools: {
rspack: (config, { rspack }) => {
config.module?.rules?.push({
resourceQuery: /raw/,
type: 'asset/source',
});
config.plugins?.push(
new rspack.NormalModuleReplacementPlugin(/\?raw$/, (resource) => {
resource.request = '!' + resource.request;
}),
);
},
},
});
26 changes: 26 additions & 0 deletions rsbuild/query-raw/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
body {
margin: 0;
color: #fff;
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
background-image: linear-gradient(to bottom, #020917, #101725);
}

.content {
display: flex;
min-height: 100vh;
line-height: 1.1;
text-align: center;
flex-direction: column;
justify-content: center;
}

.content h1 {
font-size: 3.6rem;
font-weight: 700;
}

.content p {
font-size: 1.2rem;
font-weight: 400;
opacity: 0.5;
}
16 changes: 16 additions & 0 deletions rsbuild/query-raw/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import cssStr from './App.css?raw';
import jsonStr from '../package.json?raw';

console.log('cssStr', cssStr);
console.log('jsonStr', jsonStr);

const App = () => {
return (
<div className="content">
<h1>Rsbuild with React</h1>
<p>Start building amazing things with Rsbuild.</p>
</div>
);
};

export default App;
6 changes: 6 additions & 0 deletions rsbuild/query-raw/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference types="@rsbuild/core/types" />

declare module '*?raw' {
const content: string;
export default content;
}
10 changes: 10 additions & 0 deletions rsbuild/query-raw/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
14 changes: 14 additions & 0 deletions rsbuild/query-raw/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["DOM", "ES2020"],
"module": "ESNext",
"jsx": "react-jsx",
"strict": true,
"skipLibCheck": true,
"isolatedModules": true,
"resolveJsonModule": true,
"moduleResolution": "bundler"
},
"include": ["src"]
}

0 comments on commit 2c5eb02

Please sign in to comment.