@pinnacle0/webpack-util 0.3.16-beta1 → 0.3.16-beta2

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/WebpackBuilder.d.ts +38 -0
  21. package/src/{WebpackBuilder.ts → WebpackBuilder.js} +49 -69
  22. package/src/WebpackBuilder.js.map +1 -0
  23. package/src/WebpackConfigGenerator/ConfigEntryDescriptorsFactory.d.ts +13 -0
  24. package/src/WebpackConfigGenerator/{ConfigEntryDescriptorsFactory.ts → ConfigEntryDescriptorsFactory.js} +29 -36
  25. package/src/WebpackConfigGenerator/ConfigEntryDescriptorsFactory.js.map +1 -0
  26. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.d.ts +9 -0
  27. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.js +20 -0
  28. package/src/WebpackConfigGenerator/HTMLWebpackPluginsFactory.js.map +1 -0
  29. package/src/WebpackConfigGenerator/Plugin/css.plugin.d.ts +16 -0
  30. package/src/WebpackConfigGenerator/Plugin/css.plugin.js +33 -0
  31. package/src/WebpackConfigGenerator/Plugin/css.plugin.js.map +1 -0
  32. package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.d.ts +2 -0
  33. package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.js +14 -0
  34. package/src/WebpackConfigGenerator/Plugin/esbuild.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 +29 -0
  39. package/src/WebpackConfigGenerator/Plugin/index.js +34 -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 +19 -0
  72. package/src/WebpackConfigGenerator/Rule/{ts.rule.ts → ts.rule.js} +16 -19
  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 -11
  106. package/config/tsconfig.src.json +0 -12
  107. package/config/tsconfig.test.json +0 -12
  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/WebpackConfigGenerator/HTMLWebpackPluginsFactory.ts +0 -24
  116. package/src/WebpackConfigGenerator/Plugin/css.plugin.ts +0 -31
  117. package/src/WebpackConfigGenerator/Plugin/esbuild.plugin.ts +0 -11
  118. package/src/WebpackConfigGenerator/Plugin/html.plugin.ts +0 -47
  119. package/src/WebpackConfigGenerator/Plugin/index.ts +0 -35
  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,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, transpileOnly: true, 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.esbuildMinimizer(),
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.every(_ => !filename.endsWith(_)),
185
- },
186
- module: {
187
- rules: [
188
- Rule.ts({tsconfigFilepath: this.tsconfigFilepath, transpileOnly: false, 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
- });
@@ -1,21 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`core-fe-hmr-babel-plugin sanity test for some react component files 1`] = `
4
- "import React from \\"react\\";
5
- import { ReactUtil } from \\"@pinnacle0/util\\";
6
- export const Main = ReactUtil.memo(\\"Main\\", () => {
7
- return React.createElement(\\"div\\", {});
8
- });"
9
- `;
10
-
11
- exports[`core-fe-hmr-babel-plugin should ignore when non core-fe source files 1`] = `
12
- "import { Module } from \\"not-core-fe\\";
13
- import { Main } from \\"./component/Main\\";
14
- const initialState = {};
15
-
16
- class FeatureModule extends Module {}
17
-
18
- const featureModule = register(new FeatureModule(\\"feature\\", initialState));
19
- export const actions = featureModule.getActions();
20
- export const MainComponent = featureModule.attachLifecycle(Main);"
21
- `;
@@ -1,53 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`core-fe-hmr-babel-plugin works with \`import * as CoreFENamespace\` 1`] = `
4
- "import * as CoreFENamespace from \\"core-fe\\";
5
- import { Main } from \\"./component/Main\\";
6
- const initialState = {};
7
-
8
- class FeatureModule extends CoreFENamespace.Module {}
9
-
10
- const featureModule = register(new FeatureModule(\\"feature\\", initialState));
11
- export const actions = featureModule.getActions();
12
- export const MainComponent = featureModule.attachLifecycle(Main);
13
- if (module.hot) module.hot.decline();"
14
- `;
15
-
16
- exports[`core-fe-hmr-babel-plugin works with \`import {Module as CoreFEModule}\` 1`] = `
17
- "import { Module as CoreFEModule } from \\"core-fe\\";
18
- import { Main } from \\"./component/Main\\";
19
- const initialState = {};
20
-
21
- class FeatureModule extends CoreFEModule {}
22
-
23
- const featureModule = register(new FeatureModule(\\"feature\\", initialState));
24
- export const actions = featureModule.getActions();
25
- export const MainComponent = featureModule.attachLifecycle(Main);
26
- if (module.hot) module.hot.decline();"
27
- `;
28
-
29
- exports[`core-fe-hmr-babel-plugin works with \`import {Module}\` 1`] = `
30
- "import { Module } from \\"core-fe\\";
31
- import { Main } from \\"./component/Main\\";
32
- const initialState = {};
33
-
34
- class FeatureModule extends Module {}
35
-
36
- const featureModule = register(new FeatureModule(\\"feature\\", initialState));
37
- export const actions = featureModule.getActions();
38
- export const MainComponent = featureModule.attachLifecycle(Main);
39
- if (module.hot) module.hot.decline();"
40
- `;
41
-
42
- exports[`core-fe-hmr-babel-plugin works with \`import CoreFEDefault, {Module}\` 1`] = `
43
- "import CoreFEDefault, { Module } from \\"core-fe\\";
44
- import { Main } from \\"./component/Main\\";
45
- const initialState = {};
46
-
47
- class FeatureModule extends Module {}
48
-
49
- const featureModule = register(new FeatureModule(\\"feature\\", initialState));
50
- export const actions = featureModule.getActions();
51
- export const MainComponent = featureModule.attachLifecycle(Main);
52
- if (module.hot) module.hot.decline();"
53
- `;
@@ -1,36 +0,0 @@
1
- import * as babel from "@babel/core";
2
- import plugin from "@pinnacle0/webpack-util/src/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin";
3
-
4
- describe("core-fe-hmr-babel-plugin", () => {
5
- test("should ignore when non core-fe source files", () => {
6
- const source = `
7
- import {Module} from "not-core-fe";
8
- import {Main} from "./component/Main";
9
-
10
- const initialState = {};
11
- class FeatureModule extends Module {}
12
-
13
- const featureModule = register(new FeatureModule("feature", initialState));
14
- export const actions = featureModule.getActions();
15
- export const MainComponent = featureModule.attachLifecycle(Main);
16
- `;
17
- const {code} = babel.transformSync(source, {plugins: [plugin]})!;
18
- expect(code).toMatchSnapshot();
19
- expect(code).not.toContain("if (module.hot) module.hot.decline();");
20
- });
21
-
22
- test("sanity test for some react component files", () => {
23
- const source = `
24
- import React from "react";
25
- import {ReactUtil} from "@pinnacle0/util";
26
-
27
- export const Main = ReactUtil.memo("Main", () => {
28
- return React.createElement("div", {});
29
- });
30
- `;
31
- const {code} = babel.transformSync(source, {plugins: [plugin]})!;
32
-
33
- expect(code).toMatchSnapshot();
34
- expect(code).not.toContain("if (module.hot) module.hot.decline();");
35
- });
36
- });
@@ -1,72 +0,0 @@
1
- import * as babel from "@babel/core";
2
- import plugin from "@pinnacle0/webpack-util/src/WebpackConfigGenerator/Rule/core-fe-hmr-babel-plugin";
3
-
4
- describe("core-fe-hmr-babel-plugin", () => {
5
- test("works with `import {Module}`", () => {
6
- const source = `
7
- import {Module} from "core-fe";
8
- import {Main} from "./component/Main";
9
-
10
- const initialState = {};
11
- class FeatureModule extends Module {}
12
-
13
- const featureModule = register(new FeatureModule("feature", initialState));
14
- export const actions = featureModule.getActions();
15
- export const MainComponent = featureModule.attachLifecycle(Main);
16
- `;
17
- const {code} = babel.transformSync(source, {plugins: [plugin]})!;
18
- expect(code).toMatchSnapshot();
19
- expect(code).toContain("if (module.hot) module.hot.decline();");
20
- });
21
-
22
- test("works with `import {Module as CoreFEModule}`", () => {
23
- const source = `
24
- import {Module as CoreFEModule} from "core-fe";
25
- import {Main} from "./component/Main";
26
-
27
- const initialState = {};
28
- class FeatureModule extends CoreFEModule {}
29
-
30
- const featureModule = register(new FeatureModule("feature", initialState));
31
- export const actions = featureModule.getActions();
32
- export const MainComponent = featureModule.attachLifecycle(Main);
33
- `;
34
- const {code} = babel.transformSync(source, {plugins: [plugin]})!;
35
- expect(code).toMatchSnapshot();
36
- expect(code).toContain("if (module.hot) module.hot.decline();");
37
- });
38
-
39
- test("works with `import CoreFEDefault, {Module}`", () => {
40
- const source = `
41
- import CoreFEDefault, {Module} from "core-fe";
42
- import {Main} from "./component/Main";
43
-
44
- const initialState = {};
45
- class FeatureModule extends Module {}
46
-
47
- const featureModule = register(new FeatureModule("feature", initialState));
48
- export const actions = featureModule.getActions();
49
- export const MainComponent = featureModule.attachLifecycle(Main);
50
- `;
51
- const {code} = babel.transformSync(source, {plugins: [plugin]})!;
52
- expect(code).toMatchSnapshot();
53
- expect(code).toContain("if (module.hot) module.hot.decline();");
54
- });
55
-
56
- test("works with `import * as CoreFENamespace`", () => {
57
- const source = `
58
- import * as CoreFENamespace from "core-fe";
59
- import {Main} from "./component/Main";
60
-
61
- const initialState = {};
62
- class FeatureModule extends CoreFENamespace.Module {}
63
-
64
- const featureModule = register(new FeatureModule("feature", initialState));
65
- export const actions = featureModule.getActions();
66
- export const MainComponent = featureModule.attachLifecycle(Main);
67
- `;
68
- const {code} = babel.transformSync(source, {plugins: [plugin]})!;
69
- expect(code).toMatchSnapshot();
70
- expect(code).toContain("if (module.hot) module.hot.decline();");
71
- });
72
- });
@@ -1,2 +0,0 @@
1
- const {a} = require("./script1");
2
- console.info(a);
@@ -1 +0,0 @@
1
- module.exports = {a: "123"};
@@ -1,58 +0,0 @@
1
- import path from "path";
2
- import fs from "fs";
3
- import webpack from "webpack";
4
- import HtmlWebpackPlugin from "html-webpack-plugin";
5
- import {ScriptTagCrossOriginPlugin} from "../../../src/WebpackConfigGenerator/Plugin/script-tag-crossorigin-plugin";
6
-
7
- const OUTPUT_DIR = path.join(__dirname, "./dist");
8
- type Entry = Omit<webpack.Configuration["entry"], "undefined">;
9
- const createConfig = (entry: Entry): webpack.Configuration => ({
10
- mode: "production",
11
- entry,
12
- output: {
13
- path: OUTPUT_DIR,
14
- },
15
- plugins: [
16
- new HtmlWebpackPlugin({
17
- filename: "index.html",
18
- }),
19
- new ScriptTagCrossOriginPlugin(),
20
- ],
21
- });
22
-
23
- describe("script-tag-crossorigin-plugin test: Add crossorigin='anonymous'", () => {
24
- beforeEach(done => {
25
- fs.rmSync(OUTPUT_DIR, {force: true, recursive: true});
26
- done();
27
- });
28
-
29
- const testPlugin = (entry: Entry, expectedResult: RegExp, done: () => void) => {
30
- const config: webpack.Configuration = createConfig(entry);
31
- webpack(config, error => {
32
- expect(error).toBeFalsy();
33
- const outputFile = fs.readFileSync(path.join(OUTPUT_DIR, "index.html"));
34
- expect(outputFile.toString()).toMatch(expectedResult);
35
- done();
36
- });
37
- };
38
-
39
- test("single entry script tag", done => {
40
- testPlugin(
41
- {
42
- main: path.join(__dirname, "./fixture/script.js"),
43
- },
44
- new RegExp('<script.+src.+\\main.js.+crossorigin="anonymous"', "gm"),
45
- done
46
- );
47
- });
48
- test("multiple entry script tag", done => {
49
- testPlugin(
50
- {
51
- main: path.join(__dirname, "./fixture/script.js"),
52
- second: path.join(__dirname, "./fixture/script.js"),
53
- },
54
- new RegExp('<script.+src.+\\main.js.+crossorigin="anonymous".*>.+<script.+src.+second\\.js.+crossorigin="anonymous"', "gm"),
55
- done
56
- );
57
- });
58
- });
@@ -1,5 +0,0 @@
1
- {
2
- "name": "@pinnacle0/webpack-util-test-project",
3
- "license": "UNLICENSED",
4
- "private": true
5
- }
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "files": [],
3
- "include": [],
4
- "references": [
5
- {
6
- "path": "./config/tsconfig.script.json"
7
- },
8
- {
9
- "path": "./config/tsconfig.src.json"
10
- },
11
- {
12
- "path": "./config/tsconfig.test.json"
13
- }
14
- ]
15
- }