UNPKG

sensai

Version:

Because even AI needs a master

126 lines (125 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { /** * Returns options needed to transform a file based on its extension. * TODO should source maps and drop console be options based to the function? */ default: function() { return _default; }, getCompilerOptions: function() { return getCompilerOptions; } }); const _nodepath = require("node:path"); const _constants = require("../../constants"); const _enums = require("./enums"); // variable used to generate source maps or drop consoles const isProd = process.env[_constants.ENV_NAMESPACE] === _constants.SENSAI_ENV.PROD; // allowed extensions const extensions = Object.values(_enums.JSExtensionE); const _default = (filename, compilerOptions)=>{ // TODO replace output type const extension = (0, _nodepath.extname)(filename); if (extensions.includes(extension)) { return { filename, minify: true, //...getSourceMapsOptions(), isModule: true, module: getModuleOptions(), swcrc: false, jsc: { loose: true, target: "es2022", parser: { syntax: getSyntax(extension), tsx: false, dynamicImport: true, decorators: true }, minify: getMinifyOptions(), transform: { // decoratorVersion: "2022-03", legacyDecorator: true, decoratorMetadata: true, constModules: { // TODO allow to inject env variables from endpoints globals: { "@simpi/env": { SOME_VAR: "true" } } } }, ...compilerOptions } }; } }; /** * Get source maps options based on environment. * On production, source maps will be generated in separate files using the version 3 * without `sourcesContent` (`inlineSourcesContent: false`). */ const getSourceMapsOptions = ()=>{ return { sourceMaps: isProd ? false : "inline", inlineSourcesContent: false }; }; /** * Returns minification options based on environment. */ const getMinifyOptions = ()=>{ return { mangle: true, compress: { dead_code: true, drop_console: isProd ? true : false }, module: true }; }; /** * Get module options based on file extension and type. */ const getModuleOptions = ()=>{ return { type: _enums.ModuleTypeE.COMMON, // lazy: true, ignoreDynamic: false, noInterop: false }; }; /** * Get file syntax (either typescript or ecmascript) */ const getSyntax = (extension)=>{ return extension === _enums.JSExtensionE.TYPESCRIPT ? _enums.FileSyntaxE.TYPESCRIPT : _enums.FileSyntaxE.JAVASCRIPT; }; const getCompilerOptions = (cwdPath)=>{ try { const { compilerOptions } = require((0, _nodepath.join)(cwdPath, "tsconfig.json")); if (compilerOptions) { const { baseUrl, paths } = compilerOptions; return { baseUrl, paths }; } return { baseUrl: undefined, paths: undefined }; } catch { return { baseUrl: undefined, paths: undefined }; } };