build: simplify asset compilation
This commit is contained in:
@@ -1,76 +1,62 @@
|
|||||||
import esbuild from 'esbuild'
|
import esbuild from "esbuild";
|
||||||
import {sassPlugin} from 'esbuild-sass-plugin';
|
import {sassPlugin} from "esbuild-sass-plugin";
|
||||||
import {readdirSync} from 'fs';
|
import {readdirSync, rmSync} from "node:fs";
|
||||||
import {join} from 'path';
|
import {join} from "node:path";
|
||||||
import * as fs from "node:fs";
|
|
||||||
|
|
||||||
async function build_main() {
|
const OUTPUT_DIR = "templates/assets/dist";
|
||||||
const srcfile = 'src/main.js';
|
const FONT_LOADERS = {
|
||||||
const outfile = 'templates/assets/dist/main.js';
|
".woff": "file",
|
||||||
|
".woff2": "file",
|
||||||
await esbuild.build({
|
".ttf": "file"
|
||||||
entryPoints: [srcfile],
|
};
|
||||||
|
const COMMON_OPTIONS = {
|
||||||
bundle: true,
|
bundle: true,
|
||||||
outfile: outfile,
|
target: "es2020",
|
||||||
assetNames: '[name]',
|
|
||||||
plugins: [
|
|
||||||
sassPlugin(),
|
|
||||||
],
|
|
||||||
loader: {
|
|
||||||
'.woff': 'file',
|
|
||||||
'.woff2': 'file',
|
|
||||||
'.ttf': 'file',
|
|
||||||
},
|
|
||||||
format: 'esm',
|
|
||||||
target: 'es2020',
|
|
||||||
minify: true,
|
minify: true,
|
||||||
sourcemap: false
|
sourcemap: false
|
||||||
})
|
};
|
||||||
|
|
||||||
const tempEntry = "src/pixel.js"
|
function buildMain() {
|
||||||
const tempOutfile = "templates/assets/dist/pixel.js";
|
return esbuild.build({
|
||||||
await esbuild.build({
|
...COMMON_OPTIONS,
|
||||||
entryPoints: [tempEntry],
|
entryPoints: ["src/main.js"],
|
||||||
bundle: true,
|
outfile: join(OUTPUT_DIR, "main.js"),
|
||||||
outfile: tempOutfile,
|
assetNames: "[name]",
|
||||||
assetNames: '[name]',
|
plugins: [sassPlugin()],
|
||||||
plugins: [
|
loader: FONT_LOADERS,
|
||||||
sassPlugin(),
|
format: "esm"
|
||||||
],
|
|
||||||
loader: {
|
|
||||||
'.woff': 'file',
|
|
||||||
'.woff2': 'file',
|
|
||||||
'.ttf': 'file',
|
|
||||||
},
|
|
||||||
format: 'esm',
|
|
||||||
target: 'es2020',
|
|
||||||
minify: true,
|
|
||||||
sourcemap: false
|
|
||||||
})
|
|
||||||
|
|
||||||
if (fs.existsSync(tempOutfile)) {
|
|
||||||
fs.unlinkSync(tempOutfile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async function build_libs() {
|
|
||||||
const srcDir = 'src/libs';
|
|
||||||
const outDir = 'templates/assets/dist/libs';
|
|
||||||
|
|
||||||
await esbuild.build({
|
|
||||||
entryPoints: readdirSync(srcDir)
|
|
||||||
.filter(file => file.endsWith('.js'))
|
|
||||||
.map(file => join(srcDir, file)),
|
|
||||||
bundle: true,
|
|
||||||
outdir: outDir,
|
|
||||||
format: 'esm',
|
|
||||||
target: 'es2020',
|
|
||||||
minify: true,
|
|
||||||
splitting: false,
|
|
||||||
sourcemap: false
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await build_main();
|
function buildPixelStyles() {
|
||||||
await build_libs();
|
return esbuild.build({
|
||||||
|
...COMMON_OPTIONS,
|
||||||
|
entryPoints: ["src/styles/pixel.scss"],
|
||||||
|
outfile: join(OUTPUT_DIR, "pixel.css"),
|
||||||
|
assetNames: "[name]",
|
||||||
|
plugins: [sassPlugin()],
|
||||||
|
loader: FONT_LOADERS
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildLibraries() {
|
||||||
|
const sourceDirectory = "src/libs";
|
||||||
|
|
||||||
|
return esbuild.build({
|
||||||
|
...COMMON_OPTIONS,
|
||||||
|
entryPoints: readdirSync(sourceDirectory)
|
||||||
|
.filter(file => file.endsWith(".js"))
|
||||||
|
.map(file => join(sourceDirectory, file)),
|
||||||
|
outdir: join(OUTPUT_DIR, "libs"),
|
||||||
|
format: "esm",
|
||||||
|
splitting: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
rmSync(OUTPUT_DIR, {recursive: true, force: true});
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
buildMain(),
|
||||||
|
buildPixelStyles(),
|
||||||
|
buildLibraries()
|
||||||
|
]);
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
import './styles/pixel.scss';
|
|
||||||
Reference in New Issue
Block a user