@masterofbob777/grunt-config
Version:
A grunt config for js and ts, for use in all projects I work on.
114 lines (106 loc) • 2.36 kB
JavaScript
/**
*
* @param {import("grunt")} grunt
* @param {any} opts
*/
module.exports = (
grunt,
{
configs: { platform: defaultPlat = "node", target = "node12.9" } = {},
options: { typescript = false, jsx = false, external = [] } = {},
general: { indir = "src", outdir = "app", entry = "main" } = {},
} = {}
) => {
const ext = typescript ? "ts" : "js";
const packages = [
"grunt-contrib-clean",
"grunt-contrib-copy",
"grunt-contrib-watch",
"grunt-eslint",
"grunt-plugin-esbuild",
"grunt-plugin-jscc",
"grunt-prettier",
"grunt-shell",
];
for (const package of packages) {
grunt.loadNpmTasks(package);
}
const env = require(`./config/envs/${grunt.option("env") || "development"}.json`);
const platform = require(`./config/plats/${grunt.option("platform") || defaultPlat}.json`);
const jsccMix = {
values: {
_ENV: env,
_PLATFORM: platform,
},
sourceMap: false,
};
/**
* @type {import("esbuild").BuildOptions}
*/
const esbuildMix = {
bundle: true,
outdir: outdir,
outbase: `jscc_temp/${indir}`,
sourcemap: true,
minify: env.normalName !== "development",
external: external,
target: target,
platform: platform.normalName,
};
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
watch: {
dev: {
files: [`${indir}/**/*.${ext}`, "static/**/*"],
tasks: ["softbuild", "clean:jscc_temp"],
options: {
debounceDelay: 700,
},
},
},
eslint: {
options: {
fix: true,
},
target: [`${indir}/**/*.${ext}`],
},
jscc: {
dev: {
inpaths: [
env === "test" ? `${indir}/**/*.spec.${ext}` : `${indir}/**/*!(.spec).${ext}`,
],
out: "jscc_temp",
...jsccMix,
},
},
esbuild: {
main: {
platform: "node",
entryPoints: [`jscc_temp/${indir}/${entry}.${ext}`],
...esbuildMix,
},
},
copy: {
static: {
expand: true,
cwd: "static",
src: "**",
dest: "app/",
},
},
clean: {
jscc_temp: ["jscc_temp/**/*"],
},
shell: {
dtsbundle: {
command: `npx dts-bundle-generator -o "./${outdir}/${entry}.d.ts" "./${indir}/${entry}.${ext}"`,
},
},
});
grunt.registerTask("lint", ["eslint"]);
grunt.registerTask("build", ["jscc:dev", "esbuild:main", "clean:jscc_temp"]);
grunt.registerTask("gen_types", ["shell:dtsbundle"]);
grunt.registerTask("test", [
/* "unit", "e2e"*/
]);
};