Updates
This commit is contained in:
119
lowcoder/client/packages/lowcoder-cli/config/modules.js
Normal file
119
lowcoder/client/packages/lowcoder-cli/config/modules.js
Normal file
@@ -0,0 +1,119 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import paths from "./paths.js";
|
||||
import resolve from "resolve";
|
||||
|
||||
function getAdditionalEntries() {}
|
||||
|
||||
/**
|
||||
* Get additional module paths based on the baseUrl of a compilerOptions object.
|
||||
*
|
||||
* @param {Object} options
|
||||
*/
|
||||
function getAdditionalModulePaths(options = {}) {
|
||||
const baseUrl = options.baseUrl;
|
||||
|
||||
if (!baseUrl) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
|
||||
|
||||
// We don't need to do anything if `baseUrl` is set to `node_modules`. This is
|
||||
// the default behavior.
|
||||
if (path.relative(paths.appNodeModules, baseUrlResolved) === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Allow the user set the `baseUrl` to `appSrc`.
|
||||
if (path.relative(paths.appSrc, baseUrlResolved) === "") {
|
||||
return [paths.appSrc];
|
||||
}
|
||||
|
||||
// If the path is equal to the root directory we ignore it here.
|
||||
// We don't want to allow importing from the root directly as source files are
|
||||
// not transpiled outside of `src`. We do allow importing them with the
|
||||
// absolute path (e.g. `src/Components/Button.js`) but we set that up with
|
||||
// an alias.
|
||||
if (path.relative(paths.appPath, baseUrlResolved) === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Otherwise, throw an error.
|
||||
throw new Error("Your project's `baseUrl` can only be set to `src` or `node_modules`.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get webpack aliases based on the baseUrl of a compilerOptions object.
|
||||
*
|
||||
* @param {*} options
|
||||
*/
|
||||
function getWebpackAliases(options = {}) {
|
||||
const baseUrl = options.baseUrl;
|
||||
|
||||
if (!baseUrl) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
|
||||
|
||||
if (path.relative(paths.appPath, baseUrlResolved) === "") {
|
||||
return {
|
||||
src: paths.appSrc,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get jest aliases based on the baseUrl of a compilerOptions object.
|
||||
*
|
||||
* @param {*} options
|
||||
*/
|
||||
function getJestAliases(options = {}) {
|
||||
const baseUrl = options.baseUrl;
|
||||
|
||||
if (!baseUrl) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
|
||||
|
||||
if (path.relative(paths.appPath, baseUrlResolved) === "") {
|
||||
return {
|
||||
"^src/(.*)$": "<rootDir>/src/$1",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function getModules() {
|
||||
// Check if TypeScript is setup
|
||||
const hasTsConfig = fs.existsSync(paths.appTsConfig);
|
||||
|
||||
let config;
|
||||
|
||||
// If there's a tsconfig.json we assume it's a
|
||||
// TypeScript project and set up the config
|
||||
// based on tsconfig.json
|
||||
if (hasTsConfig) {
|
||||
const ts = require(resolve.sync("typescript", {
|
||||
basedir: paths.appNodeModules,
|
||||
}));
|
||||
config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
|
||||
// Otherwise we'll check if there is jsconfig.json
|
||||
// for non TS projects.
|
||||
}
|
||||
|
||||
config = config || {};
|
||||
const options = config.compilerOptions || {};
|
||||
|
||||
const additionalModulePaths = getAdditionalModulePaths(options);
|
||||
|
||||
return {
|
||||
additionalModulePaths: additionalModulePaths,
|
||||
webpackAliases: getWebpackAliases(options),
|
||||
jestAliases: getJestAliases(options),
|
||||
hasTsConfig,
|
||||
};
|
||||
}
|
||||
|
||||
export default getModules();
|
||||
57
lowcoder/client/packages/lowcoder-cli/config/paths.js
Normal file
57
lowcoder/client/packages/lowcoder-cli/config/paths.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
import { currentDirName } from "../dev-utils/util.js";
|
||||
|
||||
const currentDir = currentDirName(import.meta.url);
|
||||
const appDirectory = fs.realpathSync(process.cwd());
|
||||
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
|
||||
|
||||
const moduleFileExtensions = [
|
||||
"web.mjs",
|
||||
"mjs",
|
||||
"web.js",
|
||||
"js",
|
||||
"web.ts",
|
||||
"ts",
|
||||
"web.tsx",
|
||||
"tsx",
|
||||
"json",
|
||||
"web.jsx",
|
||||
"jsx",
|
||||
];
|
||||
|
||||
const resolveModule = (resolveFn, filePath) => {
|
||||
const extension = moduleFileExtensions.find((extension) =>
|
||||
fs.existsSync(resolveFn(`${filePath}.${extension}`))
|
||||
);
|
||||
|
||||
if (extension) {
|
||||
return resolveFn(`${filePath}.${extension}`);
|
||||
}
|
||||
|
||||
return resolveFn(`${filePath}.js`);
|
||||
};
|
||||
|
||||
const resolveOwn = (relativePath) => path.resolve(currentDir, "..", relativePath);
|
||||
|
||||
const paths = {
|
||||
resolveApp,
|
||||
appOutPath: resolveOwn(".out"),
|
||||
appOutPackageJson: resolveOwn(".out/package.json"),
|
||||
appPath: resolveApp("."),
|
||||
appHtml: resolveOwn("ide/index.html"),
|
||||
appRoot: resolveOwn("ide"),
|
||||
appBaseTsConfig: resolveOwn("ide/tsconfig.json"),
|
||||
appPackageJson: resolveApp("package.json"),
|
||||
appSrc: resolveApp("src"),
|
||||
appLocales: resolveApp("locales"),
|
||||
compsIndexJs: resolveModule(resolveApp, "src/index"),
|
||||
appViteConfigJs: resolveModule(resolveApp, "vite.config"),
|
||||
appTsConfig: resolveApp("tsconfig.json"),
|
||||
yarnLockFile: resolveApp("yarn.lock"),
|
||||
appNodeModules: resolveApp("node_modules"),
|
||||
appWebpackCache: resolveApp("node_modules/.cache"),
|
||||
appTsBuildInfoFile: resolveApp("node_modules/.cache/tsconfig.tsbuildinfo"),
|
||||
};
|
||||
|
||||
export default paths;
|
||||
67
lowcoder/client/packages/lowcoder-cli/config/vite.config.js
Normal file
67
lowcoder/client/packages/lowcoder-cli/config/vite.config.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import react from "@vitejs/plugin-react";
|
||||
import svgrPlugin from "vite-plugin-svgr";
|
||||
import global from "rollup-plugin-external-globals";
|
||||
import { buildVars } from "../dev-utils/buildVars.js";
|
||||
import injectCss from "vite-plugin-css-injected-by-js";
|
||||
import { getLibNames, getAllLibGlobalVarNames } from "../dev-utils/external.js";
|
||||
import paths from "./paths.js";
|
||||
import { defineConfig } from "vite";
|
||||
import { readJson } from "../dev-utils/util.js";
|
||||
|
||||
const isProduction = process.env.NODE_ENV === "production";
|
||||
const packageJson = readJson(paths.appPackageJson);
|
||||
|
||||
const define = {};
|
||||
buildVars.forEach(({ name, defaultValue }) => {
|
||||
define[name] = JSON.stringify(process.env[name] || defaultValue);
|
||||
});
|
||||
|
||||
export default defineConfig({
|
||||
define: {
|
||||
...define,
|
||||
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || "development"),
|
||||
__LOWCODER_ORG__: JSON.stringify({}),
|
||||
},
|
||||
assetsInclude: ["**/*.md"],
|
||||
resolve: {
|
||||
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"],
|
||||
},
|
||||
build: {
|
||||
target: "es2020",
|
||||
cssTarget: "chrome87",
|
||||
outDir: paths.appOutPath,
|
||||
emptyOutDir: true,
|
||||
lib: {
|
||||
formats: ["es"],
|
||||
entry: paths.compsIndexJs,
|
||||
fileName: "index",
|
||||
},
|
||||
rollupOptions: {
|
||||
external: getLibNames(),
|
||||
output: {
|
||||
chunkFileNames: "[hash].js",
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
react({
|
||||
babel: {
|
||||
compact: false,
|
||||
parserOpts: {
|
||||
plugins: ["decorators-legacy"],
|
||||
},
|
||||
},
|
||||
}),
|
||||
svgrPlugin({
|
||||
svgrOptions: {
|
||||
exportType: "named",
|
||||
prettier: false,
|
||||
svgo: false,
|
||||
titleProp: true,
|
||||
ref: true,
|
||||
},
|
||||
}),
|
||||
isProduction && global(getAllLibGlobalVarNames(), { exclude: [/\.css$/] }),
|
||||
isProduction && injectCss({ styleId: `${packageJson.name}-${packageJson.version}` }),
|
||||
].filter(Boolean),
|
||||
});
|
||||
Reference in New Issue
Block a user