|
| 1 | +import babelTranspiler from '../../transpilers/babel'; |
| 2 | +import jsonTranspiler from '../../transpilers/json'; |
| 3 | +import stylesTranspiler from '../../transpilers/style'; |
| 4 | +import sassTranspiler from '../../transpilers/sass'; |
| 5 | +import rawTranspiler from '../../transpilers/raw'; |
| 6 | +import stylusTranspiler from '../../transpilers/stylus'; |
| 7 | +import lessTranspiler from '../../transpilers/less'; |
| 8 | +import tsTranspiler from '../../transpilers/typescript'; |
| 9 | + |
| 10 | +import Preset from '../'; |
| 11 | + |
| 12 | +export default function initialize() { |
| 13 | + const cxjsPreset = new Preset( |
| 14 | + 'cxjs', |
| 15 | + ['js', 'jsx', 'ts', 'tsx', 'json', 'less', 'scss', 'sass', 'styl', 'css'], |
| 16 | + {}, |
| 17 | + {} |
| 18 | + ); |
| 19 | + |
| 20 | + cxjsPreset.registerTranspiler(module => /\.jsx?$/.test(module.path), [ |
| 21 | + { |
| 22 | + transpiler: babelTranspiler, |
| 23 | + options: { |
| 24 | + dynamicCSSModules: true, |
| 25 | + }, |
| 26 | + }, |
| 27 | + ]); |
| 28 | + |
| 29 | + cxjsPreset.registerTranspiler(module => /\.tsx?$/.test(module.path), [ |
| 30 | + { transpiler: tsTranspiler }, |
| 31 | + ]); |
| 32 | + |
| 33 | + cxjsPreset.registerTranspiler(module => /\.css$/.test(module.path), [ |
| 34 | + { transpiler: stylesTranspiler }, |
| 35 | + ]); |
| 36 | + |
| 37 | + cxjsPreset.registerTranspiler(module => /\.json$/.test(module.path), [ |
| 38 | + { transpiler: jsonTranspiler }, |
| 39 | + ]); |
| 40 | + |
| 41 | + const sassWithConfig = { |
| 42 | + transpiler: sassTranspiler, |
| 43 | + options: {}, |
| 44 | + }; |
| 45 | + |
| 46 | + const lessWithConfig = { |
| 47 | + transpiler: lessTranspiler, |
| 48 | + options: {}, |
| 49 | + }; |
| 50 | + |
| 51 | + const stylusWithConfig = { |
| 52 | + transpiler: stylusTranspiler, |
| 53 | + options: {}, |
| 54 | + }; |
| 55 | + const styles = { |
| 56 | + css: [], |
| 57 | + scss: [sassWithConfig], |
| 58 | + sass: [sassWithConfig], |
| 59 | + less: [lessWithConfig], |
| 60 | + styl: [stylusWithConfig], |
| 61 | + }; |
| 62 | + |
| 63 | + /** |
| 64 | + * Registers transpilers for all different combinations |
| 65 | + * |
| 66 | + * @returns |
| 67 | + */ |
| 68 | + function registerStyleTranspilers() { |
| 69 | + return Object.keys(styles).forEach(type => { |
| 70 | + cxjsPreset.registerTranspiler( |
| 71 | + module => new RegExp(`\\.${type}`).test(module.path), |
| 72 | + [...styles[type], { transpiler: stylesTranspiler }] |
| 73 | + ); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + registerStyleTranspilers(); |
| 78 | + |
| 79 | + cxjsPreset.registerTranspiler(() => true, [{ transpiler: rawTranspiler }]); |
| 80 | + |
| 81 | + return cxjsPreset; |
| 82 | +} |
0 commit comments