Skip to content

Commit 3f29ab3

Browse files
committed
feat: 改造为使用TypeScript开发
1 parent 60cfe9d commit 3f29ab3

File tree

10 files changed

+444
-39
lines changed

10 files changed

+444
-39
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ npm install
4040
{
4141
"name": "userscript-foo",
4242
"version": "0.0.1",
43-
"main": "index.js",
43+
"main": "index.ts",
4444
"repository": "https://github.com/JSREI/userscript-template.git",
4545
"scripts": {
4646
// ...
@@ -99,7 +99,7 @@ npm install
9999
npm add xxx
100100
```
101101

102-
但是需要注意,这些依赖最终会被打包进`./dist/index.js`,而这个文件是不适合太大的,所以尽可能不要引用太多的第三方库。
102+
但是需要注意,这些依赖最终会被打包进`./dist/index.ts`,而这个文件是不适合太大的,所以尽可能不要引用太多的第三方库。
103103

104104
同时你现在可以在`src`目录下以模块化的方式组织你的代码,而不必像之前那样来回上下拉扯一个几千行的`JavaScript`文件(单文件开发那简直是一种对脑力的摧残...):
105105

@@ -111,17 +111,17 @@ npm add xxx
111111
npm run watch
112112
```
113113

114-
然后把`./dist/index.js`中的文件头复制到你的油猴中:
114+
然后把`./dist/index.ts`中的文件头复制到你的油猴中:
115115

116116
![image-20230817000716664](README.assets/image-20230817000716664.png)
117117

118-
并在最后添加一行引入编译后的文件,注意这个`file://`后面的地址是指向你的编译后的`./dist/index.js`的绝对路径:
118+
并在最后添加一行引入编译后的文件,注意这个`file://`后面的地址是指向你的编译后的`./dist/index.ts`的绝对路径:
119119

120120
```js
121-
// @require file://D:/workspace/userscript-template/dist/index.js
121+
// @require file://D:/workspace/userscript-template/dist/index.ts
122122
```
123123

124-
比如下面是一个开发时使用的油猴脚本的实际例子,油猴中没有实际代码,而是使用`require`指向我们`build`后的文件,这样当修改完代码`webpack`自动热编译的时候浏览器中引用的`./dist/index.js`也是最新的:
124+
比如下面是一个开发时使用的油猴脚本的实际例子,油猴中没有实际代码,而是使用`require`指向我们`build`后的文件,这样当修改完代码`webpack`自动热编译的时候浏览器中引用的`./dist/index.ts`也是最新的:
125125

126126
```js
127127
// ==UserScript==
@@ -133,12 +133,12 @@ npm run watch
133133
// @author CC11001100 <CC11001100@qq.com>
134134
// @match *://*/*
135135
// @run-at document-start
136-
// @require file://D:/workspace/userscript-template/dist/index.js
136+
// @require file://D:/workspace/userscript-template/dist/index.ts
137137
// ==/UserScript==
138138
(() => {})();
139139
```
140140

141-
注意,当你使用`// @require file://D:/workspace/userscript-template/dist/index.js`这种方式来调试的时候,你的油猴插件应该配置了允许访问文件网址(默认情况下是不允许的),在浏览器插件图标上右键,选择”管理扩展程序“:
141+
注意,当你使用`// @require file://D:/workspace/userscript-template/dist/index.ts`这种方式来调试的时候,你的油猴插件应该配置了允许访问文件网址(默认情况下是不允许的),在浏览器插件图标上右键,选择”管理扩展程序“:
142142

143143
![image-20240723005213833](./README.assets/image-20240723005213833.png)
144144

@@ -154,7 +154,7 @@ npm run watch
154154
npm run build
155155
```
156156

157-
然后把`./dist/index.js`文件拿去发布即可。
157+
然后把`./dist/index.ts`文件拿去发布即可。
158158

159159

160160

README_en.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Then, modify the `package.json` file, replacing the relevant fields with your ow
5555
{
5656
"name": "userscript-foo",
5757
"version": "0.0.1",
58-
"main": "index.js",
58+
"main": "index.ts",
5959
"repository": "https://github.com/JSREI/userscript-template.git",
6060
"scripts": {
6161
// ...
@@ -114,7 +114,7 @@ Then you can happily start coding. While writing code, you can use the `npm` com
114114
npm add xxx
115115
```
116116

117-
However, it is important to note that these dependencies will ultimately be packed into `./dist/index.js`, and this file should not be too large, so try to avoid referencing too many third-party libraries.
117+
However, it is important to note that these dependencies will ultimately be packed into `./dist/index.ts`, and this file should not be too large, so try to avoid referencing too many third-party libraries.
118118

119119
At the same time, you can now organize your code in a modular way under the `src` directory, instead of struggling with a single `JavaScript` file that is thousands of lines long as before (single-file development is simply a form of mental torture...):
120120

@@ -126,17 +126,17 @@ When you need to test, execute:
126126
npm run watch
127127
```
128128

129-
Then copy the file header from `./dist/index.js` to your Tampermonkey extension:
129+
Then copy the file header from `./dist/index.ts` to your Tampermonkey extension:
130130

131131
![image-20230817000716664](README.assets/image-20230817000716664.png)
132132

133-
And at the end, add a line to import the compiled file, noting that the `file://` followed by the address points to the absolute path of your compiled `./dist/index.js`:
133+
And at the end, add a line to import the compiled file, noting that the `file://` followed by the address points to the absolute path of your compiled `./dist/index.ts`:
134134

135135
```js
136-
// @require file://D:/workspace/userscript-template/dist/index.js
136+
// @require file://D:/workspace/userscript-template/dist/index.ts
137137
```
138138

139-
For example, here is an actual example of a Tampermonkey script used during development. The Tampermonkey script does not contain actual code but uses `require` to point to our `build` files. This way, when the code is modified and `webpack` automatically hot compiles, the `./dist/index.js` referenced in the browser is also the latest:
139+
For example, here is an actual example of a Tampermonkey script used during development. The Tampermonkey script does not contain actual code but uses `require` to point to our `build` files. This way, when the code is modified and `webpack` automatically hot compiles, the `./dist/index.ts` referenced in the browser is also the latest:
140140

141141
```js
142142
// ==UserScript==
@@ -148,12 +148,12 @@ For example, here is an actual example of a Tampermonkey script used during deve
148148
// @author CC11001100 <CC11001100@qq.com>
149149
// @match *://*/*
150150
// @run-at document-start
151-
// @require file://D:/workspace/userscript-template/dist/index.js
151+
// @require file://D:/workspace/userscript-template/dist/index.ts
152152
// ==/UserScript==
153153
(() => {})();
154154
```
155155

156-
Please note that when you debug using the method `// @require file://D:/workspace/userscript-template/dist/index.js`, your Tampermonkey extension should be configured to allow access to file URLs (by default, it is not allowed). Right-click on the browser plugin icon and select "Manage Extensions":
156+
Please note that when you debug using the method `// @require file://D:/workspace/userscript-template/dist/index.ts`, your Tampermonkey extension should be configured to allow access to file URLs (by default, it is not allowed). Right-click on the browser plugin icon and select "Manage Extensions":
157157

158158
![image-20240723005213833](./README.assets/image-20240723005213833.png)
159159

@@ -169,7 +169,7 @@ When you need to publish:
169169
npm run build
170170
```
171171

172-
Then simply take the `./dist/index.js` file and publish it.
172+
Then simply take the `./dist/index.ts` file and publish it.
173173

174174

175175

0 commit comments

Comments
 (0)