-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
536 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "foo", | ||
"items": [1, 2] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,3 @@ | ||
import type React from 'react'; | ||
import { CounterButton } from './components/CounterButton'; | ||
import { useCounter } from './useCounter'; | ||
import './index.scss'; | ||
import { name } from './example.json'; | ||
|
||
export const Counter: React.FC = () => { | ||
const { count, increment, decrement } = useCounter(); | ||
|
||
return ( | ||
<div> | ||
<h1 className="counter-title">React</h1> | ||
<h2 className="counter-text">Counter: {count}</h2> | ||
<CounterButton onClick={decrement} label="-" /> | ||
<CounterButton onClick={increment} label="+" /> | ||
</div> | ||
); | ||
}; | ||
console.log(name); // 'foo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "foo", | ||
"items": [1, 2] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,3 @@ | ||
import type React from 'react'; | ||
import { CounterButton } from './components/CounterButton'; | ||
import { useCounter } from './useCounter'; | ||
import './index.scss'; | ||
import { name } from './example.json'; | ||
|
||
export const Counter: React.FC = () => { | ||
const { count, increment, decrement } = useCounter(); | ||
|
||
return ( | ||
<div> | ||
<h1 className="counter-title">React</h1> | ||
<h2 className="counter-text">Counter: {count}</h2> | ||
<CounterButton onClick={decrement} label="-" /> | ||
<CounterButton onClick={increment} label="+" /> | ||
</div> | ||
); | ||
}; | ||
console.log(name); // 'foo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import a from './assets/data.json'; | ||
import { bar, foo } from './assets/data.json'; | ||
|
||
a; | ||
console.log(foo); | ||
console.log(bar); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# 引用 JSON 文件 | ||
|
||
Rslib 支持在代码中引用 JSON 文件。 | ||
|
||
## JSON 文件 | ||
|
||
你可以直接在 JavaScript 文件中引用 JSON 文件。 | ||
|
||
### 默认引用 | ||
|
||
```json title="example.json" | ||
{ | ||
"name": "foo", | ||
"items": [1, 2] | ||
} | ||
``` | ||
|
||
```js title="index.js" | ||
import example from './example.json'; | ||
|
||
console.log(example.name); // 'foo'; | ||
console.log(example.items); // [1, 2]; | ||
``` | ||
|
||
:::warning | ||
|
||
在 Bundle 模式下,JSON 文件支持默认引用和具名引用。 | ||
|
||
在 Bundleless 模式下,JSON 文件仅支持具名引用。 | ||
|
||
::: | ||
|
||
|
||
### 具名引用 | ||
|
||
Rslib 同样支持通过 named import 来引用 JSON 文件: | ||
|
||
下面是一个使用示例,假设源码如下: | ||
|
||
import { Tabs, Tab } from '@theme'; | ||
|
||
<Tabs> | ||
<Tab label="src/index.ts"> | ||
```js | ||
import { name } from './example.json'; | ||
|
||
console.log(name); // 'foo'; | ||
|
||
``` | ||
</Tab> | ||
<Tab label="src/example.json"> | ||
```json title="example.json" | ||
{ | ||
"name": "foo", | ||
"items": [1, 2] | ||
} | ||
``` | ||
</Tab> | ||
</Tabs> | ||
|
||
会根据配置文件中的 [产物结构](/guide/basic/output-structure) 配置,打包出如下产物: | ||
|
||
<Tabs> | ||
<Tab label="Bundle"> | ||
<Tabs> | ||
<Tab label="dist/index.js"> | ||
```tsx | ||
var example_namespaceObject = { | ||
"name": "foo" | ||
}; | ||
console.log(example_namespaceObject.name); | ||
``` | ||
|
||
</Tab> | ||
</Tabs> | ||
</Tab> | ||
|
||
<Tab label="Bundleless"> | ||
|
||
<Tabs> | ||
<Tab label="dist/index.js"> | ||
|
||
```tsx | ||
import * as example from './example.js'; | ||
|
||
console.log(example.name); | ||
``` | ||
|
||
</Tab> | ||
<Tab label="dist/example.js"> | ||
```tsx | ||
var example_namespaceObject = JSON.parse('{"name":"foo","items":[1,2]}'); | ||
var __webpack_exports__items = example_namespaceObject.items; | ||
var __webpack_exports__name = example_namespaceObject.name; | ||
export { __webpack_exports__items as items, __webpack_exports__name as name }; | ||
``` | ||
</Tab> | ||
</Tabs> | ||
</Tab> | ||
|
||
</Tabs> |
Oops, something went wrong.