Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using nested entries #121

Merged
merged 7 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +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).

### 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).

- 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.
- [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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justin808 can we add something in the readme suggesting when you would want to use that and examples?

## [v6.3.0] - May 19, 2022

Expand Down Expand Up @@ -41,7 +46,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).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!


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.

Expand Down Expand Up @@ -132,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
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -168,6 +168,7 @@ default: &default
source_entry_path: packs
public_root_path: public
public_output_path: packs
nested_entries: false
# And more
```

Expand All @@ -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.

Expand Down
8 changes: 8 additions & 0 deletions lib/install/config/webpacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 30 additions & 3 deletions package/environments/__tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
// 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')
const config = require("../../config");

describe('Base config', () => {
beforeEach(() => jest.resetModules() && resetEnv())

afterAll(chdirCwd)

describe('config', () => {
Expand All @@ -21,11 +24,33 @@ describe('Base config', () => {
)
})

test('should return multi file entry points', () => {
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')
])
expect(baseConfig.entry['generated/something']).toEqual(undefined)
})

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");
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')
])
expect(baseConfig.entry['generated/something']).toEqual(
resolve('app', 'packs', 'entrypoints', 'generated', 'something.js')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is failing ATM

)
})

test('should return output', () => {
Expand All @@ -36,6 +61,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

Expand Down
9 changes: 8 additions & 1 deletion package/environments/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/test_app/app/packs/entrypoints/generated/something.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint no-console:0 */
console.log('entrypoints/nested/something')
1 change: 1 addition & 0 deletions test/test_app/config/webpacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
default: &default
source_path: app/packs
source_entry_path: entrypoints
nested_entries: false
public_root_path: public
public_output_path: packs
cache_path: tmp/webpacker
Expand Down
83 changes: 83 additions & 0 deletions test/test_app/config/webpacker_nested_entries.yml
Original file line number Diff line number Diff line change
@@ -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