From df707eb8918466e9b2b8960d473741784284e5da Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Fri, 20 May 2022 10:04:14 -0700 Subject: [PATCH] Allow using nested entries Nested entries were removed in v6.0. This PR allows using them with a configuration switch. By default they are turned off. --- CHANGELOG.md | 5 ++++- lib/install/config/webpacker.yml | 8 ++++++++ package/environments/__tests__/base.js | 14 ++++++++++++++ package/environments/base.js | 9 ++++++++- test/test_app/config/webpacker.yml | 1 + 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed1586f12..9f73d35a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ Changes since last non-beta release. *Please add entries here for your pull requests that are not yet released.* +## Added +- Configuration boolean option `nested_entries` to use nested entries. This was the default prior to v6.0. Because entries maybe generated, it's useful to allow a `generated` subdirectory. + ## [v6.3.0] - May 19, 2022 ### Improved @@ -34,7 +37,7 @@ Note: [Rubygem is 6.3.0.pre.rc.1](https://rubygems.org/gems/shakapacker/versions ### Improved - Use last modified timestamps rather than file digest to determine compiler freshness. [PR 97](https://github.com/shakacode/shakapacker/pull/97) by [tomdracz](https://github.com/tomdracz). - Rather than calculating SHA digest of all the files in the paths watched by the compiler, we are now comparing the modified time of the `manifest.json` file versues the latest modified timestamp of files and directories in watched paths. Unlike calculating digest, which only looked at the files, the new calculation also considers directory timestamps, including the parent ones (i.e. `config.source_path` folder timestamp will be checked together will timestamps of all files and directories inside of it). + Rather than calculating SHA digest of all the files in the paths watched by the compiler, we are now comparing the modified time of the `manifest.json` file versus the latest modified timestamp of files and directories in watched paths. Unlike calculating digest, which only looked at the files, the new calculation also considers directory timestamps, including the parent ones (i.e. `config.source_path` folder timestamp will be checked together will timestamps of all files and directories inside of it). This change should result in improved compiler checks performance but might be breaking for certain setups and edge cases. If you encounter any issues, please report them at https://github.com/shakacode/shakapacker/issues. diff --git a/lib/install/config/webpacker.yml b/lib/install/config/webpacker.yml index b4feeb0b9..8e10b8422 100644 --- a/lib/install/config/webpacker.yml +++ b/lib/install/config/webpacker.yml @@ -2,7 +2,15 @@ default: &default source_path: app/javascript + + # You can have a subdirectory of the source_path, like 'packs' (recommended). + # Alternatively, you can use '/' to use the whole source_path directory. source_entry_path: / + + # If nested_entries is true, then we'll pick up subdirectories within the source_entry_path. + # You cannot set this option to true if you set source_entry_path to '/' + nested_entries: false + public_root_path: public public_output_path: packs cache_path: tmp/webpacker diff --git a/package/environments/__tests__/base.js b/package/environments/__tests__/base.js index 79971596b..c14151542 100644 --- a/package/environments/__tests__/base.js +++ b/package/environments/__tests__/base.js @@ -28,6 +28,20 @@ describe('Base config', () => { ]) }) + test('should return only 2 entry points with config.nested_entries == false', () => { + expect(baseConfig.entry.multi_entry.sort()).toEqual([ + resolve('app', 'packs', 'entrypoints', 'multi_entry.css'), + resolve('app', 'packs', 'entrypoints', 'multi_entry.js') + ]) + expect(baseConfig.entry['generated/something']).toEqual( + resolve('app', 'packs', 'entrypoints', 'generated', 'something.js') + ) + }) + + test('should return 3 entry points with config.nested_entries == true', () => { + expect(baseConfig.entry.multi_entry.length).toEqual(2) + }) + test('should return output', () => { expect(baseConfig.output.filename).toEqual('js/[name].js') expect(baseConfig.output.chunkFilename).toEqual( diff --git a/package/environments/base.js b/package/environments/base.js index 4325b199c..68089941d 100644 --- a/package/environments/base.js +++ b/package/environments/base.js @@ -14,8 +14,15 @@ const { moduleExists } = require('../utils/helpers') const getEntryObject = () => { const entries = {} const rootPath = join(config.source_path, config.source_entry_path) + if (config.source_entry_path === '/' && config.nested_entries) { + throw new Error( + "Your webpacker config specified using a source_entry_path of '/' with 'nested_entries' == " + + "'true'. Doing this would result in packs for every one of your source files" + ) + } + const nesting = config.nested_entries ? '**/' : '' - globSync(`${rootPath}/*.*`).forEach((path) => { + globSync(`${rootPath}/${nesting}*.*`).forEach((path) => { const namespace = relative(join(rootPath), dirname(path)) const name = join(namespace, basename(path, extname(path))) let assetPaths = resolve(path) diff --git a/test/test_app/config/webpacker.yml b/test/test_app/config/webpacker.yml index 53fe81eef..5bec1cae8 100644 --- a/test/test_app/config/webpacker.yml +++ b/test/test_app/config/webpacker.yml @@ -3,6 +3,7 @@ default: &default source_path: app/packs source_entry_path: entrypoints + nested_entries: true public_root_path: public public_output_path: packs cache_path: tmp/webpacker