shakapacker 8.0.2 → 9.2.0
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.
- checksums.yaml +4 -4
- data/.eslintignore +1 -0
- data/.eslintrc.fast.js +40 -0
- data/.eslintrc.js +48 -0
- data/.github/STATUS.md +1 -0
- data/.github/workflows/claude-code-review.yml +54 -0
- data/.github/workflows/claude.yml +50 -0
- data/.github/workflows/dummy.yml +9 -4
- data/.github/workflows/generator.yml +32 -10
- data/.github/workflows/node.yml +23 -1
- data/.github/workflows/ruby.yml +33 -2
- data/.github/workflows/test-bundlers.yml +170 -0
- data/.gitignore +20 -0
- data/.husky/pre-commit +2 -0
- data/.npmignore +56 -0
- data/.prettierignore +3 -0
- data/.rubocop.yml +1 -0
- data/.yalcignore +26 -0
- data/CHANGELOG.md +302 -16
- data/CLAUDE.md +29 -0
- data/CONTRIBUTING.md +138 -20
- data/Gemfile.lock +83 -89
- data/README.md +343 -105
- data/Rakefile +39 -4
- data/TODO.md +50 -0
- data/TODO_v9.md +87 -0
- data/bin/export-bundler-config +11 -0
- data/conductor-setup.sh +70 -0
- data/conductor.json +7 -0
- data/docs/cdn_setup.md +379 -0
- data/docs/common-upgrades.md +615 -0
- data/docs/css-modules-export-mode.md +512 -0
- data/docs/deployment.md +62 -9
- data/docs/optional-peer-dependencies.md +198 -0
- data/docs/peer-dependencies.md +60 -0
- data/docs/react.md +6 -14
- data/docs/releasing.md +197 -0
- data/docs/rspack.md +190 -0
- data/docs/rspack_migration_guide.md +305 -0
- data/docs/subresource_integrity.md +54 -0
- data/docs/transpiler-migration.md +209 -0
- data/docs/transpiler-performance.md +179 -0
- data/docs/troubleshooting.md +157 -22
- data/docs/typescript-migration.md +379 -0
- data/docs/typescript.md +99 -0
- data/docs/using_esbuild_loader.md +3 -3
- data/docs/using_swc_loader.md +112 -10
- data/docs/v6_upgrade.md +10 -0
- data/docs/v8_upgrade.md +3 -5
- data/docs/v9_upgrade.md +458 -0
- data/gemfiles/Gemfile-rails.6.0.x +2 -1
- data/gemfiles/Gemfile-rails.6.1.x +1 -1
- data/gemfiles/Gemfile-rails.7.0.x +2 -2
- data/gemfiles/Gemfile-rails.7.1.x +1 -2
- data/gemfiles/Gemfile-rails.7.2.x +11 -0
- data/gemfiles/Gemfile-rails.8.0.x +11 -0
- data/lib/install/bin/export-bundler-config +11 -0
- data/lib/install/bin/shakapacker +4 -6
- data/lib/install/bin/shakapacker-dev-server +1 -1
- data/lib/install/config/rspack/rspack.config.js +6 -0
- data/lib/install/config/rspack/rspack.config.ts +7 -0
- data/lib/install/config/shakapacker.yml +25 -5
- data/lib/install/config/webpack/webpack.config.ts +7 -0
- data/lib/install/package.json +38 -0
- data/lib/install/template.rb +194 -44
- data/lib/shakapacker/bundler_switcher.rb +329 -0
- data/lib/shakapacker/compiler.rb +2 -1
- data/lib/shakapacker/compiler_strategy.rb +2 -2
- data/lib/shakapacker/configuration.rb +173 -2
- data/lib/shakapacker/dev_server_runner.rb +29 -8
- data/lib/shakapacker/digest_strategy.rb +2 -1
- data/lib/shakapacker/doctor.rb +905 -0
- data/lib/shakapacker/helper.rb +64 -16
- data/lib/shakapacker/manifest.rb +10 -3
- data/lib/shakapacker/mtime_strategy.rb +1 -1
- data/lib/shakapacker/railtie.rb +4 -4
- data/lib/shakapacker/rspack_runner.rb +19 -0
- data/lib/shakapacker/runner.rb +159 -10
- data/lib/shakapacker/swc_migrator.rb +384 -0
- data/lib/shakapacker/utils/manager.rb +15 -2
- data/lib/shakapacker/version.rb +1 -1
- data/lib/shakapacker/version_checker.rb +2 -2
- data/lib/shakapacker/webpack_runner.rb +6 -43
- data/lib/shakapacker.rb +22 -11
- data/lib/tasks/shakapacker/doctor.rake +8 -0
- data/lib/tasks/shakapacker/export_bundler_config.rake +72 -0
- data/lib/tasks/shakapacker/install.rake +12 -2
- data/lib/tasks/shakapacker/migrate_to_swc.rake +13 -0
- data/lib/tasks/shakapacker/switch_bundler.rake +82 -0
- data/lib/tasks/shakapacker.rake +2 -0
- data/package/.npmignore +4 -0
- data/package/babel/preset.ts +56 -0
- data/package/config.ts +175 -0
- data/package/configExporter/cli.ts +683 -0
- data/package/configExporter/configDocs.ts +102 -0
- data/package/configExporter/fileWriter.ts +92 -0
- data/package/configExporter/index.ts +5 -0
- data/package/configExporter/types.ts +36 -0
- data/package/configExporter/yamlSerializer.ts +266 -0
- data/package/{dev_server.js → dev_server.ts} +8 -5
- data/package/env.ts +92 -0
- data/package/environments/__type-tests__/rspack-plugin-compatibility.ts +30 -0
- data/package/environments/{base.js → base.ts} +56 -60
- data/package/environments/development.ts +90 -0
- data/package/environments/production.ts +80 -0
- data/package/environments/test.ts +53 -0
- data/package/environments/types.ts +98 -0
- data/package/esbuild/index.ts +42 -0
- data/package/index.d.ts +3 -60
- data/package/index.ts +55 -0
- data/package/loaders.d.ts +28 -0
- data/package/optimization/rspack.ts +36 -0
- data/package/optimization/webpack.ts +57 -0
- data/package/plugins/rspack.ts +103 -0
- data/package/plugins/webpack.ts +62 -0
- data/package/rspack/index.ts +64 -0
- data/package/rules/{babel.js → babel.ts} +2 -2
- data/package/rules/{coffee.js → coffee.ts} +1 -1
- data/package/rules/css.ts +3 -0
- data/package/rules/{erb.js → erb.ts} +1 -1
- data/package/rules/esbuild.ts +10 -0
- data/package/rules/file.ts +40 -0
- data/package/rules/{jscommon.js → jscommon.ts} +4 -4
- data/package/rules/{less.js → less.ts} +4 -4
- data/package/rules/raw.ts +25 -0
- data/package/rules/rspack.ts +176 -0
- data/package/rules/{sass.js → sass.ts} +7 -3
- data/package/rules/{stylus.js → stylus.ts} +4 -8
- data/package/rules/swc.ts +10 -0
- data/package/rules/webpack.ts +16 -0
- data/package/swc/index.ts +56 -0
- data/package/types/README.md +88 -0
- data/package/types/index.ts +61 -0
- data/package/types.ts +108 -0
- data/package/utils/configPath.ts +6 -0
- data/package/utils/debug.ts +49 -0
- data/package/utils/defaultConfigPath.ts +4 -0
- data/package/utils/errorCodes.ts +219 -0
- data/package/utils/errorHelpers.ts +143 -0
- data/package/utils/getStyleRule.ts +64 -0
- data/package/utils/helpers.ts +85 -0
- data/package/utils/{inliningCss.js → inliningCss.ts} +3 -3
- data/package/utils/pathValidation.ts +139 -0
- data/package/utils/requireOrError.ts +15 -0
- data/package/utils/snakeToCamelCase.ts +5 -0
- data/package/utils/typeGuards.ts +342 -0
- data/package/utils/validateDependencies.ts +61 -0
- data/package/webpack-types.d.ts +33 -0
- data/package/webpackDevServerConfig.ts +117 -0
- data/package-lock.json +13047 -0
- data/package.json +154 -18
- data/scripts/remove-use-strict.js +45 -0
- data/scripts/type-check-no-emit.js +27 -0
- data/test/helpers.js +1 -1
- data/test/package/config.test.js +43 -0
- data/test/package/env.test.js +42 -7
- data/test/package/environments/base.test.js +5 -1
- data/test/package/rules/babel.test.js +16 -0
- data/test/package/rules/esbuild.test.js +1 -1
- data/test/package/rules/raw.test.js +40 -7
- data/test/package/rules/swc.test.js +1 -1
- data/test/package/rules/webpack.test.js +35 -0
- data/test/package/staging.test.js +4 -3
- data/test/package/transpiler-defaults.test.js +127 -0
- data/test/peer-dependencies.sh +85 -0
- data/test/scripts/remove-use-strict.test.js +125 -0
- data/test/typescript/build.test.js +118 -0
- data/test/typescript/environments.test.js +107 -0
- data/test/typescript/pathValidation.test.js +142 -0
- data/test/typescript/securityValidation.test.js +182 -0
- data/tools/README.md +124 -0
- data/tools/css-modules-v9-codemod.js +179 -0
- data/tsconfig.eslint.json +16 -0
- data/tsconfig.json +38 -0
- data/yarn.lock +4165 -2706
- metadata +129 -41
- data/package/babel/preset.js +0 -37
- data/package/config.js +0 -54
- data/package/env.js +0 -48
- data/package/environments/development.js +0 -13
- data/package/environments/production.js +0 -88
- data/package/environments/test.js +0 -3
- data/package/esbuild/index.js +0 -40
- data/package/index.js +0 -40
- data/package/rules/css.js +0 -3
- data/package/rules/esbuild.js +0 -10
- data/package/rules/file.js +0 -29
- data/package/rules/index.js +0 -20
- data/package/rules/raw.js +0 -5
- data/package/rules/swc.js +0 -10
- data/package/swc/index.js +0 -50
- data/package/utils/configPath.js +0 -4
- data/package/utils/defaultConfigPath.js +0 -2
- data/package/utils/getStyleRule.js +0 -40
- data/package/utils/helpers.js +0 -58
- data/package/utils/snakeToCamelCase.js +0 -5
- data/package/webpackDevServerConfig.js +0 -71
- data/test/package/rules/index.test.js +0 -16
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { Config, DevServerConfig, YamlConfig } from "../types"
|
|
2
|
+
import { isPathTraversalSafe, validatePort } from "./pathValidation"
|
|
3
|
+
|
|
4
|
+
// Cache for validated configs with TTL
|
|
5
|
+
interface CacheEntry {
|
|
6
|
+
result: boolean
|
|
7
|
+
timestamp: number
|
|
8
|
+
configHash?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let validatedConfigs = new WeakMap<object, CacheEntry>()
|
|
12
|
+
|
|
13
|
+
// Cache computed values to avoid repeated checks
|
|
14
|
+
let cachedIsWatchMode: boolean | null = null
|
|
15
|
+
let cachedCacheTTL: number | null = null
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Detect if running in watch mode (cached)
|
|
19
|
+
*/
|
|
20
|
+
function isWatchMode(): boolean {
|
|
21
|
+
if (cachedIsWatchMode === null) {
|
|
22
|
+
cachedIsWatchMode = process.argv.includes('--watch') || process.env.WEBPACK_WATCH === 'true'
|
|
23
|
+
}
|
|
24
|
+
return cachedIsWatchMode
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Get cache TTL based on environment (cached)
|
|
29
|
+
*/
|
|
30
|
+
function getCacheTTL(): number {
|
|
31
|
+
if (cachedCacheTTL === null) {
|
|
32
|
+
if (process.env.SHAKAPACKER_CACHE_TTL) {
|
|
33
|
+
cachedCacheTTL = parseInt(process.env.SHAKAPACKER_CACHE_TTL, 10)
|
|
34
|
+
} else if (process.env.NODE_ENV === 'production' && !isWatchMode()) {
|
|
35
|
+
cachedCacheTTL = Infinity
|
|
36
|
+
} else if (isWatchMode()) {
|
|
37
|
+
cachedCacheTTL = 5000 // 5 seconds in watch mode
|
|
38
|
+
} else {
|
|
39
|
+
cachedCacheTTL = 60000 // 1 minute in dev
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return cachedCacheTTL
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Only validate in development or when explicitly enabled
|
|
46
|
+
function shouldValidate(): boolean {
|
|
47
|
+
return process.env.NODE_ENV !== 'production' || process.env.SHAKAPACKER_STRICT_VALIDATION === 'true'
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Debug logging for cache operations
|
|
51
|
+
const debugCache = process.env.SHAKAPACKER_DEBUG_CACHE === 'true'
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Clear the validation cache
|
|
55
|
+
* Useful for testing or when config files change
|
|
56
|
+
*/
|
|
57
|
+
export function clearValidationCache(): void {
|
|
58
|
+
// Reassign to a new WeakMap to clear all entries
|
|
59
|
+
validatedConfigs = new WeakMap<object, CacheEntry>()
|
|
60
|
+
if (debugCache) {
|
|
61
|
+
console.log('[SHAKAPACKER DEBUG] Validation cache cleared')
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Type guard to validate Config object at runtime
|
|
67
|
+
* In production, caches results for performance unless SHAKAPACKER_STRICT_VALIDATION is set
|
|
68
|
+
*
|
|
69
|
+
* IMPORTANT: Path traversal security checks ALWAYS run regardless of environment or validation mode.
|
|
70
|
+
* This ensures application security is never compromised for performance.
|
|
71
|
+
*/
|
|
72
|
+
export function isValidConfig(obj: unknown): obj is Config {
|
|
73
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
74
|
+
return false
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Check cache with TTL
|
|
78
|
+
const cached = validatedConfigs.get(obj as object)
|
|
79
|
+
if (cached && (Date.now() - cached.timestamp) < getCacheTTL()) {
|
|
80
|
+
if (debugCache) {
|
|
81
|
+
console.log(`[SHAKAPACKER DEBUG] Config validation cache hit (result: ${cached.result})`)
|
|
82
|
+
}
|
|
83
|
+
return cached.result
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const config = obj as Record<string, unknown>
|
|
87
|
+
|
|
88
|
+
// Check required string fields
|
|
89
|
+
const requiredStringFields = [
|
|
90
|
+
'source_path',
|
|
91
|
+
'source_entry_path',
|
|
92
|
+
'public_root_path',
|
|
93
|
+
'public_output_path',
|
|
94
|
+
'cache_path',
|
|
95
|
+
'javascript_transpiler'
|
|
96
|
+
]
|
|
97
|
+
|
|
98
|
+
for (const field of requiredStringFields) {
|
|
99
|
+
if (typeof config[field] !== 'string') {
|
|
100
|
+
// Cache negative result
|
|
101
|
+
validatedConfigs.set(obj as object, { result: false, timestamp: Date.now() })
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
// SECURITY: Path traversal validation ALWAYS runs (not subject to shouldValidate)
|
|
105
|
+
// This ensures paths are safe regardless of environment or validation mode
|
|
106
|
+
if (field.includes('path') && !isPathTraversalSafe(config[field] as string)) {
|
|
107
|
+
console.warn(`[SHAKAPACKER SECURITY] Invalid path in ${field}: ${config[field]}`)
|
|
108
|
+
validatedConfigs.set(obj as object, { result: false, timestamp: Date.now() })
|
|
109
|
+
return false
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Check required boolean fields
|
|
114
|
+
const requiredBooleanFields = [
|
|
115
|
+
'nested_entries',
|
|
116
|
+
'css_extract_ignore_order_warnings',
|
|
117
|
+
'webpack_compile_output',
|
|
118
|
+
'shakapacker_precompile',
|
|
119
|
+
'cache_manifest',
|
|
120
|
+
'ensure_consistent_versioning',
|
|
121
|
+
'useContentHash',
|
|
122
|
+
'compile'
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
for (const field of requiredBooleanFields) {
|
|
126
|
+
if (typeof config[field] !== 'boolean') {
|
|
127
|
+
// Cache negative result
|
|
128
|
+
validatedConfigs.set(obj as object, { result: false, timestamp: Date.now() })
|
|
129
|
+
return false
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Check arrays
|
|
134
|
+
if (!Array.isArray(config.additional_paths)) {
|
|
135
|
+
// Cache negative result
|
|
136
|
+
validatedConfigs.set(obj as object, { result: false, timestamp: Date.now() })
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// SECURITY: Path traversal validation for additional_paths ALWAYS runs (not subject to shouldValidate)
|
|
141
|
+
// This critical security check ensures user-provided paths cannot escape the project directory
|
|
142
|
+
for (const additionalPath of config.additional_paths as string[]) {
|
|
143
|
+
if (!isPathTraversalSafe(additionalPath)) {
|
|
144
|
+
console.warn(`[SHAKAPACKER SECURITY] Invalid additional_path: ${additionalPath}`)
|
|
145
|
+
validatedConfigs.set(obj as object, { result: false, timestamp: Date.now() })
|
|
146
|
+
return false
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// In production, skip deep validation of optional fields unless explicitly enabled
|
|
151
|
+
// Security checks above still run regardless of this flag
|
|
152
|
+
if (!shouldValidate()) {
|
|
153
|
+
// Cache positive result - basic structure and security validated
|
|
154
|
+
validatedConfigs.set(obj as object, { result: true, timestamp: Date.now() })
|
|
155
|
+
return true
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Deep validation of optional fields (only in development or with SHAKAPACKER_STRICT_VALIDATION=true)
|
|
159
|
+
if (config.dev_server !== undefined && !isValidDevServerConfig(config.dev_server)) {
|
|
160
|
+
// Cache negative result
|
|
161
|
+
validatedConfigs.set(obj as object, { result: false, timestamp: Date.now() })
|
|
162
|
+
return false
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (config.integrity !== undefined) {
|
|
166
|
+
const integrity = config.integrity as Record<string, unknown>
|
|
167
|
+
if (typeof integrity.enabled !== 'boolean' ||
|
|
168
|
+
typeof integrity.cross_origin !== 'string') {
|
|
169
|
+
// Cache negative result
|
|
170
|
+
validatedConfigs.set(obj as object, { result: false, timestamp: Date.now() })
|
|
171
|
+
return false
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Cache positive result
|
|
176
|
+
validatedConfigs.set(obj as object, { result: true, timestamp: Date.now() })
|
|
177
|
+
|
|
178
|
+
return true
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Type guard to validate DevServerConfig object at runtime
|
|
183
|
+
* In production, performs minimal validation for performance
|
|
184
|
+
*/
|
|
185
|
+
export function isValidDevServerConfig(obj: unknown): obj is DevServerConfig {
|
|
186
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
187
|
+
return false
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// In production, skip deep validation unless explicitly enabled
|
|
191
|
+
if (!shouldValidate()) {
|
|
192
|
+
return true
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const config = obj as Record<string, unknown>
|
|
196
|
+
|
|
197
|
+
// All fields are optional, just check types if present
|
|
198
|
+
if (config.hmr !== undefined &&
|
|
199
|
+
typeof config.hmr !== 'boolean' &&
|
|
200
|
+
config.hmr !== 'only') {
|
|
201
|
+
return false
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (config.port !== undefined && !validatePort(config.port)) {
|
|
205
|
+
return false
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return true
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Type guard to validate Rspack plugin instance
|
|
213
|
+
* Checks if an object looks like a valid Rspack plugin
|
|
214
|
+
*/
|
|
215
|
+
export function isValidRspackPlugin(obj: unknown): boolean {
|
|
216
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
217
|
+
return false
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const plugin = obj as Record<string, unknown>
|
|
221
|
+
|
|
222
|
+
// Check for common plugin patterns
|
|
223
|
+
// Most rspack plugins should have an apply method
|
|
224
|
+
if (typeof plugin.apply === 'function') {
|
|
225
|
+
return true
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Check for constructor name pattern (e.g., HtmlRspackPlugin)
|
|
229
|
+
const constructorName = plugin.constructor?.name || ''
|
|
230
|
+
if (constructorName.includes('Plugin') || constructorName.includes('Rspack')) {
|
|
231
|
+
return true
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Check for common plugin properties
|
|
235
|
+
if ('name' in plugin && typeof plugin.name === 'string') {
|
|
236
|
+
return true
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return false
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Type guard to validate array of Rspack plugins
|
|
244
|
+
* Ensures all items in the array are valid plugin instances
|
|
245
|
+
*/
|
|
246
|
+
export function isValidRspackPluginArray(arr: unknown): boolean {
|
|
247
|
+
if (!Array.isArray(arr)) {
|
|
248
|
+
return false
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return arr.every(item => isValidRspackPlugin(item))
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Type guard to validate YamlConfig structure
|
|
256
|
+
* In production, performs minimal validation for performance
|
|
257
|
+
*/
|
|
258
|
+
export function isValidYamlConfig(obj: unknown): obj is YamlConfig {
|
|
259
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
260
|
+
return false
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// In production, skip deep validation unless explicitly enabled
|
|
264
|
+
if (!shouldValidate()) {
|
|
265
|
+
return true
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const config = obj as Record<string, unknown>
|
|
269
|
+
|
|
270
|
+
// Each key should map to an object
|
|
271
|
+
for (const env of Object.keys(config)) {
|
|
272
|
+
if (typeof config[env] !== 'object' || config[env] === null) {
|
|
273
|
+
return false
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return true
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Validates partial config used for merging
|
|
282
|
+
* Ensures that if fields are present, they have the correct types
|
|
283
|
+
* In production, performs minimal validation for performance
|
|
284
|
+
*/
|
|
285
|
+
export function isPartialConfig(obj: unknown): obj is Partial<Config> {
|
|
286
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
287
|
+
return false
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// In production, skip deep validation unless explicitly enabled
|
|
291
|
+
if (!shouldValidate()) {
|
|
292
|
+
return true
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const config = obj as Record<string, unknown>
|
|
296
|
+
|
|
297
|
+
// Check string fields if present
|
|
298
|
+
const stringFields = [
|
|
299
|
+
'source_path', 'source_entry_path', 'public_root_path',
|
|
300
|
+
'public_output_path', 'cache_path', 'javascript_transpiler'
|
|
301
|
+
]
|
|
302
|
+
|
|
303
|
+
for (const field of stringFields) {
|
|
304
|
+
if (field in config && typeof config[field] !== 'string') {
|
|
305
|
+
return false
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Check boolean fields if present
|
|
310
|
+
const booleanFields = [
|
|
311
|
+
'nested_entries', 'css_extract_ignore_order_warnings',
|
|
312
|
+
'webpack_compile_output', 'shakapacker_precompile',
|
|
313
|
+
'cache_manifest', 'ensure_consistent_versioning'
|
|
314
|
+
]
|
|
315
|
+
|
|
316
|
+
for (const field of booleanFields) {
|
|
317
|
+
if (field in config && typeof config[field] !== 'boolean') {
|
|
318
|
+
return false
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Check arrays if present
|
|
323
|
+
if ('additional_paths' in config && !Array.isArray(config.additional_paths)) {
|
|
324
|
+
return false
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return true
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Creates a validation error with helpful context
|
|
332
|
+
*/
|
|
333
|
+
export function createConfigValidationError(
|
|
334
|
+
configPath: string,
|
|
335
|
+
environment: string,
|
|
336
|
+
details?: string
|
|
337
|
+
): Error {
|
|
338
|
+
const message = `Invalid configuration in ${configPath} for environment '${environment}'`
|
|
339
|
+
return new Error(details ? `${message}: ${details}` : message)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates that required dependencies are installed for the selected bundler
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const { moduleExists } = require("./helpers")
|
|
6
|
+
const { error } = require("./debug")
|
|
7
|
+
|
|
8
|
+
const validateRspackDependencies = (): void => {
|
|
9
|
+
const requiredDependencies = ["@rspack/core", "rspack-manifest-plugin"]
|
|
10
|
+
|
|
11
|
+
const missingDependencies = requiredDependencies.filter(
|
|
12
|
+
(dep) => !moduleExists(dep)
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
if (missingDependencies.length > 0) {
|
|
16
|
+
error(
|
|
17
|
+
`Missing required dependencies for RSpack:\n${missingDependencies
|
|
18
|
+
.map((dep) => ` - ${dep}`)
|
|
19
|
+
.join(
|
|
20
|
+
"\n"
|
|
21
|
+
)}\n\nPlease install them with:\n npm install ${missingDependencies.join(
|
|
22
|
+
" "
|
|
23
|
+
)}`
|
|
24
|
+
)
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Missing RSpack dependencies: ${missingDependencies.join(", ")}`
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const validateWebpackDependencies = (): void => {
|
|
32
|
+
const requiredDependencies = [
|
|
33
|
+
"webpack",
|
|
34
|
+
"webpack-cli",
|
|
35
|
+
"webpack-assets-manifest"
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
const missingDependencies = requiredDependencies.filter(
|
|
39
|
+
(dep) => !moduleExists(dep)
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
if (missingDependencies.length > 0) {
|
|
43
|
+
error(
|
|
44
|
+
`Missing required dependencies for Webpack:\n${missingDependencies
|
|
45
|
+
.map((dep) => ` - ${dep}`)
|
|
46
|
+
.join(
|
|
47
|
+
"\n"
|
|
48
|
+
)}\n\nPlease install them with:\n npm install ${missingDependencies.join(
|
|
49
|
+
" "
|
|
50
|
+
)}`
|
|
51
|
+
)
|
|
52
|
+
throw new Error(
|
|
53
|
+
`Missing Webpack dependencies: ${missingDependencies.join(", ")}`
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export = {
|
|
59
|
+
validateRspackDependencies,
|
|
60
|
+
validateWebpackDependencies
|
|
61
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// @ts-ignore: webpack is an optional peer dependency (using type-only import)
|
|
2
|
+
import type { Configuration, RuleSetRule, RuleSetUseItem } from 'webpack'
|
|
3
|
+
|
|
4
|
+
export interface ShakapackerWebpackConfig extends Configuration {
|
|
5
|
+
module?: Configuration['module'] & {
|
|
6
|
+
rules?: RuleSetRule[]
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ShakapackerRule extends RuleSetRule {
|
|
11
|
+
test: RegExp
|
|
12
|
+
use: RuleSetUseItem[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ShakapackerLoaderOptions {
|
|
16
|
+
[key: string]: any
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ShakapackerLoader {
|
|
20
|
+
loader: string
|
|
21
|
+
options?: ShakapackerLoaderOptions
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type LoaderType = string | ShakapackerLoader
|
|
25
|
+
|
|
26
|
+
export interface LoaderUtils {
|
|
27
|
+
resolveLoader(name: string): string
|
|
28
|
+
createRule(test: RegExp, loaders: LoaderType[]): ShakapackerRule
|
|
29
|
+
getStyleLoader(extract?: boolean): LoaderType
|
|
30
|
+
getCssLoader(modules?: boolean): LoaderType
|
|
31
|
+
getPostCssLoader(): LoaderType
|
|
32
|
+
getSassLoader(): LoaderType
|
|
33
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { DevServerConfig } from "./types"
|
|
2
|
+
const snakeToCamelCase = require("./utils/snakeToCamelCase")
|
|
3
|
+
|
|
4
|
+
const shakapackerDevServerYamlConfig = require("./dev_server") as DevServerConfig
|
|
5
|
+
const { outputPath: contentBase, publicPath } = require("./config") as {
|
|
6
|
+
outputPath: string
|
|
7
|
+
publicPath: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface WebpackDevServerConfig {
|
|
11
|
+
devMiddleware?: {
|
|
12
|
+
publicPath?: string
|
|
13
|
+
}
|
|
14
|
+
hot?: boolean | string
|
|
15
|
+
liveReload?: boolean
|
|
16
|
+
historyApiFallback?: boolean | {
|
|
17
|
+
disableDotRule?: boolean
|
|
18
|
+
}
|
|
19
|
+
static?: {
|
|
20
|
+
publicPath?: string
|
|
21
|
+
[key: string]: unknown
|
|
22
|
+
}
|
|
23
|
+
client?: Record<string, unknown>
|
|
24
|
+
allowedHosts?: "all" | "auto" | string | string[]
|
|
25
|
+
bonjour?: boolean | Record<string, unknown>
|
|
26
|
+
compress?: boolean
|
|
27
|
+
headers?: Record<string, unknown> | (() => Record<string, unknown>)
|
|
28
|
+
host?: "local-ip" | "local-ipv4" | "local-ipv6" | string
|
|
29
|
+
http2?: boolean
|
|
30
|
+
https?: boolean | Record<string, unknown>
|
|
31
|
+
ipc?: boolean | string
|
|
32
|
+
magicHtml?: boolean
|
|
33
|
+
onAfterSetupMiddleware?: (devServer: unknown) => void
|
|
34
|
+
onBeforeSetupMiddleware?: (devServer: unknown) => void
|
|
35
|
+
open?: boolean | string | string[] | Record<string, unknown> | Record<string, unknown>[]
|
|
36
|
+
port?: "auto" | string | number
|
|
37
|
+
proxy?: unknown
|
|
38
|
+
server?: string | boolean | Record<string, unknown>
|
|
39
|
+
setupExitSignals?: boolean
|
|
40
|
+
setupMiddlewares?: (middlewares: unknown[], devServer: unknown) => unknown[]
|
|
41
|
+
watchFiles?: string | string[] | unknown
|
|
42
|
+
webSocketServer?: string | boolean | Record<string, unknown>
|
|
43
|
+
[key: string]: unknown
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const webpackDevServerMappedKeys = new Set([
|
|
47
|
+
// client, server, liveReload, devMiddleware are handled separately
|
|
48
|
+
"allowedHosts",
|
|
49
|
+
"bonjour",
|
|
50
|
+
"compress",
|
|
51
|
+
"headers",
|
|
52
|
+
"historyApiFallback",
|
|
53
|
+
"host",
|
|
54
|
+
"hot",
|
|
55
|
+
"http2",
|
|
56
|
+
"https",
|
|
57
|
+
"ipc",
|
|
58
|
+
"magicHtml",
|
|
59
|
+
"onAfterSetupMiddleware",
|
|
60
|
+
"onBeforeSetupMiddleware",
|
|
61
|
+
"open",
|
|
62
|
+
"port",
|
|
63
|
+
"proxy",
|
|
64
|
+
"server",
|
|
65
|
+
"setupExitSignals",
|
|
66
|
+
"setupMiddlewares",
|
|
67
|
+
"watchFiles",
|
|
68
|
+
"webSocketServer"
|
|
69
|
+
])
|
|
70
|
+
|
|
71
|
+
function createDevServerConfig(): WebpackDevServerConfig {
|
|
72
|
+
const devServerYamlConfig = { ...shakapackerDevServerYamlConfig } as DevServerConfig & Record<string, unknown>
|
|
73
|
+
const liveReload =
|
|
74
|
+
devServerYamlConfig.live_reload !== undefined
|
|
75
|
+
? devServerYamlConfig.live_reload
|
|
76
|
+
: !devServerYamlConfig.hmr
|
|
77
|
+
delete devServerYamlConfig.live_reload
|
|
78
|
+
|
|
79
|
+
const config: WebpackDevServerConfig = {
|
|
80
|
+
devMiddleware: {
|
|
81
|
+
publicPath
|
|
82
|
+
},
|
|
83
|
+
hot: devServerYamlConfig.hmr,
|
|
84
|
+
liveReload,
|
|
85
|
+
historyApiFallback: {
|
|
86
|
+
disableDotRule: true
|
|
87
|
+
},
|
|
88
|
+
static: {
|
|
89
|
+
publicPath: contentBase
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
delete devServerYamlConfig.hmr
|
|
93
|
+
|
|
94
|
+
if (devServerYamlConfig.static) {
|
|
95
|
+
config.static = {
|
|
96
|
+
...config.static,
|
|
97
|
+
...(typeof devServerYamlConfig.static === 'object' ? devServerYamlConfig.static as Record<string, unknown> : {})
|
|
98
|
+
}
|
|
99
|
+
delete devServerYamlConfig.static
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (devServerYamlConfig.client) {
|
|
103
|
+
config.client = devServerYamlConfig.client
|
|
104
|
+
delete devServerYamlConfig.client
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
Object.keys(devServerYamlConfig).forEach((yamlKey) => {
|
|
108
|
+
const camelYamlKey = snakeToCamelCase(yamlKey)
|
|
109
|
+
if (webpackDevServerMappedKeys.has(camelYamlKey)) {
|
|
110
|
+
config[camelYamlKey] = devServerYamlConfig[yamlKey]
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
return config
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export = createDevServerConfig
|