1
1
import * as fs from 'fs-extra' ;
2
2
import * as _ from 'lodash' ;
3
3
import * as path from 'path' ;
4
+ import * as gulp from 'gulp' ;
5
+ import * as gulpBabel from 'gulp-babel' ;
4
6
import { globalState } from './global-state' ;
5
- import { logFatal } from './log' ;
7
+ import { logFatal , spinner } from './log' ;
6
8
import {
7
9
IAfterProdBuild ,
8
10
IAnalyseProject ,
@@ -20,7 +22,8 @@ import {
20
22
IDevDllList ,
21
23
IJestConfigPipe ,
22
24
} from './define' ;
23
-
25
+ import { getBabelOptions } from './babel-options' ;
26
+ import { tempPath , srcPath } from '../node' ;
24
27
import * as pluginClientSsr from '../built-in-plugins/client-ssr' ;
25
28
import * as pluginCommandAnalyse from '../built-in-plugins/command-analyse' ;
26
29
import * as pluginCommandBuild from '../built-in-plugins/command-build' ;
@@ -100,6 +103,19 @@ export const plugin: IPluginConfig = new IPluginConfig();
100
103
101
104
let hasInitPlugins = false ;
102
105
106
+ const buildPluginSource = ( packagePath : string , outdir : string ) => {
107
+ const targetPath = path . join ( packagePath , srcPath . dir , '**/*.{ts,tsx}' ) ;
108
+
109
+ return new Promise ( ( resolve , reject ) => {
110
+ gulp
111
+ . src ( targetPath )
112
+ . pipe ( gulpBabel ( getBabelOptions ( ) ) )
113
+ . on ( 'error' , reject )
114
+ . pipe ( gulp . dest ( outdir ) )
115
+ . on ( 'end' , resolve ) ;
116
+ } ) ;
117
+ } ;
118
+
103
119
export const loadPlugins = async ( pluginIncludeRoots : string [ ] = [ ] ) => {
104
120
if ( hasInitPlugins ) {
105
121
return ;
@@ -132,7 +148,7 @@ export const loadPlugins = async (pluginIncludeRoots: string[] = []) => {
132
148
} ) ;
133
149
134
150
if ( globalState . sourceConfig . type !== 'plugin' ) {
135
- getPriPlugins (
151
+ await getPriPlugins (
136
152
globalState . projectRootPath ,
137
153
pluginIncludeRoots . concat ( globalState . projectRootPath ) . map ( pluginIncludeRoot => {
138
154
return path . join ( pluginIncludeRoot , 'package.json' ) ;
@@ -151,13 +167,24 @@ export const loadPlugins = async (pluginIncludeRoots: string[] = []) => {
151
167
}
152
168
} ;
153
169
154
- function getPriPlugins ( pluginRootPath : string , packageJsonPaths : string [ ] ) {
170
+ async function getPriPlugins ( pluginRootPath : string , packageJsonPaths : string [ ] ) {
155
171
// Do not load plugins when type is 'plugin'.
156
172
// Load plugin even when type is undefined.
157
173
if ( globalState . sourceConfig . type === 'plugin' ) {
158
174
return ;
159
175
}
160
176
177
+ for ( const eachPackage of globalState . packages ) {
178
+ if ( eachPackage . config ?. type === 'plugin' ) {
179
+ const distPath = path . join ( globalState . projectRootPath , tempPath . dir , 'plugins' , eachPackage . name ) ;
180
+ await spinner ( `Build plugin ${ eachPackage . name } ` , async ( ) => {
181
+ await fs . remove ( distPath ) ;
182
+ await buildPluginSource ( eachPackage . rootPath , distPath ) ;
183
+ } ) ;
184
+ addPluginFromEntry ( distPath ) ;
185
+ }
186
+ }
187
+
161
188
const deps = packageJsonPaths . map ( packageJsonPath => {
162
189
return getDependencesByPackageJsonPath ( packageJsonPath ) ;
163
190
} ) ;
@@ -193,22 +220,7 @@ function getPriPlugins(pluginRootPath: string, packageJsonPaths: string[]) {
193
220
? getPackageJsonPathByPathOrNpmName ( subPackageName , pluginRootPath )
194
221
: path . resolve ( pluginRootPath , subPackageVersion . replace ( / ^ f i l e : / g, '' ) , 'package.json' ) ;
195
222
196
- // eslint-disable-next-line global-require,@typescript-eslint/no-var-requires,import/no-dynamic-require
197
- const instance : IPluginModule = require ( subPackageRealEntryFilePath ) ;
198
-
199
- if ( ! instance . getConfig ) {
200
- logFatal ( 'Plugin must impletement getConfig method!' ) ;
201
- }
202
-
203
- if ( ! instance . getPlugin ) {
204
- logFatal ( 'Plugin must impletement getPlugin method!' ) ;
205
- }
206
-
207
- if ( ! instance . getConfig ( ) . name ) {
208
- logFatal ( 'Plugin must have name!' ) ;
209
- }
210
-
211
- loadedPlugins . add ( instance ) ;
223
+ addPluginFromEntry ( subPackageRealEntryFilePath ) ;
212
224
213
225
if ( subPackageAbsolutePath ) {
214
226
getPriPlugins ( path . resolve ( subPackageAbsolutePath , '..' ) , [ subPackageAbsolutePath ] ) ;
@@ -309,3 +321,22 @@ function getDependencesByPackageJsonPath(packageJsonPath: string) {
309
321
..._ . get ( packageJson , 'devDependencies' , { } ) ,
310
322
} ;
311
323
}
324
+
325
+ function addPluginFromEntry ( entryPath : string ) {
326
+ // eslint-disable-next-line import/no-dynamic-require,@typescript-eslint/no-var-requires,global-require
327
+ const instance : IPluginModule = require ( entryPath ) ;
328
+
329
+ if ( ! instance . getConfig ) {
330
+ logFatal ( 'Plugin must impletement getConfig method!' ) ;
331
+ }
332
+
333
+ if ( ! instance . getPlugin ) {
334
+ logFatal ( 'Plugin must impletement getPlugin method!' ) ;
335
+ }
336
+
337
+ if ( ! instance . getConfig ( ) . name ) {
338
+ logFatal ( 'Plugin must have name!' ) ;
339
+ }
340
+
341
+ loadedPlugins . add ( instance ) ;
342
+ }
0 commit comments