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);
|
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');
|
2019-11-22 17:05:22 +08:00
|
|
|
|
|
2019-11-25 15:27:56 +08:00
|
|
|
|
if (isSite) {
|
2019-11-21 13:44:32 +08:00
|
|
|
|
return {
|
|
|
|
|
presets: [
|
2021-07-26 15:10:13 +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-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',
|
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',
|
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',
|
|
|
|
|
{
|
|
|
|
|
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 可以直接引入,不需要当作纯文本
|
2020-05-12 23:03:01 +08:00
|
|
|
|
// '.json',
|
2019-11-21 13:44:32 +08:00
|
|
|
|
'.glsl'
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
2019-11-21 17:36:04 +08:00
|
|
|
|
isCDNBundle ? {} : [
|
2019-11-22 20:05:32 +08:00
|
|
|
|
'transform-import-styles'
|
2019-11-21 17:36:04 +08:00
|
|
|
|
],
|
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
|
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',
|
2019-11-21 13:44:32 +08:00
|
|
|
|
...!isTest ? [
|
|
|
|
|
'**/*.test.tsx',
|
|
|
|
|
'**/*.test.ts',
|
|
|
|
|
'**/*.story.tsx',
|
|
|
|
|
'__snapshots__',
|
|
|
|
|
'__tests__',
|
|
|
|
|
'__stories__',
|
|
|
|
|
'**/*/__snapshots__',
|
|
|
|
|
'**/*/__tests__'
|
|
|
|
|
] : []
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
};
|