nocobase/packages/core/build/src/buildPlugin.ts

318 lines
7.8 KiB
TypeScript
Raw Normal View History

refactor!: plugins build and plugins load (#2253) * refactor: plugin build and plugin template * refactor: plugins' deps * refactor: plugins bugs * feat: add plugin static middleware * fix: bugs * refactor: frontend plugin add from remote * refactor: delete useless app/client/plugins * fix: requirejs move to local * fix: tests case * refactor: add src/client and src/server dir check * fix: lodash tree shaking * refactor: add BUILD_TIP * refactor: add file size tip * fix: bugs * fix: bug * fix: change china-division * fix: change plugins response * fix: recover dynamicImport * fix: change server src entry * fix: test error * fix: plugins sourcemap => false * fix: production file error * refactor: change build tools to vite and tsup * fix: yarn.lock * fix: bugs * fix: server build bugs * fix: delete .fatherrc.ts * fix: bug * fix: bug * fix: bugs * fix: bugs * fix: bugs * refactor: add plugin d.ts * refactor: delete fatherrc * refactor: delete father scripts * refactor: build bug * fix: bug * fix: deps adjust * fix: add build tips * fix: bug * refactor: ignore plugins when build client * docs: update doc * refactor: docs and build * fix: bug * refactor: build deps * fix: add USER_REMOTE_PLUGIN env * feat: add plugin static cache * feat: add build deps cache * fix: bugs * test: add test * fix: add plugin depden on plugin tip * fix: adjust shouldDevDependencies * fix: deps * fix: ajust deps * fix: mobile style error * fix: map error * fix: test * fix: bug * feat: lodash and dayjs import from themself * feat: @emotion/css 、ahooks and lodash to global * fix: theme-editor plugin error * fix: review * feat: move all plugins' dependencies to devDependencies * feat: change build * feat: add devPlugins * fix: bug * fix: bugs * fix: bugs * fix: bugs * feat: build bugs * fix: bugs * fix: bugs * fix: review * fix: bug * fix: change deps build * fix: bugs * fix: bug * fix: bug * fix: bugs * fix: bug * fix: bug * fix: multi language * fix: dist * fix: cronstrue * fix: getPackageClientStaticUrl * fix: antd dayjs locale * fix: plugin' d.ts import from dist * fix: multi language * fix: build types error * fix: requireModule * fix: plugin lifecycle * fix: client resource * fix: improve code * fix: locale * feat: custom build * fix: require locale * fix: improve code * fix: improve code * fix: skip preset * fix: collection undefined * feat: yarn build * fix: remove enabled * fix: update dockerfile * fix: formily version * docs: update v12 changelog * fix: devDependencies * feat: @nocobase/app * feat: generateAppDir * fix: improve code * fix: 0.11.1-alpha.5 * fix: missing @nocobase/client * fix: error * fix: add .npmignore * feat: upgrade antd version * fix: dependencies * fix: peerDependencies * fix: remove china-division dep * fix: toposort deps * fix: update dockerfile * fix: plugin template * fix: app client outputPath * feat: update docs * fix: nginx server root * fix: storage/.app-dev * fix: getChinaDivisionData * feat: plugin info * feat: update docs * fix: docs menu --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-08-01 16:07:52 +00:00
import ncc from '@vercel/ncc';
import react from '@vitejs/plugin-react';
import chalk from 'chalk';
import fg from 'fast-glob';
import fs from 'fs-extra';
import path from 'path';
import { build as tsupBuild } from 'tsup';
import { build as viteBuild } from 'vite';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';
import {
buildCheck,
formatFileSize,
getExcludePackages,
getFileSize,
getIncludePackages,
getPackageJson,
getSourcePackages,
} from './utils/buildPluginUtils';
import { getDepsConfig } from './utils/getDepsConfig';
const serverGlobalFiles: string[] = ['src/**', '!src/**/__tests__', '!src/client/**'];
const clientGlobalFiles: string[] = ['src/client/**', '!src/**/__tests__'];
const external = [
// nocobase
'@nocobase/acl',
'@nocobase/actions',
'@nocobase/auth',
'@nocobase/cache',
'@nocobase/client',
'@nocobase/database',
'@nocobase/evaluators',
'@nocobase/logger',
'@nocobase/resourcer',
'@nocobase/sdk',
'@nocobase/server',
'@nocobase/test',
'@nocobase/utils',
// @nocobase/auth
'jsonwebtoken',
// @nocobase/cache
'cache-manager',
// @nocobase/database
'sequelize',
'umzug',
'async-mutex',
// @nocobase/evaluators
'@formulajs/formulajs',
'mathjs',
// @nocobase/logger
'winston',
'winston-daily-rotate-file',
// koa
'koa',
'@koa/cors',
'@koa/router',
'multer',
'@koa/multer',
'koa-bodyparser',
'koa-static',
'koa-send',
// react
'react',
'react-dom',
'react/jsx-runtime',
// react-router
'react-router',
'react-router-dom',
// antd
'antd',
'antd-style',
'@ant-design/icons',
'@ant-design/cssinjs',
// i18next
'i18next',
'react-i18next',
// dnd-kit 相关
'@dnd-kit/accessibility',
'@dnd-kit/core',
'@dnd-kit/modifiers',
'@dnd-kit/sortable',
'@dnd-kit/utilities',
// formily 相关
'@formily/antd-v5',
'@formily/core',
'@formily/react',
'@formily/json-schema',
'@formily/path',
'@formily/validator',
'@formily/shared',
'@formily/reactive',
'@formily/reactive-react',
// utils
'dayjs',
'mysql2',
'pg',
'pg-hstore',
'sqlite3',
'supertest',
'axios',
'@emotion/css',
'ahooks',
'lodash',
'china-division',
'cronstrue',
];
const pluginPrefix = (
process.env.PLUGIN_PACKAGE_PREFIX || '@nocobase/plugin-,@nocobase/preset-,@nocobase/plugin-pro-'
).split(',');
type Log = (msg: string, ...args: any) => void;
const target_dir = 'dist';
export function deleteJsFiles(cwd: string, log: Log) {
log('delete babel js files');
const jsFiles = fg.globSync(['**/*', '!**/*.d.ts', '!node_modules'], {
cwd: path.join(cwd, target_dir),
absolute: true,
});
jsFiles.forEach((item) => {
fs.unlinkSync(item);
});
}
export async function buildServerDeps(cwd: string, serverFiles: string[], log: Log) {
log('build server dependencies');
const outDir = path.join(cwd, target_dir, 'node_modules');
const sourcePackages = getSourcePackages(serverFiles);
const includePackages = getIncludePackages(sourcePackages, external, pluginPrefix);
const excludePackages = getExcludePackages(sourcePackages, external, pluginPrefix);
let tips = [];
if (includePackages.length) {
tips.push(
`These packages ${chalk.yellow(includePackages.join(', '))} will be ${chalk.bold(
'bundled',
)} to dist/node_modules.`,
);
}
if (excludePackages.length) {
tips.push(`These packages ${chalk.yellow(excludePackages.join(', '))} will be ${chalk.bold('exclude')}.`);
}
tips.push(`For more information, please refer to: ${chalk.blue('https://docs.nocobase.com/development/deps')}.`);
log(tips.join(' '));
if (!includePackages.length) return;
const deps = getDepsConfig(cwd, outDir, includePackages, external);
// bundle deps
for (const dep of Object.keys(deps)) {
const { outputDir, mainFile, pkg, nccConfig, depDir } = deps[dep];
const outputPackageJson = path.join(outputDir, 'package.json');
// cache check
if (fs.existsSync(outputPackageJson)) {
const outputPackage = require(outputPackageJson);
if (outputPackage.version === pkg.version) {
continue;
}
}
// copy package
await fs.copy(depDir, outputDir, { errorOnExist: false });
// delete files
const deleteFiles = fg.sync(
[
'./**/*.map',
'./**/*.js.map',
'./**/*.md',
'./**/*.mjs',
'./**/*.png',
'./**/*.jpg',
'./**/*.jpeg',
'./**/*.gif',
'./**/*/.bin',
'./**/*/bin',
'./**/*/LICENSE',
'./**/*/tsconfig.json',
],
{ cwd: outputDir, absolute: true },
);
deleteFiles.forEach((file) => {
fs.unlinkSync(file);
});
await ncc(dep, nccConfig).then(
({ code, assets }: { code: string; assets: Record<string, { source: string; permissions: number }> }) => {
// emit dist file
fs.writeFileSync(mainFile, code, 'utf-8');
// emit assets
Object.entries(assets).forEach(([name, item]) => {
fs.writeFileSync(path.join(outputDir, name), item.source, {
encoding: 'utf-8',
mode: item.permissions,
});
});
// emit package.json
fs.writeFileSync(
outputPackageJson,
JSON.stringify({
...pkg,
_lastModified: new Date().toISOString(),
}),
'utf-8',
);
},
);
}
}
export async function buildPluginServer(cwd: string, log: Log) {
log('build server source');
const packageJson = getPackageJson(cwd);
const serverFiles = fg.globSync(serverGlobalFiles, { cwd, absolute: true });
buildCheck({ cwd, packageJson, entry: 'server', files: serverFiles, log });
await tsupBuild({
entry: serverFiles,
splitting: false,
clean: false,
bundle: false,
silent: true,
treeshake: true,
target: 'node16',
outDir: path.join(cwd, target_dir),
format: 'cjs',
skipNodeModulesBundle: true,
});
await buildServerDeps(cwd, serverFiles, log);
}
export function buildPluginClient(cwd: string, log: Log) {
log('build client');
const packageJson = getPackageJson(cwd);
const clientFiles = fg.globSync(clientGlobalFiles, { cwd, absolute: true });
const sourcePackages = getSourcePackages(clientFiles);
const excludePackages = getExcludePackages(sourcePackages, external, pluginPrefix);
buildCheck({ cwd, packageJson, entry: 'client', files: clientFiles, log });
const outDir = path.join(cwd, target_dir, 'client');
const globals = excludePackages.reduce<Record<string, string>>((prev, curr) => {
if (curr.startsWith('@nocobase')) {
prev[`${curr}/client`] = curr;
}
prev[curr] = curr;
return prev;
}, {});
const entry = fg.globSync('src/client/index.{ts,tsx,js,jsx}', { cwd });
const outputFileName = 'index.js';
return viteBuild({
mode: 'production',
define: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
logLevel: 'warn',
build: {
minify: false,
outDir,
cssCodeSplit: false,
emptyOutDir: false,
lib: {
entry,
formats: ['umd'],
name: packageJson.name,
fileName: () => outputFileName,
},
rollupOptions: {
cache: true,
external: Object.keys(globals),
output: {
exports: 'named',
globals,
},
},
},
plugins: [
react(),
cssInjectedByJsPlugin({ styleId: packageJson.name }),
{
name: 'check-file-size',
closeBundle() {
const file = path.join(outDir, outputFileName);
if (!fs.existsSync(file)) return;
const fileSize = getFileSize(path.join(outDir, outputFileName));
if (fileSize > 1024 * 1024) {
log('The bundle file size exceeds 1MB %s. ', chalk.red(formatFileSize(fileSize)));
}
},
},
],
});
}