mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-30 02:58:05 +08:00
175 lines
3.9 KiB
JavaScript
175 lines
3.9 KiB
JavaScript
// 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';
|
||
import svgr from '@svgr/rollup';
|
||
|
||
const settings = {
|
||
globals: {}
|
||
};
|
||
|
||
const external = id => {
|
||
const result = new RegExp(
|
||
`^(?:${Object.keys(dependencies)
|
||
.concat([
|
||
'monaco-editor',
|
||
'react',
|
||
'react-dom',
|
||
'rc-input-number',
|
||
'@rc-component/mini-decimal',
|
||
'@babel/runtime'
|
||
])
|
||
.map(value =>
|
||
value.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
|
||
)
|
||
.join('|')})`
|
||
).test(id);
|
||
|
||
if (!result && ~id.indexOf('node_modules')) {
|
||
console.log(id);
|
||
}
|
||
|
||
return result;
|
||
};
|
||
const input = './src/index.tsx';
|
||
|
||
/** 获取子包编译后的入口路径,需要使用相对路径 */
|
||
const getCompiledEntryPath = (repo, format) =>
|
||
path.join(
|
||
'..',
|
||
repo,
|
||
repo === 'amis-formula' || format === 'cjs' ? 'lib' : 'esm',
|
||
'index.js'
|
||
);
|
||
|
||
export default [
|
||
{
|
||
input,
|
||
|
||
output: [
|
||
{
|
||
...settings,
|
||
dir: path.dirname(main),
|
||
format: 'cjs',
|
||
exports: 'named',
|
||
preserveModulesRoot: './src',
|
||
preserveModules: true // Keep directory structure and files
|
||
}
|
||
],
|
||
external,
|
||
plugins: getPlugins('cjs')
|
||
},
|
||
|
||
{
|
||
input,
|
||
|
||
output: [
|
||
{
|
||
...settings,
|
||
dir: path.dirname(module),
|
||
format: 'esm',
|
||
exports: 'named',
|
||
preserveModulesRoot: './src',
|
||
preserveModules: true // Keep directory structure and files
|
||
}
|
||
],
|
||
external,
|
||
plugins: getPlugins('esm')
|
||
}
|
||
];
|
||
|
||
function transpileDynamicImportForCJS(options) {
|
||
return {
|
||
name: 'transpile-dynamic-import-for-cjs',
|
||
renderDynamicImport({format, targetModuleId}) {
|
||
if (format !== 'cjs') {
|
||
return null;
|
||
}
|
||
|
||
return {
|
||
left: 'Promise.resolve().then(function() {return new Promise(function(fullfill) {require([',
|
||
right: '], function(mod) {fullfill(tslib.__importStar(mod))})})})'
|
||
};
|
||
|
||
// return {
|
||
// left: 'Promise.resolve().then(function() {return new Promise(function(fullfill) {require.ensure([',
|
||
// right:
|
||
// '], function(r) {fullfill(_interopDefaultLegacy(r("' +
|
||
// targetModuleId +
|
||
// '")))})})})'
|
||
// };
|
||
}
|
||
};
|
||
}
|
||
|
||
function getPlugins(format = 'esm') {
|
||
const overridePaths = ['amis-formula', 'amis-core', 'amis-ui'].reduce(
|
||
(prev, current) => ({
|
||
...prev,
|
||
[current]: [getCompiledEntryPath(current, format)]
|
||
}),
|
||
{}
|
||
);
|
||
const typeScriptOptions = {
|
||
typescript: require('typescript'),
|
||
sourceMap: false,
|
||
outputToFilesystem: true,
|
||
...(format === 'esm'
|
||
? {
|
||
compilerOptions: {
|
||
rootDir: './src',
|
||
outDir: path.dirname(module),
|
||
/** 覆盖继承自顶层tsconfig的paths配置,编译时应该去掉,避免报错@rollup/plugin-typescript TS6305 */
|
||
paths: overridePaths
|
||
}
|
||
}
|
||
: {
|
||
compilerOptions: {
|
||
rootDir: './src',
|
||
outDir: path.dirname(main),
|
||
paths: overridePaths
|
||
}
|
||
})
|
||
};
|
||
|
||
return [
|
||
svgr({
|
||
svgProps: {
|
||
className: 'icon'
|
||
},
|
||
prettier: false,
|
||
dimensions: false
|
||
}),
|
||
,
|
||
transpileDynamicImportForCJS(),
|
||
autoExternal(),
|
||
json(),
|
||
resolve({
|
||
jsnext: true,
|
||
main: true
|
||
}),
|
||
typescript(typeScriptOptions),
|
||
commonjs({
|
||
sourceMap: false
|
||
}),
|
||
license({
|
||
banner: `
|
||
${name} v${version}
|
||
Copyright 2018<%= moment().format('YYYY') > 2018 ? '-' + moment().format('YYYY') : null %> ${author}
|
||
`
|
||
})
|
||
];
|
||
}
|