sprockets-webpackit 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +25 -0
  3. data/README.md +43 -0
  4. data/lib/sprockets/webpackit.rb +116 -0
  5. metadata +76 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8ff412f7deb742748198e489d10a6f4afd1b0e5f1a97541701d597036aae150a
4
+ data.tar.gz: 9f16f8dd606d6fce08252f6c95829caa94c096897e5f4d309dcd845dd0250cd9
5
+ SHA512:
6
+ metadata.gz: bc01a3640dae8d34484e4c6ba95463efd97e9a9e2b19d98bf245a5f92387a8aa596268e8f5fce44df5f38e02790f37bc3d9a96d2bfff325efc3e69996b78438f
7
+ data.tar.gz: 84eea06a578112d5c93d13b421da7bef6619971fa8f08a61d10715608c94d171fbdcbf3c0f120260038ab2739650026027e70a93ce9844160865616650ac9848
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Clive Andrews
4
+
5
+
6
+ Permission is hereby granted, free of charge, to any person
7
+ obtaining a copy of this software and associated documentation
8
+ files (the "Software"), to deal in the Software without
9
+ restriction, including without limitation the rights to use,
10
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the
12
+ Software is furnished to do so, subject to the following
13
+ conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Sprockets Webpackit
2
+
3
+ This is a simple gem which will call webpack-cli to compile designated js files ( including dependent modules ) within the standard sprockets asset pipeline.
4
+
5
+ ## Installation
6
+
7
+ gem install sprockets-webpackit
8
+
9
+ requires node.js to be installed along with webpack, webpack-cli and any other
10
+ node modules you require. ( eg coffeescript , truescript , etc )
11
+
12
+ npm install webpack webpack-cli --save-dev
13
+
14
+ create a `webpack.config.js` to control the compilation process.
15
+
16
+ tested with:
17
+
18
+ webpack: version 4.44.1
19
+ node: version 12.13.0
20
+
21
+
22
+ ## Configuration
23
+
24
+ require 'sprockets/webpackit'
25
+
26
+ Sprockets::Webpackit.pattern = /^*.js$/ # optional - override the default matcher
27
+ Sprockets::Webpackit.mode = 'production' # optional - default is RACK_ENV or 'development'
28
+
29
+ map '/assets' do
30
+ ...
31
+ end
32
+
33
+ # Use
34
+
35
+ to process a file the name must match the given pattern. The default
36
+ is a suffix of `.webpack.js` , `.webpack.coffee` etc.
37
+
38
+ eg: in your `application.js`
39
+
40
+ //= require application.webpack.coffee
41
+
42
+
43
+ you could also set the pattern to eg `/.*/`to accept all javascript files.
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Clive Andrews 2020
4
+ #
5
+
6
+ require 'sprockets'
7
+ require 'tempfile'
8
+ require 'json'
9
+ require 'open3'
10
+
11
+ # transform a javascript asset by running through the webpack manger.
12
+ # The file is processed if its name finishes in eg.. .webpack.js.
13
+ # The method for saving dependencies is copied from the sprockets
14
+ # sass processor.
15
+ #
16
+
17
+ module Sprockets
18
+ class Webpackit
19
+
20
+ DEFAULT_PATTERN = /\.webpack\.([a-z])+$/.freeze
21
+
22
+ class << self
23
+
24
+ # get a list of dependent filenames from the json webpack stats
25
+ def extract_dependencies(h)
26
+ out = []
27
+ h['modules'].each do |m|
28
+ if m['cacheable'] == true
29
+ out << File.absolute_path(m['name'])
30
+ out.concat(extract_dependencies(m)) if m['modules']
31
+ end
32
+ end
33
+ out
34
+ end
35
+
36
+ def extract_errors(h)
37
+ out = []
38
+ h['modules'].each do |m|
39
+ if m['errors'] > 0
40
+ out << m['name']
41
+ out.concat(extract_dependencies(m)) if m['modules']
42
+ end
43
+ end
44
+ out
45
+ end
46
+
47
+ def mode
48
+ @_mode ||= ENV['RACK_ENV'] || 'development'
49
+ end
50
+
51
+ def mode=(val)
52
+ @_mode = val
53
+ end
54
+
55
+ # run a file through webpack and return the result.
56
+ # also maintain a list of dependencies.
57
+ def process(context, path)
58
+ temp = Tempfile.new('sprockets')
59
+
60
+ command = "npx webpack #{path}"\
61
+ " --mode #{mode} --json --display normal"\
62
+ " -o #{temp.path}"
63
+
64
+ puts "command ='#{command}'" if $VERBOSE || $DEBUG
65
+ stats, err, status = Open3.capture3(command)
66
+ # stats = %x(#{command})
67
+ hash = JSON.parse(stats)
68
+ errors = nil
69
+ if status.success?
70
+ deps = extract_dependencies(hash)
71
+ deps.each do |fname|
72
+ next if fname == path
73
+
74
+ context.metadata[:dependencies] << Sprockets::URIUtils.build_file_digest_uri(fname)
75
+ end
76
+ result = File.read(temp.path)
77
+ else
78
+ errors = hash['errors']
79
+ puts errors
80
+ result = errors.join("\n")
81
+ end
82
+
83
+ temp.close
84
+ temp.unlink
85
+
86
+ context.metadata.merge(:data => result)
87
+ end
88
+
89
+ def pattern=(value)
90
+ @_pattern = Regexp.new(value)
91
+ end
92
+
93
+ def pattern
94
+ @_pattern ||= DEFAULT_PATTERN
95
+ end
96
+
97
+ def call(input)
98
+ context = input[:environment].context_class.new(input)
99
+ source = input[:data]
100
+ load_path = input[:load_path]
101
+ filename = input[:filename]
102
+
103
+ path = filename[load_path.length..-1]
104
+ if filename =~ pattern
105
+ process(context, filename)
106
+ else
107
+ { :data => source }
108
+ end
109
+ end
110
+
111
+ end # self
112
+
113
+ end
114
+ end
115
+
116
+ Sprockets.register_postprocessor('application/javascript', Sprockets::Webpackit)
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-webpackit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Clive Andrews
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sprockets
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: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: compile webpack bundles in sprockets
42
+ email:
43
+ - gems@realitybites.eu
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - README.md
48
+ - LICENSE
49
+ files:
50
+ - LICENSE
51
+ - README.md
52
+ - lib/sprockets/webpackit.rb
53
+ homepage: https://github.com/realbite/sprockets-webpackit
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.4
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: compile webpack bundles within the sprockets asset pipeline using webpack-cli
76
+ test_files: []