Skip to content
Open
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Minipack.configuration do |c|
# Register a path to a manifest file here. Right now you have to specify an absolute path.
c.manifest = Rails.root.join('public', 'assets', 'manifest.json')

# Define the way packs are being lookep up in manifests
# By default, it will look in entrypoints -> pack_name -> pack_type like the following snippet
# c.lookup_pack = -> (data, pack_name, pack_type) {
# data['entrypoints']&.dig(pack_name, pack_type)
# }

# If you are not extracting CSS in your webpack config you should set this flag to false
# c.extract_css = !Rails.env.development?

Expand All @@ -74,7 +80,7 @@ Minipack.configuration do |c|
# ]
#
# You can override it.
# c.build_cache_key = ['package.json', 'package-lock.json', 'config/webpack.config.js', 'src/**/*']
# c.build_cache_key = ['package.json', 'package-lock.json', 'config/webpack.config.js', 'src/**/*']
#
# Or you can add files in addition to the defaults:
# c.build_cache_key << 'src/**/*'
Expand Down Expand Up @@ -209,6 +215,16 @@ module.exports = {
}
```

Also you need to change the way the pack is looked up in the manifest, because `webpack-assets-manifest` wraps the types inside an `assets` key

``` ruby
Minipack.configuration do |config|
config.lookup_pack = -> (data, name, type) {
data.dig("entrypoints", name, "assets", type)
}
end
```

## Advanced Configuration

### Hot Module Replacement in development
Expand Down
4 changes: 4 additions & 0 deletions lib/minipack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def config_attr(prop, default: nil)

config_attr :manifest

config_attr :lookup_pack, default: -> (data, pack_name, pack_type) {
data['entrypoints']&.dig(pack_name, pack_type)
}

# The lazy compilation is cached until a file is change under the tracked paths.
config_attr :build_cache_key, default: BUILD_CACHE_KEY_DEFAULT.dup

Expand Down
15 changes: 8 additions & 7 deletions lib/minipack/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ def initialize(path, cache: false)
def lookup_pack_with_chunks!(name, type: nil)
manifest_pack_type = manifest_type(name, type)
manifest_pack_name = manifest_name(name, manifest_pack_type)
paths = data['entrypoints']&.dig(manifest_pack_name, manifest_pack_type) || handle_missing_entry(name)

entries = data['entrypoints']&.dig(manifest_pack_name, manifest_pack_type).map do |source|
entry_from_source(source) || handle_missing_entry(name)
end

ChunkGroup.new(entries)
paths = Minipack.configuration.lookup_pack.call(data, manifest_pack_name, manifest_pack_type)
handle_missing_entry(name) unless paths

ChunkGroup.new(
paths.map do |source|
entry_from_source(source) || handle_missing_entry(name)
end
)
end

def lookup!(name)
Expand Down
28 changes: 28 additions & 0 deletions spec/minipack/manifest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@

it { expect { subject }.to raise_error Minipack::Manifest::MissingEntryError }
end

context "when lookup_pack is defined" do
let(:configuration) { Minipack::Configuration.new }
let(:path) { File.expand_path('../support/files/assets-manifest.json', __dir__) }
let(:name) { 'application' }
let(:type) { 'js' }

before {
Minipack.configuration = configuration
Minipack.configuration do |config|
config.lookup_pack = -> (data, name, type) {
data.dig("entrypoints", name, "assets", type)
}
end
}
after {
Minipack.configuration = nil
}

it do
expected = Minipack::Manifest::ChunkGroup.new(
%w(/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js
/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js
/packs/application-k344a6d59eef8632c9d1.js),
)
is_expected.to eq expected
end
end
end

describe '#lookup!' do
Expand Down
39 changes: 39 additions & 0 deletions spec/support/files/assets-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"test.js": "/assets/web/pack/test-9a55da116417a39a9d1b.js",
"dummy_thumbnail.png": "/assets/web/pack/dummy_thumbnail-757606db2b4802bb4147a968315df2df.png",
"icon/avatar.png": "/assets/web/pack/icon/avatar-50b0773f02d0149e39468afa4b6567af.png",
"item_group_editor.css": "/packs/item_group_editor-5d7c7164b8a0a9d675fad9ab410eaa8d.css",
"item_group_editor.js": "/packs/item_group_editor-857e5bfa272e71b6384046f68ba29d44.js",
"item_group_editor.js.map": "/packs/item_group_editor.js.map",
"union-ok.png": "/packs/union-ok-857e5bfa272e71b6384046f68ba29d44.png",
"union-ok@2x.png": "/packs/union-ok@2x-5d7c7164b8a0a9d675fad9ab410eaa8d.png",
"vendors~application~bootstrap.js": "/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js",
"vendors~application.js": "/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js",
"application.js": "/packs/application-k344a6d59eef8632c9d1.js",
"application.css": "/packs/application-k344a6d59eef8632c9d1.chunk.css",
"hello_stimulus.css": "/packs/hello_stimulus-k344a6d59eef8632c9d1.chunk.css",
"1.css": "/packs/1-c20632e7baf2c81200d3.chunk.css",
"entrypoints": {
"application": {
"assets": {
"js": [
"/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js",
"/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js",
"/packs/application-k344a6d59eef8632c9d1.js"
],
"css": [
"/packs/1-c20632e7baf2c81200d3.chunk.css",
"/packs/application-k344a6d59eef8632c9d1.chunk.css"
]
}
},
"hello_stimulus": {
"assets": {
"css": [
"/packs/1-c20632e7baf2c81200d3.chunk.css",
"/packs/hello_stimulus-k344a6d59eef8632c9d1.chunk.css"
]
}
}
}
}