Skip to content

Commit

Permalink
Add the ability to define multiple source entry paths
Browse files Browse the repository at this point in the history
This should add back the ability to namespace packs which was removed in rails#3156 at the cost of needing to specify each of the nested directories in `source_entry_path`. Defining each namespace will make sure directories like `app/javascript/src`, `app/javascript/stylesheets`, etc don't get entries assigned.

* Move the entry parsing into `parseEntryPath` in `package/utils/helpers.js`
* Change `getEntryObject` to use `parseEntryPath` and to check whether `source_entry_path` is an array
* Add a nested pack into the test app
  • Loading branch information
brandoncordell committed Oct 1, 2021
1 parent a0594ff commit eb75a29
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
36 changes: 11 additions & 25 deletions package/environments/base.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */

const { basename, dirname, join, relative, resolve } = require('path')
const extname = require('path-complete-extname')
const { resolve } = require('path')
const PnpWebpackPlugin = require('pnp-webpack-plugin')
const { sync: globSync } = require('glob')
const WebpackAssetsManifest = require('webpack-assets-manifest')
const webpack = require('webpack')
const rules = require('../rules')
const { isProduction } = require('../env')
const config = require('../config')
const { moduleExists } = require('../utils/helpers')
const { moduleExists, parseEntryPath } = require('../utils/helpers')

const getEntryObject = () => {
const entries = {}
const rootPath = join(config.source_path, config.source_entry_path)

globSync(`${rootPath}/*.*`).forEach((path) => {
const namespace = relative(join(rootPath), dirname(path))
const name = join(namespace, basename(path, extname(path)))
let assetPaths = resolve(path)

// Allows for multiple filetypes per entry (https://webpack.js.org/guides/entry-advanced/)
// Transforms the config object value to an array with all values under the same name
let previousPaths = entries[name]
if (previousPaths) {
previousPaths = Array.isArray(previousPaths)
? previousPaths
: [previousPaths]
previousPaths.push(assetPaths)
assetPaths = previousPaths
}

entries[name] = assetPaths
})
let entries = {}

if (Array.isArray(config.source_entry_path)) {
config.source_entry_path.forEach((entryPath) => {
Object.assign(entries, parseEntryPath(config.source_path, entryPath))
})
} else {
entries = parseEntryPath(config.source_path, config.source_entry_path)
}

return entries
}
Expand Down
37 changes: 36 additions & 1 deletion package/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const { basename, dirname, join, relative, resolve } = require('path')
const { sync: globSync } = require('glob')
const extname = require('path-complete-extname')

const isArray = (value) => Array.isArray(value)
const isBoolean = (str) => /^true/.test(str) || /^false/.test(str)
const chdirTestApp = () => {
Expand Down Expand Up @@ -39,6 +43,36 @@ const canProcess = (rule, fn) => {
return null
}

const parseEntryPath = (sourcePath, entryPath) => {
const entries = {}
const rootPath = join(sourcePath, entryPath)

globSync(`${rootPath}/*.*`).forEach((path) => {
const namespace = relative(join(rootPath), dirname(path))
const name = join(namespace, basename(path, extname(path)))
let assetPaths = resolve(path)

// Allows for multiple filetypes per entry (https://webpack.js.org/guides/entry-advanced/)
// Transforms the config object value to an array with all values under the same name
let previousPaths = entries[name]
if (previousPaths) {
previousPaths = Array.isArray(previousPaths)
? previousPaths
: [previousPaths]
previousPaths.push(assetPaths)
assetPaths = previousPaths
}

if (entryPath === '/') {
entries[name] = assetPaths
} else {
entries[`${entryPath.substring(1)}/${name}`] = assetPaths
}
})

return entries
}

module.exports = {
chdirTestApp,
chdirCwd,
Expand All @@ -47,5 +81,6 @@ module.exports = {
ensureTrailingSlash,
canProcess,
moduleExists,
resetEnv
resetEnv,
parseEntryPath
}
3 changes: 3 additions & 0 deletions test/test_app/app/javascript/nested/pack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
* Dummy file #3 for testing namespaced packs
*/
4 changes: 3 additions & 1 deletion test/test_app/config/webpacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

default: &default
source_path: app/javascript
source_entry_path: /
source_entry_path:
- /
- /nested
public_root_path: public
public_output_path: packs
cache_path: tmp/webpacker
Expand Down

0 comments on commit eb75a29

Please sign in to comment.