Skip to content

Commit

Permalink
don't compress or minify the assets bundled by Webpack (bundle.js and…
Browse files Browse the repository at this point in the history
… bundle.css)
  • Loading branch information
did committed Nov 15, 2020
1 parent 00f7510 commit 970ac13
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Locomotive::Wagon

class PushThemeAssetsCommand < PushBaseCommand

WEBPACK_BUNDLED_ASSETS = ['stylesheets/bundle.css', 'javascripts/bundle.js'].freeze

def entities
repositories.theme_asset.all.map do |entity|
next if skip?(entity)
Expand Down Expand Up @@ -87,10 +89,14 @@ def register_url(resource)

def compress_and_minify(entity)
begin
if WEBPACK_BUNDLED_ASSETS.include?(entity.short_relative_url)
raise 'already compressed and minified by Webpack'
end

sprockets_env[entity.short_relative_url].to_s
rescue Exception => e
instrument :warning, message: "Unable to compress and minify it, error: #{e.message}"
# use the original file instead
# use the original file instead"
File.read(File.join(path, entity.source))
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@

require 'spec_helper'

require 'locomotive/steam'
require 'locomotive/wagon/commands/push_sub_commands/push_base_command'
require 'locomotive/wagon/commands/push_sub_commands/push_theme_assets_command'

describe Locomotive::Wagon::PushThemeAssetsCommand do

let(:command) { described_class.new(nil, nil, nil, nil) }
let(:command) { described_class.new(nil, nil, nil, nil) }

describe '#compress_and_minify' do

before { allow(command).to receive(:path).and_return(default_site_path) }

let(:short_relative_url) { 'stylesheets/application.css' }
let(:source) { File.join('public', short_relative_url) }
let(:entity) { Locomotive::Steam::ThemeAsset.new(source: source, short_relative_url: short_relative_url) }

subject { command.send(:compress_and_minify, entity) }

it 'calls Sprockets to compress and minify the asset' do
expect(command).to receive(:sprockets_env).and_return({ 'stylesheets/application.css' => 'body{}' })
is_expected.to eq 'body{}'
end

context 'processing bundle.css (generated by Webpack)' do
let(:short_relative_url) { 'stylesheets/bundle.css' }
it "doesn't compress or minify it" do
expect(File).to receive(:read).with(File.join(default_site_path, 'public/stylesheets/bundle.css')).and_return('body{}')
expect(command).not_to receive(:sprockets_env)
is_expected.to eq 'body{}'
end
end

context 'processing bundle.js (generated by Webpack)' do
let(:short_relative_url) { 'javascripts/bundle.js' }
it "doesn't compress or minify it" do
expect(command).not_to receive(:sprockets_env)
expect(File).to receive(:read).with(File.join(default_site_path, 'public/javascripts/bundle.js')).and_return('42')
is_expected.to eq '42'
end
end
end

describe '#replace_assets' do

Expand Down

0 comments on commit 970ac13

Please sign in to comment.