@pinnacle0/webpack-util 0.3.16-beta0 → 0.3.16-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/jest.config.ts +35 -0
- package/config/tsconfig.script.json +11 -0
- package/config/tsconfig.src.json +12 -0
- package/config/tsconfig.test.json +12 -0
- package/package.json +1 -1
- package/script/build.ts +65 -0
- package/script/format.ts +7 -0
- package/src/CanadyarnRunner.ts +35 -0
- package/src/CodeStyleChecker.ts +44 -0
- package/src/{Constant.js → Constant.ts} +1 -5
- package/src/CoreUtil.ts +24 -0
- package/src/ProjectStructureChecker.ts +119 -0
- package/src/TestRunner.ts +28 -0
- package/src/{WebpackBuilder.js → WebpackBuilder.ts} +69 -49
- package/src/WebpackConfigGenerator/{ConfigEntryDescriptorsFactory.js → ConfigEntryDescriptorsFactory.ts} +36 -29
- package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.ts +24 -0
- package/src/WebpackConfigGenerator/Plugin/css.plugin.ts +31 -0
- package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.ts +11 -0
- package/src/WebpackConfigGenerator/Plugin/html.plugin.ts +47 -0
- package/src/WebpackConfigGenerator/Plugin/index.ts +35 -0
- package/src/WebpackConfigGenerator/Plugin/{moment.plugin.js → moment.plugin.ts} +5 -12
- package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.ts +22 -0
- package/src/WebpackConfigGenerator/Plugin/ts.plugin.ts +29 -0
- package/src/WebpackConfigGenerator/Plugin/webpack.plugin.ts +21 -0
- package/src/WebpackConfigGenerator/Rule/{RegExpUtil.js → RegExpUtil.ts} +8 -14
- package/src/WebpackConfigGenerator/Rule/{core-fe-hmr-babel-plugin.js → core-fe-hmr-babel-plugin.ts} +43 -9
- package/src/WebpackConfigGenerator/Rule/{image.rule.js → image.rule.ts} +5 -8
- package/src/WebpackConfigGenerator/Rule/index.ts +17 -0
- package/src/WebpackConfigGenerator/Rule/other.rule.ts +23 -0
- package/src/WebpackConfigGenerator/Rule/{stylesheet.rule.js → stylesheet.rule.ts} +34 -30
- package/src/WebpackConfigGenerator/Rule/{ts.rule.js → ts.rule.ts} +19 -16
- package/src/WebpackConfigGenerator/{WebpackConfigSerializationUtil.js → WebpackConfigSerializationUtil.ts} +23 -22
- package/src/WebpackConfigGenerator/WebpackEntryFactory.ts +22 -0
- package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.ts +22 -0
- package/src/WebpackConfigGenerator/{WebpackResolveAliasFactory.js → WebpackResolveAliasFactory.ts} +16 -13
- package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.ts +19 -0
- package/src/WebpackConfigGenerator/{WebpackResolveModulesFactory.js → WebpackResolveModulesFactory.ts} +9 -7
- package/src/WebpackConfigGenerator/index.ts +213 -0
- package/src/WebpackServerStarter.ts +138 -0
- package/src/index.ts +9 -0
- package/src/{type.d.ts → type.ts} +8 -4
- package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/__snapshots__/shouldIgnore.test.ts.snap +21 -0
- package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/__snapshots__/shouldTransform.test.ts.snap +53 -0
- package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/shouldIgnore.test.ts +36 -0
- package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/shouldTransform.test.ts +72 -0
- package/test/WebpackConfigGenerator/plugin/fixture/script.js +2 -0
- package/test/WebpackConfigGenerator/plugin/fixture/script1.js +1 -0
- package/test/WebpackConfigGenerator/plugin/script-tag-crossorigin-plugin.test.ts +58 -0
- package/test/test-project/package.json +5 -0
- package/tsconfig.json +15 -0
- package/src/CanadyarnRunner.d.ts +0 -14
- package/src/CanadyarnRunner.js +0 -38
- package/src/CanadyarnRunner.js.map +0 -1
- package/src/CodeStyleChecker.d.ts +0 -10
- package/src/CodeStyleChecker.js +0 -44
- package/src/CodeStyleChecker.js.map +0 -1
- package/src/Constant.d.ts +0 -21
- package/src/Constant.js.map +0 -1
- package/src/CoreUtil.d.ts +0 -11
- package/src/CoreUtil.js +0 -26
- package/src/CoreUtil.js.map +0 -1
- package/src/ProjectStructureChecker.d.ts +0 -18
- package/src/ProjectStructureChecker.js +0 -113
- package/src/ProjectStructureChecker.js.map +0 -1
- package/src/TestRunner.d.ts +0 -8
- package/src/TestRunner.js +0 -30
- package/src/TestRunner.js.map +0 -1
- package/src/WebpackBuilder.d.ts +0 -38
- package/src/WebpackBuilder.js.map +0 -1
- package/src/WebpackConfigGenerator/ConfigEntryDescriptorsFactory.d.ts +0 -13
- package/src/WebpackConfigGenerator/ConfigEntryDescriptorsFactory.js.map +0 -1
- package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.d.ts +0 -9
- package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.js +0 -20
- package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.js.map +0 -1
- package/src/WebpackConfigGenerator/Plugin/css.plugin.d.ts +0 -16
- package/src/WebpackConfigGenerator/Plugin/css.plugin.js +0 -33
- package/src/WebpackConfigGenerator/Plugin/css.plugin.js.map +0 -1
- package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.d.ts +0 -2
- package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.js +0 -14
- package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.js.map +0 -1
- package/src/WebpackConfigGenerator/Plugin/html.plugin.d.ts +0 -16
- package/src/WebpackConfigGenerator/Plugin/html.plugin.js +0 -48
- package/src/WebpackConfigGenerator/Plugin/html.plugin.js.map +0 -1
- package/src/WebpackConfigGenerator/Plugin/index.d.ts +0 -29
- package/src/WebpackConfigGenerator/Plugin/index.js +0 -34
- package/src/WebpackConfigGenerator/Plugin/index.js.map +0 -1
- package/src/WebpackConfigGenerator/Plugin/moment.plugin.d.ts +0 -10
- package/src/WebpackConfigGenerator/Plugin/moment.plugin.js.map +0 -1
- package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.d.ts +0 -4
- package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.js +0 -28
- package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.js.map +0 -1
- package/src/WebpackConfigGenerator/Plugin/ts.plugin.d.ts +0 -16
- package/src/WebpackConfigGenerator/Plugin/ts.plugin.js +0 -31
- package/src/WebpackConfigGenerator/Plugin/ts.plugin.js.map +0 -1
- package/src/WebpackConfigGenerator/Plugin/webpack.plugin.d.ts +0 -14
- package/src/WebpackConfigGenerator/Plugin/webpack.plugin.js +0 -24
- package/src/WebpackConfigGenerator/Plugin/webpack.plugin.js.map +0 -1
- package/src/WebpackConfigGenerator/Rule/RegExpUtil.d.ts +0 -4
- package/src/WebpackConfigGenerator/Rule/RegExpUtil.js.map +0 -1
- package/src/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin.d.ts +0 -13
- package/src/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin.js.map +0 -1
- package/src/WebpackConfigGenerator/Rule/image.rule.d.ts +0 -9
- package/src/WebpackConfigGenerator/Rule/image.rule.js.map +0 -1
- package/src/WebpackConfigGenerator/Rule/index.d.ts +0 -13
- package/src/WebpackConfigGenerator/Rule/index.js +0 -18
- package/src/WebpackConfigGenerator/Rule/index.js.map +0 -1
- package/src/WebpackConfigGenerator/Rule/other.rule.d.ts +0 -12
- package/src/WebpackConfigGenerator/Rule/other.rule.js +0 -22
- package/src/WebpackConfigGenerator/Rule/other.rule.js.map +0 -1
- package/src/WebpackConfigGenerator/Rule/stylesheet.rule.d.ts +0 -17
- package/src/WebpackConfigGenerator/Rule/stylesheet.rule.js.map +0 -1
- package/src/WebpackConfigGenerator/Rule/ts.rule.d.ts +0 -19
- package/src/WebpackConfigGenerator/Rule/ts.rule.js.map +0 -1
- package/src/WebpackConfigGenerator/WebpackConfigSerializationUtil.d.ts +0 -10
- package/src/WebpackConfigGenerator/WebpackConfigSerializationUtil.js.map +0 -1
- package/src/WebpackConfigGenerator/WebpackEntryFactory.d.ts +0 -9
- package/src/WebpackConfigGenerator/WebpackEntryFactory.js +0 -14
- package/src/WebpackConfigGenerator/WebpackEntryFactory.js.map +0 -1
- package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.d.ts +0 -11
- package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.js +0 -16
- package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.js.map +0 -1
- package/src/WebpackConfigGenerator/WebpackResolveAliasFactory.d.ts +0 -11
- package/src/WebpackConfigGenerator/WebpackResolveAliasFactory.js.map +0 -1
- package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.d.ts +0 -7
- package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.js +0 -16
- package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.js.map +0 -1
- package/src/WebpackConfigGenerator/WebpackResolveModulesFactory.d.ts +0 -7
- package/src/WebpackConfigGenerator/WebpackResolveModulesFactory.js.map +0 -1
- package/src/WebpackConfigGenerator/index.d.ts +0 -31
- package/src/WebpackConfigGenerator/index.js +0 -190
- package/src/WebpackConfigGenerator/index.js.map +0 -1
- package/src/WebpackServerStarter.d.ts +0 -28
- package/src/WebpackServerStarter.js +0 -111
- package/src/WebpackServerStarter.js.map +0 -1
- package/src/index.d.ts +0 -10
- package/src/index.js +0 -8
- package/src/index.js.map +0 -1
- package/src/type.js +0 -3
- package/src/type.js.map +0 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type {Config} from "@jest/types";
|
|
2
|
+
|
|
3
|
+
const config: Config.InitialOptionsWithRootDir = {
|
|
4
|
+
// Stop running tests after `n` failures
|
|
5
|
+
bail: 1,
|
|
6
|
+
|
|
7
|
+
// A preset that is used as a base for Jest's configuration
|
|
8
|
+
preset: "ts-jest",
|
|
9
|
+
|
|
10
|
+
// A set of global variables that need to be available in all test environments
|
|
11
|
+
globals: {
|
|
12
|
+
"ts-jest": {
|
|
13
|
+
tsconfig: "<rootDir>/config/tsconfig.test.json",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
// The root directory that Jest should scan for tests and modules within
|
|
18
|
+
rootDir: "../",
|
|
19
|
+
|
|
20
|
+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
|
|
21
|
+
testPathIgnorePatterns: ["/node_modules/", "<rootDir>/build/"],
|
|
22
|
+
|
|
23
|
+
// A map from regular expressions to paths to transformers
|
|
24
|
+
transform: {
|
|
25
|
+
[String.raw`\.(ts|tsx)$`]: "ts-jest",
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
// Indicates whether each individual test should be reported during the run
|
|
29
|
+
verbose: true,
|
|
30
|
+
|
|
31
|
+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
|
|
32
|
+
watchPathIgnorePatterns: ["<rootDir>/build/", "dist/"],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default config;
|
package/package.json
CHANGED
package/script/build.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {PrettierUtil, TaskRunner, Utility} from "@pinnacle0/devtool-util";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
const FilePath = {
|
|
6
|
+
config: path.join(__dirname, "../config"),
|
|
7
|
+
build: path.join(__dirname, "../build"),
|
|
8
|
+
script: path.join(__dirname, "../script"),
|
|
9
|
+
src: path.join(__dirname, "../src"),
|
|
10
|
+
|
|
11
|
+
jestConfig: path.join(__dirname, "../config/jest.config.ts"),
|
|
12
|
+
tsConfigForSrc: path.join(__dirname, "../config/tsconfig.src.json"),
|
|
13
|
+
projectPackageJSON: path.join(__dirname, "../package.json"),
|
|
14
|
+
projectReadMe: path.join(__dirname, "../README.md"),
|
|
15
|
+
projectLicense: path.join(__dirname, "../LICENSE.md"),
|
|
16
|
+
buildPackageJSON: path.join(__dirname, "../build/package.json"),
|
|
17
|
+
buildReadMe: path.join(__dirname, "../build/README.md"),
|
|
18
|
+
buildLicense: path.join(__dirname, "../build/LICENSE.md"),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
new TaskRunner("build").execute([
|
|
22
|
+
{
|
|
23
|
+
name: "code style check",
|
|
24
|
+
skipInFastMode: true,
|
|
25
|
+
execute: () => {
|
|
26
|
+
PrettierUtil.check(FilePath.config);
|
|
27
|
+
PrettierUtil.check(FilePath.script);
|
|
28
|
+
PrettierUtil.check(FilePath.src);
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: "lint",
|
|
33
|
+
skipInFastMode: true,
|
|
34
|
+
execute: () => {
|
|
35
|
+
Utility.runCommand("eslint", ["--ext=.js,.jsx,.ts,.tsx", FilePath.src]);
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: "test",
|
|
40
|
+
skipInFastMode: true,
|
|
41
|
+
execute: () => {
|
|
42
|
+
Utility.runCommand("jest", ["--config", FilePath.jestConfig, "--bail"]);
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: "prepare build directory",
|
|
47
|
+
execute: () => {
|
|
48
|
+
Utility.prepareEmptyDirectory(FilePath.build);
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: "compile with tsc",
|
|
53
|
+
execute: () => {
|
|
54
|
+
Utility.runCommand("tsc", ["--project", FilePath.tsConfigForSrc]);
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "copy package.json, markdown files",
|
|
59
|
+
execute: () => {
|
|
60
|
+
fs.copyFileSync(FilePath.projectPackageJSON, FilePath.buildPackageJSON);
|
|
61
|
+
fs.copyFileSync(FilePath.projectReadMe, FilePath.buildReadMe);
|
|
62
|
+
fs.copyFileSync(FilePath.projectLicense, FilePath.buildLicense);
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
]);
|
package/script/format.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import {PrettierUtil} from "@pinnacle0/devtool-util";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
PrettierUtil.format(path.join(__dirname, "../config"));
|
|
5
|
+
PrettierUtil.format(path.join(__dirname, "../script"));
|
|
6
|
+
PrettierUtil.format(path.join(__dirname, "../src"));
|
|
7
|
+
PrettierUtil.format(path.join(__dirname, "../test"));
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {Utility} from "@pinnacle0/devtool-util";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `canadyarn` is a mono-repo package version checker for single version installation, developed by Pinnacle team.
|
|
6
|
+
*
|
|
7
|
+
* Ref: https://www.npmjs.com/package/canadyarn
|
|
8
|
+
*/
|
|
9
|
+
export class CanadyarnRunner {
|
|
10
|
+
private readonly rootDirectory: string;
|
|
11
|
+
private readonly logger = Utility.createConsoleLogger("CanadyarnRunner");
|
|
12
|
+
|
|
13
|
+
constructor({rootDirectory}: {rootDirectory: string}) {
|
|
14
|
+
this.rootDirectory = rootDirectory;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
run() {
|
|
18
|
+
this.runCanadyarn();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private runCanadyarn() {
|
|
22
|
+
const packageJSONPath = this.rootDirectory + "/package.json";
|
|
23
|
+
|
|
24
|
+
if (fs.existsSync(packageJSONPath) && fs.statSync(packageJSONPath).isFile()) {
|
|
25
|
+
const packageJSONContents: Record<string, any> = JSON.parse(fs.readFileSync(packageJSONPath, {encoding: "utf8"}));
|
|
26
|
+
|
|
27
|
+
if (packageJSONContents?.scripts?.canadyarn) {
|
|
28
|
+
this.logger.task("Running `canadyarn` at " + this.rootDirectory);
|
|
29
|
+
Utility.runCommand("yarn", ["--cwd", this.rootDirectory, "canadyarn"]);
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
throw new Error(`Cannot load root directory [${this.rootDirectory}] package.json`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {PrettierUtil, Utility} from "@pinnacle0/devtool-util";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import type {InternalCheckerOptions} from "./type";
|
|
5
|
+
|
|
6
|
+
export class CodeStyleChecker {
|
|
7
|
+
private readonly checkableSrcDirectories: [string, ...string[]];
|
|
8
|
+
private readonly logger = Utility.createConsoleLogger("CodeStyleChecker");
|
|
9
|
+
|
|
10
|
+
constructor({projectDirectory, extraCheckDirectories = []}: InternalCheckerOptions) {
|
|
11
|
+
this.checkableSrcDirectories = [path.join(projectDirectory, "src")];
|
|
12
|
+
for (const directory of extraCheckDirectories) {
|
|
13
|
+
const others = ["test", "script"].map(folder => path.join(directory, folder)).filter(path => fs.existsSync(path));
|
|
14
|
+
this.checkableSrcDirectories.push(path.join(directory, "src"), ...others);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
run() {
|
|
19
|
+
this.checkPrettier();
|
|
20
|
+
this.checkESLint();
|
|
21
|
+
this.checkStylelint();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private checkPrettier() {
|
|
25
|
+
this.logger.task("Checking Prettier");
|
|
26
|
+
for (const srcDirectory of this.checkableSrcDirectories) {
|
|
27
|
+
PrettierUtil.check(srcDirectory);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private checkESLint() {
|
|
32
|
+
this.logger.task("Checking ESLint");
|
|
33
|
+
for (const srcDirectory of this.checkableSrcDirectories) {
|
|
34
|
+
Utility.runCommand("eslint", ["--no-error-on-unmatched-pattern", "--max-warnings=1", "--ext=.js,.jsx,.ts,.tsx", srcDirectory]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private checkStylelint() {
|
|
39
|
+
this.logger.task("Checking Stylelint");
|
|
40
|
+
for (const srcDirectory of this.checkableSrcDirectories) {
|
|
41
|
+
Utility.runCommand("stylelint", ["--allow-empty-input", "--max-warnings=1", path.join(srcDirectory, "**/*.{css,less}")]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Constant = void 0;
|
|
4
|
-
exports.Constant = Object.freeze({
|
|
1
|
+
export const Constant = Object.freeze({
|
|
5
2
|
maxEntryPointKiloByte: 1500,
|
|
6
3
|
maxAssetKiloByte: 4000,
|
|
7
4
|
/**
|
|
@@ -22,4 +19,3 @@ exports.Constant = Object.freeze({
|
|
|
22
19
|
*/
|
|
23
20
|
mainEntryFilenames: ["index.tsx", "index.ts", "index.jsx", "index.js", "index.less", "index.css"],
|
|
24
21
|
});
|
|
25
|
-
//# sourceMappingURL=Constant.js.map
|
package/src/CoreUtil.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import yargs from "yargs";
|
|
2
|
+
|
|
3
|
+
function currentEnv(): string | null {
|
|
4
|
+
return (yargs.parseSync().env as string) || null;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function profilingEnabled(): boolean {
|
|
8
|
+
return Boolean(yargs.parseSync().profile);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function isFastMode(): boolean {
|
|
12
|
+
return yargs.parseSync().mode === "fast";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function verbose(): boolean {
|
|
16
|
+
return Boolean(yargs.parseSync().verbose);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const CoreUtil = Object.freeze({
|
|
20
|
+
currentEnv,
|
|
21
|
+
profilingEnabled,
|
|
22
|
+
isFastMode,
|
|
23
|
+
verbose,
|
|
24
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import {Constant} from "./Constant";
|
|
4
|
+
import type {InternalCheckerOptions} from "./type";
|
|
5
|
+
|
|
6
|
+
export class ProjectStructureChecker {
|
|
7
|
+
private readonly projectDirectory: string;
|
|
8
|
+
private readonly extraCheckDirectories: string[];
|
|
9
|
+
private readonly packageJSONPath: string;
|
|
10
|
+
private readonly tsConfigPath: string;
|
|
11
|
+
|
|
12
|
+
constructor(private readonly options: InternalCheckerOptions) {
|
|
13
|
+
this.projectDirectory = options.projectDirectory;
|
|
14
|
+
this.extraCheckDirectories = options.extraCheckDirectories ?? [];
|
|
15
|
+
this.packageJSONPath = path.join(options.projectDirectory, "package.json");
|
|
16
|
+
this.tsConfigPath = path.join(options.projectDirectory, "tsconfig.json");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
run() {
|
|
20
|
+
this.checkMainProjectDirectory();
|
|
21
|
+
this.checkMainProjectSrcDirectory();
|
|
22
|
+
this.checkExtraDirectories();
|
|
23
|
+
this.checkPackageJSON();
|
|
24
|
+
this.checkTSConfig();
|
|
25
|
+
|
|
26
|
+
this.checkPrettierInstallation();
|
|
27
|
+
this.checkESLintInstallation();
|
|
28
|
+
this.checkStyleLintInstallation();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private checkMainProjectDirectory() {
|
|
32
|
+
if (!(fs.existsSync(this.projectDirectory) && fs.statSync(this.projectDirectory).isDirectory())) {
|
|
33
|
+
throw new Error(`Cannot check project directory at "${this.projectDirectory}" because it is not a folder.`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const mainProjectStaticDirectory = path.join(this.projectDirectory, "static");
|
|
37
|
+
if (!(fs.existsSync(mainProjectStaticDirectory) && fs.statSync(mainProjectStaticDirectory).isDirectory())) {
|
|
38
|
+
throw new Error(`Cannot find "static" directory in project at "${this.projectDirectory}"`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private checkMainProjectSrcDirectory() {
|
|
43
|
+
const mainProjectSrcDirectory = path.join(this.projectDirectory, "src");
|
|
44
|
+
if (!(fs.existsSync(mainProjectSrcDirectory) && fs.statSync(mainProjectSrcDirectory).isDirectory())) {
|
|
45
|
+
throw new Error(`Cannot find "src" folder inside directory at "${this.projectDirectory}".`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let isMainEntryFound = false;
|
|
49
|
+
Constant.mainEntryFilenames
|
|
50
|
+
.map(entryName => path.join(mainProjectSrcDirectory, entryName))
|
|
51
|
+
.forEach(entryPath => {
|
|
52
|
+
if (fs.existsSync(entryPath) && fs.statSync(entryPath).isFile()) {
|
|
53
|
+
isMainEntryFound = true;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
if (!isMainEntryFound) {
|
|
57
|
+
throw new Error(`Cannot find main entry file in src/ at "${mainProjectSrcDirectory}"; checked: ${Constant.mainEntryFilenames.map(_ => `"${_}"`).join(" / ")}.`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private checkExtraDirectories() {
|
|
62
|
+
for (const extraCheckDirectory of this.extraCheckDirectories) {
|
|
63
|
+
if (!(fs.existsSync(extraCheckDirectory) && fs.statSync(extraCheckDirectory).isDirectory())) {
|
|
64
|
+
throw new Error(`Cannot check extra directory at "${extraCheckDirectory}" because it is not a folder.`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private checkPackageJSON() {
|
|
70
|
+
if (!(fs.existsSync(this.packageJSONPath) && fs.statSync(this.packageJSONPath).isFile())) {
|
|
71
|
+
throw new Error(`Cannot find package.json at "${this.packageJSONPath}".`);
|
|
72
|
+
}
|
|
73
|
+
const packageJSONContents: Record<string, any> = JSON.parse(fs.readFileSync(this.packageJSONPath, {encoding: "utf8"}));
|
|
74
|
+
for (const [depName, depVersion] of Object.entries<string>(packageJSONContents.dependencies || {})) {
|
|
75
|
+
if (!/^\d/.test(depVersion)) {
|
|
76
|
+
throw new Error(`Dependency "${depName}" must be an exact version, but found "${depVersion}" in package.json at "${this.packageJSONPath}".`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const [depName, depVersion] of Object.entries<string>(packageJSONContents.devDependencies || {})) {
|
|
80
|
+
if (!/^\d/.test(depVersion)) {
|
|
81
|
+
throw new Error(`Dependency "${depName}" must be an exact version, but found "${depVersion}" in package.json at "${this.packageJSONPath}".`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private checkTSConfig() {
|
|
87
|
+
const mainProjectSrcDirectory = path.join(this.projectDirectory, "src");
|
|
88
|
+
// Checking the first-level of src/* is enough here
|
|
89
|
+
const hasTSFiles = fs.readdirSync(mainProjectSrcDirectory).some(fileName => fileName.endsWith(".ts") || fileName.endsWith(".tsx"));
|
|
90
|
+
const hasTSConfigFile = fs.existsSync(this.tsConfigPath) && fs.statSync(this.tsConfigPath).isFile();
|
|
91
|
+
if (hasTSFiles && !hasTSConfigFile) {
|
|
92
|
+
throw new Error(`Cannot find tsconfig.json at "${this.tsConfigPath}".`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private checkPrettierInstallation() {
|
|
97
|
+
try {
|
|
98
|
+
require("prettier");
|
|
99
|
+
} catch {
|
|
100
|
+
throw new Error(`Cannot load prettier module (requiring from "webpack-util"), make sure prettier is installed.`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private checkESLintInstallation() {
|
|
105
|
+
try {
|
|
106
|
+
require("eslint");
|
|
107
|
+
} catch {
|
|
108
|
+
throw new Error(`Cannot load eslint module (requiring from "webpack-util"), make sure eslint is installed.`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private checkStyleLintInstallation() {
|
|
113
|
+
try {
|
|
114
|
+
require("stylelint");
|
|
115
|
+
} catch {
|
|
116
|
+
throw new Error(`Cannot load stylelint module (requiring from "webpack-util"), make sure stylelint is installed.`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {Utility} from "@pinnacle0/devtool-util";
|
|
2
|
+
import type {InternalCheckerOptions} from "./type";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
|
|
5
|
+
export class TestRunner {
|
|
6
|
+
private readonly directories: [string, ...string[]];
|
|
7
|
+
private readonly logger = Utility.createConsoleLogger("TestRunner");
|
|
8
|
+
|
|
9
|
+
constructor({projectDirectory, extraCheckDirectories = []}: InternalCheckerOptions) {
|
|
10
|
+
this.directories = [projectDirectory, ...extraCheckDirectories];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
run() {
|
|
14
|
+
this.directories.forEach(dir => this.runTestScript(dir));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
private runTestScript(directory: string) {
|
|
18
|
+
const packageJSONPath = directory + "/package.json";
|
|
19
|
+
if (fs.existsSync(packageJSONPath) && fs.statSync(packageJSONPath).isFile()) {
|
|
20
|
+
const packageJSONContents: Record<string, any> = JSON.parse(fs.readFileSync(packageJSONPath, {encoding: "utf8"}));
|
|
21
|
+
|
|
22
|
+
if (packageJSONContents?.scripts?.test) {
|
|
23
|
+
this.logger.task("Running `yarn test` at " + directory);
|
|
24
|
+
Utility.runCommand("yarn", ["--cwd", directory, "test"]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import {Utility} from "@pinnacle0/devtool-util";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import webpack from "webpack";
|
|
5
|
+
import type {InternalCheckerOptions} from "./type";
|
|
6
|
+
import type {WebpackConfigGeneratorOptions} from "./WebpackConfigGenerator";
|
|
7
|
+
import {ProjectStructureChecker} from "./ProjectStructureChecker";
|
|
8
|
+
import {CodeStyleChecker} from "./CodeStyleChecker";
|
|
9
|
+
import {WebpackConfigGenerator} from "./WebpackConfigGenerator";
|
|
10
|
+
import {TestRunner} from "./TestRunner";
|
|
11
|
+
import {CanadyarnRunner} from "./CanadyarnRunner";
|
|
12
|
+
import {CoreUtil} from "./CoreUtil";
|
|
13
|
+
|
|
14
|
+
export interface WebpackBuilderOptions extends WebpackConfigGeneratorOptions, InternalCheckerOptions {
|
|
15
|
+
onSuccess?: () => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
17
18
|
/**
|
|
18
19
|
* Build the website by webpack.
|
|
19
20
|
*
|
|
@@ -28,36 +29,51 @@ const CoreUtil_1 = require("./CoreUtil");
|
|
|
28
29
|
*
|
|
29
30
|
* Add "--mode fast" to command line, if you want to skip style and lint checks.
|
|
30
31
|
*/
|
|
31
|
-
class WebpackBuilder {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
export class WebpackBuilder {
|
|
33
|
+
private readonly projectDirectory: string;
|
|
34
|
+
private readonly rootDirectory: string;
|
|
35
|
+
private readonly extraCheckDirectories: string[];
|
|
36
|
+
private readonly projectStaticDirectory: string;
|
|
37
|
+
private readonly projectProfilingJSONOutputPath: string;
|
|
38
|
+
private readonly outputDirectory: string;
|
|
39
|
+
private readonly webpackConfig: webpack.Configuration;
|
|
40
|
+
private readonly isFastMode: boolean;
|
|
41
|
+
private readonly enableProfiling: boolean;
|
|
42
|
+
private readonly onSuccess?: (() => void) | undefined;
|
|
43
|
+
|
|
44
|
+
private readonly logger = Utility.createConsoleLogger("WebpackBuilder");
|
|
45
|
+
|
|
46
|
+
constructor(options: WebpackBuilderOptions) {
|
|
47
|
+
const webpackConfigGenerator = new WebpackConfigGenerator(options);
|
|
48
|
+
|
|
36
49
|
this.projectDirectory = options.projectDirectory;
|
|
37
50
|
this.rootDirectory = options.rootDirectory ? options.rootDirectory : this.projectDirectory;
|
|
38
|
-
this.extraCheckDirectories =
|
|
39
|
-
this.projectStaticDirectory =
|
|
40
|
-
this.projectProfilingJSONOutputPath =
|
|
41
|
-
this.outputDirectory =
|
|
42
|
-
|
|
43
|
-
this.
|
|
51
|
+
this.extraCheckDirectories = options.extraCheckDirectories ?? [];
|
|
52
|
+
this.projectStaticDirectory = path.join(this.projectDirectory, "static");
|
|
53
|
+
this.projectProfilingJSONOutputPath = path.join(this.projectDirectory, "profile.json");
|
|
54
|
+
this.outputDirectory = path.join(this.projectDirectory, "build/dist");
|
|
55
|
+
|
|
56
|
+
this.isFastMode = CoreUtil.isFastMode();
|
|
57
|
+
this.enableProfiling = CoreUtil.profilingEnabled();
|
|
44
58
|
this.onSuccess = options.onSuccess;
|
|
59
|
+
|
|
45
60
|
this.webpackConfig = webpackConfigGenerator.production(this.outputDirectory);
|
|
46
61
|
}
|
|
62
|
+
|
|
47
63
|
run() {
|
|
48
64
|
if (!this.isFastMode) {
|
|
49
|
-
new
|
|
65
|
+
new CanadyarnRunner({
|
|
50
66
|
rootDirectory: this.rootDirectory,
|
|
51
67
|
}).run();
|
|
52
|
-
new
|
|
68
|
+
new TestRunner({
|
|
53
69
|
projectDirectory: this.projectDirectory,
|
|
54
70
|
extraCheckDirectories: this.extraCheckDirectories,
|
|
55
71
|
}).run();
|
|
56
|
-
new
|
|
72
|
+
new ProjectStructureChecker({
|
|
57
73
|
projectDirectory: this.projectDirectory,
|
|
58
74
|
extraCheckDirectories: this.extraCheckDirectories,
|
|
59
75
|
}).run();
|
|
60
|
-
new
|
|
76
|
+
new CodeStyleChecker({
|
|
61
77
|
projectDirectory: this.projectDirectory,
|
|
62
78
|
extraCheckDirectories: this.extraCheckDirectories,
|
|
63
79
|
}).run();
|
|
@@ -66,46 +82,52 @@ class WebpackBuilder {
|
|
|
66
82
|
this.copyStatic();
|
|
67
83
|
this.bundleByWebpack();
|
|
68
84
|
}
|
|
69
|
-
|
|
85
|
+
|
|
86
|
+
private cleanDistFolder() {
|
|
70
87
|
this.logger.task("Cleaning build dist folder");
|
|
71
|
-
|
|
88
|
+
fs.emptyDirSync(this.outputDirectory);
|
|
72
89
|
}
|
|
73
|
-
|
|
90
|
+
|
|
91
|
+
private copyStatic() {
|
|
74
92
|
this.logger.task("Copying static assets to build dist folder");
|
|
75
|
-
|
|
93
|
+
fs.copySync(this.projectStaticDirectory, this.outputDirectory, {dereference: true});
|
|
76
94
|
}
|
|
77
|
-
|
|
95
|
+
|
|
96
|
+
private bundleByWebpack() {
|
|
78
97
|
this.logger.task("Starting webpack");
|
|
79
|
-
|
|
98
|
+
|
|
99
|
+
webpack(this.webpackConfig).run((error?: Error | null, stats?: webpack.Stats) => {
|
|
80
100
|
if (error) {
|
|
81
101
|
throw error;
|
|
82
|
-
}
|
|
83
|
-
else if (stats) {
|
|
102
|
+
} else if (stats) {
|
|
84
103
|
const statsJSON = stats.toJson();
|
|
85
104
|
if (this.enableProfiling) {
|
|
86
105
|
this.logger.task(["Generating profile for analysis", this.projectProfilingJSONOutputPath]);
|
|
87
|
-
|
|
106
|
+
fs.writeFileSync(this.projectProfilingJSONOutputPath, JSON.stringify(statsJSON, null, 2), {encoding: "utf8"});
|
|
88
107
|
}
|
|
108
|
+
|
|
89
109
|
if (stats.hasErrors() || stats.hasWarnings()) {
|
|
90
110
|
this.logger.error("Webpack compiled with the following errors/warnings:");
|
|
91
111
|
console.error(stats.toString("errors-warnings"));
|
|
92
112
|
process.exit(1);
|
|
93
113
|
}
|
|
114
|
+
|
|
94
115
|
this.logger.info("Build successfully");
|
|
116
|
+
|
|
95
117
|
if (this.onSuccess) {
|
|
96
118
|
this.logger.info("Running onSuccess callback");
|
|
97
119
|
this.onSuccess();
|
|
98
120
|
}
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
121
|
+
} else {
|
|
101
122
|
this.logger.error("Webpack compiler `run()` returns no `error` and no `stats`, this is unexpected.");
|
|
102
123
|
process.exit(1);
|
|
103
124
|
}
|
|
104
125
|
});
|
|
105
126
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
127
|
+
|
|
128
|
+
private getRawWarningsAndErrors(info: any): {warnings: any[]; errors: any[]} {
|
|
129
|
+
const warnings: any[] = [];
|
|
130
|
+
const errors: any[] = [];
|
|
109
131
|
if (typeof info === "object" && info !== null) {
|
|
110
132
|
if (Array.isArray(info.warnings)) {
|
|
111
133
|
warnings.push(...info.warnings);
|
|
@@ -114,15 +136,13 @@ class WebpackBuilder {
|
|
|
114
136
|
errors.push(...info.errors);
|
|
115
137
|
}
|
|
116
138
|
if (Array.isArray(info.children)) {
|
|
117
|
-
info.children.forEach((_) => {
|
|
139
|
+
info.children.forEach((_: any) => {
|
|
118
140
|
const childInfo = this.getRawWarningsAndErrors(_);
|
|
119
141
|
warnings.push(...childInfo.warnings);
|
|
120
142
|
errors.push(...childInfo.errors);
|
|
121
143
|
});
|
|
122
144
|
}
|
|
123
145
|
}
|
|
124
|
-
return {
|
|
146
|
+
return {warnings, errors};
|
|
125
147
|
}
|
|
126
148
|
}
|
|
127
|
-
exports.WebpackBuilder = WebpackBuilder;
|
|
128
|
-
//# sourceMappingURL=WebpackBuilder.js.map
|