Skip to content

Commit ff78bfe

Browse files
author
linfeng
committed
feat: support webpack for component build
1 parent 4b924fc commit ff78bfe

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pri",
3-
"version": "4.1.9",
3+
"version": "4.2.0-beta",
44
"types": "src/node/index.ts",
55
"main": "built/node/index.js",
66
"scripts": {
@@ -154,4 +154,4 @@
154154
"worker-loader": "2.0.0",
155155
"yargs": "15.0.2"
156156
}
157-
}
157+
}

src/utils/define.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ interface CustomRoute {
4949
redirect?: string;
5050
}
5151

52+
export interface IEntryPath {
53+
[key: string]: string;
54+
}
55+
5256
export type PipeCallback = (text: string) => string | Promise<string>;
5357

5458
export class ProjectInfo {
@@ -272,6 +276,11 @@ export class ProjectConfig {
272276
* is material component
273277
*/
274278
public materialComponent = false;
279+
280+
/**
281+
* entries for component build
282+
*/
283+
componentEntries: IEntryPath;
275284
}
276285

277286
export type SetPipe = (pipeName: string, callback: PipeCallback) => void;

src/utils/ts-plus-babel.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as gulp from 'gulp';
2+
import * as fs from 'fs-extra';
23
import * as gulpBabel from 'gulp-babel';
34
import * as gulpSass from 'gulp-sass';
45
import * as gulpWatch from 'gulp-watch';
@@ -13,6 +14,7 @@ import { getBabelOptions } from './babel-options';
1314
import { globalState } from './global-state';
1415
import { babelPluginTransformImport } from './babel-plugin-transfer-import';
1516
import { PackageInfo } from './define';
17+
import { runWebpack } from './webpack';
1618

1719
function getGulpByWatch(watch: boolean, filesPath: string) {
1820
if (watch) {
@@ -66,6 +68,27 @@ const buildSass = (watch: boolean, outdir: string, wholeProject: boolean, source
6668
});
6769
};
6870

71+
const buildCssWithWebpack = (outDir: string, copyDir: string) => {
72+
const cssFileNames = Object.keys(pri.sourceConfig.componentEntries).map(file => `${file}.css`);
73+
74+
return runWebpack({
75+
mode: 'production',
76+
entryPath: pri.sourceConfig.componentEntries,
77+
distDir: outDir,
78+
outFileName: '[name].js',
79+
outCssFileName: '[name].css',
80+
}).then(() => {
81+
const distFiles = fs.readdirSync(outDir);
82+
distFiles.forEach(file => {
83+
const basename = path.basename(file);
84+
if (!cssFileNames.includes(basename)) {
85+
fs.removeSync(path.join(outDir, file));
86+
}
87+
});
88+
fs.copySync(outDir, copyDir);
89+
});
90+
};
91+
6992
const mvResources = (watch: boolean, outdir: string, wholeProject: boolean, sourcePath: string, sourceType: string) => {
7093
const selectedSourceType = sourceType || pri.selectedSourceType;
7194
const targetPath =
@@ -126,9 +149,14 @@ export const tsPlusBabel = async (watch = false, wholeProject = false, packageIn
126149
const sourcePath = packageInfo ? packageInfo.rootPath : null;
127150
const sourceType = packageInfo ? packageInfo.name : null;
128151

152+
// build component with webpack if config multy entries
153+
if (pri.sourceConfig.componentEntries) {
154+
await buildCssWithWebpack(mainDistPath, moduleDistPath);
155+
}
156+
129157
return Promise.all([
130-
buildSass(watch, mainDistPath, wholeProject, sourcePath),
131-
buildSass(watch, moduleDistPath, wholeProject, sourcePath),
158+
pri.sourceConfig.componentEntries ? null : buildSass(watch, mainDistPath, wholeProject, sourcePath),
159+
pri.sourceConfig.componentEntries ? null : buildSass(watch, moduleDistPath, wholeProject, sourcePath),
132160

133161
buildTs(
134162
watch,

src/utils/webpack-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { globalState, transferToAllAbsolutePaths as transferToAllAbsolutePathsWi
88
import { plugin } from './plugins';
99
import { srcPath, tempPath } from './structor-config';
1010
import { getBabelOptions } from './babel-options';
11+
import { IEntryPath } from './define';
1112

1213
export interface IHtmlTemplateArgs {
1314
dashboardServerPort?: number;
@@ -18,7 +19,7 @@ export interface IHtmlTemplateArgs {
1819

1920
export type IOptions<T = {}> = {
2021
mode: 'development' | 'production';
21-
entryPath: string | string[];
22+
entryPath: string | string[] | IEntryPath;
2223
htmlTemplatePath?: string;
2324
htmlTemplateArgs?: IHtmlTemplateArgs;
2425
publicPath?: string;

0 commit comments

Comments
 (0)