lux_assets 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 326121204a7766f50347a2c3845f5b98139654e10832a7419dafa302692c3517
4
- data.tar.gz: f6a7777517af7ae4f45402b229de989d092cd7b473add2d85cc1449662b76234
3
+ metadata.gz: 881393b401a0889dc9f796b8188981bd46a32fdeecd24331b1968d7b800e73d4
4
+ data.tar.gz: af21cd349f444ecac98faf88ed2bba53a9a20bd80c3292093ee7828d07dd84b0
5
5
  SHA512:
6
- metadata.gz: 4220d292427da679d9072f46794b5dba50b4df8f8ff682e684e19de27e8992524472fb6f0abefcbb30f7a44f9a1a889f5053493d5e0fe067d058941e489fa18f
7
- data.tar.gz: b1e8fdfe12465a654325fd2dc31615ab11188543a52c02963bdd0dc562753b1a6bc4a4d2025378593b9a4dcc0e2c24b0de765fb35eecfbb81379d395382bd610
6
+ metadata.gz: c0215e85dc0f841a4598ab95ab76f159a39c2a34a0fdbca5e97891b9bec3c9c245d7dbd40e507f5019ae16d2a6f84e4b38dbec91a3d3bbf3fbc80fee81531e62
7
+ data.tar.gz: 5895c5a63c3a2b6f9ab556c3d1345983a7bfb05aa4dfbe0b98d67d64d67c3729af3b481a26b5402bd122f93b7c59c8b84c06874f99a12ec55bea394cd94afd1b
data/README.md CHANGED
@@ -10,6 +10,19 @@ Lightweight web assets packer that loves simplicity.
10
10
  * autoprefixer, minify, gzip the output
11
11
  * add rake tasks
12
12
 
13
+ Assets pipeline
14
+
15
+ * get list of files or data
16
+ * compile every file individually and join the result to single file
17
+ * css specific
18
+ * scan css source and put sh1 data stamp to files found in ./public dir
19
+ * autoprefixer
20
+ * js specific
21
+ * babel
22
+ * minify
23
+ * sha1 prefix, gzip, add to manifest
24
+
25
+
13
26
  ## Instalation
14
27
 
15
28
  Add `lux_assets` gem to `Gemfile` and then `bundle install`
data/bin/lux_assets CHANGED
@@ -4,11 +4,25 @@ require 'thor'
4
4
  require 'colorize'
5
5
  require 'awesome_print'
6
6
 
7
- require_relative '../lib/lux_assets'
7
+ ###
8
+
9
+ if ARGV[0]
10
+ if File.exist?('./config/application.rb')
11
+ require './config/application.rb'
12
+ end
13
+
14
+ if !defined?(LuxAssets) && File.exist?('./config/environment.rb')
15
+ require './config/environment.rb'
16
+ end
17
+
18
+ if !defined?(LuxAssets)
19
+ require_relative '../lib/lux_assets'
20
+ end
21
+ end
8
22
 
9
23
  ###
10
24
 
11
- module Cli
25
+ module ::Cli
12
26
  extend self
13
27
 
14
28
  def run what
@@ -1,7 +1,6 @@
1
1
  # Asset group, single asset that produces target css or js
2
2
 
3
3
  class LuxAssets::Asset
4
- PUBLIC_ASSETS = './public/assets'
5
4
 
6
5
  def initialize ext, name
7
6
  @ext = ext == :js ? :js : :css
@@ -44,12 +43,31 @@ class LuxAssets::Asset
44
43
 
45
44
  private
46
45
 
46
+ # add sha1 tag to referenced files in css
47
+ def tag_public_assets
48
+ data = @asset_path.read
49
+ data = data.gsub(/url\(([^\)]+)\)/) do
50
+ if $1.include?('data:') || $1.include?('#') || $1.include?('?')
51
+ 'url(%s)' % $1
52
+ else
53
+ path = $1.gsub(/^['"]|['"]$/, '')
54
+ path = path[0,1] == '/' ? Pathname.new('./public%s' % path) : Pathname.new('./public/assets').join(path)
55
+
56
+ LuxAssets.die 'Resource "%s" referenced in "%s/%s" but not found' % [path, @ext, @name] unless path.exist?
57
+
58
+ 'url("%s?%s")' % [path.to_s.sub('./public', ''), Digest::SHA1.hexdigest(path.read)[0, 6]]
59
+ end
60
+ end
61
+
62
+ @asset_path.write data
63
+ end
64
+
47
65
  def save_data data
48
66
  @asset_file = '/assets/%s' % (@target.sub('/', '-') + '-' + Digest::SHA1.hexdigest(data) + '.' + @ext.to_s)
49
- @asset_path = "./public#{@asset_file}"
67
+ @asset_path = Pathname.new "./public#{@asset_file}"
50
68
 
51
69
  if LuxAssets::Manifest.add(@target, @asset_file)
52
- File.write(@asset_path, data)
70
+ @asset_path.write data
53
71
  yield
54
72
  end
55
73
  end
@@ -63,6 +81,8 @@ class LuxAssets::Asset
63
81
 
64
82
  def compile_css
65
83
  save_data @data.join($/) do
84
+ tag_public_assets
85
+
66
86
  #autoprefixer
67
87
  LuxAssets.run './node_modules/.bin/autoprefixer-cli %s' % @asset_path
68
88
  end
@@ -1,24 +1,4 @@
1
- # LuxAssets.configure do
2
- # asset :admin do
3
- # js do
4
- # add 'js/admin/js_vendor/*'
5
- # add 'js/admin/js/*'
6
- # add 'js/shared/*'
7
- # add 'js/admin/index.coffee'
8
- # end
9
- # css do
10
- # add 'css/admin/index.scss'
11
- # end
12
- # end
13
- # end
14
- # ...
15
-
16
- # LuxAssets.css('admin').files
17
- # LuxAssets.css(:admin).compile
18
-
19
- # LuxAssets.css(:admin).compile_all do |name, path|
20
- # puts "Compile #{name} -> #{path}"
21
- # end
1
+ # Main assets module
22
2
 
23
3
  module LuxAssets
24
4
  extend self
@@ -39,7 +19,7 @@ module LuxAssets
39
19
  end
40
20
 
41
21
  def die text
42
- puts text
22
+ puts text.try(:red)
43
23
  exit
44
24
  end
45
25
 
@@ -108,10 +88,12 @@ module LuxAssets
108
88
  to_h[ext][name.to_s]
109
89
  end
110
90
 
91
+ # compile single asset
111
92
  def compile path
112
93
  LuxAssets::Element.new(path).compile
113
94
  end
114
95
 
96
+ # compile all assets
115
97
  def compile_all
116
98
  # generate master file for every resource
117
99
  for ext in [:js, :css]
@@ -127,11 +109,12 @@ module LuxAssets
127
109
  LuxAssets.run 'gzip -k %s' % file unless File.exist?('%s.gz' % file)
128
110
  end
129
111
 
130
- # touch all files reset timestamp
112
+ # touch all files and reset the timestamp
131
113
  Dir['./public/assets/*']
132
114
  .each { |file| system 'touch -t 201001010101 %s' % file }
133
115
  end
134
116
 
117
+ # get all files as a hash
135
118
  def to_h
136
119
  unless @assets_loaded
137
120
  die 'Assets file not found in %s' % CONFIG_PATH unless CONFIG_PATH.exist?
@@ -149,13 +132,21 @@ module LuxAssets
149
132
  puts ext.to_s.upcase.green
150
133
  value_hash.each do |key, files|
151
134
  puts ' %s' % key.green
135
+
136
+ total = 0
137
+
152
138
  files.each_with_index do |file, i|
153
139
  if File.exist?(file)
154
- puts ' %s kb - %s' % [(File.size(file)/1024.to_f).round(1).to_s.rjust(6), file]
140
+ size = File.size(file)
141
+ total += size
142
+ puts ' %s kB - %s' % [(size/1024.to_f).round(1).to_s.rjust(6), file]
155
143
  else
156
144
  puts ' %s' % file
157
145
  end
158
146
  end
147
+
148
+ total = '%s kB in total' % (total/1024.to_f).round(1).to_s
149
+ puts total.rjust(20)
159
150
  end
160
151
  end
161
152
  end
@@ -27,7 +27,7 @@ class LuxAssets::Element
27
27
 
28
28
  def production?
29
29
  # if building from Rake then we are compiling for production
30
- ENV['LUX_ASSETS_ENV'] == 'production' || defined?(Rake)
30
+ defined?(Rake) || $0.include?('/lux_assets')
31
31
  end
32
32
 
33
33
  def cached
@@ -55,6 +55,7 @@ class LuxAssets::Element
55
55
  @cache.read
56
56
  end
57
57
  alias :compile_sass :compile_scss
58
+ alias :compile_css :compile_sass
58
59
 
59
60
  def compile_js
60
61
  ";\n%s\n;" % @source.read
@@ -24,38 +24,52 @@ module HtmlHelper
24
24
  def asset file, opts={}
25
25
  opts = { dev_file: opts } unless opts.class == Hash
26
26
 
27
- # return joined assets if symbol given
28
- # = asset :main -> asset("css/main") + asset("js/main")
29
- return [asset("css/#{file}"), asset("js/#{file}")].join($/) if
30
- file.is_a?(Symbol)
27
+ if Lux.config(:compile_assets)
28
+ # return second link if it is defined and we are in dev mode
29
+ return asset_include opts[:dev_file] if opts[:dev_file]
31
30
 
32
- # return second link if it is defined and we are in dev mode
33
- return asset_include opts[:dev_file] if opts[:dev_file] && Lux.config(:compile_assets)
31
+ # return internet links
32
+ return asset_include file if file.starts_with?('/') || file.starts_with?('http')
34
33
 
35
- # return internet links
36
- return asset_include file if file.starts_with?('/') || file.starts_with?('http')
34
+ # try to create list of incuded files and show every one of them
35
+ files = LuxAssets.files(file) || []
36
+ data = files.inject([]) do |total, asset|
37
+ if asset.is_a?(Proc)
38
+ tag_name = file.include?('css') ? :style : :script
39
+ total.push({}.tag tag_name, asset.call)
40
+ else
41
+ total.push asset_include '/compiled_asset/' + asset
42
+ end
43
+ end
37
44
 
38
- # return asset link in production or fail unless able
39
- unless Lux.config(:compile_assets)
45
+ data.map{ |it| it.sub(/^\s\s/,'') }.join("\n")
46
+ else
47
+ # return asset link in production or fail unless able
40
48
  manifest = Lux.ram_cache('asset-manifest') { JSON.load Lux.root.join('public/manifest.json').read }
41
49
  mfile = manifest['files'][file]
42
50
 
43
- raise 'Compiled asset link for "%s" not found in manifest.json' % file if mfile.empty?
44
-
45
- return asset_include(Lux.config.assets_root.to_s + mfile, opts)
46
- end
51
+ if mfile.empty?
52
+ unless opts[:raise].is_a?(FalseClass)
53
+ raise 'Compiled asset link for "%s" not found in manifest.json' % file
54
+ end
47
55
 
48
- # try to create list of incuded files and show every one of them
49
- data = LuxAssets.files(file).inject([]) do |total, asset|
50
- if asset.is_a?(Proc)
51
- tag_name = file.include?('css') ? :style : :script
52
- total.push({}.tag tag_name, asset.call)
56
+ nil
53
57
  else
54
- total.push asset_include '/compiled_asset/' + asset
58
+ return asset_include(Lux.config.assets_root.to_s + mfile, opts)
55
59
  end
56
60
  end
61
+ end
57
62
 
58
- data.map{ |it| it.sub(/^\s\s/,'') }.join("\n")
63
+ # assets :vendor, :main
64
+ def assets *args
65
+ total = []
66
+ [:css, :js].each do |ext|
67
+ args.map do |group|
68
+ data = asset("#{ext}/#{group}", raise: false)
69
+ total.push data if data.present?
70
+ end
71
+ end
72
+ total.join($/)
59
73
  end
60
74
 
61
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lux_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dino Reic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-02 00:00:00.000000000 Z
11
+ date: 2018-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -7361,7 +7361,6 @@ files:
7361
7361
  - "./spec/assets/js/test.coffee"
7362
7362
  - "./spec/assets/js/test.js"
7363
7363
  - "./spec/assets/js/test.ts"
7364
- - "./spec/assets/manifest.json"
7365
7364
  - "./spec/spec_helper.rb"
7366
7365
  - "./spec/tests/common_spec.rb"
7367
7366
  - "./yarn.lock"
@@ -1,8 +0,0 @@
1
- {
2
- "files": {
3
- "js/single": "/assets/js-single-ea42d35142da2d7b861554a0c5d5d64df3cf6b93.js",
4
- "js/group": "/assets/js-group-ac69fcfec4e7bc02358a6f955a84ebaa26ed6f78.js",
5
- "css/single": "/assets/css-single-59df6f7bb525125eabc5cda767393b6ce09ea1bc.css",
6
- "css/group": "/assets/css-group-59df6f7bb525125eabc5cda767393b6ce09ea1bc.css"
7
- }
8
- }