diff --git a/.eslintignore b/.eslintignore index e019f3c..69192d0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,3 @@ node_modules/ -main.js +dist/ diff --git a/esbuild.config.mjs b/esbuild.config.mjs index 2f6d22b..9156132 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -1,45 +1,73 @@ -import esbuild from "esbuild"; -import process from "process"; -import builtins from "builtin-modules"; -import fs from "fs-extra"; +import esbuild from 'esbuild'; +import process from 'process'; +import builtins from 'builtin-modules'; +import fs from 'fs-extra'; +import path from 'path'; -const banner = -`/* +const banner = `/* THIS IS A GENERATED/BUNDLED FILE BY ESBUILD if you want to view the source, please visit the github repository of this plugin */ `; -const prod = (process.argv[2] === "production"); +const prod = (process.argv[2] === 'production'); + +// Plugin to copy additional files +const copyFilesToDist = { + name: 'copy-files', + setup(build) { + build.onEnd(async (result) => { + if (result.errors.length > 0) { + return; + } + + const distDir = path.join(process.cwd(), 'dist'); + + await fs.copy('manifest.json', path.join(distDir, 'manifest.json')); + + if (await fs.pathExists('styles.css')) { + await fs.copy('styles.css', path.join(distDir, 'styles.css')); + } + + if (await fs.pathExists('resources')) { + await fs.copy('resources', path.join(distDir, 'resources')); + } + + console.log('Copied additional files to /dist folder.'); + }); + }, +}; const context = await esbuild.context({ banner: { js: banner, }, - entryPoints: ["src/main.ts"], + entryPoints: ['src/main.ts'], bundle: true, external: [ - "obsidian", - "electron", - "@codemirror/autocomplete", - "@codemirror/collab", - "@codemirror/commands", - "@codemirror/language", - "@codemirror/lint", - "@codemirror/search", - "@codemirror/state", - "@codemirror/view", - "@lezer/common", - "@lezer/highlight", - "@lezer/lr", - ...builtins], - format: "cjs", - target: "es2018", - logLevel: "info", - sourcemap: prod ? false : "inline", + 'obsidian', + 'electron', + '@codemirror/autocomplete', + '@codemirror/collab', + '@codemirror/commands', + '@codemirror/language', + '@codemirror/lint', + '@codemirror/search', + '@codemirror/state', + '@codemirror/view', + '@lezer/common', + '@lezer/highlight', + '@lezer/lr', + ...builtins, + ], + format: 'cjs', + target: 'es2018', + logLevel: 'info', + sourcemap: prod ? false : 'inline', treeShaking: true, - outdir: "dist", + outdir: 'dist', minify: prod, + plugins: [copyFilesToDist], }); if (prod) { @@ -48,9 +76,3 @@ if (prod) { } else { await context.watch(); } - -// Copy additional files to dist folder -fs.copySync("manifest.json", "dist/manifest.json"); -if (fs.existsSync("resources")) { - fs.copySync("resources", "dist/resources"); -} diff --git a/tsconfig.json b/tsconfig.json index c44b729..cbe01ec 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,6 +19,6 @@ ] }, "include": [ - "**/*.ts" + "src/**/*.ts" ] }