Pārlūkot izejas kodu

feat: change to ESM

Sv443 1 gadu atpakaļ
vecāks
revīzija
f817a48218
5 mainītis faili ar 16 papildinājumiem un 13 dzēšanām
  1. 0 1
      dist/BetterYTM.user.js
  2. 4 3
      package.json
  3. 5 4
      src/tools/post-build.ts
  4. 2 1
      tsconfig.json
  5. 5 4
      webpack.config.js

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 1
dist/BetterYTM.user.js


+ 4 - 3
package.json

@@ -4,10 +4,11 @@
   "description": "Userscript that improves YouTube Music",
   "homepage": "https://github.com/Sv443/BetterYTM",
   "main": "./src/BetterYTM.user.ts",
+  "type": "module",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "build": "webpack",
-    "post-build": "ts-node ./src/tools/post-build.ts",
+    "post-build": "ts-node --esm ./src/tools/post-build.ts",
     "serve": "http-server -s -c 5 -p 8710 .",
     "watch": "nodemon --exec \"npm run build && npm run serve\"",
     "lint": "eslint ."
@@ -58,8 +59,8 @@
     ],
     "ext": "ts,js,json",
     "ignore": [
-      "BetterYTM.user.js",
+      "dist/*",
       "dev/*"
     ]
   }
-}
+}

+ 5 - 4
src/tools/post-build.ts

@@ -1,6 +1,7 @@
 import { readFile, writeFile, stat } from "fs/promises";
-import pkg from "../../package.json";
-import { join } from "path";
+import { dirname, join } from "path";
+import { fileURLToPath } from "url";
+import pkg from "../../package.json" assert { type: "json" };
 
 const userscriptName = "BetterYTM.user.js";
 const scriptUrl = `https://raw.githubusercontent.com/Sv443/BetterYTM/main/dist/${userscriptName}`;
@@ -44,11 +45,11 @@ const header = `\
 
 (async () => {
   try {
-    const path = join(__dirname, `../../dist/${userscriptName}`);
+    const path = join(dirname(fileURLToPath(import.meta.url)), `../../dist/${userscriptName}`);
     const input = String(await readFile(path));
     await writeFile(path, `${header}\n${input}${input.endsWith("\n") ? "" : "\n"}`);
     console.info("Successfully added the userscript header");
-    console.info(`\nFinal size is ${((await stat(path)).size / 1024).toFixed(2)} KiB`);
+    console.info(`\nFinal size is ${((await stat(path)).size / 1024).toFixed(2)} KiB\n`);
   }
   catch(err) {
     console.error("Error while adding userscript header:");

+ 2 - 1
tsconfig.json

@@ -1,6 +1,7 @@
 {
   "compilerOptions": {
-    "module": "NodeNext",
+    "module": "ESNext",
+    "moduleResolution": "bundler",
     "target": "ES2016",
     "outDir": ".",
     "lib": [

+ 5 - 4
webpack.config.js

@@ -1,7 +1,8 @@
-const { join } = require("path");
-const { exec } = require("child_process");
+import { dirname, join } from "path";
+import { exec } from "child_process";
+import { fileURLToPath } from "url";
 
-module.exports = {
+export default {
   entry: "./src/BetterYTM.user.ts",
   mode: "production",
   // optimization: {
@@ -48,6 +49,6 @@ module.exports = {
   },
   output: {
     filename: "BetterYTM.user.js",
-    path: join(__dirname, "/dist"),
+    path: join(dirname(fileURLToPath(import.meta.url)), "/dist"),
   },
 };

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels