1+ const glob = require ( "glob" ) ;
2+ const babel = require ( "@babel/core" ) ;
3+ const path = require ( "path" ) ;
4+ const fs = require ( "fs-extra" ) ;
5+ const getRaxBabelConfig = require ( "rax-babel-config" ) ;
6+ const getCompileBabel = require ( "build-plugin-component/src/utils/getCompileBabel" ) ;
7+ const dts = require ( 'build-plugin-component/src/compiler/dts' ) ;
8+
9+ const defaultDynamicImportLibraries = [
10+ "antd" ,
11+ "@alifd/next" ,
12+ "@alife/next" ,
13+ "@icedesign/base" ,
14+ ] ;
15+
16+ const getBabelConfig = ( {
17+ target,
18+ componentLibs = defaultDynamicImportLibraries ,
19+ rootDir,
20+ babelPlugins,
21+ babelOptions,
22+ type,
23+ alias,
24+ root = 'lowcode' ,
25+ } ) => {
26+ const params = target === "es" ? { modules : false } : { } ;
27+ let babelConfig ;
28+ if ( type === "react" ) {
29+ babelConfig = getCompileBabel ( params , {
30+ babelPlugins,
31+ babelOptions,
32+ rootDir,
33+ } ) ;
34+ } else {
35+ babelConfig = getRaxBabelConfig ( {
36+ // Be careful~ change it's value by inlineStyle may cause break-change
37+ styleSheet : true ,
38+ custom : {
39+ ignore : [ "**/**/*.d.ts" ] ,
40+ } ,
41+ ...params ,
42+ } ) ;
43+ babelConfig . presets . push ( [
44+ require . resolve ( "@babel/preset-typescript" ) ,
45+ { jsxPragma : "createElement" } ,
46+ ] ) ;
47+
48+ babelConfig . plugins = [ ...babelConfig . plugins , ...( babelPlugins || [ ] ) ] ;
49+ }
50+ // generate babel-plugin-import config
51+ const plugins = [ ] ;
52+ componentLibs . forEach ( ( libraryName ) => {
53+ // check es folder if target is es
54+ const pluginOption = {
55+ libraryName,
56+ style : false , // style file will be require in style.js
57+ } ;
58+ if ( target === "es" ) {
59+ [ "es" , "esm" ] . some ( ( item ) => {
60+ const dirPath = path . join ( rootDir , "node_modules" , libraryName , item ) ;
61+ const dirExist = fs . existsSync ( dirPath ) ;
62+
63+ if ( dirExist ) {
64+ pluginOption . libraryDirectory = item ;
65+ }
66+
67+ return dirExist ;
68+ } ) ;
69+ }
70+ plugins . push ( [
71+ require . resolve ( "babel-plugin-import" ) ,
72+ pluginOption ,
73+ libraryName ,
74+ ] ) ;
75+ } ) ;
76+ babelConfig . plugins = babelConfig . plugins . concat ( plugins ) ;
77+ if ( alias ) {
78+ const aliasRelative = { } ;
79+ Object . keys ( alias ) . forEach ( ( aliasKey ) => {
80+ aliasRelative [ aliasKey ] = alias [ aliasKey ] . startsWith ( "./" )
81+ ? alias [ aliasKey ]
82+ : `./${ alias [ aliasKey ] } ` ;
83+ } ) ;
84+ babelConfig . plugins = babelConfig . plugins . concat ( [
85+ [
86+ require . resolve ( "babel-plugin-module-resolver" ) ,
87+ {
88+ root : [ root ] ,
89+ alias : aliasRelative ,
90+ } ,
91+ ] ,
92+ ] ) ;
93+ }
94+ return babelConfig ;
95+ } ;
96+
97+ const findGitIgnorePath = ( rootDir ) => {
98+ let dir = rootDir ;
99+ let gitignorePath ;
100+ while ( dir !== '/' ) {
101+ const tempPath = path . join ( dir , ".gitignore" )
102+ const fileExists = fs . pathExistsSync ( tempPath ) ;
103+ if ( fileExists ) {
104+ gitignorePath = tempPath ;
105+ break ;
106+ } else {
107+ dir = path . dirname ( dir ) ;
108+ }
109+ }
110+ return gitignorePath ;
111+ }
112+
113+ const reg = {
114+ REG_TS : / \. ( t s x ? ) $ / ,
115+ REG_D_TS : / \. d \. t s $ / ,
116+
117+ REG_JS : / \. ( j s x ? | t s x ? ) $ / ,
118+ REG_SASS : / \. ( s a | s c | c ) s s $ / ,
119+ REG_LESS : / \. ( l e | c ) s s $ / ,
120+
121+ REG_JS_INDEX : / i n d e x \. ( j s x ? | t s x ? ) $ / ,
122+ REG_SASS_INDEX : / i n d e x \. ( s a | s c | c ) s s $ / ,
123+ REG_LESS_INDEX : / i n d e x \. ( l e | c ) s s $ / ,
124+ } ;
125+
126+
127+ const babelCompile = async ( {
128+ source,
129+ target,
130+ rootDir,
131+ userOptions,
132+ type = "react" ,
133+ } ) => {
134+ const { REG_SASS , REG_LESS , REG_JS , REG_D_TS } = reg ;
135+ const filesPath = glob . sync ( "**/*.*" , {
136+ cwd : source ,
137+ ignore : [ "node_modules/**" ] ,
138+ } ) ;
139+ const compileInfo = [ ] ;
140+ [ 'lib' , 'es' ] . forEach ( ( target ) => {
141+ const targetPath = `${ source } _${ target } ` ;
142+ const distDirPath = path . join ( rootDir , targetPath ) ;
143+ const { babelPlugins = [ ] , babelOptions = [ ] , alias } = userOptions ;
144+ fs . removeSync ( distDirPath ) ;
145+ fs . ensureDirSync ( distDirPath ) ;
146+ filesPath . forEach ( ( filePath ) => {
147+ const sourceFile = path . join ( rootDir , source , filePath ) ;
148+ if ( ! REG_JS . test ( filePath ) || REG_D_TS . test ( filePath ) ) {
149+ // copy file if it does not match REG_JS
150+ try {
151+ fs . copySync ( sourceFile , path . join ( distDirPath , filePath ) ) ;
152+ console . log ( `file ${ filePath } copied` ) ;
153+ } catch ( err ) {
154+ throw new Error ( err ) ;
155+ }
156+ } else {
157+ const distFile = path . join ( distDirPath , filePath . replace ( REG_JS , ".js" ) ) ;
158+ const babelConfig = getBabelConfig ( {
159+ target,
160+ rootDir,
161+ babelOptions,
162+ babelPlugins,
163+ type,
164+ alias,
165+ } ) ;
166+ const { code } = babel . transformFileSync ( sourceFile , {
167+ filename : distFile ,
168+ ...babelConfig ,
169+ } ) ;
170+ fs . ensureDirSync ( path . dirname ( distFile ) ) ;
171+ fs . writeFileSync ( distFile , code , "utf-8" ) ;
172+ compileInfo . push ( {
173+ filePath,
174+ sourceFile,
175+ destPath : distDirPath ,
176+ } ) ;
177+ }
178+ } ) ;
179+ // 检查 .gitignore 如果没有产出路径,则增加该配置
180+ const gitignorePath = findGitIgnorePath ( rootDir ) ;
181+ if ( gitignorePath ) {
182+ const gitignoreFile = fs . readFileSync ( gitignorePath , "utf-8" ) ;
183+ if ( ! ( new RegExp ( `${ targetPath } /` ) ) . test ( gitignoreFile ) ) {
184+ const newGitignoreFile = `${ targetPath } /\r\n${ gitignoreFile } ` ;
185+ fs . writeFileSync ( gitignorePath , newGitignoreFile ) ;
186+ }
187+ }
188+ } ) ;
189+ // 生成声明文件
190+ dts ( compileInfo , {
191+ log : console ,
192+ } )
193+ } ;
194+
195+ babelCompile . getBabelConfig = getBabelConfig ;
196+ module . exports = babelCompile ;
0 commit comments