Skip to content

Commit 126da24

Browse files
committed
feat: new command - pri bundle
1 parent 374c240 commit 126da24

17 files changed

Lines changed: 102 additions & 56 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pri",
3-
"version": "0.9.28",
3+
"version": "0.10.0",
44
"types": "src/node/index.ts",
55
"main": "built/node/index.js",
66
"scripts": {

src/built-in-plugins/command-build/generate-static-html.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as React from 'react';
44
import * as url from 'url';
55
import { globalState } from '../../utils/global-state';
66
import { ProjectConfig } from '../../utils/project-config-interface';
7-
import { tempJsAppPath, tempPath, tsBuiltPath } from '../../utils/structor-config';
87

98
export function getStaticHtmlPaths(analyseInfo: any) {
109
const pages = analyseInfo.projectAnalysePages ? analyseInfo.projectAnalysePages.pages : [];

src/built-in-plugins/command-build/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { log, spinner } from '../../utils/log';
1414
import { findNearestNodemodulesFile } from '../../utils/npm-finder';
1515
import { plugin } from '../../utils/plugins';
1616
import { ProjectConfig } from '../../utils/project-config-interface';
17-
import { tsBuiltPath } from '../../utils/structor-config';
1817
import text from '../../utils/text';
1918
import { tsPlusBabel } from '../../utils/ts-plus-babel';
2019
import { runWebpack } from '../../utils/webpack';
@@ -30,7 +29,10 @@ async function prepareBuild(instance: typeof pri) {
3029
)}`
3130
);
3231
await exec(
33-
`${findNearestNodemodulesFile('.bin/rimraf')} ${path.join(globalState.projectRootPath, tsBuiltPath.dir)}`
32+
`${findNearestNodemodulesFile('.bin/rimraf')} ${path.join(
33+
globalState.projectRootPath,
34+
globalState.projectConfig.distDir
35+
)}`
3436
);
3537

3638
// Clean .temp dir
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { execSync } from 'child_process';
2+
import * as colors from 'colors';
3+
import * as fs from 'fs-extra';
4+
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
5+
import * as path from 'path';
6+
import * as prettier from 'prettier';
7+
import { componentEntry, pri, tempPath } from '../../node';
8+
import * as pipe from '../../node/pipe';
9+
import { analyseProject } from '../../utils/analyse-project';
10+
import { createEntry } from '../../utils/create-entry';
11+
import { exec } from '../../utils/exec';
12+
import { globalState } from '../../utils/global-state';
13+
import { log, spinner } from '../../utils/log';
14+
import { findNearestNodemodulesFile } from '../../utils/npm-finder';
15+
import { plugin } from '../../utils/plugins';
16+
import { ProjectConfig } from '../../utils/project-config-interface';
17+
import text from '../../utils/text';
18+
import { tsPlusBabel } from '../../utils/ts-plus-babel';
19+
import { runWebpack } from '../../utils/webpack';
20+
21+
async function bundle(instance: typeof pri) {
22+
await instance.project.ensureProjectFiles();
23+
await instance.project.lint();
24+
await instance.project.checkProjectFiles();
25+
26+
await runWebpack({
27+
mode: 'production',
28+
outFileName: instance.projectConfig.bundleFileName,
29+
entryPath: path.join(instance.projectRootPath, path.format(componentEntry)),
30+
webpackBarOptions: {
31+
name: 'bundle'
32+
},
33+
pipeConfig: config => {
34+
config.output.libraryTarget = 'umd';
35+
return config;
36+
}
37+
});
38+
}
39+
40+
export default async (instance: typeof pri) => {
41+
instance.commands.registerCommand({
42+
name: 'bundle',
43+
description: text.commander.bundle.description,
44+
action: async () => {
45+
await bundle(instance);
46+
}
47+
});
48+
};

src/built-in-plugins/command-test/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@ import * as fs from 'fs-extra';
33
import * as open from 'opn';
44
import * as path from 'path';
55
import { pri } from '../../node';
6+
import { globalState } from '../../utils/global-state';
67
import { log } from '../../utils/log';
78
import { findNearestNodemodulesFile } from '../../utils/npm-finder';
8-
import { testsPath, tsBuiltPath } from '../../utils/structor-config';
9+
import { testsPath } from '../../utils/structor-config';
910
import text from '../../utils/text';
1011
import { tsPlusBabel } from '../../utils/ts-plus-babel';
1112

1213
export const CommandTest = async (instance: typeof pri) => {
1314
log(`Build typescript files`);
14-
execSync(`${findNearestNodemodulesFile('/.bin/rimraf')} ${tsBuiltPath.dir}`, { stdio: 'inherit' });
15+
execSync(`${findNearestNodemodulesFile('/.bin/rimraf')} ${globalState.projectConfig.distDir}`, { stdio: 'inherit' });
1516

16-
await tsPlusBabel(tsBuiltPath.dir);
17+
await tsPlusBabel(globalState.projectConfig.distDir);
1718

1819
execSync(
1920
[
2021
findNearestNodemodulesFile('/.bin/nyc'),
2122
`--reporter lcov`,
2223
`--reporter text`,
2324
`--reporter json`,
24-
`--exclude ${tsBuiltPath.dir}/${testsPath.dir}/**/*.js`,
25+
`--exclude ${globalState.projectConfig.distDir}/${testsPath.dir}/**/*.js`,
2526
`${findNearestNodemodulesFile('/.bin/ava')}`,
26-
`--files ${path.join(instance.projectRootPath, `${tsBuiltPath.dir}/${testsPath.dir}/**/*.js`)}`,
27+
`--files ${path.join(instance.projectRootPath, `${globalState.projectConfig.distDir}/${testsPath.dir}/**/*.js`)}`,
2728
`--failFast`
2829
].join(' '),
2930
{

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { globalState } from '../../utils/global-state';
88
import { prettierConfig } from '../../utils/prettier-config';
99
import { ProjectConfig } from '../../utils/project-config-interface';
1010
import {
11+
componentEntry,
1112
declarePath,
1213
docsPath,
1314
gitIgnores,
1415
npmIgnores,
1516
pagesPath,
1617
srcPath,
17-
tempTypesPath,
18-
tsBuiltPath
18+
tempTypesPath
1919
} from '../../utils/structor-config';
2020

2121
export function ensureDeclares(projectRootPath: string) {
@@ -43,21 +43,19 @@ export const ensureTsconfig = () => ({
4343
target: 'esnext',
4444
experimentalDecorators: true,
4545
skipLibCheck: true,
46-
outDir: tsBuiltPath.dir,
47-
rootDir: './', // Make sure ./src structor. # https://github.com/Microsoft/TypeScript/issues/5134
46+
outDir: globalState.projectConfig.distDir,
47+
rootDir: './',
4848
baseUrl: '.',
4949
lib: ['dom', 'es5', 'es6', 'scripthost'],
50-
paths: {
51-
[PRI_PACKAGE_NAME + '/*']: [PRI_PACKAGE_NAME, path.join(tempTypesPath.dir, '*')]
52-
}
50+
paths: { [PRI_PACKAGE_NAME + '/*']: [PRI_PACKAGE_NAME, path.join(tempTypesPath.dir, '*')] }
5351
},
5452
include: ['.temp/**/*', 'src/**/*', 'config/**/*', 'tests/**/*'],
55-
exclude: ['node_modules', tsBuiltPath.dir]
53+
exclude: ['node_modules', globalState.projectConfig.distDir]
5654
},
5755
null,
5856
2
5957
) + '\n'
60-
);
58+
); // Make sure ./src structor. # https://github.com/Microsoft/TypeScript/issues/5134
6159
}
6260
});
6361

@@ -123,6 +121,7 @@ export const ensurePackageJson = () => ({
123121
start: 'pri dev',
124122
docs: 'pri docs',
125123
build: 'pri build',
124+
bundle: 'pri bundle',
126125
preview: 'pri preview',
127126
analyse: 'pri analyse',
128127
test: 'pri test',
@@ -227,7 +226,7 @@ export default async (instance: typeof pri) => {
227226
JSON.stringify(
228227
_.merge({}, prevJson, {
229228
main: `${instance.projectConfig.distDir}/${srcPath.dir}/index.js`,
230-
types: `${srcPath.dir}/index.tsx`,
229+
types: path.format(componentEntry),
231230
peerDependencies: {
232231
react: '>=16.0.0',
233232
'react-dom': '>=16.0.0'
@@ -248,7 +247,7 @@ export default async (instance: typeof pri) => {
248247

249248
// Create entry file
250249
instance.project.addProjectFiles({
251-
fileName: `${srcPath.dir}/index.tsx`,
250+
fileName: path.format(componentEntry),
252251
pipeContent: text =>
253252
text
254253
? text

src/pri-plugin-commanders/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { tsBuiltPath } from '../utils/structor-config';
1+
import { globalState } from '../utils/global-state';
22
import { tsPlusBabel } from '../utils/ts-plus-babel';
33

44
export default async () => {
5-
await tsPlusBabel(tsBuiltPath.dir);
5+
await tsPlusBabel(globalState.projectConfig.distDir);
66
};

src/pri-plugin-commanders/test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,30 @@ import * as path from 'path';
55
import { globalState } from '../utils/global-state';
66
import { log } from '../utils/log';
77
import { findNearestNodemodulesFile } from '../utils/npm-finder';
8-
import { srcPath, testsPath, tsBuiltPath } from '../utils/structor-config';
8+
import { srcPath, testsPath } from '../utils/structor-config';
99
import text from '../utils/text';
1010
import { tsPlusBabel } from '../utils/ts-plus-babel';
1111

1212
export default async () => {
1313
log(`Build typescript files`);
14-
execSync(`${findNearestNodemodulesFile('/.bin/rimraf')} ${tsBuiltPath.dir}`, {
14+
execSync(`${findNearestNodemodulesFile('/.bin/rimraf')} ${globalState.projectConfig.distDir}`, {
1515
stdio: 'inherit'
1616
});
1717

18-
await tsPlusBabel(tsBuiltPath.dir);
18+
await tsPlusBabel(globalState.projectConfig.distDir);
1919

2020
execSync(
2121
[
2222
findNearestNodemodulesFile('/.bin/nyc'),
2323
`--reporter lcov`,
2424
`--reporter text`,
2525
`--reporter json`,
26-
`--exclude ${tsBuiltPath.dir}/${testsPath.dir}/**/*.js`,
26+
`--exclude ${globalState.projectConfig.distDir}/${testsPath.dir}/**/*.js`,
2727
`${findNearestNodemodulesFile('/.bin/ava')}`,
28-
`--files ${path.join(globalState.projectRootPath, `${tsBuiltPath.dir}/${testsPath.dir}/**/*.js`)}`,
28+
`--files ${path.join(
29+
globalState.projectRootPath,
30+
`${globalState.projectConfig.distDir}/${testsPath.dir}/**/*.js`
31+
)}`,
2932
`--failFast`
3033
].join(' '),
3134
{

src/pri-plugin-commanders/utils/ensure-plugin-files.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ensureFile } from '../../utils/ensure-files';
88
import { globalState } from '../../utils/global-state';
99
import { log } from '../../utils/log';
1010
import { prettierConfig } from '../../utils/prettier-config';
11-
import { gitIgnores, npmIgnores, tsBuiltPath } from '../../utils/structor-config';
11+
import { gitIgnores, npmIgnores } from '../../utils/structor-config';
1212

1313
export function ensureNpmIgnore() {
1414
ensureFile('.npmignore', [() => npmIgnores.map(name => `/${name}`).join('\n')]);
@@ -22,7 +22,7 @@ export function ensurePackageJson() {
2222
JSON.stringify(
2323
_.merge({}, prevJson, {
2424
types: 'src/index.ts',
25-
main: path.join(tsBuiltPath.dir, 'src/index.js'),
25+
main: path.join(globalState.projectConfig.distDir, 'src/index.js'),
2626
scripts: {
2727
start: 'pri plugin watch',
2828
prepublishOnly: 'pri plugin build',

src/pri-plugin-commanders/watch.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as colors from 'colors';
22
import * as gulp from 'gulp';
33
import { globalState } from '../utils/global-state';
44
import { log } from '../utils/log';
5-
import { tsBuiltPath } from '../utils/structor-config';
65
import { tsPlusBabel } from '../utils/ts-plus-babel';
76

87
export default async () => {
@@ -11,12 +10,12 @@ export default async () => {
1110
const sourceBlob = 'src/**/*.{tsx,ts}';
1211
const watcher = gulp.watch(sourceBlob);
1312

14-
await tsPlusBabel(tsBuiltPath.dir);
13+
await tsPlusBabel(globalState.projectConfig.distDir);
1514

1615
// TODO: On create delete?
1716
watcher.on('change', async () => {
1817
log(colors.blue(`Start rebuild.`));
19-
await tsPlusBabel(tsBuiltPath.dir);
18+
await tsPlusBabel(globalState.projectConfig.distDir);
2019
log(colors.green(`End rebuild.`));
2120
});
2221
};

0 commit comments

Comments
 (0)