antv-l7/babel.config.js

144 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-11-21 13:44:32 +08:00
// @see https://babeljs.io/docs/en/next/config-files#project-wide-configuration
module.exports = api => {
api.cache(() => process.env.NODE_ENV);
const isSite = api.env('site');
2019-11-21 13:44:32 +08:00
const isCDNBundle = api.env('bundle');
const isCommonJS = api.env('cjs');
const isESModule = api.env('esm');
const isTest = api.env('test');
2021-08-23 18:15:33 +08:00
if (isSite) {
2019-11-21 13:44:32 +08:00
return {
presets: [
2021-08-23 18:15:33 +08:00
'@babel/preset-env',
2021-08-23 19:15:21 +08:00
[
'@babel/preset-react',
{
development: isCommonJS
}
],
2021-08-23 18:15:33 +08:00
// 'babel-preset-gatsby', {
// silence: true
// },
2019-11-21 13:44:32 +08:00
],
plugins: [
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
2020-01-16 18:22:38 +08:00
'transform-inline-environment-variables',
2019-11-21 13:44:32 +08:00
[
'@babel/plugin-proposal-decorators',
{
2021-07-26 15:10:13 +08:00
legacy: true
2019-11-21 13:44:32 +08:00
}
],
[
// import glsl as raw text
'babel-plugin-inline-import',
{
extensions: [
// 由于使用了 TS 的 resolveJsonModule 选项JSON 可以直接引入,不需要当作纯文本
2020-04-24 17:01:12 +08:00
'.pbf',
2019-11-21 13:44:32 +08:00
'.glsl'
]
}
]
]
};
}
2019-11-21 13:44:32 +08:00
return {
presets: [
[
2020-09-02 18:24:23 +08:00
'@babel/preset-env',
2019-11-21 13:44:32 +08:00
{
2019-12-20 10:08:01 +08:00
// https://babeljs.io/docs/en/babel-preset-env#usebuiltins
2019-12-31 12:01:25 +08:00
// useBuiltIns: 'usage',
2020-09-02 18:24:23 +08:00
...isCDNBundle ? { corejs: 3 } : {},
2019-12-31 12:01:25 +08:00
useBuiltIns: isCDNBundle ? 'usage' : false,
2019-11-21 13:44:32 +08:00
// set `modules: false` when building CDN bundle, let rollup do commonjs works
// @see https://github.com/rollup/rollup-plugin-babel#modules
2019-12-20 16:56:06 +08:00
modules: (isCDNBundle || isESModule) ? false : 'auto',
targets: {
chrome: 58,
2020-02-13 16:36:22 +08:00
browsers: [ 'ie >= 11' ]
2019-12-20 16:56:06 +08:00
}
2019-11-21 13:44:32 +08:00
}
],
[
'@babel/preset-react',
{
development: isCommonJS
}
],
2019-12-20 00:16:00 +08:00
'@babel/preset-typescript'
2019-11-21 13:44:32 +08:00
],
plugins: [
2019-12-31 19:17:29 +08:00
isCDNBundle ? {} : '@babel/plugin-transform-runtime',
'@babel/plugin-proposal-object-rest-spread',
2019-11-21 13:44:32 +08:00
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
2019-12-20 16:56:06 +08:00
'@babel/plugin-syntax-async-generators',
2020-02-25 20:35:43 +08:00
// '@babel/plugin-transform-parameters',
'transform-node-env-inline',
2019-11-21 13:44:32 +08:00
[
'@babel/plugin-proposal-decorators',
{
legacy: true
}
],
[
'@babel/plugin-proposal-class-properties',
{
// @see https://github.com/storybookjs/storybook/issues/6069#issuecomment-472544973
2021-07-26 15:10:13 +08:00
loose: false
2019-11-21 13:44:32 +08:00
}
],
'@babel/plugin-syntax-dynamic-import',
// let rollup do commonjs works
// @see https://github.com/rollup/rollup-plugin-babel#modules
(isCDNBundle || isESModule) ? {} : '@babel/plugin-transform-modules-commonjs',
// 开发模式下以原始文本引入,便于调试
isCDNBundle ? {} : [
// import glsl as raw text
'babel-plugin-inline-import',
{
extensions: [
// 由于使用了 TS 的 resolveJsonModule 选项JSON 可以直接引入,不需要当作纯文本
// '.json',
2019-11-21 13:44:32 +08:00
'.glsl'
]
}
],
isCDNBundle ? {} : [
2021-08-05 17:02:11 +08:00
'transform-import-css-l7'
// 'transform-import-styles' // babel 编译将样式打包到js
],
2019-11-21 13:44:32 +08:00
[
// @see https://github.com/babel/babel/issues/8741#issuecomment-509041135
'const-enum',
{
transform: 'constObject'
}
],
// 按需引用 @see https://github.com/lodash/babel-plugin-lodash
2019-11-22 20:05:32 +08:00
'lodash'
2019-11-21 13:44:32 +08:00
// 内联 WebGL 常量 @see https://www.npmjs.com/package/babel-plugin-inline-webgl-constants
// isCDNBundle ? 'inline-webgl-constants' : {},
2019-11-21 13:44:32 +08:00
],
ignore: [
2020-02-25 20:35:43 +08:00
// /node_modules\/(?![kdbush|supercluster|async])/,
'node_modules',
2019-11-21 13:44:32 +08:00
...!isTest ? [
'**/*.test.tsx',
'**/*.test.ts',
'**/*.story.tsx',
'__snapshots__',
'__tests__',
'__stories__',
'**/*/__snapshots__',
'**/*/__tests__'
] : []
]
};
};