env setup

This commit is contained in:
Sebastian 2023-06-01 09:20:36 -03:00
parent 5e4c128319
commit 2332101482
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069
2 changed files with 47 additions and 23 deletions

View File

@ -70,9 +70,13 @@ function copyFilesPlugin(files: Array<string>) {
return {
name: "copy-files",
setup(build: PluginBuild) {
if (!files || !files.length) {
return;
}
const outDir = build.initialOptions.outdir;
if (outDir === undefined)
if (outDir === undefined) {
throw Error("esbuild build options does not specify outdir");
}
build.onEnd(() => {
for (const file of files) {
const name = path.parse(file).base;
@ -143,7 +147,6 @@ function linariaPlugin() {
const defaultEsBuildConfig: esbuild.BuildOptions = {
bundle: true,
minify: false,
loader: {
".svg": "file",
".inline.svg": "text",
@ -157,7 +160,6 @@ const defaultEsBuildConfig: esbuild.BuildOptions = {
target: ["es6"],
format: "esm",
platform: "browser",
sourcemap: true,
jsxFactory: "h",
jsxFragment: "Fragment",
alias: {
@ -171,6 +173,7 @@ const defaultEsBuildConfig: esbuild.BuildOptions = {
};
export interface BuildParams {
type: "development" | "test" | "production";
source: {
assets: string[];
js: string[];
@ -186,33 +189,54 @@ export function computeConfig(params: BuildParams): esbuild.BuildOptions {
copyFilesPlugin(params.source.assets),
];
switch (params.css) {
case "sass": {
plugins.push(sassPlugin);
break;
}
case "postcss": {
plugins.push(postCssPlugin);
break;
}
case "linaria": {
if (params.linariaPlugin) {
plugins.push(params.linariaPlugin());
if (params.css) {
switch (params.css) {
case "sass": {
plugins.push(sassPlugin);
break;
}
case "postcss": {
plugins.push(postCssPlugin);
break;
}
break;
}
default: {
const cssType: never = params.css;
throw Error(`not supported: ${cssType}`);
case "linaria": {
if (params.linariaPlugin) {
plugins.push(params.linariaPlugin());
}
break;
}
default: {
const cssType: never = params.css;
throw Error(`not supported: ${cssType}`);
}
}
}
if (!params.type) {
throw Error(
`missing build type, it should be "test", "development" or "production"`,
);
}
if (!params.source.js) {
throw Error(`no javascript entry points, nothing to compile?`);
}
if (!params.destination) {
throw Error(`missing destination folder`);
}
return {
...defaultEsBuildConfig,
entryPoints: params.source.js,
publicPath: params.public,
outdir: params.destination,
minify: false, //params.type === "production",
sourcemap: true, //params.type !== "production",
define: {
...defaultEsBuildConfig.define,
"process.env.NODE_ENV": JSON.stringify(params.type),
},
plugins,
};
}

View File

@ -108,9 +108,9 @@ export class BrowserHttpLib implements HttpRequestLibrary {
}, requestTimeout.d_ms);
}
for (const headerName in Object.keys(requestHeadersMap)) {
Object.keys(requestHeadersMap).forEach((headerName) => {
myRequest.setRequestHeader(headerName, requestHeadersMap[headerName]);
}
});
myRequest.responseType = "arraybuffer";
myRequest.send(myBody);