lux_assets 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +140 -0
- data/bin/lux_assets +77 -0
- data/lib/lux_assets/asset.rb +74 -0
- data/lib/lux_assets/base.rb +170 -0
- data/lib/lux_assets/element.rb +69 -0
- data/lib/lux_assets/manifest.rb +30 -0
- data/lib/lux_assets.rb +29 -0
- data/lib/vendor/lux/assets_helper.rb +61 -0
- data/lib/vendor/lux/assets_plugin.rb +13 -0
- data/lib/vendor/lux/assets_routes.rb +34 -0
- data/lib/vendor/tasks.rb +36 -0
- data/misc/assets.rb +25 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cf0499416784b64eb2a3fc814ee2d8fc41a44695c053343a731f15ddcb44e682
|
4
|
+
data.tar.gz: ec17806130acc118a8401efaa522cb423ebef09727768343eec92214debf28b3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c887341980224009f77a1261fbcdac1805d370082fc3808f3de564a2d5dabfc75ef3cfd9ee6028a55268695bd65bd5b4134eaa5f51554e959b00bdb882e39d47
|
7
|
+
data.tar.gz: e3af40781f0d76e3f005bbf1fa10fd487ae4801d56078157205518e98f73a5205c24d1047eacbfa93a30eeb57d403157ef0a76a94544b2fa70f53dab9408f97c
|
data/README.md
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
# Web assets
|
2
|
+
|
3
|
+
Lightweight web assets packer that loves simplicity.
|
4
|
+
|
5
|
+
* framework agnostic, use anywhere
|
6
|
+
* compiles js, coffee, typescript, css, sass "out of the box"
|
7
|
+
* keeps manifest in a single file
|
8
|
+
* plugable to handle any other extension
|
9
|
+
* update manifest file for production
|
10
|
+
* autoprefixer, minify, gzip the output
|
11
|
+
* add rake tasks
|
12
|
+
|
13
|
+
## Instalation
|
14
|
+
|
15
|
+
Add `lux_assets` gem to `Gemfile` and then `bundle install`
|
16
|
+
```
|
17
|
+
gem 'lux_assets'
|
18
|
+
```
|
19
|
+
|
20
|
+
## Using
|
21
|
+
|
22
|
+
To use the gem we have bin file in a path (build with Thor gem) named `lux_assets`.
|
23
|
+
|
24
|
+
```
|
25
|
+
~/dev/app $ lux_assets
|
26
|
+
Commands:
|
27
|
+
lux_assets clear # Clear all caches
|
28
|
+
lux_assets compile # Compile assets for production
|
29
|
+
lux_assets help [COMMAND] # Describe available commands or one specific command
|
30
|
+
lux_assets install # Install all needed packages via yarn
|
31
|
+
lux_assets show # Show all files/data in manifest
|
32
|
+
```
|
33
|
+
|
34
|
+
1. Start with `lux_assets install`
|
35
|
+
2. Modify ./config/assets.rb
|
36
|
+
3. Compile with `lux_assets compile`
|
37
|
+
4. Inspect added files with `lux_assets show`
|
38
|
+
|
39
|
+
### To compile
|
40
|
+
|
41
|
+
```
|
42
|
+
~/dev/app $ lux_assets compile
|
43
|
+
~/dev/app $ rake assets:compile
|
44
|
+
Compile js/admin -> /assets/js-admin-d1b55490e6327ba81e60b24539c6086ee1fe5d48.js
|
45
|
+
Compile js/main -> /assets/js-main-a72368758567e466aa01643b1e7426120dbca65b.js
|
46
|
+
Compile css/admin -> /assets/css-admin-f486d0d464932a9da584289007d170695dce23ce.css
|
47
|
+
Compile css/main -> /assets/css-main-1b6d66a32ad6af5fd34edbe4369585be351790cd.css
|
48
|
+
```
|
49
|
+
|
50
|
+
### To get list of all added files
|
51
|
+
|
52
|
+
`lux_assets show`
|
53
|
+
|
54
|
+
or
|
55
|
+
|
56
|
+
```
|
57
|
+
require 'lux_assets'
|
58
|
+
|
59
|
+
ap LuxAssets.to_h
|
60
|
+
```
|
61
|
+
|
62
|
+
|
63
|
+
## Example ./config/assets.rb
|
64
|
+
|
65
|
+
```
|
66
|
+
# relative_root './app/assets'
|
67
|
+
|
68
|
+
asset :admin do
|
69
|
+
js do
|
70
|
+
add 'js/admin/js_vendor/*' # will add all files from a folder
|
71
|
+
add 'js/shared/**' # will add all files from a folder + subfolders
|
72
|
+
add '/Used/foo/app/bar/asssets/admin.js'
|
73
|
+
add ['list', 'of', 'files']
|
74
|
+
add proc { 'js string' }
|
75
|
+
plugin :foo
|
76
|
+
end
|
77
|
+
|
78
|
+
css do
|
79
|
+
add 'css/admin/index.scss'
|
80
|
+
add proc { 'css string' }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
asset :main do
|
85
|
+
js do
|
86
|
+
# ...
|
87
|
+
end
|
88
|
+
|
89
|
+
css do
|
90
|
+
# ...
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
### What can you add
|
96
|
+
|
97
|
+
* relative file
|
98
|
+
* absolute file
|
99
|
+
* list of files
|
100
|
+
* proc
|
101
|
+
|
102
|
+
If you add proc, proc retun must be a string, not a file
|
103
|
+
|
104
|
+
|
105
|
+
## Extending
|
106
|
+
|
107
|
+
### Adding processor for new file type
|
108
|
+
|
109
|
+
This is all that is needed to add support for TypeScript compilation.
|
110
|
+
|
111
|
+
```
|
112
|
+
class LuxAssets::Element
|
113
|
+
def compile_ts
|
114
|
+
LuxAssets.run "node_modules/typescript/.bin/tsc --outFile '#{@cache}' '#{@source}'"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
### Added new plugin for file adder
|
120
|
+
|
121
|
+
You need to extend `LuxAssets` class and call method `add` with list of files or a proc.
|
122
|
+
|
123
|
+
```
|
124
|
+
module LuxAssets
|
125
|
+
# include files from a plugin
|
126
|
+
def plugin name
|
127
|
+
plugin = Lux.plugin.get name
|
128
|
+
add '%s/**' % plugin[:folder]
|
129
|
+
end
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
## Rakefile
|
134
|
+
|
135
|
+
If you want to use `Rake` tasks and not `lux_assets` bin, there is a wrapper.
|
136
|
+
|
137
|
+
In `Rakefile` just add `require 'lux_assets'` and tasks will automaticly be added to your list of tasks.
|
138
|
+
|
139
|
+
Rake taks will call :env task to set up environment.
|
140
|
+
|
data/bin/lux_assets
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
require 'colorize'
|
5
|
+
require 'awesome_print'
|
6
|
+
|
7
|
+
require_relative '../lib/lux_assets'
|
8
|
+
|
9
|
+
###
|
10
|
+
|
11
|
+
module Cli
|
12
|
+
extend self
|
13
|
+
|
14
|
+
def run what
|
15
|
+
puts what.green
|
16
|
+
system what
|
17
|
+
end
|
18
|
+
|
19
|
+
def die text
|
20
|
+
puts text.red
|
21
|
+
exit
|
22
|
+
end
|
23
|
+
|
24
|
+
def info text
|
25
|
+
puts '* %s' % text.magenta
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
###
|
30
|
+
|
31
|
+
class AssetsCli < Thor
|
32
|
+
|
33
|
+
desc :install, 'Install all needed packages via yarn'
|
34
|
+
def install
|
35
|
+
packages = ["coffee-script", "minifier", "node-sass", "typescript", "babel-cli", "autoprefixer-cli"]
|
36
|
+
|
37
|
+
if `which yarn`.empty?
|
38
|
+
Cli.die('yarn not found -> install with "npm install yarn -g"')
|
39
|
+
end
|
40
|
+
|
41
|
+
for pkg in packages
|
42
|
+
Cli.run 'yarn add %s' % pkg
|
43
|
+
end
|
44
|
+
|
45
|
+
puts '* Installed: %s' % packages.map(&:green).join(', ')
|
46
|
+
|
47
|
+
unless LuxAssets::CONFIG_PATH.exist?
|
48
|
+
source = Pathname.new(__FILE__).join("../../misc/assets.rb")
|
49
|
+
LuxAssets::CONFIG_PATH.write source.read
|
50
|
+
puts '* Added template %s' % LuxAssets::CONFIG_PATH.to_s.green
|
51
|
+
end
|
52
|
+
|
53
|
+
puts "* To include LuxAssets rake tasks in Rakefile, just require 'lux_assets'".green
|
54
|
+
end
|
55
|
+
|
56
|
+
desc :show, 'Show all files/data in manifest'
|
57
|
+
def show
|
58
|
+
ap LuxAssets.to_h
|
59
|
+
end
|
60
|
+
|
61
|
+
desc :compile, 'Compile assets for production'
|
62
|
+
def compile
|
63
|
+
LuxAssets.compile_all do |name, path|
|
64
|
+
puts "Compile #{name.green} -> #{path}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
desc :clear, 'Clear all caches'
|
69
|
+
def clear
|
70
|
+
Cli.run 'rm -rf ./tmp/assets'
|
71
|
+
Cli.run 'rm -rf ./public/assets'
|
72
|
+
Cli.run 'rm ./public/manifest.json'
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
AssetsCli.start ARGV
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Asset group, single asset that produces target css or js
|
2
|
+
|
3
|
+
class LuxAssets::Asset
|
4
|
+
PUBLIC_ASSETS = './public/assets'
|
5
|
+
|
6
|
+
def initialize ext, name
|
7
|
+
@ext = ext == :js ? :js : :css
|
8
|
+
@name = name
|
9
|
+
@files = LuxAssets.to_h[ext][name]
|
10
|
+
@target = "#{@ext}/#{@name}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def js?
|
14
|
+
@ext == :js
|
15
|
+
end
|
16
|
+
|
17
|
+
def css?
|
18
|
+
@ext == :css
|
19
|
+
end
|
20
|
+
|
21
|
+
def compile
|
22
|
+
@data = []
|
23
|
+
|
24
|
+
die "No files found for [#{@ext}/#{@name}]" unless @files[0]
|
25
|
+
|
26
|
+
for file in @files
|
27
|
+
if file.is_a?(Proc)
|
28
|
+
@data.push file.call
|
29
|
+
else
|
30
|
+
@data.push LuxAssets::Element.new(file, production: true).compile
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
send 'compile_%s' % @ext
|
35
|
+
|
36
|
+
@asset_file
|
37
|
+
end
|
38
|
+
|
39
|
+
def files
|
40
|
+
@files
|
41
|
+
end
|
42
|
+
|
43
|
+
###
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def save_data data
|
48
|
+
@asset_file = '/assets/%s' % (@target.sub('/', '-') + '-' + Digest::SHA1.hexdigest(data) + '.' + @ext.to_s)
|
49
|
+
@asset_path = "./public#{@asset_file}"
|
50
|
+
|
51
|
+
File.write(@asset_path, data)
|
52
|
+
|
53
|
+
if LuxAssets::Manifest.add(@target, @asset_file)
|
54
|
+
yield
|
55
|
+
|
56
|
+
LuxAssets.run 'touch -t 201001010101 %s' % @asset_path
|
57
|
+
LuxAssets.run 'gzip -k %s' % @asset_path
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def compile_js
|
62
|
+
save_data @data.join(";\n") do
|
63
|
+
# minify
|
64
|
+
LuxAssets.run './node_modules/minifier/index.js --no-comments -o "%{file}" "%{file}"' % { file: @asset_path }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def compile_css
|
69
|
+
save_data @data.join($/) do
|
70
|
+
#autoprefixer
|
71
|
+
LuxAssets.run './node_modules/.bin/autoprefixer-cli %s' % @asset_path
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,170 @@
|
|
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
|
22
|
+
|
23
|
+
module LuxAssets
|
24
|
+
extend self
|
25
|
+
|
26
|
+
CONFIG_PATH = Pathname.new ENV.fetch('ASSET_CONFIG') { './config/assets.rb' }
|
27
|
+
|
28
|
+
ASSET_TYPES ||= {
|
29
|
+
js: ['js', 'coffee', 'ts'],
|
30
|
+
css: ['css', 'scss']
|
31
|
+
}
|
32
|
+
|
33
|
+
@assets ||= { js: {}, css: {} }
|
34
|
+
|
35
|
+
@relative_root = './app/assets'
|
36
|
+
def relative_root name=nil
|
37
|
+
@relative_root = name if name
|
38
|
+
@relative_root
|
39
|
+
end
|
40
|
+
|
41
|
+
def die text
|
42
|
+
puts text
|
43
|
+
exit
|
44
|
+
end
|
45
|
+
|
46
|
+
def asset name
|
47
|
+
@name = name.to_s
|
48
|
+
yield
|
49
|
+
end
|
50
|
+
|
51
|
+
def configure &block
|
52
|
+
class_eval &block
|
53
|
+
end
|
54
|
+
|
55
|
+
def run what, cache_file=nil
|
56
|
+
puts what.yellow
|
57
|
+
|
58
|
+
stdin, stdout, stderr, wait_thread = Open3.popen3(what)
|
59
|
+
|
60
|
+
error = stderr.gets
|
61
|
+
while line = stderr.gets do
|
62
|
+
error += line
|
63
|
+
end
|
64
|
+
|
65
|
+
# node-sass prints to stderror on complete
|
66
|
+
error = nil if error && error.index('Rendering Complete, saving .css file...')
|
67
|
+
|
68
|
+
if error
|
69
|
+
cache_file.unlink if cache_file && cache_file.exist?
|
70
|
+
|
71
|
+
puts error.red
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def js name=nil, &block
|
76
|
+
add_files :js, name, block
|
77
|
+
end
|
78
|
+
|
79
|
+
def css name=nil, &block
|
80
|
+
add_files :css, name, block
|
81
|
+
end
|
82
|
+
|
83
|
+
# adds file or list of files
|
84
|
+
# add 'plugin:js_widgets/*'
|
85
|
+
# add 'js/vendor/*'
|
86
|
+
# add 'index.coffee'
|
87
|
+
# add proc { ... }
|
88
|
+
def add added
|
89
|
+
case added
|
90
|
+
when Array
|
91
|
+
add_local_files added
|
92
|
+
return
|
93
|
+
when Proc
|
94
|
+
@files += [added]
|
95
|
+
else
|
96
|
+
files =
|
97
|
+
if added[0,1] == '/' || added[0,2] == './'
|
98
|
+
added
|
99
|
+
else
|
100
|
+
"#{@relative_root}/#{added}"
|
101
|
+
end
|
102
|
+
|
103
|
+
files =
|
104
|
+
if files.include?('*')
|
105
|
+
files += '/*' if files =~ %r{\/\*\*$}
|
106
|
+
Dir[files].sort
|
107
|
+
else
|
108
|
+
[files]
|
109
|
+
end
|
110
|
+
|
111
|
+
files = files.select { |it| File.file?(it) }
|
112
|
+
|
113
|
+
if files[0]
|
114
|
+
add_local_files files
|
115
|
+
else
|
116
|
+
die 'No files found in "%s -> :%s" (%s)'.red % [@ext, @name, added]
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# get list of files in the resource
|
122
|
+
def files name
|
123
|
+
parts = name.split('/', 2)
|
124
|
+
to_h[parts.first.to_sym][parts[1]]
|
125
|
+
end
|
126
|
+
|
127
|
+
def compile_all
|
128
|
+
for ext in [:js, :css]
|
129
|
+
for name in to_h[ext].keys
|
130
|
+
path = LuxAssets.send(ext, name).compile
|
131
|
+
|
132
|
+
yield "#{ext}/#{name}", path if block_given?
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def to_h
|
138
|
+
unless @assets_loaded
|
139
|
+
die 'Assets file not found in %s' % CONFIG_PATH unless CONFIG_PATH.exist?
|
140
|
+
@assets_loaded = true
|
141
|
+
eval CONFIG_PATH.read
|
142
|
+
end
|
143
|
+
|
144
|
+
@assets
|
145
|
+
end
|
146
|
+
|
147
|
+
private
|
148
|
+
|
149
|
+
def add_local_files files
|
150
|
+
files = files.select { |it| ASSET_TYPES[@ext].include?(it.split('.').last) }
|
151
|
+
|
152
|
+
files = files.select do |f|
|
153
|
+
name = f.split('/').last
|
154
|
+
name.include?('.') && !name[0, 1] != '!'
|
155
|
+
end
|
156
|
+
|
157
|
+
@files += files
|
158
|
+
files
|
159
|
+
end
|
160
|
+
|
161
|
+
def add_files ext, name, block
|
162
|
+
@name = name.to_s if name
|
163
|
+
return Asset.new ext, @name unless block
|
164
|
+
|
165
|
+
@files = []
|
166
|
+
@ext = ext
|
167
|
+
class_eval &block
|
168
|
+
@assets[ext][@name] = @files
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# One file that can be scss, js, coffee, ts, etc...
|
2
|
+
class LuxAssets::Element
|
3
|
+
TMP_ASSETS = './tmp/assets'
|
4
|
+
|
5
|
+
def initialize source, opts={}
|
6
|
+
@source = Pathname.new source
|
7
|
+
@opts = opts
|
8
|
+
@cache = Pathname.new './tmp/assets/%s' % source.gsub('/','-')
|
9
|
+
end
|
10
|
+
|
11
|
+
def compile
|
12
|
+
method_name = 'compile_%s' % @source.to_s.split('.').last.downcase
|
13
|
+
|
14
|
+
if respond_to?(method_name, true)
|
15
|
+
cached || send(method_name)
|
16
|
+
else
|
17
|
+
@source.read
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def content_type
|
22
|
+
@ext ||= @source.to_s.split('.').last.to_sym
|
23
|
+
|
24
|
+
[:css, :scss].include?(@ext) ? 'text/css' : 'text/javascript'
|
25
|
+
end
|
26
|
+
|
27
|
+
###
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def production?
|
32
|
+
defined?(Rake)
|
33
|
+
end
|
34
|
+
|
35
|
+
def cached
|
36
|
+
@cache.exist? && (@cache.ctime > @source.ctime) ? @cache.read : false
|
37
|
+
end
|
38
|
+
|
39
|
+
def compile_coffee
|
40
|
+
coffee_path = './node_modules/coffee-script/bin/coffee'
|
41
|
+
coffee_opts = production? ? '-cp' : '-Mcp --no-header'
|
42
|
+
|
43
|
+
LuxAssets.run "#{coffee_path} #{coffee_opts} '#{@source}' > '#{@cache}'", @cache
|
44
|
+
|
45
|
+
data = @cache.read
|
46
|
+
data = data.gsub(%r{//#\ssourceURL=[\w\-\.\/]+/app/assets/}, '//# sourceURL=/raw_asset/')
|
47
|
+
|
48
|
+
@cache.write data
|
49
|
+
|
50
|
+
data
|
51
|
+
end
|
52
|
+
|
53
|
+
def compile_scss
|
54
|
+
node_sass = './node_modules/node-sass/bin/node-sass'
|
55
|
+
node_opts = production? ? '--output-style compressed' : '--source-comments'
|
56
|
+
LuxAssets.run "#{node_sass} #{node_opts} '#{@source}' '#{@cache}'", @cache
|
57
|
+
@cache.read
|
58
|
+
end
|
59
|
+
alias :compile_sass :compile_scss
|
60
|
+
|
61
|
+
def compile_js
|
62
|
+
";\n%s\n;" % @source.read
|
63
|
+
end
|
64
|
+
|
65
|
+
def compile_ts
|
66
|
+
LuxAssets.run "node_modules/typescript/.bin/tsc --outFile '#{@cache}' '#{@source}'"
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# manifest file
|
2
|
+
|
3
|
+
module LuxAssets::Manifest
|
4
|
+
MANIFEST = Pathname.new('./public/manifest.json')
|
5
|
+
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def add name, path
|
9
|
+
json = JSON.load MANIFEST.read
|
10
|
+
|
11
|
+
return false if json['files'][name] == path
|
12
|
+
|
13
|
+
json['files'][name] = path
|
14
|
+
|
15
|
+
MANIFEST.write JSON.pretty_generate(json)
|
16
|
+
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
def get name
|
21
|
+
json = JSON.load MANIFEST.read
|
22
|
+
json['files'][name]
|
23
|
+
end
|
24
|
+
|
25
|
+
###
|
26
|
+
|
27
|
+
MANIFEST.write '{"files":{}}' unless MANIFEST.exist?
|
28
|
+
end
|
29
|
+
|
30
|
+
|
data/lib/lux_assets.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# libs
|
2
|
+
require 'digest/sha1'
|
3
|
+
require 'json'
|
4
|
+
require 'pathname'
|
5
|
+
require 'open3'
|
6
|
+
|
7
|
+
# gem files
|
8
|
+
require_relative './lux_assets/base'
|
9
|
+
require_relative './lux_assets/asset'
|
10
|
+
require_relative './lux_assets/element'
|
11
|
+
require_relative './lux_assets/manifest'
|
12
|
+
|
13
|
+
# lux framework bindings
|
14
|
+
if defined?(Lux)
|
15
|
+
require_relative './vendor/lux/assets_helper'
|
16
|
+
require_relative './vendor/lux/assets_plugin'
|
17
|
+
require_relative './vendor/lux/assets_routes'
|
18
|
+
end
|
19
|
+
|
20
|
+
# rake bindings
|
21
|
+
if defined?(Rake)
|
22
|
+
require_relative './vendor/tasks.rb'
|
23
|
+
end
|
24
|
+
|
25
|
+
# create needed dirs unless found
|
26
|
+
for dir in ['./tmp/', './tmp/assets', './public', './public/assets']
|
27
|
+
Dir.mkdir(dir) unless Dir.exist?(dir)
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# export to all templates
|
2
|
+
# = asset 'www/index.scss'
|
3
|
+
# = asset 'www/index.coffee'
|
4
|
+
module HtmlHelper
|
5
|
+
def asset_include path, opts={}
|
6
|
+
raise ArgumentError.new("Asset path can't be empty") if path.empty?
|
7
|
+
|
8
|
+
ext = path.split('?').first.split('.').last
|
9
|
+
type = ['css', 'sass', 'scss'].include?(ext) ? :style : :script
|
10
|
+
type = :style if path.include?('fonts.googleapis.com')
|
11
|
+
|
12
|
+
current.response.early_hints path, type
|
13
|
+
|
14
|
+
if type == :style
|
15
|
+
%[<link rel="stylesheet" href="#{path}" />]
|
16
|
+
else
|
17
|
+
%[<script src="#{path}"></script>]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# builds full asset path based on resource extension
|
22
|
+
# asset('js/main')
|
23
|
+
# will render 'app/assets/js/main/index.coffee' as http://aset.path/assets/main-index-md5hash.js
|
24
|
+
def asset file, opts={}
|
25
|
+
opts = { dev_file: opts } unless opts.class == Hash
|
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)
|
31
|
+
|
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)
|
34
|
+
|
35
|
+
# return internet links
|
36
|
+
return asset_include file if file.starts_with?('/') || file.starts_with?('http')
|
37
|
+
|
38
|
+
# return asset link in production or fail unless able
|
39
|
+
unless Lux.config(:compile_assets)
|
40
|
+
manifest = Lux.ram_cache('asset-manifest') { JSON.load Lux.root.join('public/manifest.json').read }
|
41
|
+
mfile = manifest['files'][file]
|
42
|
+
|
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
|
47
|
+
|
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)
|
53
|
+
else
|
54
|
+
total.push asset_include '/compiled_asset/' + asset
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
data.map{ |it| it.sub(/^\s\s/,'') }.join("\n")
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# /compiled_asset/www/js/pjax.coffee
|
2
|
+
# /raw_asset/www/js/pjax.coffee
|
3
|
+
Lux.app.before do
|
4
|
+
# skip /favicon.ico that forces refresh
|
5
|
+
next unless nav.path[1]
|
6
|
+
next unless Lux.config(:compile_assets)
|
7
|
+
|
8
|
+
# only allow clear in dev
|
9
|
+
# clear assets every 4 seconds max
|
10
|
+
if Lux.current.no_cache?
|
11
|
+
Lux.cache.fetch('lux-clear-assets', ttl: 4, log: false, force: false) do
|
12
|
+
puts '* Clearing assets from ./tmp/assets'.yellow
|
13
|
+
`rm -rf ./tmp/assets && mkdir ./tmp/assets`
|
14
|
+
true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
case nav.root
|
19
|
+
when 'compiled_asset'
|
20
|
+
path = nav.reset.drop(1).join('/')
|
21
|
+
|
22
|
+
asset = LuxAssets::Element.new path
|
23
|
+
current.response.content_type asset.content_type
|
24
|
+
current.response.body asset.compile
|
25
|
+
|
26
|
+
when 'raw_asset'
|
27
|
+
path = nav.reset.drop(1).join('/')
|
28
|
+
|
29
|
+
Lux.error "You can watch raw files only in development" unless Lux.dev?
|
30
|
+
|
31
|
+
file = Pathname.new path
|
32
|
+
body file.exist? ? file.read : "error: File not found"
|
33
|
+
end
|
34
|
+
end
|
data/lib/vendor/tasks.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
|
3
|
+
$lux_assets_bin ||= proc do |command|
|
4
|
+
bin = Pathname.new(__dir__).join('../../bin/lux_assets').to_s
|
5
|
+
system '%s %s' % [bin, command]
|
6
|
+
end
|
7
|
+
|
8
|
+
namespace :assets do
|
9
|
+
desc 'Clear all assets'
|
10
|
+
task :clear do
|
11
|
+
$lux_assets_bin.call :clear
|
12
|
+
end
|
13
|
+
|
14
|
+
desc 'Install all needed packages via yarn'
|
15
|
+
task :install do
|
16
|
+
$lux_assets_bin.call :install
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Compile assets to public/assets and generate mainifest.json'
|
20
|
+
task compile: :env do
|
21
|
+
LuxAssets.compile_all do |name, path|
|
22
|
+
puts "Compile #{name.green} -> #{path}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Show all files/data in manifest'
|
27
|
+
task show: :env do
|
28
|
+
ap LuxAssets.to_h
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Upload assets to S3'
|
32
|
+
task :s3_upload do
|
33
|
+
puts 'aws s3 sync ./public s3://bucket.location --cache-control "max-age=31536000, public"'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
data/misc/assets.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# relative_root './app/assets' # default root for relative assets
|
2
|
+
|
3
|
+
asset :admin do
|
4
|
+
js do
|
5
|
+
# add 'js/admin/js_vendor/*'
|
6
|
+
# add '/Used/foo/app/bar/asssets/admin.js'
|
7
|
+
# add ['list', 'of', 'files']
|
8
|
+
# add proc { 'js string' }
|
9
|
+
end
|
10
|
+
|
11
|
+
css do
|
12
|
+
# add 'css/admin/index.scss'
|
13
|
+
# add proc { 'css string' }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
asset :main do
|
18
|
+
js do
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
css do
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lux_assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dino Reic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: awesome_print
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Web assets packer, convenience over configuration, simplified webpack
|
56
|
+
or sprockets with cleaner configuration
|
57
|
+
email: rejotl@gmail.com
|
58
|
+
executables:
|
59
|
+
- lux_assets
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- "./README.md"
|
64
|
+
- "./bin/lux_assets"
|
65
|
+
- "./lib/lux_assets.rb"
|
66
|
+
- "./lib/lux_assets/asset.rb"
|
67
|
+
- "./lib/lux_assets/base.rb"
|
68
|
+
- "./lib/lux_assets/element.rb"
|
69
|
+
- "./lib/lux_assets/manifest.rb"
|
70
|
+
- "./lib/vendor/lux/assets_helper.rb"
|
71
|
+
- "./lib/vendor/lux/assets_plugin.rb"
|
72
|
+
- "./lib/vendor/lux/assets_routes.rb"
|
73
|
+
- "./lib/vendor/tasks.rb"
|
74
|
+
- "./misc/assets.rb"
|
75
|
+
- bin/lux_assets
|
76
|
+
homepage: https://github.com/dux/lux_assets
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.7.5
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Web assets
|
100
|
+
test_files: []
|