Skip to content

Commit f94fc6e

Browse files
committed
ref!: new rollup template
1 parent 063fb9e commit f94fc6e

32 files changed

+2308
-3700
lines changed

.build.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"sizeKiB":8.09,"mode":"development","timestamp":1713129529244}

.env.template

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
# whether to trigger a bell sound in the terminal when the build is finished - defaults to false if empty, invalid or unset
2-
RING_BELL=false
3-
4-
# HTTP port of the dev server where the userscript will be served (when using `npm run watch`) - defaults to 8710 if empty, invalid (NaN or <1024) or unset
1+
# HTTP port of the dev server where the userscript will be served (when using `npm run dev`) - defaults to 8710 if empty
52
DEV_SERVER_PORT=8710
63

7-
# whether to log dev server requests to the console - defaults to false if empty, invalid or unset
8-
DEV_SERVER_LOG=false
4+
# Whether to trigger the bell sound in some terminals when the code has finished (re)compiling - default is false
5+
RING_BELL=false

.eslintrc.cjs

+21-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ module.exports = {
66
},
77
ignorePatterns: [
88
"*.min.*",
9-
"webpack.config.js",
109
"*.user.js",
1110
"*.map",
1211
"dist/**",
12+
"test.ts",
1313
],
1414
extends: [
1515
"eslint:recommended",
@@ -19,10 +19,15 @@ module.exports = {
1919
Atomics: "readonly",
2020
SharedArrayBuffer: "readonly",
2121
GM: "readonly",
22+
unsafeWindow: "writable",
2223
},
2324
parser: "@typescript-eslint/parser",
2425
parserOptions: {
2526
ecmaVersion: "latest",
27+
ecmaFeatures: {
28+
jsx: true,
29+
},
30+
sourceType: "module",
2631
},
2732
plugins: [
2833
"@typescript-eslint",
@@ -33,18 +38,32 @@ module.exports = {
3338
"semi": [ "error", "always" ],
3439
"eol-last": [ "error", "always" ],
3540
"no-async-promise-executor": "off",
41+
"no-cond-assign": "off",
3642
"indent": ["error", 2, { "ignoredNodes": ["VariableDeclaration[declarations.length=0]"] }],
3743
"@typescript-eslint/no-non-null-assertion": "off",
3844
"@typescript-eslint/no-unused-vars": ["warn", { "ignoreRestSiblings": true, "argsIgnorePattern": "^_" }],
3945
"@typescript-eslint/ban-ts-comment": "off",
46+
"@typescript-eslint/ban-types": ["error", {
47+
types: {
48+
"{}": false,
49+
},
50+
extendDefaults: true,
51+
}],
52+
"@typescript-eslint/no-explicit-any": "off",
4053
"comma-dangle": ["error", "only-multiline"],
4154
"no-misleading-character-class": "off",
4255
},
4356
overrides: [
4457
{
45-
files: ["**.js"],
58+
files: ["**.js", "**.mjs", "**.cjs"],
4659
rules: {
4760
"@typescript-eslint/no-var-requires": "off",
61+
"quotes": [ "error", "double" ],
62+
"semi": [ "error", "always" ],
63+
"eol-last": [ "error", "always" ],
64+
"no-async-promise-executor": "off",
65+
"indent": ["error", 2, { "ignoredNodes": ["VariableDeclaration[declarations.length=0]"] }],
66+
"comma-dangle": ["error", "only-multiline"],
4867
},
4968
},
5069
],

.github/workflows/lint.yml

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
name: "Lint"
22

33
on:
4+
workflow_dispatch:
45
pull_request:
5-
branches: [main] # runs on every push
6+
branches: [main, develop]
7+
push:
8+
branches: [develop]
69

710
jobs:
811
lint:
912
name: Lint
1013
runs-on: ubuntu-latest
1114

12-
timeout-minutes: 10
15+
timeout-minutes: 5
1316

1417
strategy:
1518
matrix:
16-
node-version: [18.x]
19+
node-version: [20.x]
20+
21+
env:
22+
CI: "true"
1723

1824
steps:
19-
- uses: actions/checkout@v3 # checkout latest commit
20-
- name: Use Node.js ${{ matrix.node-version }} # set up Node.js
21-
uses: actions/setup-node@v2
25+
- uses: actions/checkout@v3
26+
- name: Set up Node.js v${{ matrix.node-version }}
27+
uses: actions/setup-node@v3
2228
with:
2329
node-version: ${{ matrix.node-version }}
24-
- name: Clean install of dependencies # runs the npm ci command to install all dependencies
30+
- name: Install dependencies # runs the npm ci command to install from package-lock.json
2531
run: npm ci
26-
env:
27-
CI: "true"
28-
- name: Lint # runs ESLint
32+
- name: Build with tsc & lint
2933
run: npm run lint
30-
env:
31-
CI: "true"

.gitignore

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
.env
2-
test.js
3-
test.ts
4-
52
node_modules/
6-
dist/*.map
7-
dist/out/
8-
dist/*.css
3+
dist/
4+
!dist/*.user.js

.vscode/extensions.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"recommendations": [
3+
"andywang.vscode-scriptmonkey",
4+
"dbaeumer.vscode-eslint",
5+
"fabiospampinato.vscode-highlight",
6+
],
7+
"unwantedRecommendations": [
8+
"adpyke.vscode-userscript",
9+
],
10+
}

.vscode/settings.json

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"javascript.preferences.importModuleSpecifier": "relative",
3+
"typescript.tsdk": "node_modules/typescript/lib",
4+
5+
"search.exclude": {
6+
"**/*.user.js": true,
7+
},
8+
"files.associations": {
9+
"*.env": "dotenv",
10+
"*.env.template": "dotenv",
11+
},
12+
"editor.tabSize": 2,
13+
14+
// requires extension: fabiospampinato.vscode-highlight
15+
"highlight.regexes": {
16+
"(TODO(\\((\\s|\\d|\\w|[,.-_+*&])+\\))?:?)": { // TODO: or TODO or TODO(xy): but not todo or todo:
17+
"backgroundColor": "#ed0",
18+
"color": "black",
19+
"overviewRulerColor": "#ed0",
20+
},
21+
"((//\\s?)?#region ([^\\S\\r\\n]*[\\(\\)\\w,.\\-_&+#*'\"]+)*[:]*)": { //#region test (abc):
22+
"backgroundColor": "#069",
23+
"color": "#fff",
24+
"overviewRulerColor": "#069",
25+
},
26+
"((<!--\\s*)?</?\\{\\{[A-Z_-]+\\}\\}>(\\s*-->)?)": { // <!-- <{{FOO}}> --> and <!-- </{{FOO}}> --> or <{{BAR}}> and </{{BAR}}>
27+
"backgroundColor": "#9af",
28+
"overviewRulerColor": "#9af",
29+
"color": "#000",
30+
},
31+
"(#?(DEBUG|DBG)#?)": { // #DEBUG or DEBUG or #DBG or #DBG#
32+
"backgroundColor": "#ff0",
33+
"color": "blue",
34+
"overviewRulerColor": "#ff0",
35+
},
36+
"(IMPORTANT:)": { // IMPORTANT:
37+
"backgroundColor": "#a22",
38+
"color": "#fff",
39+
},
40+
"(FIXME:)": { // FIXME:
41+
"backgroundColor": "#a22",
42+
"color": "#fff",
43+
"overviewRulerColor": "#752020",
44+
},
45+
},
46+
}
47+

README.md

+92-18
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
<h1><img alt="icon" src="./assets/icon.png"><br>Userscript.ts</h1>
44

5-
Typescript ESM template for making [userscripts](https://en.wikipedia.org/wiki/Userscript) that supports importing and parsing HTML, CSS, Markdown and misc. files directly in code, packing it all up with webpack and applying custom injections for the userscript header and more.
5+
Extensive Typescript ESNext template and boilerplate for [userscripts](https://en.wikipedia.org/wiki/Userscript).
6+
Supports importing and parsing HTML and Markdown files directly in code, packing it all up with rollup and applying custom injections for the userscript header and more.
67
It also offers ESLint to lint and auto-fix the code and GitHub Actions with ESLint to lint the code in pull requests and CodeQL to check it for vulnerabilities on every push.
8+
Intended to be used in conjunction with a Git repo as a resource CDN and build versioning system for the userscript.
79

810
Like this template? Please consider [supporting the development ❤️](https://github.com/sponsors/Sv443)
911

@@ -24,26 +26,99 @@ Like this template? Please consider [supporting the development ❤️](https://
2426

2527
## First steps:
2628
1. Copy `.env.template` to `.env` and change the values inside if needed
27-
2. Inside `package.json`, update the properties `name`, `userscriptName`, `description`, `homepage`, `author`, `license` and `repository.url`
29+
2. Search for `#REPLACE:` with your IDE in the entire project and replace all placeholders with your own values
2830
3. Replace the icon at `assets/icon.png` with your own or use [Google's or DuckDuckGo's favicon API](https://codepen.io/djekl/pen/QWKNNjv) in the userscript header
2931
4. Replace the LICENSE.txt file with your own license or remove it if you want your code to be "all rights reserved"
30-
5. Modify the variables and userscript header inside `src/tools/post-build.ts` to whatever you need (see also [GM header reference](https://wiki.greasespot.net/Metadata_Block))
32+
5. Modify the userscript header inside `src/tools/post-build.ts` to whatever you need (see also [GM header reference](https://wiki.greasespot.net/Metadata_Block))
3133
6. The eslint configuration at `.eslintrc.cjs` is what I use, feel free to remove rules if there are too many or modify them to your preferences
32-
7. Add your own initialization functions to `init()` and `onLoad()` inside the entrypoint file at `src/index.ts` (read the comments for more info)
33-
8. Remove the example HTML, CSS and TS files
34-
9. Refer to [commands](#commands) and [development](#development-tips-and-notes)
34+
7. Add your own initialization functions to `init()` and `run()` inside the entrypoint file at `src/index.ts` (read the comments for more info)
35+
8. Refer to the sections [project structure](#project-structure), [commands](#commands) and [development](#development-tips-and-notes) next
36+
37+
<br>
38+
39+
## Project structure:
40+
- **`assets/`**
41+
Contains all the assets that are used in the userscript, like images, stylesheets, and any other misc files.
42+
- **`assets/require.json`**
43+
Contains a list of all modules that should be loaded using the `@require` directive.
44+
Props:
45+
- `pkgName` (required) - The npm name of the package
46+
- `baseUrl` - The base URL of the CDN to load from (defaults to `https://cdn.jsdelivr.net/npm/`)
47+
- `path` - The path to the file inside the package (uses jsdelivr's standard path by default)
48+
- **`assets/resources.json`**
49+
Contains a list of all resources that should be loaded using the `@resource` directive.
50+
This is useful for loading files like stylesheets, images, and other assets that should not be included in the code bundle, but loaded in and cached by the userscript extension.
51+
The object's key is used with the `getResourceUrl()` function in `src/utils.ts` to get the URL of the resource. The value is the path to the file inside the `assets/` folder.
52+
If the path starts with a slash, it will be resolved relative to the root of the project (where the `package.json` file is located).
53+
- **`src/`**
54+
Contains all the source files for the userscript.
55+
- **`src/tools/`**
56+
Contains all scripts that are used in the build process.
57+
- **`src/tools/post-build.ts`**
58+
Contains the post-build script that is run after the userscript is built.
59+
This script inserts the userscript header and other custom injections into the final userscript file.
60+
If you need to modify userscript directives or add custom injections, this is the place to do it.
61+
- **`src/tools/serve.ts`**
62+
Contains the development server that serves the userscript on port 8710 (by default).
63+
- **`src/index.ts`**
64+
The entrypoint for the userscript. This is where you should call your own code from.
65+
- **`src/config.ts`**
66+
This file contains the DataStore instance that should be used to hold your userscript's configuration object.
67+
The DataStore class is really powerful and does a lot of the heavy lifting for you. More instances can also be created to hold different types of data that should be persisted between sessions.
68+
For more info, please read the [DataStore documentation.](https://github.com/Sv443-Network/UserUtils#datastore)
69+
- **`src/constants.ts`**
70+
Contains some constant variables that are used throughout the userscript's runtime code.
71+
This is where arguments are injected into the userscript's runtime code by `tools/post-build.ts`
72+
- **`src/declarations.d.ts`**
73+
The declarations in this file allow you to import HTML and MD files directly in your code, using rollup plugins.
74+
- **`src/observers.ts`**
75+
This file should be filled up with many `SelectorObserver` instances. These observe a designated part of the DOM for changes.
76+
With the function `addSelectorListener()`, you can then add a listener, given a specific CSS selector, that gets called when the selector has been found in the DOM.
77+
For more info, please read the [SelectorObserver documentation.](https://github.com/Sv443-Network/UserUtils#selectorobserver)
78+
- **`src/types.ts`**
79+
Contains all generic TypeScript types that are used in multiple files.
80+
- **`src/utils.ts`**
81+
Contains utility functions that are used throughout the userscript's runtime code.
82+
This is where you should put functions that are used in multiple places in your code.
83+
Once it gets too big you should split it up into multiple files.
84+
3585

3686
<br>
3787

3888
## Commands:
39-
| Command | Description |
40-
| --- | --- |
41-
| `npm i` | Run once to install dependencies |
42-
| `npm run lint` | Lints the userscript with ESLint |
43-
| `npm run build-dev` | Builds the userscript in development mode (sourcemaps enabled) |
44-
| `npm run build-prod` | Builds the userscript in production mode (optimized code, sourcemaps disabled) |
45-
| `npm run dev` | Watches, rebuilds and serves the userscript so it can be updated live ([more info below](#development-tips-and-notes)) |
46-
<!-- first column uses non-breaking space U+00A0 (' ') -->
89+
- **`npm i`**
90+
Run once to install dependencies
91+
- **`npm run dev`**
92+
This is the command you want to use to locally develop and test your script.
93+
It watches for any changes, then rebuilds and serves the userscript on port 8710, so it can be updated live if set up correctly in the userscript manager (see [development tips](#development-tips-and-notes)).
94+
Once it has finished building, a link will be printed to the console. Open it to install the userscript.
95+
You can also configure request logging and more in `.env` and `src/tools/serve.ts`, just make sure to restart the dev server after changing anything.
96+
- **`npm run build-prod`**
97+
Builds the userscript for production for all host platforms (GitHub, GreasyFork and OpenUserJS) with their respective options already set.
98+
Outputs the files using a suffix predefined in the `package.json` file.
99+
Use this to build the userscript for distribution on all hosts.
100+
- **`npm run build -- <arguments>`**
101+
Builds the userscript with custom options
102+
Arguments:
103+
- `--config-mode=<value>` - The mode to build in. Can be either `production` or `development` (default)
104+
- `--config-branch=<value>` - The GitHub branch to target. Can be any branch name, but should be `main` for production and `develop` for development (default)
105+
- `--config-host=<value>` - The host to build for. Can be either `github` (default), `greasyfork` or `openuserjs`
106+
- `--config-assetSource=<value>` - Where to get the resource files from. Can be either `local` or `github` (default)
107+
- `--config-suffix=<value>` - Suffix to add just before the `.user.js` extension. Defaults to an empty string
108+
109+
Shorthand commands:
110+
- `npm run build-prod-base` - Sets `--config-mode=production` and `--config-branch=main`
111+
Used for building for production, targeting the main branch
112+
- `npm run build-develop` - Sets `--config-mode=development`, `--config-branch=develop` and `--config-assetSource=github`
113+
Used for building for experimental versions, targeting the develop branch
114+
- **`npm run lint`**
115+
Builds the userscript with the TypeScript compiler and lints it with ESLint. Doesn't verify *all* of the functionality of the script, only syntax and TypeScript errors!
116+
- **`npm run --silent invisible -- "<command>"`**
117+
Runs the passed command as a detached child process without giving any console output.
118+
Remove `--silent` to see npm's info and error messages.
119+
- **`npm run node-ts -- <path>`**
120+
Runs the TypeScript file at the given path using the regular node binary and the node-ts loader.
121+
Also enables source map support and disables experimental warnings.
47122

48123
<br>
49124

@@ -65,9 +140,8 @@ Like this template? Please consider [supporting the development ❤️](https://
65140
Make sure to check out and follow their rules and guidelines before publishing.
66141
- Use an IDE like [VS Code](https://code.visualstudio.com/) so Intellisense and Typescript can work together to give you really awesome code completion and warn you about potential runtime errors before you even build the code.
67142
- If you are using VS Code, install the ESLint extension ([`dbaeumer.vscode-eslint`](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)) and bind a hotkey for the `ESLint: Fix all auto-fixable problems` command so you can quickly format the currently active file according to the rules in `.eslintrc.cjs`
68-
- Try to get familiar with the [webpack config](https://webpack.js.org/configuration/) at `webpack.config.js`
69-
In there you may add and configure webpack plugins and configure the build process.
70-
- To see an example of how the code in `src/index.ts` will be packed in production mode, check out the file at [`dist/EXAMPLE.user.js`](./dist/EXAMPLE.user.js)
143+
- Try to get familiar with the [rollup config](https://rollupjs.org/configuration-options/) at `rollup.config.mjs`
144+
In there you may add and configure rollup plugins and configure the build process.
71145

72146
<br><br><br>
73147

@@ -76,6 +150,6 @@ Like this template? Please consider [supporting the development ❤️](https://
76150
Made with ❤️ by [Sv443](https://github.com/Sv443)
77151
If you like this template, please consider [supporting me](https://github.com/sponsors/Sv443)
78152

79-
© 2023 Sv443 - [WTFPL license](./LICENSE.txt)
153+
© 2024 Sv443 - [WTFPL license](./LICENSE.txt)
80154

81155
</div>

assets/require.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"pkgName": "@sv443-network/userutils",
4+
"path": "dist/index.global.js",
5+
"global": "UserUtils"
6+
}
7+
]
8+

assets/resources.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"img-logo": "images/logo.png",
3+
"img-logo-48": "images/logo_48.png",
4+
"doc-changelog": "/CHANGELOG.md"
5+
}

changelog.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
1-
<!--
2-
## 0.2.0
3-
- ...
4-
5-
<br>
6-
-->
7-
8-
## 0.1.0
9-
- Initial release
1+
# 0.1.0
2+
- Initial release

dependencies.json

-6
This file was deleted.

0 commit comments

Comments
 (0)