This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(babel-preset-remax): 支持配置 TypeScript preset 参数 (#420)
resolve #417
- Loading branch information
Showing
12 changed files
with
187 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import runTest from './helpers/runTest'; | ||
|
||
describe('custom root dir config', () => { | ||
describe('customize root dir config', () => { | ||
runTest('customRootDir'); | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/remax-cli/src/__tests__/fixtures/alipay/babel.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = function(api) { | ||
api.cache(true); | ||
|
||
return { | ||
presets: [['babel-preset-remax']], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
packages/remax-cli/src/__tests__/fixtures/babelrc/src/pages/index.js
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
packages/remax-cli/src/__tests__/fixtures/babelrc/src/pages/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as React from 'react'; | ||
import { View } from 'remax/alipay'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace N { | ||
export const V = 1; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace N { | ||
export const W = V; | ||
} | ||
|
||
function timesTwo(arr: number[]) { | ||
return arr.map(n => n * 2); | ||
} | ||
|
||
export default () => ( | ||
<View> | ||
{timesTwo([1, 2, 3])} | ||
{N.V} | ||
{N.W} | ||
</View> | ||
); |
7 changes: 7 additions & 0 deletions
7
packages/remax-cli/src/__tests__/fixtures/toutiao/babel.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = function(api) { | ||
api.cache(true); | ||
|
||
return { | ||
presets: [['babel-preset-remax']], | ||
}; | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/remax-cli/src/__tests__/fixtures/wechat/babel.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = function(api) { | ||
api.cache(true); | ||
|
||
return { | ||
presets: [['babel-preset-remax']], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import babel from 'rollup-plugin-babel'; | ||
import { PartialConfig, ConfigItem } from '@babel/core'; | ||
import { merge } from 'lodash'; | ||
|
||
interface CustomOptions { | ||
reactPreset: boolean; | ||
usePlugins: any[]; | ||
} | ||
|
||
function processPresets(presets: ConfigItem[], babel: any, react: boolean) { | ||
const remaxPresetIndex = presets.findIndex( | ||
preset => | ||
preset.file && | ||
(preset.file.resolved === require.resolve('babel-preset-remax') || | ||
preset.file.request === 'remax' || | ||
preset.file.request === 'babel-preset-remax') | ||
); | ||
|
||
const defaultOptions = { | ||
react, | ||
}; | ||
|
||
const existOptions = | ||
remaxPresetIndex !== -1 ? presets[remaxPresetIndex].options : {}; | ||
|
||
const remaxPreset = babel.createConfigItem( | ||
[ | ||
require.resolve('babel-preset-remax'), | ||
merge({}, defaultOptions, existOptions), | ||
], | ||
{ | ||
type: `preset`, | ||
} | ||
); | ||
|
||
if (remaxPresetIndex === -1) { | ||
presets.unshift(remaxPreset); | ||
} else { | ||
presets[remaxPreset] = remaxPreset; | ||
} | ||
|
||
return presets; | ||
} | ||
|
||
export default babel.custom((babelCore: any) => { | ||
return { | ||
options({ reactPreset, usePlugins, ...pluginOptions }: CustomOptions) { | ||
return { | ||
customOptions: { | ||
reactPreset, | ||
usePlugins, | ||
}, | ||
pluginOptions, | ||
}; | ||
}, | ||
|
||
config( | ||
cfg: PartialConfig, | ||
{ customOptions }: { customOptions: CustomOptions } | ||
) { | ||
const presets = processPresets( | ||
cfg.options.presets as ConfigItem[], | ||
babelCore, | ||
customOptions.reactPreset | ||
); | ||
|
||
return { | ||
...cfg.options, | ||
presets, | ||
plugins: [ | ||
...(cfg.options.plugins || []), | ||
...(customOptions.usePlugins || []), | ||
], | ||
}; | ||
}, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters