mirror of
https://github.com/JosunLP/UserScriptProjectTemplate.git
synced 2025-10-14 00:50:10 +00:00
feat: Enhance build optimization and add development-only debug information
This commit is contained in:
parent
c753e6fb72
commit
0a0b521948
6 changed files with 165 additions and 11 deletions
19
README.md
19
README.md
|
@ -126,6 +126,25 @@ npm run clean # Clean dist folder
|
|||
npm run type-check # TypeScript type checking
|
||||
```
|
||||
|
||||
### Build Optimization
|
||||
|
||||
The template features advanced build optimization for production:
|
||||
|
||||
| Build Type | File Size | Compressed | Features |
|
||||
| --------------- | --------- | ---------- | -------------------------------------- |
|
||||
| **Development** | ~115 KB | ~30 KB | Source maps, debug info, readable code |
|
||||
| **Production** | ~25 KB | ~6 KB | Minified, tree-shaken, optimized |
|
||||
|
||||
**Production optimizations include:**
|
||||
|
||||
- ⚡ **Terser minification** with aggressive compression settings
|
||||
- 🌳 **Tree-shaking** to remove unused code
|
||||
- 🎯 **Dead code elimination** for **DEV** blocks
|
||||
- 📦 **Module inlining** for single-file output
|
||||
- 🔧 **Property mangling** for smaller variable names
|
||||
- 🚀 **ES2020 target** for modern JavaScript features
|
||||
- 💾 **GZIP compression** reducing size by ~75%
|
||||
|
||||
### Development Workflow
|
||||
|
||||
1. **Configure your script** in `header.config.json`
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
"dev:header": "npm run build-userScriptHeader",
|
||||
"dev:build": "vite build --mode development && npm run build-userScriptHeader",
|
||||
"build": "vite build && npm run build-userScriptHeader",
|
||||
"build:prod": "vite build --mode production && npm run build-userScriptHeader",
|
||||
"build:prod": "npm run clean && vite build --mode production && npm run build-userScriptHeader",
|
||||
"build-tooling": "tsc ./tools/userScriptHeader.ts --resolveJsonModule --esModuleInterop",
|
||||
"build-userScriptHeader": "npm run build-tooling && node ./tools/userScriptHeader.js",
|
||||
"validate": "npm run type-check && npm run lint",
|
||||
|
|
|
@ -25,6 +25,14 @@ class App extends EventEmitter<AppEvents> {
|
|||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
// Development-only debug information
|
||||
if (__DEV__) {
|
||||
console.log('🔧 UserScript starting in development mode');
|
||||
console.log('📦 Version:', __VERSION__);
|
||||
console.log('🕐 Build time:', __BUILD_TIME__);
|
||||
}
|
||||
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ interface ExampleModuleEvents {
|
|||
export class ExampleModule extends EventEmitter<ExampleModuleEvents> {
|
||||
private isInitialized = false;
|
||||
private actionCount = 0;
|
||||
private lastActionTime = 0;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
@ -36,6 +37,7 @@ export class ExampleModule extends EventEmitter<ExampleModuleEvents> {
|
|||
|
||||
// Load persistent data
|
||||
this.actionCount = Storage.get<number>('exampleModule.actionCount', 0) || 0;
|
||||
this.lastActionTime = Storage.get<number>('exampleModule.lastActionTime', 0) || 0;
|
||||
|
||||
// Wait for required DOM elements (example)
|
||||
await this.waitForPageElements();
|
||||
|
@ -164,9 +166,11 @@ export class ExampleModule extends EventEmitter<ExampleModuleEvents> {
|
|||
public performAction(trigger: string): void {
|
||||
this.actionCount++;
|
||||
const timestamp = Date.now();
|
||||
this.lastActionTime = timestamp;
|
||||
|
||||
// Store the updated count
|
||||
// Store the updated count and last action time
|
||||
Storage.set('exampleModule.actionCount', this.actionCount);
|
||||
Storage.set('exampleModule.lastActionTime', timestamp);
|
||||
|
||||
// Emit event
|
||||
this.emit('actionPerformed', { action: trigger, timestamp });
|
||||
|
@ -205,7 +209,7 @@ export class ExampleModule extends EventEmitter<ExampleModuleEvents> {
|
|||
const stats = {
|
||||
initialized: this.isInitialized,
|
||||
actionCount: this.actionCount,
|
||||
lastAction: Storage.get<number>('exampleModule.lastActionTime', 0),
|
||||
lastAction: this.lastActionTime,
|
||||
};
|
||||
|
||||
const message = [
|
||||
|
@ -230,6 +234,7 @@ export class ExampleModule extends EventEmitter<ExampleModuleEvents> {
|
|||
Storage.remove('exampleModule.actionCount');
|
||||
Storage.remove('exampleModule.lastActionTime');
|
||||
this.actionCount = 0;
|
||||
this.lastActionTime = 0;
|
||||
console.log('🧹 Example module data reset');
|
||||
this.showNotification('Module data reset!');
|
||||
}
|
||||
|
|
9
src/types/userscript.d.ts
vendored
9
src/types/userscript.d.ts
vendored
|
@ -94,6 +94,15 @@ declare global {
|
|||
* UnsafeWindow for accessing page's global scope
|
||||
*/
|
||||
const unsafeWindow: Window & typeof globalThis;
|
||||
|
||||
/**
|
||||
* Build-time constants injected by Vite
|
||||
*/
|
||||
const __DEV__: boolean;
|
||||
const __VERSION__: string;
|
||||
const __DEBUG__: boolean;
|
||||
const __USERSCRIPT__: boolean;
|
||||
const __BUILD_TIME__: string;
|
||||
}
|
||||
|
||||
export {};
|
||||
|
|
129
vite.config.ts
129
vite.config.ts
|
@ -1,7 +1,7 @@
|
|||
import { resolve } from "path";
|
||||
import { defineConfig } from "vite";
|
||||
import tsconfigPaths from "vite-tsconfig-paths";
|
||||
import pkgjsn from "./package.json";
|
||||
import { resolve } from 'path';
|
||||
import { defineConfig } from 'vite';
|
||||
import tsconfigPaths from 'vite-tsconfig-paths';
|
||||
import pkgjsn from './package.json';
|
||||
|
||||
export default defineConfig(({ mode }) => {
|
||||
const isDev = mode === 'development';
|
||||
|
@ -9,18 +9,106 @@ export default defineConfig(({ mode }) => {
|
|||
return {
|
||||
build: {
|
||||
rollupOptions: {
|
||||
input: resolve(__dirname, "src/index.ts"),
|
||||
input: resolve(__dirname, 'src/index.ts'),
|
||||
output: {
|
||||
entryFileNames: `${pkgjsn.name}${isDev ? '.dev' : ''}.user.js`,
|
||||
dir: resolve(__dirname, "dist"),
|
||||
dir: resolve(__dirname, 'dist'),
|
||||
// Disable code splitting - everything in one file for UserScript
|
||||
inlineDynamicImports: true,
|
||||
manualChunks: undefined,
|
||||
// Optimize output format
|
||||
format: 'iife',
|
||||
// Remove unnecessary comments in production
|
||||
banner: isDev ? undefined : '',
|
||||
footer: isDev ? undefined : '',
|
||||
},
|
||||
// Prevent any external dependencies
|
||||
external: [],
|
||||
// Tree-shaking optimizations
|
||||
treeshake: {
|
||||
moduleSideEffects: false,
|
||||
propertyReadSideEffects: false,
|
||||
unknownGlobalSideEffects: false,
|
||||
},
|
||||
},
|
||||
sourcemap: isDev ? "inline" : false,
|
||||
sourcemap: isDev ? 'inline' : false,
|
||||
minify: isDev ? false : 'terser',
|
||||
// Enhanced Terser options for maximum compression
|
||||
terserOptions: isDev
|
||||
? undefined
|
||||
: {
|
||||
compress: {
|
||||
drop_console: false, // Keep console for UserScript debugging
|
||||
drop_debugger: true,
|
||||
pure_funcs: ['console.debug'],
|
||||
passes: 2,
|
||||
unsafe: true,
|
||||
unsafe_arrows: true,
|
||||
unsafe_comps: true,
|
||||
unsafe_math: true,
|
||||
unsafe_methods: true,
|
||||
unsafe_proto: true,
|
||||
unsafe_regexp: true,
|
||||
unsafe_undefined: true,
|
||||
hoist_funs: true,
|
||||
hoist_props: true,
|
||||
hoist_vars: false,
|
||||
if_return: true,
|
||||
join_vars: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
switches: true,
|
||||
typeofs: true,
|
||||
booleans: true,
|
||||
collapse_vars: true,
|
||||
comparisons: true,
|
||||
computed_props: true,
|
||||
conditionals: true,
|
||||
dead_code: true,
|
||||
directives: true,
|
||||
evaluate: true,
|
||||
expression: false,
|
||||
global_defs: {},
|
||||
keep_fargs: false,
|
||||
keep_infinity: false,
|
||||
loops: true,
|
||||
negate_iife: true,
|
||||
properties: true,
|
||||
reduce_funcs: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
},
|
||||
mangle: {
|
||||
toplevel: true,
|
||||
safari10: false,
|
||||
properties: {
|
||||
regex: /^_/,
|
||||
},
|
||||
},
|
||||
format: {
|
||||
comments: false,
|
||||
beautify: false,
|
||||
},
|
||||
ecma: 2020,
|
||||
toplevel: true,
|
||||
safari10: false,
|
||||
ie8: false,
|
||||
},
|
||||
// Ensure all assets are inlined
|
||||
assetsInlineLimit: Number.MAX_SAFE_INTEGER,
|
||||
// Disable CSS code splitting
|
||||
cssCodeSplit: false,
|
||||
// Target modern browsers for better optimization
|
||||
target: ['es2020', 'chrome80', 'firefox78', 'safari14'],
|
||||
// Report compressed file sizes
|
||||
reportCompressedSize: true,
|
||||
// Chunk size warnings
|
||||
chunkSizeWarningLimit: 500,
|
||||
},
|
||||
plugins: [tsconfigPaths()],
|
||||
resolve: {
|
||||
extensions: [".tsx", ".ts", ".js"],
|
||||
extensions: ['.tsx', '.ts', '.js'],
|
||||
alias: {
|
||||
'@': resolve(__dirname, 'src'),
|
||||
},
|
||||
|
@ -28,6 +116,31 @@ export default defineConfig(({ mode }) => {
|
|||
define: {
|
||||
__DEV__: isDev,
|
||||
__VERSION__: JSON.stringify(pkgjsn.version),
|
||||
// Production optimizations
|
||||
'process.env.NODE_ENV': JSON.stringify(isDev ? 'development' : 'production'),
|
||||
// Remove debug code in production
|
||||
__DEBUG__: isDev,
|
||||
// UserScript environment flags
|
||||
__USERSCRIPT__: true,
|
||||
__BUILD_TIME__: JSON.stringify(new Date().toISOString()),
|
||||
},
|
||||
// Optimize dependencies
|
||||
optimizeDeps: {
|
||||
include: [],
|
||||
exclude: [],
|
||||
},
|
||||
// Enable esbuild optimizations
|
||||
esbuild: {
|
||||
target: 'es2020',
|
||||
legalComments: 'none',
|
||||
...(isDev
|
||||
? {}
|
||||
: {
|
||||
drop: ['debugger'],
|
||||
minifyIdentifiers: true,
|
||||
minifySyntax: true,
|
||||
minifyWhitespace: true,
|
||||
}),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue