From c83240a02b8636712f3f8ae5ed4278fb349978fd Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Fri, 20 May 2022 10:04:14 -0700 Subject: [PATCH 1/7] 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 a384a684e..1e051bbe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ _Please add entries here for your pull requests that are not yet released._ - Removed the `yarn:install` Rake task, and no longer enhance `assets:precompile` with said task. These tasks were used to ensure required NPM packages were installed before asset precompilation. Going forward you will need to ensure these packages are already installed yourself. Should you wish to restore this behaviour you'll need to [reimplement the task](https://github.com/shakacode/shakapacker/blob/bee661422f2c902aa8ac9cf8fa1f7ccb8142c914/lib/tasks/yarn.rake) in your own application. +## 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 @@ -41,7 +44,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 From d73838371123682ec6baf2692b350bb3f40d7bfc Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Wed, 1 Jun 2022 16:19:41 -1000 Subject: [PATCH 2/7] fix tests --- package/environments/__tests__/base.js | 20 +++-- test/test_app/config/webpacker.yml | 2 +- .../config/webpacker_nested_entries.yml | 83 +++++++++++++++++++ 3 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 test/test_app/config/webpacker_nested_entries.yml diff --git a/package/environments/__tests__/base.js b/package/environments/__tests__/base.js index c14151542..7e4fc23e4 100644 --- a/package/environments/__tests__/base.js +++ b/package/environments/__tests__/base.js @@ -3,15 +3,16 @@ // environment.js expects to find config/webpacker.yml and resolved modules from // the root of a Rails project -const { chdirTestApp, chdirCwd } = require('../../utils/helpers') +const { chdirTestApp, chdirCwd, resetEnv } = require('../../utils/helpers') chdirTestApp() const { resolve } = require('path') -const rules = require('../../rules') const baseConfig = require('../base') describe('Base config', () => { + beforeEach(() => jest.resetModules() && resetEnv()) + afterAll(chdirCwd) describe('config', () => { @@ -21,14 +22,19 @@ describe('Base config', () => { ) }) - test('should return multi file entry points', () => { + 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(undefined) }) - test('should return only 2 entry points with config.nested_entries == false', () => { + test('should return only 3 entry points with config.nested_entries == true', () => { + process.env.WEBPACKER_CONFIG = 'config/webpacker_nested_entries.yml' + require('../../config.js') + const baseConfig = require('../base') + expect(baseConfig.entry.multi_entry.sort()).toEqual([ resolve('app', 'packs', 'entrypoints', 'multi_entry.css'), resolve('app', 'packs', 'entrypoints', 'multi_entry.js') @@ -38,10 +44,6 @@ describe('Base config', () => { ) }) - 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( @@ -50,6 +52,8 @@ describe('Base config', () => { }) test('should return default loader rules for each file in config/loaders', () => { + const rules = require('../../rules') + const defaultRules = Object.keys(rules) const configRules = baseConfig.module.rules diff --git a/test/test_app/config/webpacker.yml b/test/test_app/config/webpacker.yml index 5bec1cae8..60a6fc159 100644 --- a/test/test_app/config/webpacker.yml +++ b/test/test_app/config/webpacker.yml @@ -3,7 +3,7 @@ default: &default source_path: app/packs source_entry_path: entrypoints - nested_entries: true + nested_entries: false public_root_path: public public_output_path: packs cache_path: tmp/webpacker diff --git a/test/test_app/config/webpacker_nested_entries.yml b/test/test_app/config/webpacker_nested_entries.yml new file mode 100644 index 000000000..5bec1cae8 --- /dev/null +++ b/test/test_app/config/webpacker_nested_entries.yml @@ -0,0 +1,83 @@ +# Note: You must restart bin/webpacker-dev-server for changes to take effect + +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 + webpack_compile_output: false + webpack_loader: babel + + # Location for manifest.json, defaults to {public_output_path}/manifest.json if unset + # manifest_path: public/packs/manifest.json + + # Additional paths webpack should look up modules + # ['app/assets', 'engine/foo/app/assets'] + additional_paths: + - app/assets + - /etc/yarn + - some.config.js + - app/elm + + # Reload manifest.json on all requests so we reload latest compiled packs + cache_manifest: false + + static_assets_extensions: + - .jpg + - .jpeg + - .png + - .gif + - .tiff + - .ico + - .svg + + extensions: + - .mjs + - .js + +development: + <<: *default + compile: true + ensure_consistent_versioning: true + + # Reference: https://webpack.js.org/configuration/dev-server/ + dev_server: + https: false + host: localhost + port: 3035 + public: localhost:3035 + hmr: false + overlay: true + disable_host_check: true + use_local_ip: false + pretty: false + +test: + <<: *default + compile: true + + # Compile test packs to a separate directory + public_output_path: packs-test + +production: + <<: *default + + # Production depends on precompilation of packs prior to booting for performance. + compile: false + + # Cache manifest.json for performance + cache_manifest: true + +staging: + <<: *default + + # Production depends on precompilation of packs prior to booting for performance. + compile: false + + # Cache manifest.json for performance + cache_manifest: true + + # Compile staging packs to a separate directory + public_output_path: packs-staging From 06b6848ea1afa2f4fac15127280e38c366fde668 Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Wed, 1 Jun 2022 16:29:54 -1000 Subject: [PATCH 3/7] Update docs --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 24aad6bed..d14991be7 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ At it's core, Shakapacker's essential functionality is to: You will need your file system to correspond to the setup of your `webpacker.yml` file. -Suppose you have the following files: +Suppose you have the following configuration: `webacker.yml` ```yml @@ -168,6 +168,7 @@ default: &default source_entry_path: packs public_root_path: public public_output_path: packs + nested_entries: false # And more ``` @@ -189,6 +190,8 @@ public/packs # webpack output Webpack intelligently includes only necessary files. In this example, the file `packs/application.js` would reference `../src/my_component.js` +`nested_entries` allows you to have webpack entry points nested in subdirectories. This defaults to false so you don't accidentally create entry points for an entire tree of files. In other words, with `nested_entries: false`, you can have your entire `source_path` used for your source (using the `source_entry_path: /`) and you place files at the top level that you want as entry points. `nested_entries: true` allows you to have entries that are in subdirectories. This is useful if you have entries that are generated, so you can have a `generated` subdirectory and easily separate generated files from the rest of your codebase. + ### View Helpers The Shakapacker view helpers generate the script and link tags to get the webpack output onto your views. From 74bba026516faab73b58e933214a6aac821ef366 Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Thu, 2 Jun 2022 15:01:42 -1000 Subject: [PATCH 4/7] Update tests --- package/environments/__tests__/base.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/package/environments/__tests__/base.js b/package/environments/__tests__/base.js index 7e4fc23e4..0ab1f9d1f 100644 --- a/package/environments/__tests__/base.js +++ b/package/environments/__tests__/base.js @@ -30,11 +30,16 @@ describe('Base config', () => { expect(baseConfig.entry['generated/something']).toEqual(undefined) }) - test('should return only 3 entry points with config.nested_entries == true', () => { + test('should returns top level and nested entry points with config.nested_entries == true', () => { process.env.WEBPACKER_CONFIG = 'config/webpacker_nested_entries.yml' - require('../../config.js') + const config = require('../../config.js') const baseConfig = require('../base') + expect(config.nested_entries).toEqual(true) + + expect(baseConfig.entry.application).toEqual( + resolve('app', 'packs', 'entrypoints', 'application.js') + ) expect(baseConfig.entry.multi_entry.sort()).toEqual([ resolve('app', 'packs', 'entrypoints', 'multi_entry.css'), resolve('app', 'packs', 'entrypoints', 'multi_entry.js') From 9b4447b0d5ae65240c7c21eff58d51d0f1ebfa21 Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Thu, 2 Jun 2022 15:12:39 -1000 Subject: [PATCH 5/7] tests --- package/environments/__tests__/base.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/package/environments/__tests__/base.js b/package/environments/__tests__/base.js index 0ab1f9d1f..0ee2bff80 100644 --- a/package/environments/__tests__/base.js +++ b/package/environments/__tests__/base.js @@ -8,9 +8,11 @@ const { chdirTestApp, chdirCwd, resetEnv } = require('../../utils/helpers') chdirTestApp() const { resolve } = require('path') -const baseConfig = require('../base') describe('Base config', () => { + const baseConfig = require('../base') + const config = require("../../config"); + beforeEach(() => jest.resetModules() && resetEnv()) afterAll(chdirCwd) @@ -23,6 +25,8 @@ describe('Base config', () => { }) test('should return only 2 entry points with config.nested_entries == false', () => { + expect(config.nested_entries).toEqual(false) + expect(baseConfig.entry.multi_entry.sort()).toEqual([ resolve('app', 'packs', 'entrypoints', 'multi_entry.css'), resolve('app', 'packs', 'entrypoints', 'multi_entry.js') From 0e5c5f3d1549b29050bcc2aca03c536f8389f204 Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Thu, 2 Jun 2022 15:32:41 -1000 Subject: [PATCH 6/7] add missing file --- package/environments/__tests__/base.js | 8 ++++---- .../test_app/app/packs/entrypoints/generated/something.js | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 test/test_app/app/packs/entrypoints/generated/something.js diff --git a/package/environments/__tests__/base.js b/package/environments/__tests__/base.js index 0ee2bff80..d24a651c1 100644 --- a/package/environments/__tests__/base.js +++ b/package/environments/__tests__/base.js @@ -9,10 +9,10 @@ chdirTestApp() const { resolve } = require('path') -describe('Base config', () => { - const baseConfig = require('../base') - const config = require("../../config"); +const baseConfig = require('../base') +const config = require("../../config"); +describe('Base config', () => { beforeEach(() => jest.resetModules() && resetEnv()) afterAll(chdirCwd) @@ -36,7 +36,7 @@ describe('Base config', () => { test('should returns top level and nested entry points with config.nested_entries == true', () => { process.env.WEBPACKER_CONFIG = 'config/webpacker_nested_entries.yml' - const config = require('../../config.js') + const config = require("../../config"); const baseConfig = require('../base') expect(config.nested_entries).toEqual(true) diff --git a/test/test_app/app/packs/entrypoints/generated/something.js b/test/test_app/app/packs/entrypoints/generated/something.js new file mode 100644 index 000000000..90c6f94e3 --- /dev/null +++ b/test/test_app/app/packs/entrypoints/generated/something.js @@ -0,0 +1,2 @@ +/* eslint no-console:0 */ +console.log('entrypoints/nested/something') From 1b0c521d494a78b7f162a5986a1ef85773a1047c Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Thu, 2 Jun 2022 15:48:22 -1000 Subject: [PATCH 7/7] Update changelog.md --- CHANGELOG.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e051bbe0..2903e452c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,17 +6,19 @@ ## [Unreleased] Changes since last non-beta release. -*Please add entries here for your pull requests that are not yet released.* -### Improved -- Allow v10 of `compression-webpack-plugin` as a peer dependency. [PR 117](https://github.com/shakacode/shakapacker/pull/117) by [aried3r](https://github.com/aried3r). _Please add entries here for your pull requests that are not yet released._ -### Changed +## [v6.4.0] - June 2, 2022 +### Fixed +- Fixed [Issue 123: Rails 7.0.3 - Webpacker configuration file not found when running rails webpacker:install (shakapacker v6.3)](https://github.com/shakacode/shakapacker/issues/123) in [PR 136: Don't enhance precompile if no config #136](https://github.com/shakacode/shakapacker/pull/136) by [justin808](https://github.com/justin808). -- Removed the `yarn:install` Rake task, and no longer enhance `assets:precompile` with said task. These tasks were used to ensure required NPM packages were installed before asset precompilation. Going forward you will need to ensure these packages are already installed yourself. Should you wish to restore this behaviour you'll need to [reimplement the task](https://github.com/shakacode/shakapacker/blob/bee661422f2c902aa8ac9cf8fa1f7ccb8142c914/lib/tasks/yarn.rake) in your own application. +### 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. [PR 121](https://github.com/shakacode/shakapacker/pull/121) by [justin808](https://github.com/justin808). + +### Improved +- Allow v10 of `compression-webpack-plugin` as a peer dependency. [PR 117](https://github.com/shakacode/shakapacker/pull/117) by [aried3r](https://github.com/aried3r). -## 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. +- [Remove assets:precompile task enhancement #131](https://github.com/shakacode/shakapacker/pull/131) by [James Herdman](https://github.com/jherdman): Removed the `yarn:install` Rake task, and no longer enhance `assets:precompile` with said task. These tasks were used to ensure required NPM packages were installed before asset precompilation. Going forward you will need to ensure these packages are already installed yourself. Should you wish to restore this behaviour you'll need to [reimplement the task](https://github.com/shakacode/shakapacker/blob/bee661422f2c902aa8ac9cf8fa1f7ccb8142c914/lib/tasks/yarn.rake) in your own application. ## [v6.3.0] - May 19, 2022 @@ -135,9 +137,9 @@ Note: [Rubygem is 6.3.0.pre.rc.1](https://rubygems.org/gems/shakapacker/versions ## v5.4.3 and prior changes from rails/webpacker See [CHANGELOG.md in rails/webpacker (up to v5.4.3)](https://github.com/rails/webpacker/blob/master/CHANGELOG.md) -[Unreleased]: https://github.com/shakacode/shakapacker/compare/v6.3.0...master -[v6.3.0]: https://github.com/shakacode/shakapacker/compare/v6.3.0-rc.1...v6.3.0 -[v6.3.0-rc.1]: https://github.com/shakacode/shakapacker/compare/v6.2.1...v6.3.0-rc.1 +[Unreleased]: https://github.com/shakacode/shakapacker/compare/v6.4.0...master +[v6.4.0]: https://github.com/shakacode/shakapacker/compare/v6.3.0...v6.4.0 +[v6.3.0]: https://github.com/shakacode/shakapacker/compare/v6.2.1...v6.3.0 [v6.2.1]: https://github.com/shakacode/shakapacker/compare/v6.2.0...v6.2.1 [v6.2.0]: https://github.com/shakacode/shakapacker/compare/v6.1.1...v6.2.0 [v6.1.1]: https://github.com/shakacode/shakapacker/compare/v6.1.0...v6.1.1