2022-06-01 15:06:00 +08:00
|
|
|
|
// rollup.config.js
|
|
|
|
|
import commonjs from '@rollup/plugin-commonjs';
|
|
|
|
|
import json from '@rollup/plugin-json';
|
|
|
|
|
import resolve from '@rollup/plugin-node-resolve';
|
|
|
|
|
import typescript from '@rollup/plugin-typescript';
|
|
|
|
|
import license from 'rollup-plugin-license';
|
|
|
|
|
import autoExternal from 'rollup-plugin-auto-external';
|
|
|
|
|
import {
|
|
|
|
|
name,
|
|
|
|
|
version,
|
|
|
|
|
author,
|
|
|
|
|
main,
|
|
|
|
|
module,
|
|
|
|
|
dependencies
|
|
|
|
|
} from './package.json';
|
|
|
|
|
import path from 'path';
|
2024-09-27 15:22:41 +08:00
|
|
|
|
import fs from 'fs';
|
2022-06-01 15:06:00 +08:00
|
|
|
|
import svgr from '@svgr/rollup';
|
2023-09-26 17:31:39 +08:00
|
|
|
|
import moment from 'moment';
|
2024-09-13 10:57:25 +08:00
|
|
|
|
import babel from 'rollup-plugin-babel';
|
2022-06-01 15:06:00 +08:00
|
|
|
|
|
|
|
|
|
const settings = {
|
2023-09-26 17:31:39 +08:00
|
|
|
|
globals: {},
|
2024-09-27 15:22:41 +08:00
|
|
|
|
commonConfig: {}
|
2022-06-01 15:06:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-09-27 15:22:41 +08:00
|
|
|
|
const pkgs = [];
|
|
|
|
|
// 读取所有的node_modules目录,获取所有的包名
|
|
|
|
|
[
|
|
|
|
|
path.join(__dirname, './node_modules'),
|
|
|
|
|
path.join(__dirname, '../../node_modules')
|
|
|
|
|
].forEach(dir => {
|
|
|
|
|
if (fs.existsSync(dir)) {
|
|
|
|
|
fs.readdirSync(dir).forEach(item => {
|
|
|
|
|
if (item.startsWith('.')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.startsWith('@')) {
|
|
|
|
|
fs.readdirSync(path.join(dir, item)).forEach(subItem => {
|
|
|
|
|
pkgs.push(item + '/' + subItem);
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-11-03 12:12:14 +08:00
|
|
|
|
|
2024-09-27 15:22:41 +08:00
|
|
|
|
return pkgs.push(item);
|
|
|
|
|
});
|
2022-11-03 12:12:14 +08:00
|
|
|
|
}
|
2024-09-27 15:22:41 +08:00
|
|
|
|
});
|
|
|
|
|
const external = id =>
|
|
|
|
|
pkgs.some(pkg => id.startsWith(pkg) || ~id.indexOf(`node_modules/${pkg}`));
|
2022-11-03 12:12:14 +08:00
|
|
|
|
|
2022-06-01 15:06:00 +08:00
|
|
|
|
const input = './src/index.tsx';
|
|
|
|
|
|
2022-11-02 11:02:06 +08:00
|
|
|
|
/** 获取子包编译后的入口路径,需要使用相对路径 */
|
|
|
|
|
const getCompiledEntryPath = (repo, format) =>
|
|
|
|
|
path.join(
|
|
|
|
|
'..',
|
|
|
|
|
repo,
|
|
|
|
|
repo === 'amis-formula' || format === 'cjs' ? 'lib' : 'esm',
|
|
|
|
|
'index.js'
|
|
|
|
|
);
|
|
|
|
|
|
2022-06-01 15:06:00 +08:00
|
|
|
|
export default [
|
|
|
|
|
{
|
|
|
|
|
input,
|
|
|
|
|
|
|
|
|
|
output: [
|
|
|
|
|
{
|
|
|
|
|
...settings,
|
2023-09-26 17:31:39 +08:00
|
|
|
|
...settings.commonConfig,
|
2022-06-01 15:06:00 +08:00
|
|
|
|
dir: path.dirname(main),
|
|
|
|
|
format: 'cjs',
|
|
|
|
|
exports: 'named',
|
|
|
|
|
preserveModulesRoot: './src',
|
|
|
|
|
preserveModules: true // Keep directory structure and files
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
external,
|
|
|
|
|
plugins: getPlugins('cjs')
|
2022-06-16 21:07:14 +08:00
|
|
|
|
},
|
2022-06-01 15:06:00 +08:00
|
|
|
|
|
2022-06-16 21:07:14 +08:00
|
|
|
|
{
|
|
|
|
|
input,
|
2022-06-01 15:06:00 +08:00
|
|
|
|
|
2022-06-16 21:07:14 +08:00
|
|
|
|
output: [
|
|
|
|
|
{
|
|
|
|
|
...settings,
|
2023-09-26 17:31:39 +08:00
|
|
|
|
...settings.commonConfig,
|
2022-06-16 21:07:14 +08:00
|
|
|
|
dir: path.dirname(module),
|
|
|
|
|
format: 'esm',
|
|
|
|
|
exports: 'named',
|
|
|
|
|
preserveModulesRoot: './src',
|
|
|
|
|
preserveModules: true // Keep directory structure and files
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
external,
|
|
|
|
|
plugins: getPlugins('esm')
|
|
|
|
|
}
|
2022-06-01 15:06:00 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function transpileDynamicImportForCJS(options) {
|
|
|
|
|
return {
|
|
|
|
|
name: 'transpile-dynamic-import-for-cjs',
|
|
|
|
|
renderDynamicImport({format, targetModuleId}) {
|
|
|
|
|
if (format !== 'cjs') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2022-06-06 11:12:38 +08:00
|
|
|
|
left: 'Promise.resolve().then(function() {return new Promise(function(fullfill) {require([',
|
2024-09-13 10:57:25 +08:00
|
|
|
|
right:
|
|
|
|
|
', "tslib"], function(mod, tslib) {fullfill(tslib.__importStar(mod))})})})'
|
2022-06-01 15:06:00 +08:00
|
|
|
|
};
|
2022-06-06 11:12:38 +08:00
|
|
|
|
|
|
|
|
|
// return {
|
|
|
|
|
// left: 'Promise.resolve().then(function() {return new Promise(function(fullfill) {require.ensure([',
|
|
|
|
|
// right:
|
|
|
|
|
// '], function(r) {fullfill(_interopDefaultLegacy(r("' +
|
|
|
|
|
// targetModuleId +
|
|
|
|
|
// '")))})})})'
|
|
|
|
|
// };
|
2022-06-01 15:06:00 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 12:24:45 +08:00
|
|
|
|
// 参考:https://github.com/theKashey/jsx-compress-loader/blob/master/src/index.js
|
|
|
|
|
function transpileReactCreateElement() {
|
|
|
|
|
return {
|
|
|
|
|
name: 'transpile-react-create-element',
|
|
|
|
|
enforce: 'post',
|
|
|
|
|
transform: (code, id) => {
|
|
|
|
|
if (
|
2023-07-18 19:04:20 +08:00
|
|
|
|
/\.(?:tsx|jsx|svg)$/.test(id) &&
|
2023-07-18 12:24:45 +08:00
|
|
|
|
code.indexOf('React.createElement') !== -1
|
|
|
|
|
) {
|
|
|
|
|
const separator = '\n\n;';
|
|
|
|
|
const appendText =
|
|
|
|
|
`\n` +
|
|
|
|
|
`var __react_jsx__ = require('react');\n` +
|
|
|
|
|
`var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;\n` +
|
|
|
|
|
`var _J$F_ = (__react_jsx__["default"] || __react_jsx__).Fragment;\n`;
|
|
|
|
|
|
|
|
|
|
const newSource = code
|
|
|
|
|
.replace(/React\.createElement\(/g, '_J$X_(')
|
|
|
|
|
.replace(/React\.createElement\(/g, '_J$F_(');
|
|
|
|
|
|
|
|
|
|
code = [appendText, newSource].join(separator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
code
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 15:06:00 +08:00
|
|
|
|
function getPlugins(format = 'esm') {
|
2023-03-20 22:54:42 +08:00
|
|
|
|
const overridePaths = [
|
|
|
|
|
'amis-formula',
|
|
|
|
|
'amis-core',
|
|
|
|
|
'amis-ui',
|
2023-07-03 14:31:27 +08:00
|
|
|
|
'office-viewer'
|
2023-03-20 22:54:42 +08:00
|
|
|
|
].reduce(
|
2022-11-02 11:02:06 +08:00
|
|
|
|
(prev, current) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
[current]: [getCompiledEntryPath(current, format)]
|
|
|
|
|
}),
|
|
|
|
|
{}
|
|
|
|
|
);
|
2022-06-01 15:06:00 +08:00
|
|
|
|
const typeScriptOptions = {
|
|
|
|
|
typescript: require('typescript'),
|
|
|
|
|
sourceMap: false,
|
|
|
|
|
outputToFilesystem: true,
|
|
|
|
|
...(format === 'esm'
|
|
|
|
|
? {
|
|
|
|
|
compilerOptions: {
|
|
|
|
|
rootDir: './src',
|
2022-11-02 11:02:06 +08:00
|
|
|
|
outDir: path.dirname(module),
|
|
|
|
|
/** 覆盖继承自顶层tsconfig的paths配置,编译时应该去掉,避免报错@rollup/plugin-typescript TS6305 */
|
|
|
|
|
paths: overridePaths
|
2022-06-01 15:06:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
: {
|
|
|
|
|
compilerOptions: {
|
|
|
|
|
rootDir: './src',
|
2022-11-02 11:02:06 +08:00
|
|
|
|
outDir: path.dirname(main),
|
|
|
|
|
paths: overridePaths
|
2022-06-01 15:06:00 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return [
|
2022-06-13 14:38:39 +08:00
|
|
|
|
svgr({
|
|
|
|
|
svgProps: {
|
|
|
|
|
className: 'icon'
|
|
|
|
|
},
|
|
|
|
|
prettier: false,
|
|
|
|
|
dimensions: false
|
|
|
|
|
}),
|
|
|
|
|
,
|
2022-06-01 15:06:00 +08:00
|
|
|
|
transpileDynamicImportForCJS(),
|
|
|
|
|
autoExternal(),
|
|
|
|
|
json(),
|
|
|
|
|
resolve({
|
|
|
|
|
jsnext: true,
|
|
|
|
|
main: true
|
|
|
|
|
}),
|
2024-09-13 10:57:25 +08:00
|
|
|
|
format === 'esm'
|
|
|
|
|
? null
|
|
|
|
|
: babel({
|
|
|
|
|
exclude: 'node_modules/**',
|
|
|
|
|
extensions: ['.jsx', '.tsx', '.js', '.ts'],
|
|
|
|
|
plugins: [
|
|
|
|
|
[
|
|
|
|
|
'import',
|
|
|
|
|
{
|
|
|
|
|
libraryName: 'amis-ui',
|
|
|
|
|
libraryDirectory: 'lib',
|
|
|
|
|
camel2DashComponentName: false,
|
|
|
|
|
customName: (name, file) => {
|
|
|
|
|
if (
|
|
|
|
|
['alert', 'confirm', 'setRenderSchemaFn'].includes(name)
|
|
|
|
|
) {
|
|
|
|
|
return `amis-ui/lib/components/Alert`;
|
|
|
|
|
} else if (['toast'].includes(name)) {
|
|
|
|
|
return `amis-ui/lib/components/Toast`;
|
|
|
|
|
} else if ('NotFound' === name) {
|
|
|
|
|
return `amis-ui/lib/components/404`;
|
2024-09-18 19:03:49 +08:00
|
|
|
|
} else if (['withStore', 'withRemoteConfig'].includes(name)) {
|
2024-09-13 10:57:25 +08:00
|
|
|
|
return `amis-ui/lib/${name}`;
|
|
|
|
|
} /* else if (name[0].toUpperCase() === name[0]) {
|
|
|
|
|
return `amis-ui/lib/components/${name}`;
|
|
|
|
|
}*/
|
|
|
|
|
return `amis-ui/lib/components/${name}`;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'amis-ui'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
}),
|
2022-06-01 15:06:00 +08:00
|
|
|
|
typescript(typeScriptOptions),
|
|
|
|
|
commonjs({
|
|
|
|
|
sourceMap: false
|
|
|
|
|
}),
|
2023-07-18 12:24:45 +08:00
|
|
|
|
format === 'esm' ? null : transpileReactCreateElement(),
|
2022-06-01 15:06:00 +08:00
|
|
|
|
license({
|
|
|
|
|
banner: `
|
|
|
|
|
${name} v${version}
|
2023-09-26 17:31:39 +08:00
|
|
|
|
build time: <%=moment().format('YYYY-MM-DD')%>
|
2022-06-01 15:06:00 +08:00
|
|
|
|
Copyright 2018<%= moment().format('YYYY') > 2018 ? '-' + moment().format('YYYY') : null %> ${author}
|
|
|
|
|
`
|
|
|
|
|
})
|
2023-07-18 12:24:45 +08:00
|
|
|
|
].filter(item => item);
|
2022-06-01 15:06:00 +08:00
|
|
|
|
}
|