2019-11-21 13:44:32 +08:00
|
|
|
|
// @see https://babeljs.io/docs/en/next/config-files#project-wide-configuration
|
2022-08-02 19:05:27 +08:00
|
|
|
|
module.exports = (api) => {
|
2019-11-21 13:44:32 +08:00
|
|
|
|
api.cache(() => process.env.NODE_ENV);
|
2019-11-25 15:27:56 +08:00
|
|
|
|
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');
|
2022-08-02 19:05:27 +08:00
|
|
|
|
|
2019-11-25 15:27:56 +08:00
|
|
|
|
if (isSite) {
|
2019-11-21 13:44:32 +08:00
|
|
|
|
return {
|
2022-08-08 16:41:30 +08:00
|
|
|
|
skipEnvCheck: true,
|
2019-11-21 13:44:32 +08:00
|
|
|
|
presets: [
|
2021-08-23 18:15:33 +08:00
|
|
|
|
'@babel/preset-env',
|
2021-08-23 19:15:21 +08:00
|
|
|
|
[
|
|
|
|
|
'@babel/preset-react',
|
|
|
|
|
{
|
2022-08-02 19:05:27 +08:00
|
|
|
|
development: isCommonJS,
|
|
|
|
|
},
|
2021-08-23 19:15:21 +08:00
|
|
|
|
],
|
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',
|
|
|
|
|
{
|
2022-08-02 19:05:27 +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',
|
2022-08-02 19:05:27 +08:00
|
|
|
|
'.glsl',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
],
|
2019-11-21 13:44:32 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2019-11-22 17:05:22 +08:00
|
|
|
|
|
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',
|
2022-08-02 19:05:27 +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
|
2022-08-02 19:05:27 +08:00
|
|
|
|
modules: isCDNBundle || isESModule ? false : 'auto',
|
2019-12-20 16:56:06 +08:00
|
|
|
|
targets: {
|
|
|
|
|
chrome: 58,
|
2022-08-02 19:05:27 +08:00
|
|
|
|
browsers: ['ie >= 11'],
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-11-21 13:44:32 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'@babel/preset-react',
|
|
|
|
|
{
|
2022-08-02 19:05:27 +08:00
|
|
|
|
development: isCommonJS,
|
|
|
|
|
},
|
2019-11-21 13:44:32 +08:00
|
|
|
|
],
|
2022-08-02 19:05:27 +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',
|
2019-11-29 14:44:40 +08:00
|
|
|
|
'@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',
|
2020-02-24 14:10:54 +08:00
|
|
|
|
'transform-node-env-inline',
|
2019-11-21 13:44:32 +08:00
|
|
|
|
[
|
|
|
|
|
'@babel/plugin-proposal-decorators',
|
|
|
|
|
{
|
2022-08-02 19:05:27 +08:00
|
|
|
|
legacy: true,
|
|
|
|
|
},
|
2019-11-21 13:44:32 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'@babel/plugin-proposal-class-properties',
|
|
|
|
|
{
|
|
|
|
|
// @see https://github.com/storybookjs/storybook/issues/6069#issuecomment-472544973
|
2022-08-02 19:05:27 +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
|
2022-08-02 19:05:27 +08:00
|
|
|
|
isCDNBundle || isESModule
|
|
|
|
|
? {}
|
|
|
|
|
: '@babel/plugin-transform-modules-commonjs',
|
2019-11-21 13:44:32 +08:00
|
|
|
|
// 开发模式下以原始文本引入,便于调试
|
2022-08-02 19:05:27 +08:00
|
|
|
|
isCDNBundle
|
|
|
|
|
? {}
|
|
|
|
|
: [
|
|
|
|
|
// import glsl as raw text
|
|
|
|
|
'babel-plugin-inline-import',
|
|
|
|
|
{
|
|
|
|
|
extensions: [
|
|
|
|
|
// 由于使用了 TS 的 resolveJsonModule 选项,JSON 可以直接引入,不需要当作纯文本
|
|
|
|
|
// '.json',
|
|
|
|
|
'.glsl',
|
|
|
|
|
'.worker.js',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
isCDNBundle
|
|
|
|
|
? {}
|
|
|
|
|
: [
|
|
|
|
|
'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',
|
|
|
|
|
{
|
2022-08-02 19:05:27 +08:00
|
|
|
|
transform: 'constObject',
|
|
|
|
|
},
|
2019-11-21 13:44:32 +08:00
|
|
|
|
],
|
|
|
|
|
// 按需引用 @see https://github.com/lodash/babel-plugin-lodash
|
2022-08-02 19:05:27 +08:00
|
|
|
|
'lodash',
|
2019-11-21 13:44:32 +08:00
|
|
|
|
// 内联 WebGL 常量 @see https://www.npmjs.com/package/babel-plugin-inline-webgl-constants
|
2019-11-25 15:27:56 +08:00
|
|
|
|
// 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',
|
2022-08-02 19:05:27 +08:00
|
|
|
|
...(!isTest
|
|
|
|
|
? [
|
|
|
|
|
'**/*.test.tsx',
|
|
|
|
|
'**/*.test.ts',
|
|
|
|
|
'**/*.story.tsx',
|
|
|
|
|
'__snapshots__',
|
|
|
|
|
'__tests__',
|
|
|
|
|
'__stories__',
|
|
|
|
|
'**/*/__snapshots__',
|
|
|
|
|
'**/*/__tests__',
|
|
|
|
|
]
|
|
|
|
|
: []),
|
|
|
|
|
],
|
2019-11-21 13:44:32 +08:00
|
|
|
|
};
|
|
|
|
|
};
|