superleggera 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 85dcff1791e37bcafe968360f90c4a064a55b9ee84a6d13848ab43e4a6ae207f
4
+ data.tar.gz: d01efa7e2cf3839a4073e6fbae2c935b271e2b3a9a0cdad50a7c4359ce2ea857
5
+ SHA512:
6
+ metadata.gz: 8605c5bd446d59bab7f613075af5ac80fc7ae70ca575f2d8d2fde4041c872a3978890e8043450b89bbd4ee999bd662ac830dbb13ca2be134d6ab2ef04803251b
7
+ data.tar.gz: 6601a01aeb2ad964481cab1bb08112dfc548b3ab6850ad92b73e9b4523f73502e32b84651219855f1af0897610eb1d14e634e53b03fcbee13ee7a6cec5718d51
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Chris Oliver
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,74 @@
1
+ # Superleggera
2
+ Monitor your CSS in development to create a whitelist for PurgeCSS
3
+ in Rails with Webpacker.
4
+
5
+ This can be used to purge unused CSS classes from Bootstrap,
6
+ TailwindCSS, or any other CSS framework you include in Webpacker.
7
+
8
+ ## Usage
9
+ How to use my plugin.
10
+
11
+ ## Installation
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'superleggera'
16
+ ```
17
+
18
+ And then execute:
19
+ ```bash
20
+ $ bundle
21
+ ```
22
+
23
+ Or install it yourself as:
24
+ ```bash
25
+ $ gem install superleggera
26
+ ```
27
+
28
+ Make sure your app has Bootstrap, TailwindCSS, or another CSS framework
29
+ loaded via Webpacker and not the asset pipeline.
30
+
31
+ Then update your `config/webpack/development.js` file:
32
+
33
+ ```
34
+ process.env.NODE_ENV = process.env.NODE_ENV || 'development'
35
+
36
+ const environment = require('./environment')
37
+
38
+
39
+ // Configure PurgeCSS
40
+ const PurgecssPlugin = require('purgecss-webpack-plugin')
41
+ const fs = require("fs")
42
+ const glob = require('glob-all')
43
+ const path = require('path')
44
+
45
+ var whitelist_path = "config/whitelist.json"
46
+ var whitelist = []
47
+
48
+ if (fs.existsSync(whitelist_path)) {
49
+ whitelist = JSON.parse(fs.readFileSync(whitelist_path))
50
+ }
51
+
52
+ environment.plugins.append('PurgecssPlugin', new PurgecssPlugin({
53
+ paths: glob.sync([
54
+ path.join(__dirname, '../../app/views/**/*.html.erb'),
55
+ ]),
56
+ whitelist: whitelist
57
+ }))
58
+
59
+
60
+
61
+
62
+ module.exports = environment.toWebpackConfig()
63
+ ```
64
+
65
+ ## TODO
66
+ * When a Rails view is updated, we should also trigger webpack to rebuild the
67
+ CSS and autoreload if the webpack-dev-server is running. This is ideally
68
+ done via Webpacker monitoring of app/views, but we could also triggers
69
+ this when a request comes through the middleware.
70
+ * Process Vue, React, and Stimulus files to find more classes to
71
+ whitelist
72
+
73
+ ## License
74
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Superleggera'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1 @@
1
+ {["test"]
@@ -0,0 +1,52 @@
1
+ require "superleggera/engine"
2
+
3
+ module Superleggera
4
+ class Error < StandardError; end
5
+
6
+ class Middleware
7
+ def initialize app
8
+ @app = app
9
+ end
10
+
11
+ def call env
12
+ @status, @headers, @response = @app.call(env)
13
+
14
+ merge_whitelist
15
+
16
+ [@status, @headers, @response]
17
+ end
18
+
19
+ def merge_whitelist
20
+ classes = load_whitelist
21
+ classes += parse_classes(@response)
22
+ classes.uniq!
23
+ classes.sort!
24
+ write_whitelist(classes)
25
+ end
26
+
27
+ def parse_classes(document)
28
+ matches = document.scan(/class=["'](.+)["']/)
29
+ matches.flatten.flat_map(&:split)
30
+ end
31
+
32
+ def write_whitelist(classes)
33
+ File.open(whitelist_path, "w") do |f|
34
+ f.write(classes.to_json)
35
+ end
36
+ end
37
+
38
+ def load_whitelist
39
+ if File.exist?(whitelist_path)
40
+ JSON.load File.open(whitelist_path)
41
+ else
42
+ []
43
+ end
44
+ rescue JSON::ParserError
45
+ raise Error, "Unable to parse #{whitelist_path}. The contents should be a valid JSON array of class names."
46
+ end
47
+
48
+ def whitelist_path
49
+ File.join(Engine.root, "config/whitelist.json")
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ module Superleggera
2
+ class Engine < ::Rails::Engine
3
+ initializer "superleggera.insert_middleware" do |app|
4
+ app.config.middleware.use Superleggera::Middleware
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Superleggera
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :superleggera do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: superleggera
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Oliver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Monitor your CSS in development to create a whitelist for PurgeCSS in
42
+ Rails with Webpacker
43
+ email:
44
+ - excid3@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - config/whitelist.json
53
+ - lib/superleggera.rb
54
+ - lib/superleggera/engine.rb
55
+ - lib/superleggera/version.rb
56
+ - lib/tasks/superleggera_tasks.rake
57
+ homepage: https://github.com/excid3/superleggera
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.7.6
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: PurgeCSS for Rails & Webpacker
81
+ test_files: []