Skip to content

Commit a95b49f

Browse files
author
chengyu.chengyulia
committed
feat: support use plugin source
1 parent 70a76ed commit a95b49f

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

src/built-in-plugins/ensure-project-files/plugin/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function ensureTsconfig() {
102102
...(pri.sourceConfig.type === 'project' && { 'src/*': ['src/*'] }),
103103
// Packages alias names
104104
...globalState.packages.reduce((obj, eachPackage) => {
105-
if (eachPackage.packageJson && eachPackage.packageJson.name) {
105+
if (eachPackage.packageJson?.name && eachPackage.config?.type !== 'plugin') {
106106
return {
107107
...obj,
108108
[eachPackage.packageJson.name]: [

src/utils/plugins.ts

+51-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as fs from 'fs-extra';
22
import * as _ from 'lodash';
33
import * as path from 'path';
4+
import * as gulp from 'gulp';
5+
import * as gulpBabel from 'gulp-babel';
46
import { globalState } from './global-state';
5-
import { logFatal } from './log';
7+
import { logFatal, spinner } from './log';
68
import {
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';
2427
import * as pluginClientSsr from '../built-in-plugins/client-ssr';
2528
import * as pluginCommandAnalyse from '../built-in-plugins/command-analyse';
2629
import * as pluginCommandBuild from '../built-in-plugins/command-build';
@@ -100,6 +103,19 @@ export const plugin: IPluginConfig = new IPluginConfig();
100103

101104
let 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+
103119
export 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(/^file:/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+
}

src/utils/type-checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function typeChecker() {
2727

2828
logInfo('Checking TypeScript type, please wait');
2929
try {
30-
execSync(`npx tsc -p . || exit 1`, {
30+
execSync(`npx tsc -p . --isolatedModules false || exit 1`, {
3131
stdio: [0, 1, 2],
3232
});
3333
} catch (e) {

0 commit comments

Comments
 (0)