@pinnacle0/webpack-util 0.3.15-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.
Files changed (139) hide show
  1. package/config/jest.config.ts +35 -0
  2. package/config/tsconfig.script.json +11 -0
  3. package/config/tsconfig.src.json +12 -0
  4. package/config/tsconfig.test.json +12 -0
  5. package/package.json +1 -1
  6. package/script/build.ts +65 -0
  7. package/script/format.ts +7 -0
  8. package/src/CanadyarnRunner.ts +35 -0
  9. package/src/CodeStyleChecker.ts +44 -0
  10. package/src/{Constant.js → Constant.ts} +1 -5
  11. package/src/CoreUtil.ts +24 -0
  12. package/src/ProjectStructureChecker.ts +119 -0
  13. package/src/TestRunner.ts +28 -0
  14. package/src/{WebpackBuilder.js → WebpackBuilder.ts} +69 -49
  15. package/src/WebpackConfigGenerator/{ConfigEntryDescriptorsFactory.js → ConfigEntryDescriptorsFactory.ts} +36 -29
  16. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.ts +24 -0
  17. package/src/WebpackConfigGenerator/Plugin/css.plugin.ts +31 -0
  18. package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.ts +11 -0
  19. package/src/WebpackConfigGenerator/Plugin/html.plugin.ts +47 -0
  20. package/src/WebpackConfigGenerator/Plugin/index.ts +35 -0
  21. package/src/WebpackConfigGenerator/Plugin/{moment.plugin.js → moment.plugin.ts} +5 -12
  22. package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.ts +22 -0
  23. package/src/WebpackConfigGenerator/Plugin/ts.plugin.ts +29 -0
  24. package/src/WebpackConfigGenerator/Plugin/webpack.plugin.ts +21 -0
  25. package/src/WebpackConfigGenerator/Rule/{RegExpUtil.js → RegExpUtil.ts} +8 -14
  26. package/src/WebpackConfigGenerator/Rule/{core-fe-hmr-babel-plugin.js → core-fe-hmr-babel-plugin.ts} +43 -9
  27. package/src/WebpackConfigGenerator/Rule/{image.rule.js → image.rule.ts} +5 -8
  28. package/src/WebpackConfigGenerator/Rule/index.ts +17 -0
  29. package/src/WebpackConfigGenerator/Rule/other.rule.ts +23 -0
  30. package/src/WebpackConfigGenerator/Rule/{stylesheet.rule.js → stylesheet.rule.ts} +34 -30
  31. package/src/WebpackConfigGenerator/Rule/{ts.rule.js → ts.rule.ts} +19 -16
  32. package/src/WebpackConfigGenerator/{WebpackConfigSerializationUtil.js → WebpackConfigSerializationUtil.ts} +23 -22
  33. package/src/WebpackConfigGenerator/WebpackEntryFactory.ts +22 -0
  34. package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.ts +22 -0
  35. package/src/WebpackConfigGenerator/{WebpackResolveAliasFactory.js → WebpackResolveAliasFactory.ts} +16 -13
  36. package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.ts +19 -0
  37. package/src/WebpackConfigGenerator/{WebpackResolveModulesFactory.js → WebpackResolveModulesFactory.ts} +9 -7
  38. package/src/WebpackConfigGenerator/index.ts +213 -0
  39. package/src/WebpackServerStarter.ts +138 -0
  40. package/src/index.ts +9 -0
  41. package/src/{type.d.ts → type.ts} +9 -4
  42. package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/__snapshots__/shouldIgnore.test.ts.snap +21 -0
  43. package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/__snapshots__/shouldTransform.test.ts.snap +53 -0
  44. package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/shouldIgnore.test.ts +36 -0
  45. package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/shouldTransform.test.ts +72 -0
  46. package/test/WebpackConfigGenerator/plugin/fixture/script.js +2 -0
  47. package/test/WebpackConfigGenerator/plugin/fixture/script1.js +1 -0
  48. package/test/WebpackConfigGenerator/plugin/script-tag-crossorigin-plugin.test.ts +58 -0
  49. package/test/test-project/package.json +5 -0
  50. package/tsconfig.json +15 -0
  51. package/src/CanadyarnRunner.d.ts +0 -14
  52. package/src/CanadyarnRunner.js +0 -38
  53. package/src/CanadyarnRunner.js.map +0 -1
  54. package/src/CodeStyleChecker.d.ts +0 -10
  55. package/src/CodeStyleChecker.js +0 -44
  56. package/src/CodeStyleChecker.js.map +0 -1
  57. package/src/Constant.d.ts +0 -21
  58. package/src/Constant.js.map +0 -1
  59. package/src/CoreUtil.d.ts +0 -11
  60. package/src/CoreUtil.js +0 -26
  61. package/src/CoreUtil.js.map +0 -1
  62. package/src/ProjectStructureChecker.d.ts +0 -18
  63. package/src/ProjectStructureChecker.js +0 -113
  64. package/src/ProjectStructureChecker.js.map +0 -1
  65. package/src/TestRunner.d.ts +0 -8
  66. package/src/TestRunner.js +0 -30
  67. package/src/TestRunner.js.map +0 -1
  68. package/src/WebpackBuilder.d.ts +0 -38
  69. package/src/WebpackBuilder.js.map +0 -1
  70. package/src/WebpackConfigGenerator/ConfigEntryDescriptorsFactory.d.ts +0 -13
  71. package/src/WebpackConfigGenerator/ConfigEntryDescriptorsFactory.js.map +0 -1
  72. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.d.ts +0 -9
  73. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.js +0 -20
  74. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.js.map +0 -1
  75. package/src/WebpackConfigGenerator/Plugin/css.plugin.d.ts +0 -16
  76. package/src/WebpackConfigGenerator/Plugin/css.plugin.js +0 -33
  77. package/src/WebpackConfigGenerator/Plugin/css.plugin.js.map +0 -1
  78. package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.d.ts +0 -2
  79. package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.js +0 -14
  80. package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.js.map +0 -1
  81. package/src/WebpackConfigGenerator/Plugin/html.plugin.d.ts +0 -16
  82. package/src/WebpackConfigGenerator/Plugin/html.plugin.js +0 -48
  83. package/src/WebpackConfigGenerator/Plugin/html.plugin.js.map +0 -1
  84. package/src/WebpackConfigGenerator/Plugin/index.d.ts +0 -29
  85. package/src/WebpackConfigGenerator/Plugin/index.js +0 -34
  86. package/src/WebpackConfigGenerator/Plugin/index.js.map +0 -1
  87. package/src/WebpackConfigGenerator/Plugin/moment.plugin.d.ts +0 -10
  88. package/src/WebpackConfigGenerator/Plugin/moment.plugin.js.map +0 -1
  89. package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.d.ts +0 -4
  90. package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.js +0 -28
  91. package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.js.map +0 -1
  92. package/src/WebpackConfigGenerator/Plugin/ts.plugin.d.ts +0 -16
  93. package/src/WebpackConfigGenerator/Plugin/ts.plugin.js +0 -31
  94. package/src/WebpackConfigGenerator/Plugin/ts.plugin.js.map +0 -1
  95. package/src/WebpackConfigGenerator/Plugin/webpack.plugin.d.ts +0 -14
  96. package/src/WebpackConfigGenerator/Plugin/webpack.plugin.js +0 -24
  97. package/src/WebpackConfigGenerator/Plugin/webpack.plugin.js.map +0 -1
  98. package/src/WebpackConfigGenerator/Rule/RegExpUtil.d.ts +0 -4
  99. package/src/WebpackConfigGenerator/Rule/RegExpUtil.js.map +0 -1
  100. package/src/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin.d.ts +0 -13
  101. package/src/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin.js.map +0 -1
  102. package/src/WebpackConfigGenerator/Rule/image.rule.d.ts +0 -9
  103. package/src/WebpackConfigGenerator/Rule/image.rule.js.map +0 -1
  104. package/src/WebpackConfigGenerator/Rule/index.d.ts +0 -13
  105. package/src/WebpackConfigGenerator/Rule/index.js +0 -18
  106. package/src/WebpackConfigGenerator/Rule/index.js.map +0 -1
  107. package/src/WebpackConfigGenerator/Rule/other.rule.d.ts +0 -8
  108. package/src/WebpackConfigGenerator/Rule/other.rule.js +0 -22
  109. package/src/WebpackConfigGenerator/Rule/other.rule.js.map +0 -1
  110. package/src/WebpackConfigGenerator/Rule/stylesheet.rule.d.ts +0 -17
  111. package/src/WebpackConfigGenerator/Rule/stylesheet.rule.js.map +0 -1
  112. package/src/WebpackConfigGenerator/Rule/ts.rule.d.ts +0 -19
  113. package/src/WebpackConfigGenerator/Rule/ts.rule.js.map +0 -1
  114. package/src/WebpackConfigGenerator/WebpackConfigSerializationUtil.d.ts +0 -10
  115. package/src/WebpackConfigGenerator/WebpackConfigSerializationUtil.js.map +0 -1
  116. package/src/WebpackConfigGenerator/WebpackEntryFactory.d.ts +0 -9
  117. package/src/WebpackConfigGenerator/WebpackEntryFactory.js +0 -14
  118. package/src/WebpackConfigGenerator/WebpackEntryFactory.js.map +0 -1
  119. package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.d.ts +0 -11
  120. package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.js +0 -16
  121. package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.js.map +0 -1
  122. package/src/WebpackConfigGenerator/WebpackResolveAliasFactory.d.ts +0 -11
  123. package/src/WebpackConfigGenerator/WebpackResolveAliasFactory.js.map +0 -1
  124. package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.d.ts +0 -7
  125. package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.js +0 -16
  126. package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.js.map +0 -1
  127. package/src/WebpackConfigGenerator/WebpackResolveModulesFactory.d.ts +0 -7
  128. package/src/WebpackConfigGenerator/WebpackResolveModulesFactory.js.map +0 -1
  129. package/src/WebpackConfigGenerator/index.d.ts +0 -30
  130. package/src/WebpackConfigGenerator/index.js +0 -189
  131. package/src/WebpackConfigGenerator/index.js.map +0 -1
  132. package/src/WebpackServerStarter.d.ts +0 -28
  133. package/src/WebpackServerStarter.js +0 -110
  134. package/src/WebpackServerStarter.js.map +0 -1
  135. package/src/index.d.ts +0 -10
  136. package/src/index.js +0 -8
  137. package/src/index.js.map +0 -1
  138. package/src/type.js +0 -3
  139. 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;
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "lib": ["ES2020"],
5
+ "module": "CommonJS",
6
+ "target": "ES2015",
7
+ "noEmit": true,
8
+ "rootDir": "../script/"
9
+ },
10
+ "include": ["../script/"]
11
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "lib": ["ES2020"],
5
+ "module": "CommonJS",
6
+ "target": "ES2015",
7
+ "rootDir": "../",
8
+ "outDir": "../build/",
9
+ "isolatedModules": true
10
+ },
11
+ "include": ["../src/"]
12
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "lib": ["ES2020"],
5
+ "module": "CommonJS",
6
+ "target": "ES2015",
7
+ "rootDir": "../",
8
+ "outDir": "../build/",
9
+ "isolatedModules": true
10
+ },
11
+ "include": ["../src/", "../test/"]
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinnacle0/webpack-util",
3
- "version": "0.3.15-beta0",
3
+ "version": "0.3.16-beta1",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -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
+ ]);
@@ -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
- "use strict";
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
@@ -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
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WebpackBuilder = void 0;
7
- const devtool_util_1 = require("@pinnacle0/devtool-util");
8
- const fs_extra_1 = __importDefault(require("fs-extra"));
9
- const path_1 = __importDefault(require("path"));
10
- const webpack_1 = __importDefault(require("webpack"));
11
- const ProjectStructureChecker_1 = require("./ProjectStructureChecker");
12
- const CodeStyleChecker_1 = require("./CodeStyleChecker");
13
- const WebpackConfigGenerator_1 = require("./WebpackConfigGenerator");
14
- const TestRunner_1 = require("./TestRunner");
15
- const CanadyarnRunner_1 = require("./CanadyarnRunner");
16
- const CoreUtil_1 = require("./CoreUtil");
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
- constructor(options) {
33
- var _a;
34
- this.logger = devtool_util_1.Utility.createConsoleLogger("WebpackBuilder");
35
- const webpackConfigGenerator = new WebpackConfigGenerator_1.WebpackConfigGenerator(options);
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 = (_a = options.extraCheckDirectories) !== null && _a !== void 0 ? _a : [];
39
- this.projectStaticDirectory = path_1.default.join(this.projectDirectory, "static");
40
- this.projectProfilingJSONOutputPath = path_1.default.join(this.projectDirectory, "profile.json");
41
- this.outputDirectory = path_1.default.join(this.projectDirectory, "build/dist");
42
- this.isFastMode = CoreUtil_1.CoreUtil.isFastMode();
43
- this.enableProfiling = CoreUtil_1.CoreUtil.profilingEnabled();
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 CanadyarnRunner_1.CanadyarnRunner({
65
+ new CanadyarnRunner({
50
66
  rootDirectory: this.rootDirectory,
51
67
  }).run();
52
- new TestRunner_1.TestRunner({
68
+ new TestRunner({
53
69
  projectDirectory: this.projectDirectory,
54
70
  extraCheckDirectories: this.extraCheckDirectories,
55
71
  }).run();
56
- new ProjectStructureChecker_1.ProjectStructureChecker({
72
+ new ProjectStructureChecker({
57
73
  projectDirectory: this.projectDirectory,
58
74
  extraCheckDirectories: this.extraCheckDirectories,
59
75
  }).run();
60
- new CodeStyleChecker_1.CodeStyleChecker({
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
- cleanDistFolder() {
85
+
86
+ private cleanDistFolder() {
70
87
  this.logger.task("Cleaning build dist folder");
71
- fs_extra_1.default.emptyDirSync(this.outputDirectory);
88
+ fs.emptyDirSync(this.outputDirectory);
72
89
  }
73
- copyStatic() {
90
+
91
+ private copyStatic() {
74
92
  this.logger.task("Copying static assets to build dist folder");
75
- fs_extra_1.default.copySync(this.projectStaticDirectory, this.outputDirectory, { dereference: true });
93
+ fs.copySync(this.projectStaticDirectory, this.outputDirectory, {dereference: true});
76
94
  }
77
- bundleByWebpack() {
95
+
96
+ private bundleByWebpack() {
78
97
  this.logger.task("Starting webpack");
79
- (0, webpack_1.default)(this.webpackConfig).run((error, stats) => {
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
- fs_extra_1.default.writeFileSync(this.projectProfilingJSONOutputPath, JSON.stringify(statsJSON, null, 2), { encoding: "utf8" });
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
- getRawWarningsAndErrors(info) {
107
- const warnings = [];
108
- const errors = [];
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 { warnings, errors };
146
+ return {warnings, errors};
125
147
  }
126
148
  }
127
- exports.WebpackBuilder = WebpackBuilder;
128
- //# sourceMappingURL=WebpackBuilder.js.map