Skip to content

Commit 8d9b643

Browse files
authored
Feature: Add support for multiple files per entry (#2476)
* feature: enable multiple file types per entry It is possible to provide different types of files when using an array of values for entry to achieve separate bundles for CSS and JavaScript (and other) files in applications that are not using import for styles in JavaScript (pre Single Page Applications or different reasons). For additional info: https://webpack.js.org/guides/entry-advanced/ This is particularly useful if you are migrating from a per page css/js organization as was common with rails <= 6 * chore: rework implementation to prevent breaking changes * fix: linter errors with spacing * chore: add tests for new functionality
1 parent 10b5af1 commit 8d9b643

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

package/environments/__tests__/base.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ describe('Environment', () => {
2929
)
3030
})
3131

32+
test('should return multi file entry points', () => {
33+
const config = environment.toWebpackConfig()
34+
expect(config.entry.multi_entry.sort()).toEqual(
35+
[
36+
resolve('app', 'javascript', 'packs', 'multi_entry.css'),
37+
resolve('app', 'javascript', 'packs', 'multi_entry.js')
38+
]
39+
)
40+
})
41+
3242
test('should return output', () => {
3343
const config = environment.toWebpackConfig()
3444
expect(config.output.filename).toEqual('js/[name]-[contenthash].js')

package/environments/base.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,18 @@ const getEntryObject = () => {
6565
paths.forEach((path) => {
6666
const namespace = relative(join(rootPath), dirname(path))
6767
const name = join(namespace, basename(path, extname(path)))
68-
result.set(name, resolve(path))
68+
let assetPaths = resolve(path)
69+
70+
// Allows for multiple filetypes per entry (https://webpack.js.org/guides/entry-advanced/)
71+
// Transforms the config object value to an array with all values under the same name
72+
let previousPaths = result.get(name)
73+
if (previousPaths) {
74+
previousPaths = Array.isArray(previousPaths) ? previousPaths : [previousPaths]
75+
previousPaths.push(assetPaths)
76+
assetPaths = previousPaths
77+
}
78+
79+
result.set(name, assetPaths)
6980
})
7081
return result
7182
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
* Dummy file #1 for Multi File Entry points: https://webpack.js.org/guides/entry-advanced/
3+
* This file must be named the same
4+
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
* Dummy file #2 for Multi File Entry points: https://webpack.js.org/guides/entry-advanced/
3+
* This file must be named the same
4+
*/

0 commit comments

Comments
 (0)