11import * as fs from 'fs-extra' ;
22import * as _ from 'lodash' ;
33import * as path from 'path' ;
4+ import * as gulp from 'gulp' ;
5+ import * as gulpBabel from 'gulp-babel' ;
46import { globalState } from './global-state' ;
5- import { logFatal } from './log' ;
7+ import { logFatal , spinner } from './log' ;
68import {
79 IAfterProdBuild ,
810 IAnalyseProject ,
@@ -20,7 +22,8 @@ import {
2022 IDevDllList ,
2123 IJestConfigPipe ,
2224} from './define' ;
23-
25+ import { getBabelOptions } from './babel-options' ;
26+ import { tempPath , srcPath } from '../node' ;
2427import * as pluginClientSsr from '../built-in-plugins/client-ssr' ;
2528import * as pluginCommandAnalyse from '../built-in-plugins/command-analyse' ;
2629import * as pluginCommandBuild from '../built-in-plugins/command-build' ;
@@ -100,6 +103,19 @@ export const plugin: IPluginConfig = new IPluginConfig();
100103
101104let hasInitPlugins = false ;
102105
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+
103119export const loadPlugins = async ( pluginIncludeRoots : string [ ] = [ ] ) => {
104120 if ( hasInitPlugins ) {
105121 return ;
@@ -132,7 +148,7 @@ export const loadPlugins = async (pluginIncludeRoots: string[] = []) => {
132148 } ) ;
133149
134150 if ( globalState . sourceConfig . type !== 'plugin' ) {
135- getPriPlugins (
151+ await getPriPlugins (
136152 globalState . projectRootPath ,
137153 pluginIncludeRoots . concat ( globalState . projectRootPath ) . map ( pluginIncludeRoot => {
138154 return path . join ( pluginIncludeRoot , 'package.json' ) ;
@@ -151,13 +167,24 @@ export const loadPlugins = async (pluginIncludeRoots: string[] = []) => {
151167 }
152168} ;
153169
154- function getPriPlugins ( pluginRootPath : string , packageJsonPaths : string [ ] ) {
170+ async function getPriPlugins ( pluginRootPath : string , packageJsonPaths : string [ ] ) {
155171 // Do not load plugins when type is 'plugin'.
156172 // Load plugin even when type is undefined.
157173 if ( globalState . sourceConfig . type === 'plugin' ) {
158174 return ;
159175 }
160176
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+
161188 const deps = packageJsonPaths . map ( packageJsonPath => {
162189 return getDependencesByPackageJsonPath ( packageJsonPath ) ;
163190 } ) ;
@@ -193,22 +220,7 @@ function getPriPlugins(pluginRootPath: string, packageJsonPaths: string[]) {
193220 ? getPackageJsonPathByPathOrNpmName ( subPackageName , pluginRootPath )
194221 : path . resolve ( pluginRootPath , subPackageVersion . replace ( / ^ f i l e : / g, '' ) , 'package.json' ) ;
195222
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 ) ;
212224
213225 if ( subPackageAbsolutePath ) {
214226 getPriPlugins ( path . resolve ( subPackageAbsolutePath , '..' ) , [ subPackageAbsolutePath ] ) ;
@@ -309,3 +321,22 @@ function getDependencesByPackageJsonPath(packageJsonPath: string) {
309321 ..._ . get ( packageJson , 'devDependencies' , { } ) ,
310322 } ;
311323}
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