Skip to content

Commit

Permalink
feat: support ssr (#28)
Browse files Browse the repository at this point in the history
* chore: upgrade dependencies

* feat: add new problem

* feat: support ssr

* refactor: refactor some code

* chore: delete ssr output

* fix: theme script place
  • Loading branch information
ZLY201 committed Nov 16, 2023
1 parent 3206cb2 commit 59969c3
Show file tree
Hide file tree
Showing 35 changed files with 1,212 additions and 897 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"node": true,
"browser": true
},
"globals": {
"WEBPACK_IS_SSR": true
},
"plugins": [
"react",
"import",
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ node_modules
.npmignore
.DS_Store
yarn-error.log
output
7 changes: 5 additions & 2 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<!DOCTYPE html>
<html lang="zh-CN">
<html lang='en'>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TypeScript Tutorial Exercises</title>
</head>
<body>
<div id="root"></div>
<script>try{if(JSON.parse(localStorage.getItem('__setting_cache__')).theme==='dark'){document.body.setAttribute('arco-theme','dark');}}catch{}</script>
<div id="root">
<%= ROOT_CONTENT %>
</div>
</body>
</html>
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"reset": "rm -rf node_modules",
"setup": "yarn reset && yarn",
"clean": "rm -rf dist",
"dev": "rspack serve --watch --mode=development",
"build": "yarn clean && rspack build --mode=production",
"dev": "rspack serve --watch",
"build:ssr": "yarn clean && rspack build --config=rspack.ssr.config.ts",
"build": "yarn build:ssr && rspack build && rm -rf dist/ssr",
"lint": "eslint --fix --color --cache --quiet .",
"prepare": "husky install"
},
Expand Down Expand Up @@ -52,6 +53,7 @@
"devDependencies": {
"@arco-plugins/unplugin-react": "^1.0.0-alpha.1",
"@rspack/cli": "^0.3.5",
"@rspack/plugin-html": "^0.3.11",
"@svgr/webpack": "^8.1.0",
"@types/event-emitter": "^0.3.4",
"@types/lodash.debounce": "^4.0.7",
Expand Down
6 changes: 6 additions & 0 deletions problems/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["../.eslintrc.json"],
"rules": {
"compat/compat": "off"
}
}
6 changes: 6 additions & 0 deletions problems/basic-tutorial/2-everyday-types/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ assert<Equal<typeof constant.str, string>>();
// @ts-ignore
assert<Equal<typeof constant.num, number>>();
// @ts-ignore
assert<Equal<typeof constant.big_int, bigint>>();
// @ts-ignore
assert<Equal<typeof constant.bool, boolean>>();
// @ts-ignore
assert<Equal<typeof constant.mySymbol, symbol>>();
// @ts-ignore
assert<Equal<typeof constant.undef, undefined>>();
// @ts-ignore
assert<Equal<typeof constant.arr, number[]>>();
// @ts-ignore
assert<Equal<Parameters<typeof constant.greet>[0], string>>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
```typescript
export const str: string = 'Hello, world';
export const num: number = 42;
export const big_int: bigint = 1n;
export const bool: boolean = true || false;
export const arr: number[] = [1];
export const mySymbol: symbol = Symbol('mySymbol');
export const undef: undefined = undefined;
export const arr: number[] = [1, 2, 3];

export function greet(name: string): void {
return console.log('Hello, ' + name.toUpperCase() + '!!');
Expand All @@ -13,6 +16,7 @@ export function printName(obj: { first: string; last?: string }): string {
return `${last}${first}`;
}

// should be `string` or `number`
export function printId(id: string | number) {
if (typeof id === 'string') {
// In this branch, id is of type 'string'
Expand Down
6 changes: 5 additions & 1 deletion problems/basic-tutorial/2-everyday-types/template.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export const str: unknown = 'Hello, world';
export const num: unknown = 42;
export const big_int: unknown = 1n;
export const bool: unknown = true || false;
export const arr: unknown = [];
export const mySymbol: unknown = Symbol('mySymbol');
export const undef: unknown = undefined;
export const arr: unknown = [1, 2, 3];

export function greet(name: any): unknown {
return console.log('Hello, ' + name.toUpperCase() + '!!');
Expand All @@ -12,6 +15,7 @@ export function printName(obj: { first: unknown; last?: unknown }): unknown {
return `${last}${first}`;
}

// should be `string` or `number`
export function printId(id: unknown) {
if (typeof id === 'string') {
// In this branch, id is of type 'string'
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### What is TypeScript

TypeScript is a syntactic superset of JavaScript which adds static typing.

This basically means that TypeScript adds syntax on top of JavaScript, allowing developers to add types.

### Why TypeScript

JavaScript is a loosely typed language. It can be difficult to understand what types of data are being passed around in JavaScript.

In JavaScript, function parameters and variables don't have any information! So developers need to look at documentation, or guess based on the implementation.

TypeScript allows specifying the types of data being passed around within the code, and has the ability to report errors when the types don't match.

For example, TypeScript will report an error when passing a string into a function that expects a number. JavaScript will not.

### About this site

To teach more people to use TypeScript including basic and advanced usage, we create this site which provides some problems should be solved by TypeScript. And part of the answers of the problems could be found at [official TypeScript document](https://www.typescriptlang.org/).

This problem require you to export a type which equals to string `Hello, world`.

Now, you can go to the editor at right of the page to start your TypeScript travel.

**Reference**

> 1.https://www.typescriptlang.org/
>
> 2.https://www.w3schools.com/typescript/typescript_intro.php
>
> 3.https://github.com/typescript-exercises/typescript-exercises
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
### 关于 TypeScript

TypeScript 是 JavaScript 的语法超集,它添加了静态类型

这意味着 TypeScript 在 JavaScript 之上添加了语法,允许开发人员添加类型

### 为什么选择 TypeScript

JavaScript 是一种松散类型语言。 理解 JavaScript 中传递的数据类型可能很困难

在 JavaScript 中,函数参数和变量没有任何信息!因此开发人员需要查看文档,或者根据实现进行猜测

TypeScript 允许指定代码中传递的数据类型,并且能够在类型不匹配时报告错误

例如,当将字符串传递到需要数字的函数时,TypeScript 将报告错误而 JavaScript 不会

### 关于本站

为了教会更多的人使用 TypeScript(包括基本和高级用法),我们创建了这个网站,根据 [TypeScript 官方文档](https://www.typescriptlang.org/)提供了从入门到精通 TypeScript 的练习题

这个问题要求你导出一个等于字符串`Hello, world`的类型

现在,您可以前往页面右侧的编辑器开始学习 TypeScript

**参考**

> 1.https://www.typescriptlang.org/
>
> 2.https://www.w3schools.com/typescript/typescript_intro.php
>
> 3.https://github.com/typescript-exercises/typescript-exercises
4 changes: 4 additions & 0 deletions problems/basic-tutorial/3-narrowing/docs/description/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import en from './description.en.md?url';
import zhCN from './description.zh-cn.md?url';

export { en, zhCN };
3 changes: 3 additions & 0 deletions problems/basic-tutorial/3-narrowing/docs/tutorial/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import en from './tutorial.en.md?url';

export { en };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```typescript
export type HelloWorld = 'Hello, world';
```
6 changes: 6 additions & 0 deletions problems/basic-tutorial/3-narrowing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import check from './check?url';
import template from './template?url';
import * as description from './docs/description';
import * as tutorial from './docs/tutorial';

export { check, template, tutorial, description };
8 changes: 8 additions & 0 deletions problems/basic-tutorial/3-narrowing/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function padLeft(padding: unknown, input: string) {
if (typeof padding === 'number') {
return ' '.repeat(padding) + input;
} else if (typeof padding === 'string') {
return padding + input;
}
return input;
}
9 changes: 9 additions & 0 deletions problems/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module '*?raw' {
const raw: string;
export default raw;
}

declare module '*?url' {
const url: string;
export default url;
}
10 changes: 10 additions & 0 deletions problems/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"target": "ESNext",
"noImplicitAny": false
},
"include": ["."],
"exclude": []
}
3 changes: 3 additions & 0 deletions problems/type-assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import typeAssertions from '../node_modules/type-assertions/lib/index.d.ts?raw';

export default typeAssertions;
76 changes: 76 additions & 0 deletions rspack.base.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import path from 'path';
import type { Configuration } from '@rspack/cli';

export default function createBaseRspackConfig(): Configuration {
return {
context: __dirname,
entry: {
main: './src/main.tsx',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash:8].bundle.js',
chunkFilename: '[name].[contenthash:8].bundle.js',
cssChunkFilename: '[name].[contenthash:8].bundle.js',
},
resolve: {
alias: {
'@config': path.resolve(__dirname, './config'),
'@problems': path.resolve(__dirname, './problems'),
'@src': path.resolve(__dirname, './src'),
},
},
builtins: {
css: {
modules: {
localIdentName: '[path][name]__[local]--[hash:6]',
},
},
},
module: {
rules: [
{
resourceQuery: /url$/,
type: 'asset/resource',
},
{
resourceQuery: /raw$/,
type: 'asset/source',
},
{
test: /\.less$/i,
use: [
{
loader: 'less-loader',
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
type: 'css',
},
{
test: /\.module\.less$/i,
use: [
{
loader: 'less-loader',
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
type: 'css/module',
},
{
test: /\.svg$/,
issuer: /\.[jt]sx?$/,
use: ['@svgr/webpack'],
},
],
},
};
}
Loading

0 comments on commit 59969c3

Please sign in to comment.