|
1 | | -"use strict"; |
2 | 1 | /* eslint global-require: 0 */ |
3 | 2 | /* eslint import/no-dynamic-require: 0 */ |
4 | | -const { basename, dirname, join, relative, resolve } = require("path"); |
5 | | -const { existsSync, readdirSync } = require("fs"); |
6 | | -const extname = require("path-complete-extname"); |
7 | | -const config = require("../config"); |
8 | | -const { isProduction } = require("../env"); |
9 | | -const pluginsPath = resolve(__dirname, "..", "plugins", `${config.assets_bundler}.js`); |
10 | | -const { getPlugins } = require(pluginsPath); |
11 | | -const rulesPath = resolve(__dirname, "..", "rules", `${config.assets_bundler}.js`); |
12 | | -const rules = require(rulesPath); |
| 3 | +const { basename, dirname, join, relative, resolve } = require("path") |
| 4 | +const { existsSync, readdirSync } = require("fs") |
| 5 | +const extname = require("path-complete-extname") |
| 6 | +const config = require("../config") |
| 7 | +const { isProduction } = require("../env") |
| 8 | +const pluginsPath = resolve( |
| 9 | + __dirname, |
| 10 | + "..", |
| 11 | + "plugins", |
| 12 | + `${config.assets_bundler}.js` |
| 13 | +) |
| 14 | +const { getPlugins } = require(pluginsPath) |
| 15 | +const rulesPath = resolve( |
| 16 | + __dirname, |
| 17 | + "..", |
| 18 | + "rules", |
| 19 | + `${config.assets_bundler}.js` |
| 20 | +) |
| 21 | +const rules = require(rulesPath) |
13 | 22 | // Don't use contentHash except for production for performance |
14 | 23 | // https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling |
15 | | -const hash = isProduction || config.useContentHash ? "-[contenthash]" : ""; |
| 24 | +const hash = isProduction || config.useContentHash ? "-[contenthash]" : "" |
16 | 25 | const getFilesInDirectory = (dir, includeNested) => { |
17 | | - if (!existsSync(dir)) { |
18 | | - return []; |
| 26 | + if (!existsSync(dir)) { |
| 27 | + return [] |
| 28 | + } |
| 29 | + return readdirSync(dir, { withFileTypes: true }).flatMap((dirent) => { |
| 30 | + const filePath = join(dir, dirent.name) |
| 31 | + if (dirent.isDirectory() && includeNested) { |
| 32 | + return getFilesInDirectory(filePath, includeNested) |
19 | 33 | } |
20 | | - return readdirSync(dir, { withFileTypes: true }).flatMap((dirent) => { |
21 | | - const filePath = join(dir, dirent.name); |
22 | | - if (dirent.isDirectory() && includeNested) { |
23 | | - return getFilesInDirectory(filePath, includeNested); |
24 | | - } |
25 | | - if (dirent.isFile()) { |
26 | | - return filePath; |
27 | | - } |
28 | | - return []; |
29 | | - }); |
30 | | -}; |
| 34 | + if (dirent.isFile()) { |
| 35 | + return filePath |
| 36 | + } |
| 37 | + return [] |
| 38 | + }) |
| 39 | +} |
31 | 40 | const getEntryObject = () => { |
32 | | - const entries = {}; |
33 | | - const rootPath = join(config.source_path, config.source_entry_path); |
34 | | - if (config.source_entry_path === "/" && config.nested_entries) { |
35 | | - throw new Error(`Invalid Shakapacker configuration detected!\n\n` + |
36 | | - `You have set source_entry_path to '/' with nested_entries enabled.\n` + |
37 | | - `This would create webpack entry points for EVERY file in your source directory,\n` + |
38 | | - `which would severely impact build performance.\n\n` + |
39 | | - `To fix this issue, either:\n` + |
40 | | - `1. Set 'nested_entries: false' in your shakapacker.yml\n` + |
41 | | - `2. Change 'source_entry_path' to a specific subdirectory (e.g., 'packs')\n` + |
42 | | - `3. Or use both options for better organization of your entry points`); |
| 41 | + const entries = {} |
| 42 | + const rootPath = join(config.source_path, config.source_entry_path) |
| 43 | + if (config.source_entry_path === "/" && config.nested_entries) { |
| 44 | + throw new Error( |
| 45 | + `Invalid Shakapacker configuration detected!\n\n` + |
| 46 | + `You have set source_entry_path to '/' with nested_entries enabled.\n` + |
| 47 | + `This would create webpack entry points for EVERY file in your source directory,\n` + |
| 48 | + `which would severely impact build performance.\n\n` + |
| 49 | + `To fix this issue, either:\n` + |
| 50 | + `1. Set 'nested_entries: false' in your shakapacker.yml\n` + |
| 51 | + `2. Change 'source_entry_path' to a specific subdirectory (e.g., 'packs')\n` + |
| 52 | + `3. Or use both options for better organization of your entry points` |
| 53 | + ) |
| 54 | + } |
| 55 | + getFilesInDirectory(rootPath, config.nested_entries).forEach((path) => { |
| 56 | + const namespace = relative(join(rootPath), dirname(path)) |
| 57 | + const name = join(namespace, basename(path, extname(path))) |
| 58 | + const assetPath = resolve(path) |
| 59 | + // Allows for multiple filetypes per entry (https://webpack.js.org/guides/entry-advanced/) |
| 60 | + // Transforms the config object value to an array with all values under the same name |
| 61 | + const previousPaths = entries[name] |
| 62 | + if (previousPaths) { |
| 63 | + const pathArray = Array.isArray(previousPaths) |
| 64 | + ? previousPaths |
| 65 | + : [previousPaths] |
| 66 | + pathArray.push(assetPath) |
| 67 | + entries[name] = pathArray |
| 68 | + } else { |
| 69 | + entries[name] = assetPath |
43 | 70 | } |
44 | | - getFilesInDirectory(rootPath, config.nested_entries).forEach((path) => { |
45 | | - const namespace = relative(join(rootPath), dirname(path)); |
46 | | - const name = join(namespace, basename(path, extname(path))); |
47 | | - const assetPath = resolve(path); |
48 | | - // Allows for multiple filetypes per entry (https://webpack.js.org/guides/entry-advanced/) |
49 | | - // Transforms the config object value to an array with all values under the same name |
50 | | - const previousPaths = entries[name]; |
51 | | - if (previousPaths) { |
52 | | - const pathArray = Array.isArray(previousPaths) |
53 | | - ? previousPaths |
54 | | - : [previousPaths]; |
55 | | - pathArray.push(assetPath); |
56 | | - entries[name] = pathArray; |
57 | | - } |
58 | | - else { |
59 | | - entries[name] = assetPath; |
60 | | - } |
61 | | - }); |
62 | | - return entries; |
63 | | -}; |
| 71 | + }) |
| 72 | + return entries |
| 73 | +} |
64 | 74 | const getModulePaths = () => { |
65 | | - const result = [resolve(config.source_path)]; |
66 | | - if (config.additional_paths) { |
67 | | - config.additional_paths.forEach((path) => result.push(resolve(path))); |
68 | | - } |
69 | | - result.push("node_modules"); |
70 | | - return result; |
71 | | -}; |
| 75 | + const result = [resolve(config.source_path)] |
| 76 | + if (config.additional_paths) { |
| 77 | + config.additional_paths.forEach((path) => result.push(resolve(path))) |
| 78 | + } |
| 79 | + result.push("node_modules") |
| 80 | + return result |
| 81 | +} |
72 | 82 | const baseConfig = { |
73 | | - mode: "production", |
74 | | - output: { |
75 | | - filename: `js/[name]${hash}.js`, |
76 | | - chunkFilename: `js/[name]${hash}.chunk.js`, |
77 | | - // https://webpack.js.org/configuration/output/#outputhotupdatechunkfilename |
78 | | - hotUpdateChunkFilename: "js/[id].[fullhash].hot-update.js", |
79 | | - path: config.outputPath, |
80 | | - publicPath: config.publicPath, |
81 | | - // This is required for SRI to work. |
82 | | - crossOriginLoading: config.integrity && config.integrity.enabled |
83 | | - ? config.integrity.cross_origin |
84 | | - : false |
85 | | - }, |
86 | | - entry: getEntryObject(), |
87 | | - resolve: { |
88 | | - extensions: [".js", ".jsx", ".mjs", ".ts", ".tsx", ".coffee"], |
89 | | - modules: getModulePaths() |
90 | | - }, |
91 | | - plugins: getPlugins(), |
92 | | - resolveLoader: { |
93 | | - modules: ["node_modules"] |
94 | | - }, |
95 | | - optimization: { |
96 | | - splitChunks: { chunks: "all" }, |
97 | | - runtimeChunk: "single" |
98 | | - }, |
99 | | - module: { |
100 | | - rules |
101 | | - } |
102 | | -}; |
103 | | -module.exports = baseConfig; |
| 83 | + mode: "production", |
| 84 | + output: { |
| 85 | + filename: `js/[name]${hash}.js`, |
| 86 | + chunkFilename: `js/[name]${hash}.chunk.js`, |
| 87 | + // https://webpack.js.org/configuration/output/#outputhotupdatechunkfilename |
| 88 | + hotUpdateChunkFilename: "js/[id].[fullhash].hot-update.js", |
| 89 | + path: config.outputPath, |
| 90 | + publicPath: config.publicPath, |
| 91 | + // This is required for SRI to work. |
| 92 | + crossOriginLoading: |
| 93 | + config.integrity && config.integrity.enabled |
| 94 | + ? config.integrity.cross_origin |
| 95 | + : false |
| 96 | + }, |
| 97 | + entry: getEntryObject(), |
| 98 | + resolve: { |
| 99 | + extensions: [".js", ".jsx", ".mjs", ".ts", ".tsx", ".coffee"], |
| 100 | + modules: getModulePaths() |
| 101 | + }, |
| 102 | + plugins: getPlugins(), |
| 103 | + resolveLoader: { |
| 104 | + modules: ["node_modules"] |
| 105 | + }, |
| 106 | + optimization: { |
| 107 | + splitChunks: { chunks: "all" }, |
| 108 | + runtimeChunk: "single" |
| 109 | + }, |
| 110 | + module: { |
| 111 | + rules |
| 112 | + } |
| 113 | +} |
| 114 | +module.exports = baseConfig |
0 commit comments