Skip to content

Remove lowcoder-dev-utils #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
37 changes: 21 additions & 16 deletions client/config/test/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import path from "node:path";
import { buildVars } from "../../packages/lowcoder-dev-utils/buildVars.js";
import { currentDirName } from "../../packages/lowcoder-dev-utils/util.js";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { buildVars } from "../../scripts/buildVars.js";

export function currentDirName(importMetaUrl) {
return dirname(fileURLToPath(importMetaUrl));
}


const globals = {};
buildVars.forEach(({ name, defaultValue }) => {
Expand All @@ -9,34 +14,34 @@ buildVars.forEach(({ name, defaultValue }) => {
const edition = process.env.REACT_APP_EDITION;
const isEEGlobal = edition === "enterprise-global";
const isEE = edition === "enterprise" || isEEGlobal;
const dirname = currentDirName(import.meta.url);
const currentDir = currentDirName(import.meta.url);

export default {
testEnvironment: "jsdom",
moduleNameMapper: {
"react-markdown": path.resolve(dirname, "./mocks/react-markdown.js"),
"\\.md\\?url$": path.resolve(dirname, "./mocks/markdown-url-module.js"),
"react-markdown": path.resolve(currentDir, "./mocks/react-markdown.js"),
"\\.md\\?url$": path.resolve(currentDir, "./mocks/markdown-url-module.js"),
"^@lowcoder-ee(.*)$": path.resolve(
dirname,
currentDir,
isEE ? "../../packages/lowcoder/src/ee/$1" : "../../packages/lowcoder/src/$1"
),
"lowcoder-sdk": path.resolve(dirname, "../../packages/lowcoder/src/index.sdk"),
"lowcoder-sdk": path.resolve(currentDir, "../../packages/lowcoder/src/index.sdk"),
},
globals,
// roots: ["<rootDir>/src"],
modulePaths: [
"<rootDir>/src",
path.resolve(dirname, "../../packages/lowcoder/src"),
path.resolve(dirname, "../../packages/lowcoder-comps/src"),
path.resolve(dirname, "../../packages/lowcoder-design/src"),
path.resolve(currentDir, "../../packages/lowcoder/src"),
path.resolve(currentDir, "../../packages/lowcoder-comps/src"),
path.resolve(currentDir, "../../packages/lowcoder-design/src"),
],
setupFiles: [path.resolve(dirname, "./jest.setup.js")],
setupFilesAfterEnv: [path.resolve(dirname, "./jest.setup-after-env.js")],
setupFiles: [path.resolve(currentDir, "./jest.setup.js")],
setupFilesAfterEnv: [path.resolve(currentDir, "./jest.setup-after-env.js")],
transform: {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": path.resolve(dirname, "./transform/babelTransform.js"),
"^.+\\.css$": path.resolve(dirname, "./transform/cssTransform.js"),
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": path.resolve(currentDir, "./transform/babelTransform.js"),
"^.+\\.css$": path.resolve(currentDir, "./transform/cssTransform.js"),
"^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": path.resolve(
dirname,
currentDir,
"./transform/fileTransform.js"
),
},
Expand Down
1 change: 0 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"jest-environment-jsdom": "^29.5.0",
"lint-staged": "^13.0.1",
"lowcoder-cli": "workspace:^",
"lowcoder-dev-utils": "workspace:^",
"mq-polyfill": "^1.1.8",
"prettier": "^3.1.0",
"rimraf": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion client/packages/create-lowcoder-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { spawn } from "cross-spawn";
import { writeFileSync, existsSync } from "node:fs";
import chalk from "chalk";
import { createCommand } from "commander";
import { readJson, currentDirName } from "../lowcoder-dev-utils/util.js";
import { readJson, currentDirName } from "../../dev-utils/util.js";

const currentDir = currentDirName(import.meta.url);
const pkg = readJson(path.resolve(currentDir, "./package.json"));
Expand Down
3 changes: 1 addition & 2 deletions client/packages/create-lowcoder-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"chalk": "4",
"commander": "^9.4.1",
"cross-spawn": "^7.0.3",
"fs-extra": "^10.1.0",
"lowcoder-dev-utils": "workspace:^"
"fs-extra": "^10.1.0"
},
"license": "MIT",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion client/packages/lowcoder-cli/config/paths.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";
import fs from "node:fs";
import { currentDirName } from "../../lowcoder-dev-utils/util.js";
import { currentDirName } from "../dev-utils/util.js";

const currentDir = currentDirName(import.meta.url);
const appDirectory = fs.realpathSync(process.cwd());
Expand Down
6 changes: 3 additions & 3 deletions client/packages/lowcoder-cli/config/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import react from "@vitejs/plugin-react";
import svgrPlugin from "vite-plugin-svgr";
import global from "rollup-plugin-external-globals";
import { buildVars } from "../../lowcoder-dev-utils/buildVars.js";
import { buildVars } from "../dev-utils/buildVars.js";
import injectCss from "vite-plugin-css-injected-by-js";
import { getLibNames, getAllLibGlobalVarNames } from "../../lowcoder-dev-utils/external.js";
import { getLibNames, getAllLibGlobalVarNames } from "../dev-utils/external.js";
import paths from "./paths.js";
import { defineConfig } from "vite";
import { readJson } from "../../lowcoder-dev-utils/util.js";
import { readJson } from "../dev-utils/util.js";

const isProduction = process.env.NODE_ENV === "production";
const packageJson = readJson(paths.appPackageJson);
Expand Down
1 change: 0 additions & 1 deletion client/packages/lowcoder-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"commander": "^9.4.1",
"cross-spawn": "^7.0.3",
"fs-extra": "^10.1.0",
"lowcoder-dev-utils": "workspace:^",
"react": "^17",
"react-dom": "^17",
"react-json-view": "^1.21.3",
Expand Down
11 changes: 0 additions & 11 deletions client/packages/lowcoder-dev-utils/package.json

This file was deleted.

6 changes: 4 additions & 2 deletions client/packages/lowcoder-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"@rollup/plugin-url": "^7.0.0",
"@svgr/rollup": "^6.3.1",
"@vitejs/plugin-react": "^2.2.0",
"lowcoder-dev-utils": "workspace:^",
"rollup": "^2",
"rollup-plugin-cleaner": "^1.0.0",
"rollup-plugin-node-builtins": "^2.1.2",
Expand All @@ -55,5 +54,8 @@
"keywords": [
"lowcoder"
],
"license": "MIT"
"license": "MIT",
"dependencies": {
"prettier": "^3.1.1"
}
}
58 changes: 58 additions & 0 deletions client/packages/lowcoder-sdk/src/dev-utils/buildVars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export const buildVars = [
{
name: "PUBLIC_URL",
defaultValue: "/",
},
{
name: "REACT_APP_EDITION",
defaultValue: "community",
},
{
name: "REACT_APP_LANGUAGES",
defaultValue: "",
},
{
name: "REACT_APP_COMMIT_ID",
defaultValue: "00000",
},
{
name: "REACT_APP_API_HOST",
defaultValue: "",
},
{
name: "LOWCODER_NODE_SERVICE_URL",
defaultValue: "",
},
{
name: "REACT_APP_ENV",
defaultValue: "production",
},
{
name: "REACT_APP_BUILD_ID",
defaultValue: "",
},
{
name: "REACT_APP_LOG_LEVEL",
defaultValue: "error",
},
{
name: "REACT_APP_IMPORT_MAP",
defaultValue: "{}",
},
{
name: "REACT_APP_SERVER_IPS",
defaultValue: "",
},
{
name: "REACT_APP_BUNDLE_BUILTIN_PLUGIN",
defaultValue: "",
},
{
name: "REACT_APP_BUNDLE_TYPE",
defaultValue: "app",
},
{
name: "REACT_APP_DISABLE_JS_SANDBOX",
defaultValue: "",
},
];
102 changes: 102 additions & 0 deletions client/packages/lowcoder-sdk/src/dev-utils/external.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* libs to import as global var
* name: module name
* mergeDefaultAndNameExports: whether to merge default and named exports
*/
export const libs = [
"axios",
"redux",
"react-router",
"react-router-dom",
"react-redux",
"react",
"react-dom",
"lodash",
"history",
"antd",
"@dnd-kit/core",
"@dnd-kit/modifiers",
"@dnd-kit/sortable",
"@dnd-kit/utilities",
{
name: "moment",
extractDefault: true,
},
{
name: "dayjs",
extractDefault: true,
},
{
name: "lowcoder-sdk",
from: "./src/index.sdk.ts",
},
{
name: "styled-components",
mergeDefaultAndNameExports: true,
},
];

/**
* get global var name from module name
* @param {string} name
* @returns
*/
export const getLibGlobalVarName = (name) => {
return "$" + name.replace(/@/g, "$").replace(/[\/\-]/g, "_");
};

export const getLibNames = () => {
return libs.map((i) => {
if (typeof i === "object") {
return i.name;
}
return i;
});
};

export const getAllLibGlobalVarNames = () => {
const ret = {};
libs.forEach((lib) => {
let name = lib;
if (typeof lib === "object") {
name = lib.name;
}
ret[name] = getLibGlobalVarName(name);
});
return ret;
};

export const libsImportCode = (exclude = []) => {
const importLines = [];
const assignLines = [];
libs.forEach((i) => {
let name = i;
let merge = false;
let from = name;
let extractDefault = false;

if (typeof i === "object") {
name = i.name;
merge = i.mergeDefaultAndNameExports ?? false;
from = i.from ?? name;
extractDefault = i.extractDefault ?? false;
}

if (exclude.includes(name)) {
return;
}

const varName = getLibGlobalVarName(name);
if (merge) {
importLines.push(`import * as ${varName}_named_exports from '${from}';`);
importLines.push(`import ${varName} from '${from}';`);
assignLines.push(`Object.assign(${varName}, ${varName}_named_exports);`);
} else if (extractDefault) {
importLines.push(`import ${varName} from '${from}';`);
} else {
importLines.push(`import * as ${varName} from '${from}';`);
}
assignLines.push(`window.${varName} = ${varName};`);
});
return importLines.concat(assignLines).join("\n");
};
18 changes: 18 additions & 0 deletions client/packages/lowcoder-sdk/src/dev-utils/globalDepPlguin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { libsImportCode } from "./external.js";

export function globalDepPlugin(exclude = []) {
const virtualModuleId = "virtual:globals";
return {
name: "lowcoder-global-plugin",
resolveId(id) {
if (id === virtualModuleId) {
return id;
}
},
load(id) {
if (id === virtualModuleId) {
return libsImportCode(exclude);
}
},
};
}
28 changes: 28 additions & 0 deletions client/packages/lowcoder-sdk/src/dev-utils/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from "node:fs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";

export function stripLastSlash(str) {
if (str.endsWith("/")) {
return str.slice(0, str.length - 1);
}
return str;
}

export function ensureLastSlash(str) {
if (!str) {
return "/";
}
if (!str.endsWith("/")) {
return `${str}/`;
}
return str;
}

export function readJson(file) {
return JSON.parse(fs.readFileSync(file).toString());
}

export function currentDirName(importMetaUrl) {
return dirname(fileURLToPath(importMetaUrl));
}
4 changes: 2 additions & 2 deletions client/packages/lowcoder-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-nocheck
import "../../lowcoder/src/index.less";
import "virtual:globals";
import * as sdk from "lowcoder";
export * from "lowcoder";
import * as sdk from "../../lowcoder";
export * from "../../lowcoder";

window.$lowcoder_sdk = sdk;
6 changes: 3 additions & 3 deletions client/packages/lowcoder-sdk/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import react from "@vitejs/plugin-react";
import viteTsconfigPaths from "vite-tsconfig-paths";
import svgrPlugin from "vite-plugin-svgr";
import path from "path";
import { ensureLastSlash } from "../lowcoder-dev-utils/util";
import { buildVars } from "../lowcoder-dev-utils/buildVars";
import { globalDepPlugin } from "../lowcoder-dev-utils/globalDepPlguin";
import { ensureLastSlash } from "./src/dev-utils/util";
import { buildVars } from "./src/dev-utils/buildVars";
import { globalDepPlugin } from "./src/dev-utils/globalDepPlguin";

const define = {};
buildVars.forEach(({ name, defaultValue }) => {
Expand Down
Loading