webpack_native 0.2.9

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f7f1a68d09162386737acf2c548fa7f5b15f41b583d442d11d3a4f565f79c9c8
4
+ data.tar.gz: 2f0e25eb8bd5716680a903b5fefe38dc0f798971b43755b452f67bba7b8057a1
5
+ SHA512:
6
+ metadata.gz: c7c21e4f8779da0840b67d4521ee2b6860cfacdd8378c42f0f4442200e73a7f24dfe36fcf9b30cc4aa9b16666f886d65c9d31fd7de92f03d286771f60b400c2f
7
+ data.tar.gz: acf37985bcfded4ad4f3f61ab63aa7844145200004441555517d301d119b8316f5e7dade17ccdcab9c0ee1ac7a12caa737839a289aff477964c5f58fd0320386
@@ -0,0 +1,16 @@
1
+ class WebpackNative::DevGenerator < Rails::Generators::Base
2
+
3
+ def generate_development_assets
4
+ compiling_notice = "Compiling for development..."
5
+ puts "\n" + "\e[36m#{compiling_notice}\e[0m" + "\n\n"
6
+
7
+ Mutex.new.synchronize do
8
+ Dir.chdir "#{Rails.root}/app/webpack_native" do
9
+ result = %x{ npm run build:dev }
10
+ puts "\n"
11
+ puts result
12
+ end
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,22 @@
1
+ class WebpackNative::HelperGenerator < Rails::Generators::Base
2
+
3
+ # our templates location:
4
+ source_root File.expand_path('templates', __dir__)
5
+
6
+ #this is used to generate/add the helper file to rails project
7
+ def add_webpack_helper
8
+ template "webpack_native_helper.rb", File.join("app/helpers", "webpack_native_helper.rb")
9
+ end
10
+
11
+ # include_webpack_helper in application_controller.rb
12
+ def include_webpack_helper
13
+ application_controller = "#{Rails.root}/app/controllers/application_controller.rb"
14
+
15
+ include_webpack_helper = "\n\tinclude WebpackNativeHelper"
16
+
17
+ class_declaration = 'class ApplicationController < ActionController::Base'
18
+
19
+ inject_into_file application_controller, include_webpack_helper, after: class_declaration
20
+ end
21
+
22
+ end
@@ -0,0 +1,60 @@
1
+ class WebpackNative::InstallGenerator < Rails::Generators::Base
2
+
3
+ # our templates location:
4
+ source_root File.expand_path('templates', __dir__)
5
+
6
+ desc "Generates webpack_native foldere with basic files package.json webpack.config.js as well as the src folder with stylesheets & javascripts & images folders"
7
+
8
+ # Note: everything we we will copy to our Rails app is within 'templates' folder
9
+
10
+ # (1) we copy webpack_native folder into Rails /app folder
11
+ def add_webpack_native_folder
12
+ directory 'webpack_native', 'app/webpack_native'
13
+ # Not sure but using the gem from rubygems... seems like the "images" directory isn't created when this generator runs, in the meantime let's add it (once again maybe?)
14
+ images_directory = 'app/webpack_native/src/images'
15
+ empty_directory(images_directory) unless Dir.exist?(images_directory)
16
+ end
17
+
18
+ # (2) insert necessary helpers in the layouts/application.html.erb to render the <link> and <javascript> tags
19
+ def inject_stylesheets_and_javascript_tags
20
+ application_layout = "#{Rails.root}/app/views/layouts/application.html.erb"
21
+
22
+ stylesheets_tag = "<%= webpack_stylesheet_url 'application', media: 'all', 'data-turbolinks-track': 'reload' %>"
23
+
24
+ javascripts_tag = "<%= webpack_javascript_url 'application', 'data-turbolinks-track': 'reload' %>"
25
+
26
+ inject_into_file application_layout, "\n\t\t#{stylesheets_tag}\n\t\t#{javascripts_tag}\n", :before => '</head>'
27
+ end
28
+
29
+ # (3) run 'npm install' inside app/webpack_native folder to install the base modules
30
+ def run_npm_install
31
+ install_notice = "\n||==>> Installing Node Modules... \n"
32
+ wait_notice = "** This can take a while. Please be patient!\n\n"
33
+
34
+ puts "\e[36m#{install_notice}\e[0m" # colorize output in cyan
35
+ puts "\e[33m#{wait_notice}\e[0m" # colorize output in brown
36
+ # more colors can be found at: https://stackoverflow.com/questions/1489183/colorized-ruby-output-to-the-terminal
37
+
38
+ Dir.chdir "#{Rails.root}/app/webpack_native" do
39
+ %x{ npm install }
40
+ end
41
+ end
42
+
43
+ # This step was moved to railtie class, it's better to run webpack within the gem after initialize than injecting the code in config.ru
44
+ # (4) When the server starts, start webpack in a separate thread (this should be done in config.ru)
45
+ # def inject_webpack_command
46
+ # webpack_command = <<-RUBY
47
+ # Thread.new do
48
+ # Dir.chdir "#{Rails.root}/app/webpack_native" do
49
+ # %x{ npm run build }
50
+ # end
51
+ # end
52
+ # RUBY
53
+ #
54
+ # # https://stackoverflow.com/questions/8543904/how-to-run-my-ruby-code-after-rails-server-start
55
+ # config_ru = "#{Rails.root}/config.ru"
56
+ #
57
+ # inject_into_file config_ru, webpack_command, before: 'run Rails.application'
58
+ # end
59
+
60
+ end
@@ -0,0 +1,16 @@
1
+ class WebpackNative::ProdGenerator < Rails::Generators::Base
2
+
3
+ def generate_production_assets
4
+ compiling_notice = "Compiling for production..."
5
+ puts "\n" + "\e[36m#{compiling_notice}\e[0m" + "\n\n"
6
+
7
+ Mutex.new.synchronize do
8
+ Dir.chdir "#{Rails.root}/app/webpack_native" do
9
+ result = %x{ npm run build:prod }
10
+ puts "\n"
11
+ puts result
12
+ end
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "webpack_native",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "webpack.config.js",
6
+ "scripts": {
7
+ "build": "webpack --mode=development --watch --colors",
8
+ "build:dev": "webpack --mode=development --colors",
9
+ "build:prod": "webpack --mode=production --colors"
10
+ },
11
+ "author": "",
12
+ "license": "ISC",
13
+ "devDependencies": {
14
+ "@babel/core": "^7.11.6",
15
+ "@babel/preset-env": "^7.11.5",
16
+ "autoprefixer": "^10.0.0",
17
+ "babel-loader": "^8.1.0",
18
+ "clean-webpack-plugin": "^3.0.0",
19
+ "css-loader": "^4.3.0",
20
+ "file-loader": "^6.1.0",
21
+ "mini-css-extract-plugin": "^0.11.2",
22
+ "node-sass": "^4.14.1",
23
+ "optimize-css-assets-webpack-plugin": "^5.0.4",
24
+ "postcss-loader": "^4.0.2",
25
+ "sass-loader": "^10.0.2",
26
+ "url-loader": "^4.1.0",
27
+ "webpack": "^4.44.2",
28
+ "webpack-cli": "^3.3.12",
29
+ "webpack-manifest-plugin": "^2.2.0"
30
+ },
31
+ "dependencies": {
32
+ "turbolinks": "^5.2.0",
33
+ "@rails/ujs": "^6.0.0"
34
+ }
35
+ }
@@ -0,0 +1,12 @@
1
+ // Do not remove this line, all your @import will goes into webpack_native/stylesheets/application.scss
2
+ import '../stylesheets/application.scss';
3
+ // the following line requires that images folder exists
4
+ require.context('../images', true, /\.(gif|jpeg|jpg|png|svg)$/i);
5
+
6
+ require("@rails/ujs").start();
7
+ require("turbolinks").start();
8
+
9
+ // your JavaScript code here...
10
+ // document.addEventListener('turbolinks:load', () => {
11
+ // alert('hello webpackNative!')
12
+ // });
@@ -0,0 +1 @@
1
+ // This is where you write your css or import other scss/css files
@@ -0,0 +1,94 @@
1
+ const path = require('path');
2
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin");
3
+ const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
4
+ const TerserJSPlugin = require('terser-webpack-plugin');
5
+ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
6
+ const WebpackManifestPlugin = require('webpack-manifest-plugin');
7
+ const webpack = require('webpack');
8
+
9
+ module.exports = (env, options) => {
10
+
11
+ const devMode = options.mode !== 'production';
12
+
13
+ return {
14
+
15
+ devtool: "cheap-module-eval-source-map",
16
+ optimization: {
17
+ minimizer: [
18
+ new TerserJSPlugin({}),
19
+ new OptimizeCSSAssetsPlugin({})
20
+ ],
21
+ },
22
+ entry: {
23
+ application: "./src/javascripts/application.js"
24
+ },
25
+ output: {
26
+ filename: devMode ? '[name].js' : '[name]-[contenthash].js',
27
+ path: path.resolve(__dirname, '../../public/webpack_native')
28
+ },
29
+ module: {
30
+ rules: [
31
+ {
32
+ test: /\.m?js$/,
33
+ exclude: /(node_modules|bower_components)/,
34
+ use: {
35
+ loader: 'babel-loader',
36
+ options: {
37
+ presets: ['@babel/preset-env']
38
+ }
39
+ }
40
+ },
41
+ {
42
+ test: /\.css|.scss$/i,
43
+ use: [
44
+ // 'style-loader',
45
+ // MiniCssExtractPlugin.loader,
46
+ {
47
+ loader: MiniCssExtractPlugin.loader,
48
+ options: {
49
+ hmr: true,
50
+ },
51
+ },
52
+ { loader: 'css-loader', options: { importLoaders: 1 } },
53
+
54
+ 'sass-loader'
55
+ ],
56
+ },
57
+ {
58
+ test: /\.(png|jpg|gif|svg)$/i,
59
+ use: [
60
+ {
61
+ loader: 'url-loader',
62
+ options: {
63
+ limit: 8192,
64
+ name: devMode ? '[name].[ext]' : '[name]-[hash:7].[ext]'
65
+ },
66
+ },
67
+ // { loader: 'image-webpack-loader' }
68
+ ],
69
+ }
70
+ ]
71
+ },
72
+ plugins: [
73
+ new webpack.ProvidePlugin({
74
+ // $: 'jquery',
75
+ // jQuery: 'jquery'
76
+ }),
77
+ new WebpackManifestPlugin(),
78
+ new CleanWebpackPlugin(),
79
+ new MiniCssExtractPlugin({
80
+ filename: devMode ? '[name].css' : '[name]-[contenthash].css',
81
+ })
82
+ ],
83
+ // resolve: {
84
+ // alias: {},
85
+ // modules: [
86
+ // path.resolve(__dirname, '../../vendor/javascripts'),
87
+ // path.resolve(__dirname, '../../vendor/stylesheets',
88
+ // 'node_modules'
89
+ // ]
90
+ // }
91
+
92
+ }
93
+
94
+ }
@@ -0,0 +1,45 @@
1
+ module WebpackNativeHelper
2
+
3
+ def webpack_stylesheet_url(asset, **html_options)
4
+ html_options = html_options.merge(
5
+ href: "/webpack_native/#{webpack_manifest_file.fetch("#{asset}.css")}",
6
+ rel: "stylesheet"
7
+ )
8
+ tag.link(html_options).html_safe
9
+ end
10
+
11
+ def webpack_javascript_url(asset, **html_options)
12
+ html_options = html_options.merge(
13
+ type: "text/javascript",
14
+ src: "/webpack_native/#{webpack_manifest_file.fetch("#{asset}.js")}"
15
+ )
16
+ content_tag("script".freeze, nil, html_options).html_safe
17
+ # or tag.script(html_options).html_safe
18
+ end
19
+
20
+ def webpack_image_url(file_name, **options)
21
+ image_tag("/webpack_native/#{file_name}", **options)
22
+ end
23
+
24
+ private
25
+
26
+ def webpack_manifest_file
27
+ # in production, webpack_manifest_file is initialized in railtie.rb file to load one time only, while in development we call load_webpack_manifest on each new request
28
+
29
+ # so we always start by checking webpack_manifest_file from the initialized configuration, if not found (which is the case for development) we fetch get it from the filesystem
30
+
31
+ Rails.configuration.x.webpack_native.webpack_manifest_file || load_webpack_manifest
32
+ end
33
+
34
+ def load_webpack_manifest
35
+ # Set WEBPACK_MANIFEST_PATH to point to the manifest file
36
+ webpack_manifest_path = Rails.root.join('public', 'webpack_native', 'manifest.json')
37
+ JSON.parse(File.read(webpack_manifest_path))
38
+ rescue Errno::ENOENT
39
+ fail "The webpack manifest file does not exist. Hint: run webpack command."
40
+ end
41
+
42
+ # we make load_webpack_manifest method as a module function so we can call it from railtie.rb file withing the initializer.
43
+ module_function :load_webpack_manifest
44
+
45
+ end
@@ -0,0 +1,20 @@
1
+ require "webpack_native/version"
2
+ require 'generators/webpack_native/templates/webpack_native_helper'
3
+ require 'webpack_native/runner'
4
+ require 'webpack_native/railtie' if defined?(Rails)
5
+
6
+ module WebpackNative
7
+ # require the webpack_helper file which is within templates folder
8
+ def self.logger
9
+ @@logger ||= defined?(Rails) ? ActiveSupport::Logger.new(STDOUT) : nil
10
+ end
11
+
12
+ def self.logger=(logger)
13
+ @@logger = logger
14
+ end
15
+ end
16
+
17
+ # include WebpackNativeHelper for ActionView
18
+ ActiveSupport.on_load :action_view do
19
+ ::ActionView::Base.send :include, WebpackNativeHelper
20
+ end
@@ -0,0 +1,35 @@
1
+ require 'rails'
2
+ require "open3"
3
+
4
+ class WebpackNative::Railtie < ::Rails::Railtie
5
+
6
+ initializer 'Rails logger' do
7
+ WebpackNative.logger = ActiveSupport::Logger.new(STDOUT)
8
+ end
9
+
10
+ initializer "webpack_native_set_manifest" do
11
+ if Rails.env.production?
12
+ require_relative 'generators/webpack_native/templates/webpack_native_helper.rb'
13
+ Rails.configuration.x.webpack_native.webpack_manifest_file = WebpackNativeHelper.load_webpack_manifest
14
+ end
15
+ end
16
+
17
+ def start_webpack
18
+ Mutex.new.synchronize do
19
+ Dir.chdir "#{Rails.root}/app/webpack_native" do
20
+ # %x{ ./bin/webpack --watch --colors --progress }
21
+ runner = WebpackNative::Runner.new('npm run build')
22
+ runner.run
23
+ end
24
+ end
25
+ end
26
+
27
+ initializer "run_webpack_build_cmd" do
28
+ config.after_initialize do
29
+ if defined?(Rails::Server)
30
+ Thread.new { start_webpack }
31
+ end
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,88 @@
1
+ require 'open3'
2
+
3
+ class WebpackNative::Runner
4
+ attr_reader :cmd, :exit_status, :stdout, :stderr
5
+
6
+ # Run a command, return runner instance
7
+ # @param cmd [String,Array<String>] command to execute
8
+ def self.run(*cmd)
9
+ Runner.new(*cmd).run
10
+ end
11
+
12
+ # Run a command, raise Runner::Error if it fails
13
+ # @param cmd [String,Array<String>] command to execute
14
+ # @raise [Runner::Error]
15
+ def self.run!(*cmd)
16
+ Runner.new(*cmd).run!
17
+ end
18
+
19
+ # Run a command, return true if it succeeds, false if not
20
+ # @param cmd [String,Array<String>] command to execute
21
+ # @return [Boolean]
22
+ def self.run?(*cmd)
23
+ Runner.new(*cmd).run?
24
+ end
25
+
26
+ Error = Class.new(StandardError)
27
+
28
+ # @param cmd [String,Array<String>] command to execute
29
+ def initialize(cmd)
30
+ @cmd = cmd.is_a?(Array) ? cmd.join(' ') : cmd
31
+ @stdout = +''
32
+ @stderr = +''
33
+ @exit_status = nil
34
+ end
35
+
36
+ # @return [Boolean] success or failure?
37
+ def success?
38
+ exit_status.zero?
39
+ end
40
+
41
+ # Run the command, return self
42
+ # @return [Runner]
43
+ def run
44
+ Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
45
+ until [stdout, stderr].all?(&:eof?)
46
+ readable = IO.select([stdout, stderr])
47
+ next unless readable&.first
48
+
49
+ readable.first.each do |stream|
50
+ data = +''
51
+ # rubocop:disable Lint/HandleExceptions
52
+ begin
53
+ stream.read_nonblock(1024, data)
54
+ rescue EOFError
55
+ # ignore, it's expected for read_nonblock to raise EOFError
56
+ # when all is read
57
+ end
58
+
59
+ if stream == stdout
60
+ @stdout << data
61
+ $stdout.write(data)
62
+ else
63
+ @stderr << data
64
+ $stderr.write(data)
65
+ end
66
+ end
67
+ end
68
+ @exit_status = wait_thr.value.exitstatus
69
+ end
70
+
71
+ self
72
+ end
73
+
74
+ # Run the command and return stdout, raise if fails
75
+ # @return stdout [String]
76
+ # @raise [Runner::Error]
77
+ def run!
78
+ return run.stdout if run.success?
79
+
80
+ raise(Error, "command failed, exit: %d - stdout: %s / stderr: %s" % [exit_status, stdout, stderr])
81
+ end
82
+
83
+ # Run the command and return true if success, false if failure
84
+ # @return success [Boolean]
85
+ def run?
86
+ run.success?
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module WebpackNative
2
+ VERSION = "0.2.9"
3
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpack_native
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.9
5
+ platform: ruby
6
+ authors:
7
+ - scratchoo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-10-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use vanilla webpack to manage your assets efficiently, no webpacker or
14
+ asset pipeline anymore!
15
+ email:
16
+ - hello@scratchoo.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/generators/webpack_native/dev_generator.rb
22
+ - lib/generators/webpack_native/helper_generator.rb
23
+ - lib/generators/webpack_native/install_generator.rb
24
+ - lib/generators/webpack_native/prod_generator.rb
25
+ - lib/generators/webpack_native/templates/webpack_native/package.json
26
+ - lib/generators/webpack_native/templates/webpack_native/src/javascripts/application.js
27
+ - lib/generators/webpack_native/templates/webpack_native/src/stylesheets/application.scss
28
+ - lib/generators/webpack_native/templates/webpack_native/webpack.config.js
29
+ - lib/generators/webpack_native/templates/webpack_native_helper.rb
30
+ - lib/webpack_native.rb
31
+ - lib/webpack_native/railtie.rb
32
+ - lib/webpack_native/runner.rb
33
+ - lib/webpack_native/version.rb
34
+ homepage: https://www.github.com/scratchoo/webpack_native
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.3.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.1.2
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Vanilla Webpack For Rails Applications
57
+ test_files: []