@pinnacle0/webpack-util 0.3.26 → 0.3.27

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/package.json +1 -1
  2. package/src/CanadyarnRunner.d.ts +14 -0
  3. package/src/CanadyarnRunner.js +38 -0
  4. package/src/CanadyarnRunner.js.map +1 -0
  5. package/src/CodeStyleChecker.d.ts +10 -0
  6. package/src/CodeStyleChecker.js +44 -0
  7. package/src/CodeStyleChecker.js.map +1 -0
  8. package/src/Constant.d.ts +21 -0
  9. package/src/{Constant.ts → Constant.js} +5 -1
  10. package/src/Constant.js.map +1 -0
  11. package/src/CoreUtil.d.ts +11 -0
  12. package/src/CoreUtil.js +26 -0
  13. package/src/CoreUtil.js.map +1 -0
  14. package/src/ProjectStructureChecker.d.ts +18 -0
  15. package/src/ProjectStructureChecker.js +113 -0
  16. package/src/ProjectStructureChecker.js.map +1 -0
  17. package/src/TestRunner.d.ts +8 -0
  18. package/src/TestRunner.js +30 -0
  19. package/src/TestRunner.js.map +1 -0
  20. package/src/TypescriptTypeChecker.d.ts +11 -0
  21. package/src/TypescriptTypeChecker.js +32 -0
  22. package/src/TypescriptTypeChecker.js.map +1 -0
  23. package/src/WebpackBuilder.d.ts +38 -0
  24. package/src/{WebpackBuilder.ts → WebpackBuilder.js} +51 -71
  25. package/src/WebpackBuilder.js.map +1 -0
  26. package/src/WebpackConfigGenerator/ConfigEntryDescriptorsFactory.d.ts +13 -0
  27. package/src/WebpackConfigGenerator/{ConfigEntryDescriptorsFactory.ts → ConfigEntryDescriptorsFactory.js} +29 -36
  28. package/src/WebpackConfigGenerator/ConfigEntryDescriptorsFactory.js.map +1 -0
  29. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.d.ts +9 -0
  30. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.js +20 -0
  31. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.js.map +1 -0
  32. package/src/WebpackConfigGenerator/Plugin/css.plugin.d.ts +16 -0
  33. package/src/WebpackConfigGenerator/Plugin/css.plugin.js +33 -0
  34. package/src/WebpackConfigGenerator/Plugin/css.plugin.js.map +1 -0
  35. package/src/WebpackConfigGenerator/Plugin/html.plugin.d.ts +16 -0
  36. package/src/WebpackConfigGenerator/Plugin/html.plugin.js +48 -0
  37. package/src/WebpackConfigGenerator/Plugin/html.plugin.js.map +1 -0
  38. package/src/WebpackConfigGenerator/Plugin/index.d.ts +27 -0
  39. package/src/WebpackConfigGenerator/Plugin/index.js +32 -0
  40. package/src/WebpackConfigGenerator/Plugin/index.js.map +1 -0
  41. package/src/WebpackConfigGenerator/Plugin/moment.plugin.d.ts +10 -0
  42. package/src/WebpackConfigGenerator/Plugin/{moment.plugin.ts → moment.plugin.js} +12 -5
  43. package/src/WebpackConfigGenerator/Plugin/moment.plugin.js.map +1 -0
  44. package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.d.ts +4 -0
  45. package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.js +28 -0
  46. package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.js.map +1 -0
  47. package/src/WebpackConfigGenerator/Plugin/ts.plugin.d.ts +16 -0
  48. package/src/WebpackConfigGenerator/Plugin/ts.plugin.js +31 -0
  49. package/src/WebpackConfigGenerator/Plugin/ts.plugin.js.map +1 -0
  50. package/src/WebpackConfigGenerator/Plugin/webpack.plugin.d.ts +14 -0
  51. package/src/WebpackConfigGenerator/Plugin/webpack.plugin.js +24 -0
  52. package/src/WebpackConfigGenerator/Plugin/webpack.plugin.js.map +1 -0
  53. package/src/WebpackConfigGenerator/Rule/RegExpUtil.d.ts +4 -0
  54. package/src/WebpackConfigGenerator/Rule/{RegExpUtil.ts → RegExpUtil.js} +14 -8
  55. package/src/WebpackConfigGenerator/Rule/RegExpUtil.js.map +1 -0
  56. package/src/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin.d.ts +13 -0
  57. package/src/WebpackConfigGenerator/Rule/{core-fe-hmr-babel-plugin.ts → core-fe-hmr-babel-plugin.js} +9 -43
  58. package/src/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin.js.map +1 -0
  59. package/src/WebpackConfigGenerator/Rule/image.rule.d.ts +9 -0
  60. package/src/WebpackConfigGenerator/Rule/{image.rule.ts → image.rule.js} +8 -5
  61. package/src/WebpackConfigGenerator/Rule/image.rule.js.map +1 -0
  62. package/src/WebpackConfigGenerator/Rule/index.d.ts +13 -0
  63. package/src/WebpackConfigGenerator/Rule/index.js +18 -0
  64. package/src/WebpackConfigGenerator/Rule/index.js.map +1 -0
  65. package/src/WebpackConfigGenerator/Rule/other.rule.d.ts +12 -0
  66. package/src/WebpackConfigGenerator/Rule/other.rule.js +22 -0
  67. package/src/WebpackConfigGenerator/Rule/other.rule.js.map +1 -0
  68. package/src/WebpackConfigGenerator/Rule/stylesheet.rule.d.ts +17 -0
  69. package/src/WebpackConfigGenerator/Rule/{stylesheet.rule.ts → stylesheet.rule.js} +30 -34
  70. package/src/WebpackConfigGenerator/Rule/stylesheet.rule.js.map +1 -0
  71. package/src/WebpackConfigGenerator/Rule/ts.rule.d.ts +18 -0
  72. package/src/WebpackConfigGenerator/Rule/{ts.rule.ts → ts.rule.js} +16 -18
  73. package/src/WebpackConfigGenerator/Rule/ts.rule.js.map +1 -0
  74. package/src/WebpackConfigGenerator/WebpackConfigSerializationUtil.d.ts +10 -0
  75. package/src/WebpackConfigGenerator/{WebpackConfigSerializationUtil.ts → WebpackConfigSerializationUtil.js} +22 -23
  76. package/src/WebpackConfigGenerator/WebpackConfigSerializationUtil.js.map +1 -0
  77. package/src/WebpackConfigGenerator/WebpackEntryFactory.d.ts +9 -0
  78. package/src/WebpackConfigGenerator/WebpackEntryFactory.js +14 -0
  79. package/src/WebpackConfigGenerator/WebpackEntryFactory.js.map +1 -0
  80. package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.d.ts +11 -0
  81. package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.js +16 -0
  82. package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.js.map +1 -0
  83. package/src/WebpackConfigGenerator/WebpackResolveAliasFactory.d.ts +11 -0
  84. package/src/WebpackConfigGenerator/{WebpackResolveAliasFactory.ts → WebpackResolveAliasFactory.js} +13 -16
  85. package/src/WebpackConfigGenerator/WebpackResolveAliasFactory.js.map +1 -0
  86. package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.d.ts +7 -0
  87. package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.js +16 -0
  88. package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.js.map +1 -0
  89. package/src/WebpackConfigGenerator/WebpackResolveModulesFactory.d.ts +7 -0
  90. package/src/WebpackConfigGenerator/{WebpackResolveModulesFactory.ts → WebpackResolveModulesFactory.js} +7 -9
  91. package/src/WebpackConfigGenerator/WebpackResolveModulesFactory.js.map +1 -0
  92. package/src/WebpackConfigGenerator/index.d.ts +31 -0
  93. package/src/WebpackConfigGenerator/index.js +191 -0
  94. package/src/WebpackConfigGenerator/index.js.map +1 -0
  95. package/src/WebpackServerStarter.d.ts +28 -0
  96. package/src/WebpackServerStarter.js +111 -0
  97. package/src/WebpackServerStarter.js.map +1 -0
  98. package/src/index.d.ts +10 -0
  99. package/src/index.js +8 -0
  100. package/src/index.js.map +1 -0
  101. package/src/{type.ts → type.d.ts} +4 -8
  102. package/src/type.js +3 -0
  103. package/src/type.js.map +1 -0
  104. package/config/jest.config.ts +0 -35
  105. package/config/tsconfig.script.json +0 -10
  106. package/config/tsconfig.src.json +0 -12
  107. package/config/tsconfig.test.json +0 -11
  108. package/script/build.ts +0 -65
  109. package/script/format.ts +0 -7
  110. package/src/CanadyarnRunner.ts +0 -35
  111. package/src/CodeStyleChecker.ts +0 -44
  112. package/src/CoreUtil.ts +0 -24
  113. package/src/ProjectStructureChecker.ts +0 -119
  114. package/src/TestRunner.ts +0 -28
  115. package/src/TypescriptTypeChecker.ts +0 -29
  116. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.ts +0 -24
  117. package/src/WebpackConfigGenerator/Plugin/css.plugin.ts +0 -31
  118. package/src/WebpackConfigGenerator/Plugin/html.plugin.ts +0 -47
  119. package/src/WebpackConfigGenerator/Plugin/index.ts +0 -33
  120. package/src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin.ts +0 -22
  121. package/src/WebpackConfigGenerator/Plugin/ts.plugin.ts +0 -29
  122. package/src/WebpackConfigGenerator/Plugin/webpack.plugin.ts +0 -21
  123. package/src/WebpackConfigGenerator/Rule/index.ts +0 -17
  124. package/src/WebpackConfigGenerator/Rule/other.rule.ts +0 -23
  125. package/src/WebpackConfigGenerator/WebpackEntryFactory.ts +0 -22
  126. package/src/WebpackConfigGenerator/WebpackOutputPublicURLFactory.ts +0 -22
  127. package/src/WebpackConfigGenerator/WebpackResolveExtensionsFactory.ts +0 -19
  128. package/src/WebpackConfigGenerator/index.ts +0 -213
  129. package/src/WebpackServerStarter.ts +0 -138
  130. package/src/index.ts +0 -9
  131. package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/__snapshots__/shouldIgnore.test.ts.snap +0 -21
  132. package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/__snapshots__/shouldTransform.test.ts.snap +0 -53
  133. package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/shouldIgnore.test.ts +0 -36
  134. package/test/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin/shouldTransform.test.ts +0 -72
  135. package/test/WebpackConfigGenerator/plugin/fixture/script.js +0 -2
  136. package/test/WebpackConfigGenerator/plugin/fixture/script1.js +0 -1
  137. package/test/WebpackConfigGenerator/plugin/script-tag-crossorigin-plugin.test.ts +0 -58
  138. package/test/test-project/package.json +0 -5
  139. package/tsconfig.json +0 -15
@@ -1,31 +0,0 @@
1
- import MiniCssExtractPlugin from "mini-css-extract-plugin";
2
- import CssMinimizerWebpackPlugin from "css-minimizer-webpack-plugin";
3
- import type webpack from "webpack";
4
- import {WebpackConfigSerializationUtil} from "../WebpackConfigSerializationUtil";
5
-
6
- interface ExtractCssPluginOptions {
7
- enableProfiling: boolean;
8
- }
9
-
10
- /**
11
- * Applies CssNano to minimize stylesheets
12
- * after bundles/chunks are built.
13
- */
14
- export function cssMinimizerPlugin(): webpack.WebpackPluginInstance {
15
- return WebpackConfigSerializationUtil.serializablePlugin("CssMinimizerWebpackPlugin", CssMinimizerWebpackPlugin);
16
- }
17
-
18
- /**
19
- * Extract output of css transformation pipeline to standalone css files.
20
- * Must be used with `MiniCssExtractPlugin.loader`, which is included with
21
- * `Rule.stylesheet({minimize: true})`.
22
- */
23
- export function miniCssExtractPlugin({enableProfiling}: ExtractCssPluginOptions): webpack.WebpackPluginInstance {
24
- return WebpackConfigSerializationUtil.serializablePlugin("MiniCssExtractPlugin", MiniCssExtractPlugin, {
25
- filename: enableProfiling ? "static/css/[name].[contenthash:8].css" : "static/css/[contenthash:8].css",
26
- // order of css output depends on the order of imports in js,
27
- // unless all imports in js are sorted (e.g. by alphabetical order),
28
- // this flag must be set to true to avoid error
29
- ignoreOrder: true,
30
- });
31
- }
@@ -1,47 +0,0 @@
1
- import HTMLWebpackPlugin from "html-webpack-plugin";
2
- import {ScriptTagCrossOriginPlugin} from "./script-tag-crossorigin-plugin";
3
- import type webpack from "webpack";
4
- import type {HTMLEntryDescriptor} from "../../type";
5
- import {WebpackConfigSerializationUtil} from "../WebpackConfigSerializationUtil";
6
-
7
- interface HTMLPluginOptions {
8
- entry: HTMLEntryDescriptor;
9
- }
10
-
11
- /**
12
- * Creates a html file from a template with <script> and <link> injected
13
- * with the respective hashed output filenames.
14
- */
15
- export function htmlPlugin({entry}: HTMLPluginOptions): webpack.WebpackPluginInstance {
16
- return WebpackConfigSerializationUtil.serializablePlugin("HTMLWebpackPlugin", HTMLWebpackPlugin, {
17
- template: entry.htmlPath,
18
- filename: `${entry.name}.html`,
19
- chunks: [entry.name],
20
- minify: {
21
- collapseBooleanAttributes: true,
22
- collapseInlineTagWhitespace: true,
23
- collapseWhitespace: true,
24
- includeAutoGeneratedTags: false,
25
- keepClosingSlash: true,
26
- minifyCSS: true,
27
- minifyJS: true,
28
- minifyURLs: true,
29
- removeAttributeQuotes: true,
30
- removeComments: true,
31
- removeEmptyAttributes: true,
32
- removeRedundantAttributes: true,
33
- removeScriptTypeAttributes: true,
34
- removeStyleLinkTypeAttributes: true,
35
- removeTagWhitespace: true,
36
- useShortDoctype: true,
37
- },
38
- });
39
- }
40
-
41
- /**
42
- * Adds attributes to `<script>` tag inside html file generated by
43
- * HTMLWebpackPlugin. Used to add `crossorigin="anonymous"`.
44
- */
45
- export function scriptTagCrossOriginPlugin(): webpack.WebpackPluginInstance {
46
- return WebpackConfigSerializationUtil.serializablePlugin("ScriptTagCrossOriginPlugin", ScriptTagCrossOriginPlugin);
47
- }
@@ -1,33 +0,0 @@
1
- import {cssMinimizerPlugin, miniCssExtractPlugin} from "./css.plugin";
2
- import {scriptTagCrossOriginPlugin, htmlPlugin} from "./html.plugin";
3
- import {ignoreMomentLocalePlugin} from "./moment.plugin";
4
- import {reactRefreshPlugin, terserPlugin} from "./ts.plugin";
5
- import {webpackDefinePlugin, webpackProgressPlugin} from "./webpack.plugin";
6
-
7
- /**
8
- * Static factories to create \`webpack.config#plugins\` items.
9
- *
10
- * Plugins with similar functionality are grouped under a js object (as a namespace).
11
- */
12
- export class Plugin {
13
- static readonly scriptTagCrossOriginPlugin = scriptTagCrossOriginPlugin;
14
-
15
- static readonly ignoreMomentLocale = ignoreMomentLocalePlugin;
16
-
17
- static readonly reactRefresh = reactRefreshPlugin;
18
-
19
- static readonly fileOutput = {
20
- html: htmlPlugin,
21
- miniCssExtract: miniCssExtractPlugin,
22
- };
23
-
24
- static readonly minimizer = {
25
- terser: terserPlugin,
26
- css: cssMinimizerPlugin,
27
- };
28
-
29
- static readonly webpack = {
30
- progress: webpackProgressPlugin,
31
- define: webpackDefinePlugin,
32
- };
33
- }
@@ -1,22 +0,0 @@
1
- import HtmlWebpackPlugin from "html-webpack-plugin";
2
- import type webpack from "webpack";
3
-
4
- const PLUGIN_NAME = "ScriptTagCrossOriginPlugin";
5
-
6
- // This plugin adds crossorigin="anonymous" to html-webpack-plugin generated <script> tags, only work with html-webpack-plugin@4.0.0+
7
- export class ScriptTagCrossOriginPlugin implements webpack.WebpackPluginInstance {
8
- apply(compiler: webpack.Compiler): void {
9
- compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
10
- const hook = HtmlWebpackPlugin.getHooks(compilation).alterAssetTags;
11
- hook.tap(PLUGIN_NAME, result => {
12
- const {assetTags} = result;
13
- for (const scriptTag of assetTags.scripts) {
14
- if (scriptTag.attributes?.src && /.js$/.test(scriptTag.attributes.src.toString())) {
15
- scriptTag.attributes.crossorigin = "anonymous";
16
- }
17
- }
18
- return result;
19
- });
20
- });
21
- }
22
- }
@@ -1,29 +0,0 @@
1
- import ReactRefreshWebpackPlugin from "@pmmmwh/react-refresh-webpack-plugin";
2
- import TerserWebpackPlugin from "terser-webpack-plugin";
3
- import type webpack from "webpack";
4
- import {WebpackConfigSerializationUtil} from "../WebpackConfigSerializationUtil";
5
-
6
- interface TerserPluginOptions {
7
- sourceMap: boolean;
8
- }
9
-
10
- /**
11
- * Applies Terser to minimize javascript
12
- * after bundles/chunks are built.
13
- */
14
- export function terserPlugin({sourceMap}: TerserPluginOptions): webpack.WebpackPluginInstance {
15
- return WebpackConfigSerializationUtil.serializablePlugin("TerserWebpackPlugin", TerserWebpackPlugin, {
16
- terserOptions: {
17
- sourceMap,
18
- },
19
- });
20
- }
21
-
22
- /**
23
- * Adds react fast refresh functionality.
24
- * Requires babel plugin "react-refresh/babel".
25
- * Should not be used in production.
26
- */
27
- export function reactRefreshPlugin(): webpack.WebpackPluginInstance {
28
- return WebpackConfigSerializationUtil.serializablePlugin("ReactRefreshPlugin", ReactRefreshWebpackPlugin);
29
- }
@@ -1,21 +0,0 @@
1
- import webpack from "webpack";
2
- import {WebpackConfigSerializationUtil} from "../WebpackConfigSerializationUtil";
3
-
4
- interface WebpackProgressPluginOptions {
5
- enableProfiling: boolean;
6
- }
7
-
8
- /**
9
- * Reports progress during compilation.
10
- * Basically the same behavior as running webpack-cli with:
11
- * `$ webpack --progress`
12
- */
13
- export function webpackProgressPlugin({enableProfiling}: WebpackProgressPluginOptions): webpack.WebpackPluginInstance {
14
- return WebpackConfigSerializationUtil.serializablePlugin("webpack.ProgressPlugin", webpack.ProgressPlugin, {
15
- profile: enableProfiling,
16
- });
17
- }
18
-
19
- export function webpackDefinePlugin(map: {[key: string]: string}): webpack.WebpackPluginInstance {
20
- return WebpackConfigSerializationUtil.serializablePlugin("webpack.DefinePlugin", webpack.DefinePlugin, map);
21
- }
@@ -1,17 +0,0 @@
1
- import {imageRule} from "./image.rule";
2
- import {otherRule} from "./other.rule";
3
- import {stylesheetRule} from "./stylesheet.rule";
4
- import {tsRule} from "./ts.rule";
5
-
6
- /**
7
- * Static factories to create `webpack.config#modules.rules` items.
8
- */
9
- export class Rule {
10
- static readonly image = imageRule;
11
-
12
- static readonly other = otherRule;
13
-
14
- static readonly ts = tsRule;
15
-
16
- static readonly stylesheet = stylesheetRule;
17
- }
@@ -1,23 +0,0 @@
1
- import type webpack from "webpack";
2
- import {Constant} from "../../Constant";
3
- import {RegExpUtil} from "./RegExpUtil";
4
-
5
- interface OtherRuleDeps {
6
- extraExtensionsForOtherRule: string[];
7
- }
8
-
9
- /**
10
- * Handles dependency requests to file assets
11
- * by emitting as separate files.
12
- *
13
- * @see https://webpack.js.org/guides/asset-modules/
14
- */
15
- export function otherRule({extraExtensionsForOtherRule}: OtherRuleDeps): webpack.RuleSetRule {
16
- return {
17
- test: RegExpUtil.fileExtension(".ico", ...Constant.mediaExtensions, ...Constant.fontExtensions, ...extraExtensionsForOtherRule),
18
- type: "asset",
19
- generator: {
20
- filename: "static/other/[name].[hash:8][ext][query]",
21
- },
22
- };
23
- }
@@ -1,22 +0,0 @@
1
- import type webpack from "webpack";
2
- import type {EntryDescriptor} from "../type";
3
-
4
- interface StrictWebpackEntry {
5
- [entryName: string]: [string];
6
- }
7
-
8
- interface WebpackEntryFactoryOptions {
9
- configEntryDescriptors: EntryDescriptor[];
10
- }
11
-
12
- export class WebpackEntryFactory {
13
- static generate({configEntryDescriptors}: WebpackEntryFactoryOptions): NonNullable<webpack.Configuration["entry"]> {
14
- const entry: StrictWebpackEntry = {};
15
-
16
- for (const {name, entryPath} of configEntryDescriptors) {
17
- entry[name] = [entryPath];
18
- }
19
-
20
- return entry;
21
- }
22
- }
@@ -1,22 +0,0 @@
1
- export interface WebpackOutputPublicURLFactoryOptions {
2
- env: string | null;
3
- /**
4
- * Function to dynamically compute additional webpack config from `env`.
5
- * Should validates if the config contains `publicUrl` field.
6
- */
7
- webpackPublicPath: string | ((env: string) => string) | undefined;
8
- }
9
-
10
- export class WebpackOutputPublicURLFactory {
11
- static generate({env, webpackPublicPath}: WebpackOutputPublicURLFactoryOptions): string {
12
- if (!env || !webpackPublicPath) {
13
- return "/";
14
- }
15
-
16
- if (typeof webpackPublicPath === "string") {
17
- return webpackPublicPath;
18
- }
19
-
20
- return webpackPublicPath(env);
21
- }
22
- }
@@ -1,19 +0,0 @@
1
- import {Constant} from "../Constant";
2
-
3
- interface WebpackResolveExtensionsFactoryOptions {
4
- prioritizedExtensionPrefixes?: string[] | undefined;
5
- }
6
-
7
- export class WebpackResolveExtensionsFactory {
8
- static generate({prioritizedExtensionPrefixes = []}: WebpackResolveExtensionsFactoryOptions): string[] {
9
- const resolveExtensions = [];
10
-
11
- for (const prefix of prioritizedExtensionPrefixes) {
12
- resolveExtensions.push(...Constant.resolveExtensions.map(_ => `.${prefix}${_}`));
13
- }
14
-
15
- resolveExtensions.push(...Constant.resolveExtensions);
16
-
17
- return resolveExtensions;
18
- }
19
- }
@@ -1,213 +0,0 @@
1
- import {Utility} from "@pinnacle0/devtool-util";
2
- import path from "path";
3
- import type webpack from "webpack";
4
- import {Constant} from "../Constant";
5
- import {CoreUtil} from "../CoreUtil";
6
- import type {EntryDescriptor, WebpackConfigGeneratorOptions} from "../type";
7
- import {ConfigEntryDescriptorsFactory} from "./ConfigEntryDescriptorsFactory";
8
- import {HTMLWebpackPluginsFactory} from "./HTMLWebpackPluginsFactory";
9
- import {WebpackConfigSerializationUtil} from "./WebpackConfigSerializationUtil";
10
- import {WebpackEntryFactory} from "./WebpackEntryFactory";
11
- import {WebpackOutputPublicURLFactory} from "./WebpackOutputPublicURLFactory";
12
- import {WebpackResolveAliasFactory} from "./WebpackResolveAliasFactory";
13
- import {WebpackResolveExtensionsFactory} from "./WebpackResolveExtensionsFactory";
14
- import {WebpackResolveModulesFactory} from "./WebpackResolveModulesFactory";
15
- import {Plugin} from "./Plugin";
16
- import {Rule} from "./Rule";
17
-
18
- /**
19
- * Generates a webpack config with sane defaults.
20
- */
21
- export class WebpackConfigGenerator {
22
- private readonly env: string | null;
23
- private readonly projectDirectory: string;
24
- private readonly projectSrcDirectory: string;
25
- private readonly tsconfigFilepath: string;
26
- private readonly enableProfiling: boolean;
27
- private readonly maxEntryPointKiloByte: number;
28
- private readonly maxAssetKiloByte: number;
29
- private readonly isFastMode: boolean;
30
- private readonly verbose: boolean;
31
- private readonly defineVars: {[key: string]: string};
32
- private readonly extraExtensionsForOtherRule: string[];
33
-
34
- private readonly configEntryDescriptors: EntryDescriptor[];
35
- private readonly entry: NonNullable<webpack.Configuration["entry"]>;
36
- private readonly htmlWebpackPluginInstances: NonNullable<webpack.Configuration["plugins"]>;
37
- private readonly resolveExtensions: NonNullable<NonNullable<webpack.Configuration["resolve"]>["extensions"]>;
38
- private readonly resolveModules: NonNullable<NonNullable<webpack.Configuration["resolve"]>["modules"]>;
39
- private readonly resolveAliases: NonNullable<NonNullable<webpack.Configuration["resolve"]>["alias"]>;
40
- private readonly outputPublicPath: string;
41
-
42
- private readonly logger = Utility.createConsoleLogger("WebpackConfigGenerator");
43
-
44
- constructor(private readonly options: WebpackConfigGeneratorOptions) {
45
- this.env = CoreUtil.currentEnv();
46
- this.projectDirectory = options.projectDirectory;
47
- this.projectSrcDirectory = path.join(options.projectDirectory, "src");
48
- this.tsconfigFilepath = path.join(options.projectDirectory, "tsconfig.json");
49
-
50
- this.enableProfiling = CoreUtil.profilingEnabled();
51
- this.isFastMode = CoreUtil.isFastMode();
52
- this.maxEntryPointKiloByte = options.maxEntryPointKiloByte ?? Constant.maxEntryPointKiloByte;
53
- this.maxAssetKiloByte = options.maxAssetKiloByte ?? Constant.maxAssetKiloByte;
54
- this.verbose = options.verbose || false;
55
- this.defineVars = options.defineVars || {};
56
- this.extraExtensionsForOtherRule = options.extraExtensionsForOtherRule || [];
57
-
58
- this.configEntryDescriptors = ConfigEntryDescriptorsFactory.generate({
59
- indexName: options.indexName || "index",
60
- projectSrcDirectory: this.projectSrcDirectory,
61
- extraEntries: options.extraEntries || {},
62
- });
63
- this.entry = WebpackEntryFactory.generate({
64
- configEntryDescriptors: this.configEntryDescriptors,
65
- });
66
- this.htmlWebpackPluginInstances = HTMLWebpackPluginsFactory.generate({
67
- configEntryDescriptors: this.configEntryDescriptors,
68
- });
69
- this.outputPublicPath = WebpackOutputPublicURLFactory.generate({
70
- env: this.env,
71
- webpackPublicPath: options.webpackPublicPath,
72
- });
73
- this.resolveExtensions = WebpackResolveExtensionsFactory.generate({
74
- prioritizedExtensionPrefixes: options.prioritizedExtensionPrefixes,
75
- });
76
- this.resolveModules = WebpackResolveModulesFactory.generate({
77
- projectSrcDirectory: this.projectSrcDirectory,
78
- });
79
- this.resolveAliases = WebpackResolveAliasFactory.generate({
80
- env: this.env,
81
- resolvers: options.dynamicPathResolvers ?? [],
82
- });
83
-
84
- this.logger.info("Config constructed:");
85
- for (const info of [
86
- `-- Code Checking: ${this.isFastMode ? "Minimal Check" : "Default"}`,
87
- `-- Env: ${this.env || "[N/A]"}`,
88
- `-- Src Directory: ${this.projectSrcDirectory}`,
89
- `-- HTML Entries: ${Object.keys(this.entry).join(" / ")}`,
90
- `-- Webpack Public URL: ${this.outputPublicPath}`,
91
- `-- Dynamic Aliases: ${JSON.stringify(this.resolveAliases, null, 4)}`,
92
- `-- Use Cache: ${this.env === null}`,
93
- ]) {
94
- console.info(info);
95
- }
96
- }
97
-
98
- development(): webpack.Configuration {
99
- const config: webpack.Configuration = {
100
- mode: "development",
101
- entry: this.entry,
102
- target: "web", // https://github.com/webpack/webpack-dev-server/issues/2758 Hot-reload will break if we provide list to target
103
- output: {
104
- filename: "static/js/[name].js",
105
- publicPath: "/",
106
- },
107
- resolve: {
108
- extensions: this.resolveExtensions,
109
- modules: this.resolveModules,
110
- alias: this.resolveAliases,
111
- },
112
- devtool: "inline-cheap-module-source-map",
113
- optimization: {
114
- usedExports: true,
115
- splitChunks: {
116
- automaticNameDelimiter: "-",
117
- maxAsyncRequests: 30,
118
- },
119
- },
120
- module: {
121
- rules: [
122
- Rule.ts({tsconfigFilepath: this.tsconfigFilepath, fastRefresh: true}),
123
- Rule.stylesheet({minimize: false}),
124
- Rule.image(),
125
- Rule.other({extraExtensionsForOtherRule: this.extraExtensionsForOtherRule}),
126
- // prettier-format-preserve
127
- ],
128
- },
129
- plugins: [
130
- ...this.htmlWebpackPluginInstances,
131
- Plugin.ignoreMomentLocale(),
132
- Plugin.reactRefresh(),
133
- Plugin.webpack.progress({enableProfiling: false}),
134
- Plugin.webpack.define(this.defineVars),
135
- // prettier-format-preserve
136
- ],
137
- cache:
138
- this.env === null
139
- ? {
140
- type: "filesystem",
141
- cacheDirectory: path.join(this.projectDirectory, ".webpack-cache"),
142
- }
143
- : false,
144
- };
145
- if (this.verbose || CoreUtil.verbose()) {
146
- this.logger.info("Full webpack config:");
147
- console.info(WebpackConfigSerializationUtil.configToString(config));
148
- }
149
- return config;
150
- }
151
-
152
- production(outputDirectory: string): webpack.Configuration {
153
- const config: webpack.Configuration = {
154
- mode: "production",
155
- entry: this.entry,
156
- target: ["web", "es5"],
157
- output: {
158
- path: outputDirectory,
159
- filename: this.enableProfiling ? "static/js/[name].js" : pathInfo => this.configEntryDescriptors.find(_ => _.name === pathInfo.chunk!.name)!.outputFilename,
160
- chunkFilename: this.enableProfiling ? "static/js/[id].[name].js" : "static/js/[id].[chunkhash:8].js",
161
- publicPath: this.outputPublicPath,
162
- crossOriginLoading: "anonymous",
163
- },
164
- resolve: {
165
- extensions: this.resolveExtensions,
166
- modules: this.resolveModules,
167
- alias: this.resolveAliases,
168
- },
169
- bail: true,
170
- optimization: {
171
- splitChunks: {
172
- automaticNameDelimiter: "-",
173
- maxAsyncRequests: 30,
174
- },
175
- minimizer: [
176
- Plugin.minimizer.terser({sourceMap: true}),
177
- Plugin.minimizer.css(),
178
- // prettier-format-preserve
179
- ],
180
- },
181
- performance: {
182
- maxEntrypointSize: this.enableProfiling ? Number.MAX_SAFE_INTEGER : this.maxEntryPointKiloByte * 1000,
183
- maxAssetSize: this.maxAssetKiloByte * 1000,
184
- assetFilter: (filename: string) => Constant.mediaExtensions.concat(this.extraExtensionsForOtherRule).every(_ => !filename.endsWith(_)),
185
- },
186
- module: {
187
- rules: [
188
- Rule.ts({tsconfigFilepath: this.tsconfigFilepath, fastRefresh: false}),
189
- Rule.stylesheet({minimize: true}),
190
- Rule.image(),
191
- Rule.other({extraExtensionsForOtherRule: this.extraExtensionsForOtherRule}),
192
- // prettier-format-preserve
193
- ],
194
- },
195
- plugins: [
196
- ...this.htmlWebpackPluginInstances,
197
- Plugin.scriptTagCrossOriginPlugin(),
198
- Plugin.ignoreMomentLocale(),
199
- Plugin.fileOutput.miniCssExtract({enableProfiling: this.enableProfiling}),
200
- ...(this.enableProfiling ? [Plugin.webpack.progress({enableProfiling: true})] : []), // disable to not bloat up CI logs
201
- Plugin.webpack.define(this.defineVars),
202
- // prettier-format-preserve
203
- ],
204
- };
205
- if (this.verbose || CoreUtil.verbose()) {
206
- this.logger.info("Full webpack config:");
207
- console.info(WebpackConfigSerializationUtil.configToString(config));
208
- }
209
- return config;
210
- }
211
- }
212
-
213
- export type {WebpackConfigGeneratorOptions};
@@ -1,138 +0,0 @@
1
- import {Utility} from "@pinnacle0/devtool-util";
2
- import path from "path";
3
- import webpack from "webpack";
4
- import DevServer from "webpack-dev-server";
5
- import type {Application as ExpressApplication} from "express";
6
- import type {WebpackConfigGeneratorOptions} from "./WebpackConfigGenerator";
7
- import {WebpackConfigGenerator} from "./WebpackConfigGenerator";
8
- import type WebpackDevServer from "webpack-dev-server";
9
-
10
- export interface WebpackServerStarterOptions
11
- extends Pick<
12
- WebpackConfigGeneratorOptions,
13
- // prettier-reserve
14
- "projectDirectory" | "dynamicPathResolvers" | "extraEntries" | "prioritizedExtensionPrefixes" | "defineVars" | "extraExtensionsForOtherRule"
15
- > {
16
- port: number;
17
- apiProxy?: {
18
- target: string;
19
- context: string[];
20
- };
21
- interceptExpressApp?: (app: ExpressApplication) => void;
22
- }
23
-
24
- /**
25
- * Start webpack dev server, by creating WebpackServerStarter instance and then run.
26
- *
27
- ***************************************
28
- *
29
- * Add "--env envName" to command line, if you want to switch config folder dynamically.
30
- */
31
-
32
- export class WebpackServerStarter {
33
- private readonly devServerConfigContentBase: string;
34
- private readonly onBeforeSetupMiddleware: WebpackDevServer.Configuration["onBeforeSetupMiddleware"];
35
- private readonly port: number;
36
- private readonly apiProxy:
37
- | {
38
- target: string;
39
- context: string[];
40
- secure: boolean;
41
- changeOrigin: boolean;
42
- }[]
43
- | undefined;
44
- private readonly logger = Utility.createConsoleLogger("WebpackServerStarter");
45
- private readonly webpackConfig: webpack.Configuration;
46
-
47
- constructor({
48
- projectDirectory,
49
- port,
50
- apiProxy,
51
- interceptExpressApp,
52
- dynamicPathResolvers,
53
- extraEntries,
54
- prioritizedExtensionPrefixes,
55
- defineVars,
56
- extraExtensionsForOtherRule,
57
- }: WebpackServerStarterOptions) {
58
- this.devServerConfigContentBase = path.join(projectDirectory, "static");
59
- this.port = port;
60
- this.apiProxy = apiProxy
61
- ? [
62
- {
63
- context: apiProxy.context,
64
- target: apiProxy.target,
65
- secure: false,
66
- changeOrigin: true,
67
- },
68
- ]
69
- : undefined;
70
- this.onBeforeSetupMiddleware = interceptExpressApp ? devServer => (devServer.app ? interceptExpressApp(devServer.app) : undefined) : undefined;
71
- this.webpackConfig = new WebpackConfigGenerator({
72
- projectDirectory,
73
- dynamicPathResolvers,
74
- extraEntries,
75
- prioritizedExtensionPrefixes,
76
- defineVars,
77
- extraExtensionsForOtherRule,
78
- }).development();
79
- }
80
-
81
- async run() {
82
- try {
83
- this.logger.info(["Starting dev server on port", String(this.port)]);
84
- const server = this.createDevServerInstance();
85
- const signals: NodeJS.Signals[] = ["SIGINT", "SIGTERM"];
86
- for (const signal of signals) {
87
- process.on(signal, () => {
88
- // force stop webpack dev server
89
- server.stopCallback(() => {});
90
- process.exit();
91
- });
92
- }
93
- await server.start();
94
- } catch (e) {
95
- this.logger.error(e);
96
- console.error(e);
97
- process.exit(1);
98
- }
99
- }
100
-
101
- private createDevServerInstance() {
102
- return new DevServer(
103
- {
104
- host: "0.0.0.0",
105
- port: this.port,
106
- static: {
107
- directory: this.devServerConfigContentBase,
108
- },
109
- historyApiFallback: true,
110
- server: {
111
- type: "https",
112
- },
113
- compress: true,
114
- hot: true,
115
- client: {
116
- overlay: {
117
- errors: true,
118
- },
119
- },
120
- onBeforeSetupMiddleware: this.onBeforeSetupMiddleware,
121
- devMiddleware: {
122
- stats: {
123
- colors: true,
124
- // https://github.com/webpack/webpack/blob/b65d060040a26255cbf6f50350fef4d4ffcce4d7/lib/stats/DefaultStatsPresetPlugin.js#L96-L103
125
- all: false,
126
- errors: true,
127
- errorsCount: true,
128
- warnings: true,
129
- warningsCount: true,
130
- logging: "warn",
131
- },
132
- },
133
- proxy: this.apiProxy,
134
- },
135
- webpack(this.webpackConfig)
136
- );
137
- }
138
- }
package/src/index.ts DELETED
@@ -1,9 +0,0 @@
1
- import {WebpackBuilder} from "./WebpackBuilder";
2
- import {WebpackServerStarter} from "./WebpackServerStarter";
3
- import {CoreUtil} from "./CoreUtil";
4
-
5
- export const WebpackUtil = Object.freeze({
6
- Builder: WebpackBuilder,
7
- ServerStarter: WebpackServerStarter,
8
- ...CoreUtil,
9
- });