Update build script

This commit is contained in:
SuperSaltyGamer
2023-07-28 22:25:29 +03:00
parent 894311b519
commit a22857e3cf
8 changed files with 302 additions and 871 deletions

6
.prettierrc Normal file
View File

@@ -0,0 +1,6 @@
{
"printWidth": 120,
"useTabs": true,
"trailingComma": "none",
"arrowParens": "avoid"
}

1052
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,12 @@
{
"private": true,
"name": "ame",
"author": "SuperSaltyGamer",
"homepage": "https://notabug.org/SuperSaltyGamer/ame",
"type": "module",
"homepage": "https://notabug.org/SuperSaltyGamer/ame/raw/main/dist/",
"config": {
"cdn": "https://notabug.org/SuperSaltyGamer/ame/raw/main/dist/"
},
"dependencies": {
"handsontable": "^13.0.0",
"jszip": "^3.9.1",
@@ -11,13 +16,12 @@
},
"devDependencies": {
"@types/tampermonkey": "^4.0.5",
"globby": "^13.1.3",
"typescript": "^4.9.3",
"vite": "^4.0.0"
"typescript": "^5.1.6",
"vite": "^4.4.7"
},
"scripts": {
"start": "node out-scripts/build.js",
"build": "tsc && node out-scripts/build.js --production",
"start": "node out/scripts/build.js",
"build": "node out/scripts/build.js --production",
"postinstall": "tsc -p tsconfig.scripts.json"
}
}

View File

@@ -1,8 +1,8 @@
import { readFile } from "fs/promises";
import { globbyStream } from "globby";
import { parseArgs } from "util";
import { build, createServer, InlineConfig } from "vite";
import { _userscript } from "./_userscript.js";
import { readFile } from "fs/promises";
import { InlineConfig, build, createServer } from "vite";
import { UserScriptPlugin } from "./utils/userscript.js";
import { walk } from "./utils/path.js";
const pkg = JSON.parse(await readFile("package.json", "utf8"));
@@ -15,30 +15,16 @@ const args = parseArgs({
}
});
const server = await createServer({
plugins: [
{
name: "rewrite",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
req.url = "/out" + req.url;
next();
});
}
}
],
optimizeDeps: {
disabled: true
}
});
const server = await createServer();
if (!args.values.production) {
await server.listen();
await server.printUrls();
server.printUrls();
}
const entries: string[] = [];
for await (const path of await globbyStream("src/**/main.ts")) {
for await (const path of walk("src/")) {
if (!path.endsWith("main.ts")) continue;
const code = await readFile(path, { encoding: "utf8" });
if (!code.startsWith("// ==UserScript==")) continue;
entries.push(path.toString());
@@ -47,16 +33,16 @@ for await (const path of await globbyStream("src/**/main.ts")) {
const configs = entries.map<InlineConfig>(entry => ({
mode: args.values.production ? "production" : "development",
plugins: [
_userscript({
UserScriptPlugin({
entry: entry,
format: "umd",
port: server.config.server.port,
cdn: args.values.production ? pkg.homepage : ""
cdn: args.values.production ? pkg["config"]["cdn"] : ""
})
],
build: {
emptyOutDir: false,
watch: args.values.production ? undefined : {},
watch: args.values.production ? null : {},
outDir: args.values.production ? "dist/" : "out/"
}
}));

13
scripts/utils/path.ts Normal file
View File

@@ -0,0 +1,13 @@
import { join } from "path";
import { readdir } from "fs/promises";
// Walks a directory tree starting from path.
export async function* walk(path: string): AsyncGenerator<string> {
for (const entry of await readdir(path, { withFileTypes: true })) {
if (entry.isDirectory()) {
yield* walk(join(path, entry.name));
} else {
yield join(path, entry.name);
}
}
}

View File

@@ -1,6 +1,6 @@
import { readFile } from 'fs/promises';
import { basename, dirname } from 'path';
import { LibraryFormats, Plugin, ResolvedConfig } from 'vite';
import { readFile } from "fs/promises";
import { basename, dirname } from "path";
import { LibraryFormats, Plugin, ResolvedConfig } from "vite";
export interface UserScriptOptions {
entry: string;
@@ -10,11 +10,11 @@ export interface UserScriptOptions {
cdn?: string;
}
export function _userscript(options: UserScriptOptions): Plugin {
export function UserScriptPlugin(options: UserScriptOptions): Plugin {
let config: ResolvedConfig;
return {
name: 'userscript',
name: "userscript",
config() {
const name = options.name ?? basename(dirname(options.entry));
@@ -22,14 +22,14 @@ export function _userscript(options: UserScriptOptions): Plugin {
build: {
lib: {
name: name,
formats: [ options.format ],
formats: [options.format],
entry: {
[name]: options.entry
}
},
rollupOptions: {
output: {
entryFileNames: '[name].user.js'
entryFileNames: "[name].user.js"
}
}
}
@@ -40,35 +40,36 @@ export function _userscript(options: UserScriptOptions): Plugin {
},
async generateBundle(outputOptions, bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type !== 'chunk') continue;
if (chunk.type !== "chunk") continue;
if (!chunk.isEntry) continue;
if (!chunk.facadeModuleId) continue;
const code = await readFile(chunk.facadeModuleId, { encoding: 'utf8' });
const code = await readFile(chunk.facadeModuleId, { encoding: "utf8" });
let header = '';
for (const line of code.split('\n')) {
if (!line.startsWith('//')) break;
header += line + '\n';
let header = "";
for (const line of code.split("\n")) {
if (!line.startsWith("//")) break;
header += line + "\n";
}
let url = '';
if (config.mode === 'production') {
let url = "";
if (config.mode === "production") {
if (options.cdn) url = `${options.cdn}${chunk.name}.user.js`;
} else {
if (options.port) url = `http://localhost:${options.port}/${chunk.name}.user.js`;
}
// prettier-ignore
if (url) {
header = header.replaceAll(
'// ==/UserScript==',
`// ==/UserScript==`,
`// @downloadURL ${url}\n` +
`// @updateURL ${url}\n` +
'// ==/UserScript=='
`// ==/UserScript==`
);
}
chunk.code = header + '\n' + chunk.code;
chunk.code = header + "\n" + chunk.code;
}
}
};

View File

@@ -5,13 +5,6 @@
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"strict": true,
"noEmit": true,
"lib": [
"esnext",
"dom"
]
},
"include": [
"src/"
]
"noEmit": true
}
}

View File

@@ -4,7 +4,7 @@
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"outDir": "out-scripts/"
"outDir": "out/scripts/"
},
"include": [
"scripts/"