Skip to content

Commit

Permalink
Initial adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
ComiR committed Oct 23, 2022
1 parent 8fb8242 commit 70ff112
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 188 deletions.
2 changes: 0 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ indent_style=space
indent_size=4

[{.babelrc,.stylelintrc,.eslintrc,jest.config,*.bowerrc,*.jsb3,*.jsb2,*.json,*.yaml,*.yml}]
indent_style=space
indent_size=2

[{*.js,*.vue,*.ts,*.cjs,.swcrc}]
indent_style=space
indent_size=2
30 changes: 9 additions & 21 deletions config/metadata.cjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
const {
author,
dependencies,
repository,
version,
} = require("../package.json");
const { name, author, repository, version } = require("../package.json");

module.exports = {
name: {
"": "webpack-userscript-template",
cn: "中文名",
en: "english name",
},
namespace: "https://trim21.me/",
version: version,
author: author,
"": name,
en: "Twitch Drops Manager",
}, // 'license': 'MIT',
author,
description: "Twitch Drops Manager",
match: ["https://*.twitch.tv/drops/*"],
source: repository.url,
// 'license': 'MIT',
match: ["*://www.example.com/", "*://example.com/*"],
require: [
`https://cdn.jsdelivr.net/npm/jquery@${dependencies.jquery}/dist/jquery.min.js`,
],
grant: ["GM.xmlHttpRequest"],
connect: ["httpbin.org"],
"run-at": "document-end",
version,
require: [],
};
70 changes: 14 additions & 56 deletions package-lock.json

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

19 changes: 8 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
{
"name": "webpack-userscript-template",
"description": "Build your UserScript with webpack",
"version": "0.0.0",
"name": "twich-drops-manager",
"description": "Twitch Drops Manager",
"version": "dev",
"author": {
"name": "Trim21",
"email": "[email protected]"
"name": "ComiR",
"email": "[email protected]"
},
"scripts": {
"format": "prettier -w ./",
"analize": "cross-env npm_config_report=true npm run build",
"build": "webpack --config config/webpack.config.prod.cjs",
"dev": "webpack --config config/webpack.config.dev.cjs",
"prepare": "husky install",
"lint-staged": "lint-staged"
},
"repository": {
"type": "git",
"url": "https://github.com/trim21/webpack-userscript-template"
"url": "https://github.com/ComiR/twitch-drops-manager"
},
"private": true,
"dependencies": {
"@trim21/gm-fetch": "^0.1.6",
"jquery": "^3.6.1"
"@trim21/gm-fetch": "^0.1.6"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,json}": [
"prettier --ignore-path ./.prettierignore --write "
]
},
"devDependencies": {
"@types/greasemonkey": "^4.0.4",
"@types/jquery": "^3.5.14",
"@types/node": "^18.11.3",
"@types/tampermonkey": "^4.0.5",
"browserslist": "^4.21.4",
"cross-env": "^7.0.3",
"css-loader": "^6.7.1",
Expand Down
61 changes: 0 additions & 61 deletions readme.cn.md

This file was deleted.

53 changes: 28 additions & 25 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
# This is a project help you build userscript with webpack

Just [use this git repo as a template](https://github.com/Trim21/webpack-userscript-template/generate).

[中文说明](./readme.cn.md)
# Twitch Drops Manager

## dev

1. Allow Tampermonkey's access to local file URIs [tampermonkey/faq](https://tampermonkey.net/faq.php?ext=dhdg#Q204)
1. Allow Tampermonkey access to local file URIs [tampermonkey/faq](https://tampermonkey.net/faq.php?ext=dhdg#Q204)
2. install deps with `npm i` or `npm ci`.
3. `npm run dev` to start your development.

Now you will see 2 files in `./dist/`

- `dist/index.dev.user.js`: **You should install this userscript in your browser.** It's a simple loader that load `dist/index.debug.js` on matched web page.
- `dist/index.debug.js`: This is the development build with `eval-source-map`. It will be automatically loaded by `dist/index.dev.user.js` via `@require file://.../dist/index.debug.js` metadata, **Don't add it to your userscript manager.**

Now you will see 2 files in `./dist/`
- `dist/index.dev.user.js`: **You should install this userscript in your browser.
** It's a simple loader that load `dist/index.debug.js` on matched web page.
- `dist/index.debug.js`: This is the development build with `eval-source-map`.
It will be automatically loaded by `dist/index.dev.user.js` via `@require file://.../dist/index.debug.js`
metadata, **Don't add it to your userscript manager.**
4. edit [src/index.ts](./src/index.ts), you can even import css or less files. You can use scss if you like.
5. go wo <https://www.example.com/> and open console, you'll see it's working.
5. go to <https://twitch.com/> and open the console, you'll see it's working.

livereload is default enabled, use [this Chrome extension](https://chrome.google.com/webstore/detail/jnihajbhpnppcggbcgedagnkighmdlei)
livereload is enabled by default,
use [this Chrome extension](https://chrome.google.com/webstore/detail/jnihajbhpnppcggbcgedagnkighmdlei)

### NOTICE

Everytime you change your metadata config,
you'll have to restart webpack server and install newly generated `dist/index.dev.user.js` UserScript in your browser again.
Everytime you change your metadata config, you'll have to restart webpack server and install the newly
generated `dist/index.dev.user.js` UserScript in your browser again.

## Cross Site Request

you can call `GM.xmlHttpRequest` directly or use a fetch API based on `GM.xmlHttpRequest` <https://github.com/Trim21/gm-fetch>
You can call `GM.xmlHttpRequest` directly or use a fetch API based
on `GM.xmlHttpRequest` <https://github.com/Trim21/gm-fetch>

## TypeScript

use typescript as normal, see [example](src/index.ts)
Use typescript as normal, see [src/index.ts](src/index.ts)

## dependencies

There are two ways to using a package on npm.

### UserScript way

like original UserScript way, you will need to add them to your [user script metadata's require section](./config/metadata.cjs#L16-L18) , and exclude them in [config/webpack.config.base.cjs](./config/webpack.config.base.cjs#L18-L20)
like the original UserScript way, you will need to add them to
your [user script metadata's require section](./config/metadata.cjs#L16-L18) , and exclude them
in [config/webpack.config.base.cjs](./config/webpack.config.base.cjs#L18-L20)

### Webpack way

just install packages with npm and import them in your code, webpack will take care them.
Just install packages with npm and import them in your code, webpack will take care them.

## Build

```bash
npm run build
```

`dist/index.prod.user.js` is the final script. you can manually copy it to greaskfork for deploy.
`dist/index.prod.user.js` is the final script. you can manually copy it to greasyfork for deploy.

### Minify

There is a [limitation in greasyfork](https://greasyfork.org/en/help/code-rules), your code must not be obfuscated or minified.
There is a [limitation in greasyfork](https://greasyfork.org/en/help/code-rules), your code must not be obfuscated or
minified.

If you don't need to deploy your script to greaskfork, enable minify as you like.
If you don't need to deploy your script to greasyfork, enable minify as you like.

## automatically Deploy

[github actions](./.github/workflows/deploy.yaml#L36) will deploy production userscript to gh-pages branch.
[GitHub Actions](./.github/workflows/deploy.yaml#L36) will deploy production userscript to gh-pages branch.

[example](https://github.com/Trim21/webpack-userscript-template/tree/gh-pages)

Expand All @@ -71,6 +72,8 @@ You can auto use greasyfork's auto update function.

## Q&A

you may find enabling source map not working well in production code, because Tampermonkey will add extra lines (all your `@require`) before your script. I don't know if there is a good fix for this, You need to use webpack config `devtool` with `eval` prefix to make it work as expected, so source map is disabled in this template.
You may find enabling source map not working well in production code, because Tampermonkey will add extra lines (all
your `@require`) before your script. I don't know if there is a good fix for this, You need to use webpack
config `devtool` with `eval` prefix to make it work as expected, so source map is disabled in this template.

<https://webpack.js.org/configuration/devtool/#development>
Loading

0 comments on commit 70ff112

Please sign in to comment.