webpacker_cli 0.9.3 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e63a0b5dbf4f7c5a19cebf3903b6de9dbc685b1a9ab3644277e1f7f135a76e8f
4
- data.tar.gz: b77d5af79d62c0a1b0e902bcf0918516ff75827de448459fe3ec823e05b4a972
3
+ metadata.gz: 19d7e1da92f14e38e1316642e1dc3d1b6dee824388c87bf4472ae4e5776a9cae
4
+ data.tar.gz: e51508803dccc8dd937e0e9d0b44db6e6076e969dc1f2949f8a6cd273b3ae063
5
5
  SHA512:
6
- metadata.gz: 14865804cf30c8634c2c731afe002a19527193ec9b6b1c19b77f9c423c35a4c25c6dac822e37f55a1694d3618d6f7283d4b8390d7f322f1179931bb0bb0cf709
7
- data.tar.gz: fbef0f507074f78c6de2fb9b3748d68a1f4ee4bf9f9bd945e71679049015887a5fc34a8a66f16fa69ca576c2df6a0b7a0023bb0896f30aee400161268d681ab9
6
+ metadata.gz: 5f50568be45820415483bad47df5649dd16db246cb5fa03bfa507975448d542636109b77574ae8b892c2e586b7829d7e5f6c88ebe3d641012f4437fc100b1a4d
7
+ data.tar.gz: 8f5a6d531c006f12722d9fbd14225c5279fd056856cce3c38386170436f2b9e12a29381ce4f05488ebe6ab58c1867cae497c5bb808b550ad59902018583e8ec3
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ Complete rewrite; no longer depends on Webpacker or Rails.
6
+
7
+ - Drives Webpack 5 directly through a generated, editable `config/webpack/webpack.config.js`.
8
+ - No runtime gem dependencies.
9
+ - `init` generates `config/webpacker.yml`, an entry pack and `package.json`; existing files are kept unless `--force`.
10
+ - New commands: `compile`, `watch`, `clobber`, `check`, `info`, `version`.
11
+ - `public/packs/manifest.json` keeps its flat `"application.js" => "/packs/application-<hash>.js"` format.
12
+ - **Breaking:** the `webpacker:*` rake tasks and the generated `Gemfile`, `Rakefile`, `bin/rails`, `config/application.rb` and `config/webpack/loaders` are gone. Requires Ruby >= 3.0 and Node >= 20.9.
13
+
14
+ ## 0.9.4 and earlier
15
+
16
+ Mocked a Rails app to run Webpacker 4's rake tasks.
data/Gemfile CHANGED
@@ -1,6 +1,3 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in webpacker-cli.gemspec
6
3
  gemspec
data/README.md CHANGED
@@ -1,68 +1,65 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/webpacker_cli.svg)](http://badge.fury.io/rb/webpacker_cli)
2
- [![Build Status](https://travis-ci.org/danielpclark/webpacker-cli.svg)](https://travis-ci.org/danielpclark/webpacker-cli)
2
+ [![Build Status](https://github.com/danielpclark/webpacker-cli/actions/workflows/ci.yml/badge.svg)](https://github.com/danielpclark/webpacker-cli/actions)
3
3
 
4
4
  # WebpackerCli
5
5
 
6
- Bringing the impressive work of the Rails' **Webpacker** project **to be available for frameworks
7
- other than Rails**. This is done by mocking the minimal amount of Rails for the Webpacker's
8
- Rake commands to be able to run.
6
+ **Webpacker's conventions for any framework, without Rails.** Webpacker was abandoned by the Rails
7
+ team, and the old version of this tool depended on it (and on a mocked Rails app). Version 1.0
8
+ removes that dependency entirely: the gem has **no runtime dependencies**, and drives plain
9
+ **Webpack 5** through a small, generated `webpack.config.js` that lives in your project.
9
10
 
10
- Webpacker itself takes Webpack, which has a primary focus for pre-processing JavaScript related
11
- code, and make pre-processing other kinds of files such as CSS, SASS, Images, VueJS, React,
12
- CoffeeScript, and other similar content as simple as a couple very minor edits. Without Webpacker
13
- you would need to go through quite a bit of extra work to get Webpack to handle non JS items.
11
+ You keep the parts of Webpacker that matter:
14
12
 
15
- This project makes asset packing and cache invalidation easy for any web framework by bringing
16
- Webpacker's tooling to the command line. This cli tool is not meant to be used in a Rails project
17
- _(at least not until it's designed to not overwrite a few files that exist in Rails)_.
13
+ * `config/webpacker.yml` (`source_path`, `source_entry_path`, `public_output_path`, `extensions`, …)
14
+ * every file in `app/javascript/packs` is an entry point
15
+ * content-hashed output in `public/packs` plus a `manifest.json` for cache invalidation
18
16
 
19
- This project has the same dependencies/prerequisites as [Webpacker](https://github.com/rails/webpacker#prerequisites).
17
+ ## Prerequisites
20
18
 
21
- ## Installation
19
+ Ruby 3.0+, Node.js 20.9+, and Yarn 1.x or npm.
22
20
 
23
- Install it yourself as:
21
+ ## Installation
24
22
 
25
23
  $ gem install webpacker_cli
26
24
 
27
- _Note: If you have trouble compiling the Nokogiri dependency on Linux try installing the following packages: `build-essential ruby-dev libxml2-dev`_
28
-
29
25
  ## Usage
30
26
 
31
- General help is found with
32
-
33
- $ webpacker-cli --help
34
-
35
- The help menu will change after initialization within the project directory.
36
-
37
- And get started using it in your target project with:
27
+ $ webpacker-cli init # create config + entry pack + package.json, install Node packages
28
+ $ webpacker-cli compile # build packs into public/packs
29
+ $ webpacker-cli watch # rebuild on change
30
+ $ webpacker-cli clobber # remove compiled packs and cache
31
+ $ webpacker-cli check # verify prerequisites
32
+ $ webpacker-cli info # tool versions
38
33
 
39
- $ webpacker-cli init
34
+ The environment comes from `WEBPACKER_ENV`, `NODE_ENV` or `RAILS_ENV` (default `development`).
35
+ Production builds use content hashes and minification, e.g. `NODE_ENV=production webpacker-cli compile`.
40
36
 
41
- This creates the minimum files for Webpacker to work and runs `bundle` & `rake webpacker:install`.
42
- From here you have your configuration files for Webpack/Webpacker in the `config` directory.
43
-
44
- To compile your assets you may now run:
45
-
46
- $ webpacker-cli compile
47
-
48
- To add support for another file type you can add a file to the `config/webpack/loaders`
49
- directory. Then load it from `config/webpack/environment.js` and lastly add the file extension
50
- to the `config/webpacker.yml` under `default.extensions`. Be sure to add the JS package with
51
- a command like `yarn add`.
37
+ The generated `config/webpack/webpack.config.js` and `package.json` are yours to edit: add a loader
38
+ by installing its package and adding a rule, and add the extension to `default.extensions` in
39
+ `config/webpacker.yml`.
52
40
 
53
41
  > Server Tip: It is recommended to compile your assets upon deploy rather than per web request.
54
42
 
55
- Once your assets are compiled for deployment you will need to have your web application route
56
- the assets by the file stored in `public/packs/manifest.json` which will look similar to:
43
+ Route assets using `public/packs/manifest.json`:
57
44
 
58
45
  {
59
46
  "application.js": "/packs/application-9578bdd78b657fa4358f.js",
60
47
  "application.js.map": "/packs/application-9578bdd78b657fa4358f.js.map"
61
48
  }
62
49
 
63
- This way Webpacker handles cache invalidation for you whenever you make changes to your assets.
64
- So the `manifest.json` is the key-value pair of data used to be substitued in your web page views
65
- for the `src` references.
50
+ ## Staying current with Webpack
51
+
52
+ `package.json` pins each tool to a **major version range** (`webpack ^5`, `webpack-cli ^6`, …), so
53
+ `yarn upgrade`/`npm update` picks up fixes without breaking changes. A new Webpack major is an
54
+ explicit, reviewed change to the templates in this gem (and to your project's config when you
55
+ choose to adopt it) — never a surprise. Existing projects are never overwritten by `init` unless
56
+ you pass `--force`.
57
+
58
+ ### Migrating from 0.x
59
+
60
+ The Rails mock (`Gemfile`, `Rakefile`, `bin/rails`, `config/application.rb`) and the `webpacker:*`
61
+ rake tasks are gone. Delete those files, run `webpacker-cli init` for the new config, and move any
62
+ custom loaders from `config/webpack/loaders` into `webpack.config.js` rules.
66
63
 
67
64
  ## Example Integration
68
65
 
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "webpacker/cli"
4
+ require "webpacker_cli"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
data/bin/webpacker-cli CHANGED
@@ -2,50 +2,56 @@
2
2
 
3
3
  $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
4
 
5
- require 'tty/command'
6
5
  require 'webpacker_cli'
7
6
 
8
- def init_menu
9
- <<~INITIAL_MENU
10
- To first initialize Webpacker in this project run the following command.
11
-
12
- webpacker-cli init
13
-
14
- This will generate the minimal content to use webpacker.
15
-
16
- Dependencies:
17
- * Ruby 2.2+
18
- * Node.js 6.14.0+
19
- * Yarn 1.x+
20
- INITIAL_MENU
21
- end
22
-
23
- # For menu creation
24
- def rake_webpacker
25
- TTY::Command.
26
- new(printer: :null).
27
- run!('rake webpacker')
28
- end
29
-
30
- def usage
31
- result = rake_webpacker
32
- if result.failure?
33
- puts init_menu
7
+ USAGE = <<~TEXT
8
+ webpacker-cli #{WebpackerCli::VERSION}
9
+
10
+ Usage: webpacker-cli <command> [options]
11
+
12
+ Commands:
13
+ init Create config, entry pack and package.json, then install Node packages
14
+ --force overwrite existing files
15
+ --skip-install don't install Node packages
16
+ compile Compile packs into the public output directory (writes manifest.json)
17
+ watch Compile and recompile on changes
18
+ clobber Remove compiled packs and the Webpack cache
19
+ check Verify Node, package manager and installed packages
20
+ info Show tool versions
21
+ version Show the webpacker-cli version
22
+
23
+ The environment is taken from WEBPACKER_ENV, NODE_ENV or RAILS_ENV (default: development).
24
+ Run `webpacker-cli init` first in your project directory.
25
+ TEXT
26
+
27
+ command, *args = ARGV
28
+
29
+ begin
30
+ case command
31
+ when 'init'
32
+ puts 'INITIALIZING WEBPACKER…'
33
+ WebpackerCli.init(force: args.include?('--force'), install: !args.include?('--skip-install'))
34
+ when 'compile'
35
+ WebpackerCli.compile
36
+ when 'watch'
37
+ WebpackerCli.watch
38
+ when 'clobber'
39
+ WebpackerCli.clobber
40
+ when 'check'
41
+ problems = WebpackerCli.check
42
+ puts(problems.empty? ? 'Everything looks good.' : problems)
43
+ exit(problems.empty? ? 0 : 1)
44
+ when 'info'
45
+ WebpackerCli.info.each { |k, v| puts "#{k}: #{v}" }
46
+ when 'version', '--version', '-v'
47
+ puts WebpackerCli::VERSION
48
+ when nil, 'help', '--help', '-h'
49
+ puts USAGE
34
50
  else
35
- puts result.out.gsub(/webpacker:/, "webpacker-cli ")
51
+ warn "Unknown command: #{command}\n\n#{USAGE}"
52
+ exit 1
36
53
  end
37
- end
38
-
39
- case ARGV[0]
40
- when /^(?:--help|-h)$/ then
41
- usage
42
- when /^init$/
43
- puts "INITIALIZING WEBPACKER…"
44
- WebpackerCli.init()
45
- when /^[a-z:]+$/
46
- TTY::Command.
47
- new(color: false, uuid: false).
48
- run("rake webpacker:#{ARGV.join(' ')}")
49
- else
50
- usage
54
+ rescue WebpackerCli::Error => e
55
+ warn e.message
56
+ exit 1
51
57
  end
@@ -0,0 +1,3 @@
1
+ // Entry point compiled by `webpacker-cli compile`. Every file in this
2
+ // directory becomes its own pack (e.g. "application.js" in manifest.json).
3
+ console.log('Hello World from webpacker-cli')
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "application",
3
+ "private": true,
4
+ "engines": {
5
+ "node": ">=20.9.0"
6
+ },
7
+ "dependencies": {
8
+ "@babel/core": "^7.26.0",
9
+ "@babel/preset-env": "^7.26.0",
10
+ "babel-loader": "^10.0.0",
11
+ "css-loader": "^7.1.0",
12
+ "css-minimizer-webpack-plugin": "^7.0.0",
13
+ "js-yaml": "^4.1.0",
14
+ "mini-css-extract-plugin": "^2.9.0",
15
+ "webpack": "^5.90.0",
16
+ "webpack-assets-manifest": "^6.0.0",
17
+ "webpack-cli": "^6.0.0"
18
+ }
19
+ }
@@ -0,0 +1,83 @@
1
+ // Webpack configuration generated by webpacker-cli. This file is yours to edit.
2
+ //
3
+ // Conventions (identical to Rails' Webpacker):
4
+ // * every file in <source_path>/<source_entry_path> is an entry point
5
+ // * output goes to <public_root_path>/<public_output_path>
6
+ // * a manifest.json mapping "application.js" => "/packs/application-<hash>.js"
7
+ // is written next to the output
8
+ const fs = require('fs')
9
+ const path = require('path')
10
+ const yaml = require('js-yaml')
11
+ const { WebpackAssetsManifest } = require('webpack-assets-manifest')
12
+ const MiniCssExtractPlugin = require('mini-css-extract-plugin')
13
+ const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
14
+
15
+ const root = path.resolve(__dirname, '..', '..')
16
+ const envName = process.env.WEBPACKER_ENV || process.env.NODE_ENV || 'development'
17
+ const isProduction = envName === 'production'
18
+
19
+ const settings = yaml.load(fs.readFileSync(path.join(root, 'config', 'webpacker.yml'), 'utf8'))
20
+ const cfg = Object.assign({}, settings.default, settings[envName])
21
+
22
+ const sourcePath = path.resolve(root, cfg.source_path)
23
+ const entryPath = path.join(sourcePath, cfg.source_entry_path)
24
+ const outputPath = path.resolve(root, cfg.public_root_path, cfg.public_output_path)
25
+ const publicPath = `/${cfg.public_output_path.replace(/^\/+|\/+$/g, '')}/`
26
+
27
+ const entry = {}
28
+ const collect = (dir, prefix) => {
29
+ if (!fs.existsSync(dir)) return
30
+ for (const item of fs.readdirSync(dir, { withFileTypes: true })) {
31
+ if (item.isDirectory()) {
32
+ collect(path.join(dir, item.name), `${prefix}${item.name}/`)
33
+ } else if (/\.(m?js|jsx|ts|tsx|css)$/.test(item.name)) {
34
+ entry[prefix + item.name.replace(/\.[^.]+$/, '')] = path.join(dir, item.name)
35
+ }
36
+ }
37
+ }
38
+ collect(entryPath, '')
39
+
40
+ const hash = isProduction ? '-[contenthash]' : ''
41
+
42
+ module.exports = {
43
+ mode: isProduction ? 'production' : 'development',
44
+ devtool: isProduction ? 'source-map' : 'cheap-module-source-map',
45
+ context: root,
46
+ entry,
47
+ output: {
48
+ path: outputPath,
49
+ publicPath,
50
+ filename: `[name]${hash}.js`,
51
+ chunkFilename: `[name]${hash}.chunk.js`,
52
+ assetModuleFilename: `media/[name]${hash}[ext]`,
53
+ clean: true
54
+ },
55
+ cache: {
56
+ type: 'filesystem',
57
+ cacheDirectory: path.resolve(root, cfg.cache_path),
58
+ buildDependencies: { config: [__filename] }
59
+ },
60
+ module: {
61
+ rules: [
62
+ {
63
+ test: /\.(js|mjs)$/,
64
+ exclude: /node_modules/,
65
+ use: { loader: 'babel-loader', options: { presets: [['@babel/preset-env', { targets: 'defaults' }]] } }
66
+ },
67
+ { test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] },
68
+ { test: /\.(png|jpe?g|gif|svg|webp|avif|ico|woff2?|eot|ttf|otf)$/i, type: 'asset/resource' }
69
+ ]
70
+ },
71
+ plugins: [
72
+ new MiniCssExtractPlugin({ filename: `[name]${hash}.css`, chunkFilename: `[name]${hash}.chunk.css` }),
73
+ new WebpackAssetsManifest({ output: 'manifest.json', publicPath: true, writeToDisk: true })
74
+ ],
75
+ optimization: {
76
+ minimizer: ['...', new CssMinimizerPlugin()]
77
+ },
78
+ resolve: {
79
+ extensions: cfg.extensions,
80
+ modules: [sourcePath, 'node_modules']
81
+ },
82
+ performance: { hints: false }
83
+ }
@@ -0,0 +1,30 @@
1
+ # Configuration for webpacker-cli. Same layout and key names as Rails' Webpacker.
2
+ default: &default
3
+ source_path: app/javascript
4
+ source_entry_path: packs
5
+ public_root_path: public
6
+ public_output_path: packs
7
+ cache_path: tmp/cache/webpacker
8
+
9
+ # Files with these extensions are resolved when imported without an extension.
10
+ extensions:
11
+ - .js
12
+ - .mjs
13
+ - .json
14
+ - .css
15
+ - .png
16
+ - .jpg
17
+ - .jpeg
18
+ - .gif
19
+ - .svg
20
+ - .woff
21
+ - .woff2
22
+
23
+ development:
24
+ <<: *default
25
+
26
+ test:
27
+ <<: *default
28
+
29
+ production:
30
+ <<: *default
@@ -1,3 +1,3 @@
1
1
  module WebpackerCli
2
- VERSION = "0.9.3"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/webpacker_cli.rb CHANGED
@@ -1,80 +1,156 @@
1
- require 'webpacker_cli/version'
2
- require 'webpacker_cli/rails_template'
3
1
  require 'fileutils'
4
- require 'tty/command'
5
- require 'bundler'
2
+ require 'json'
3
+ require 'open3'
4
+ require 'yaml'
5
+ require 'webpacker_cli/version'
6
6
 
7
+ # webpacker-cli: Webpacker's conventions (webpacker.yml, app/javascript/packs,
8
+ # public/packs/manifest.json) for any framework, without Rails.
9
+ #
10
+ # Ruby only scaffolds the project and launches Webpack; the Webpack setup itself
11
+ # is a plain JavaScript config generated into the project so it can be updated
12
+ # independently of this gem.
7
13
  module WebpackerCli
8
- FILE_MANIFEST = %w[
9
- bin
10
- bin/rails
11
- config
12
- config/application.rb
13
- config/webpack/development.js
14
- config/webpack/environment.js
15
- config/webpack/loaders
16
- config/webpack/production.js
17
- config/webpack/test.js
18
- package.json
19
- Gemfile
20
- Rakefile
21
- ]
14
+ class Error < StandardError; end
22
15
 
23
- class << self
24
- def init(**opts)
25
- dir = opts.fetch(:dir) { Dir.pwd }
26
- test = opts.fetch(:test) { !!ENV['WEBPACKER_CLI'].to_s['TEST'] }
16
+ TEMPLATE_DIR = File.expand_path('webpacker_cli/templates', __dir__)
27
17
 
28
- Dir.chdir(dir) do
29
- File.open('Gemfile', 'w') {|file|
30
- file.write(WebpackerCli::RailsTemplate.gemfile(test))
31
- }
32
- File.open('Rakefile', 'w') {|file| file.write(WebpackerCli::RailsTemplate.rakefile) }
18
+ # Generated file => template it is copied from.
19
+ TEMPLATES = {
20
+ 'config/webpacker.yml' => 'webpacker.yml',
21
+ 'config/webpack/webpack.config.js' => 'webpack.config.js',
22
+ 'app/javascript/packs/application.js' => 'application.js',
23
+ 'package.json' => 'package.json'
24
+ }.freeze
33
25
 
34
- FileUtils.mkdir_p('bin')
35
- File.open('bin/rails', 'w') {|file| file.write(WebpackerCli::RailsTemplate.bin_rails) }
26
+ FILE_MANIFEST = (%w[config config/webpack app/javascript app/javascript/packs] + TEMPLATES.keys).freeze
36
27
 
37
- File.open('package.json', 'w') {|file| file.write(WebpackerCli::RailsTemplate.package_json) }
28
+ WEBPACK_CONFIG = 'config/webpack/webpack.config.js'.freeze
38
29
 
39
- FileUtils.mkdir_p('config/webpack/loaders')
40
- File.open('config/application.rb', 'w') {|file| file.write(WebpackerCli::RailsTemplate.config_application) }
41
-
42
- env = {
43
- BUNDLE_GEMFILE: "#{dir}/Gemfile"
44
- }
30
+ class << self
31
+ # Scaffold a project. Existing files are kept unless force: true.
32
+ def init(dir: Dir.pwd, force: false, install: true, **)
33
+ dir = File.expand_path(dir)
34
+ FileUtils.mkdir_p(dir)
45
35
 
46
- if test
47
- env.update( {
48
- GEM_HOME: ENV["GEM_HOME"],
49
- GEM_PATH: ENV["GEM_PATH"],
50
- })
36
+ TEMPLATES.each do |dest, template|
37
+ path = File.join(dir, dest)
38
+ if File.exist?(path) && !force
39
+ puts " exist #{dest}"
40
+ next
51
41
  end
42
+ FileUtils.mkdir_p(File.dirname(path))
43
+ FileUtils.cp(File.join(TEMPLATE_DIR, template), path)
44
+ puts " create #{dest}"
45
+ end
52
46
 
53
- bundler_env(test) do
54
- TTY::Command.
55
- new(color: false, uuid: false).
56
- run(env, 'bundle install', chdir: dir)
47
+ install_packages(dir) if install
48
+ dir
49
+ end
57
50
 
58
- TTY::Command.
59
- new(color: false, uuid: false).
60
- run(env, "bundle exec #{executable} install", chdir: dir)
61
- end
51
+ def install_packages(dir)
52
+ manager = package_manager(dir)
53
+ run(dir, {}, manager, 'install')
54
+ end
55
+
56
+ def compile(dir: Dir.pwd, env: current_env)
57
+ webpack(dir, env)
58
+ end
59
+
60
+ def watch(dir: Dir.pwd, env: current_env)
61
+ webpack(dir, env, '--watch')
62
+ end
63
+
64
+ # Remove compiled packs and the Webpack cache.
65
+ def clobber(dir: Dir.pwd, env: current_env)
66
+ cfg = settings(dir, env)
67
+ [File.join(cfg['public_root_path'], cfg['public_output_path']), cfg['cache_path']].each do |rel|
68
+ FileUtils.rm_rf(File.join(dir, rel))
69
+ puts " remove #{rel}"
62
70
  end
63
71
  end
64
72
 
65
- def bundler_env(test)
66
- if test
67
- Bundler.with_clean_env { yield }
68
- else
69
- yield
73
+ # Verify prerequisites, returning a list of problems (empty when healthy).
74
+ def check(dir: Dir.pwd)
75
+ problems = []
76
+ problems << 'Node.js was not found in PATH' unless which('node')
77
+ problems << 'Neither yarn nor npm was found in PATH' unless which('yarn') || which('npm')
78
+ problems << "Missing #{WEBPACK_CONFIG}; run `webpacker-cli init`" unless File.exist?(File.join(dir, WEBPACK_CONFIG))
79
+ unless File.exist?(File.join(dir, 'node_modules', '.bin', 'webpack'))
80
+ problems << 'Node packages are not installed; run `webpacker-cli init` or install them yourself'
70
81
  end
82
+ problems
83
+ end
84
+
85
+ def info(dir: Dir.pwd)
86
+ {
87
+ 'webpacker-cli' => VERSION,
88
+ 'ruby' => RUBY_VERSION,
89
+ 'node' => capture('node', '--version', dir: dir),
90
+ 'package manager' => "#{package_manager(dir)} #{capture(package_manager(dir), '--version', dir: dir)}",
91
+ 'webpack' => webpack_version(dir)
92
+ }
71
93
  end
72
94
 
95
+ def current_env
96
+ ENV['WEBPACKER_ENV'] || ENV['NODE_ENV'] || ENV['RAILS_ENV'] || 'development'
97
+ end
98
+
99
+ # Merged `default` + environment section of config/webpacker.yml.
100
+ def settings(dir, env = current_env)
101
+ file = File.join(dir, 'config', 'webpacker.yml')
102
+ raise Error, "Missing config/webpacker.yml; run `webpacker-cli init`" unless File.exist?(file)
103
+ all = YAML.safe_load(File.read(file), aliases: true) || {}
104
+ (all['default'] || {}).merge(all[env] || {})
105
+ end
106
+
107
+ def package_manager(dir)
108
+ return 'npm' if File.exist?(File.join(dir, 'package-lock.json')) && !File.exist?(File.join(dir, 'yarn.lock'))
109
+ return 'yarn' if File.exist?(File.join(dir, 'yarn.lock')) || which('yarn')
110
+ 'npm'
111
+ end
112
+
113
+ # Path of the webpacker-cli executable (kept for integrations such as webpacker-rs).
73
114
  def executable
74
- begin
75
- Gem.bin_path("webpacker_cli", "webpacker-cli")
76
- rescue
77
- File.expand_path('../bin/webpacker-cli', __dir__)
115
+ Gem.bin_path('webpacker_cli', 'webpacker-cli')
116
+ rescue StandardError
117
+ File.expand_path('../bin/webpacker-cli', __dir__)
118
+ end
119
+
120
+ private
121
+
122
+ def webpack(dir, env, *args)
123
+ problems = check(dir: dir)
124
+ raise Error, problems.join("\n") unless problems.empty?
125
+ run(dir, { 'NODE_ENV' => env, 'WEBPACKER_ENV' => env },
126
+ 'node_modules/.bin/webpack', '--config', WEBPACK_CONFIG, *args)
127
+ end
128
+
129
+ def run(dir, env, *cmd)
130
+ success = system(env, *cmd, chdir: dir)
131
+ raise Error, "Command failed: #{cmd.join(' ')}" unless success
132
+ true
133
+ rescue Errno::ENOENT
134
+ raise Error, "Command not found: #{cmd.first}"
135
+ end
136
+
137
+ def webpack_version(dir)
138
+ JSON.parse(File.read(File.join(dir, 'node_modules', 'webpack', 'package.json')))['version']
139
+ rescue StandardError
140
+ 'not found'
141
+ end
142
+
143
+ def capture(*cmd, dir:)
144
+ out, status = Open3.capture2e(*cmd, chdir: dir)
145
+ status.success? ? out.strip : 'not found'
146
+ rescue Errno::ENOENT
147
+ 'not found'
148
+ end
149
+
150
+ def which(cmd)
151
+ ENV['PATH'].to_s.split(File::PATH_SEPARATOR).any? do |p|
152
+ f = File.join(p, cmd)
153
+ File.executable?(f) && !File.directory?(f)
78
154
  end
79
155
  end
80
156
  end
@@ -9,25 +9,22 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ['6ftdan@gmail.com']
10
10
 
11
11
  spec.summary = %q{Bringing Webpacker to any framework.}
12
- spec.description = %q{Bringing the impressive work of the Rails' Webpacker project to be available for other frameworks.}
13
- spec.license = 'LGPL-3.0'
12
+ spec.description = %q{Webpacker's conventions (webpacker.yml, packs, manifest.json) for any framework, powered by Webpack 5 and without Rails.}
13
+ spec.license = 'LGPL-3.0-only'
14
14
  spec.homepage = 'https://github.com/danielpclark/webpacker-cli'
15
- spec.required_ruby_version = '>= 2.2'
15
+ spec.required_ruby_version = '>= 3.0'
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"
16
18
 
17
19
  # Specify which files should be added to the gem when it is released.
18
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
21
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|\.github)/}) }
21
23
  end
22
24
  spec.bindir = 'bin'
23
25
  spec.executables = ['webpacker-cli']
24
26
  spec.require_paths = ['lib']
25
27
 
26
- spec.add_development_dependency 'rake', '>= 11.0'
28
+ spec.add_development_dependency 'rake', '~> 13.0'
27
29
  spec.add_development_dependency 'minitest', '~> 5.11'
28
- spec.add_development_dependency 'minitest-reporters', '~> 1.1'
29
- spec.add_development_dependency 'color_pound_spec_reporter', '~> 0.0.6'
30
- spec.add_dependency 'bundler', '~> 1.16'
31
- spec.add_dependency 'tty-command', '~> 0.8'
32
- spec.add_dependency 'webpacker', '~> 4.0.0.pre.3'
33
30
  end
metadata CHANGED
@@ -1,29 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpacker_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2018-12-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rake
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - ">="
16
+ - - "~>"
18
17
  - !ruby/object:Gem::Version
19
- version: '11.0'
18
+ version: '13.0'
20
19
  type: :development
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - ">="
23
+ - - "~>"
25
24
  - !ruby/object:Gem::Version
26
- version: '11.0'
25
+ version: '13.0'
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: minitest
29
28
  requirement: !ruby/object:Gem::Requirement
@@ -38,78 +37,8 @@ dependencies:
38
37
  - - "~>"
39
38
  - !ruby/object:Gem::Version
40
39
  version: '5.11'
41
- - !ruby/object:Gem::Dependency
42
- name: minitest-reporters
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.1'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.1'
55
- - !ruby/object:Gem::Dependency
56
- name: color_pound_spec_reporter
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: 0.0.6
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: 0.0.6
69
- - !ruby/object:Gem::Dependency
70
- name: bundler
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '1.16'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '1.16'
83
- - !ruby/object:Gem::Dependency
84
- name: tty-command
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '0.8'
90
- type: :runtime
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '0.8'
97
- - !ruby/object:Gem::Dependency
98
- name: webpacker
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: 4.0.0.pre.3
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: 4.0.0.pre.3
111
- description: Bringing the impressive work of the Rails' Webpacker project to be available
112
- for other frameworks.
40
+ description: Webpacker's conventions (webpacker.yml, packs, manifest.json) for any
41
+ framework, powered by Webpack 5 and without Rails.
113
42
  email:
114
43
  - 6ftdan@gmail.com
115
44
  executables:
@@ -118,7 +47,7 @@ extensions: []
118
47
  extra_rdoc_files: []
119
48
  files:
120
49
  - ".gitignore"
121
- - ".travis.yml"
50
+ - CHANGELOG.md
122
51
  - Gemfile
123
52
  - LICENSE
124
53
  - README.md
@@ -127,14 +56,18 @@ files:
127
56
  - bin/setup
128
57
  - bin/webpacker-cli
129
58
  - lib/webpacker_cli.rb
130
- - lib/webpacker_cli/rails_template.rb
59
+ - lib/webpacker_cli/templates/application.js
60
+ - lib/webpacker_cli/templates/package.json
61
+ - lib/webpacker_cli/templates/webpack.config.js
62
+ - lib/webpacker_cli/templates/webpacker.yml
131
63
  - lib/webpacker_cli/version.rb
132
64
  - webpacker_cli.gemspec
133
65
  homepage: https://github.com/danielpclark/webpacker-cli
134
66
  licenses:
135
- - LGPL-3.0
136
- metadata: {}
137
- post_install_message:
67
+ - LGPL-3.0-only
68
+ metadata:
69
+ source_code_uri: https://github.com/danielpclark/webpacker-cli
70
+ changelog_uri: https://github.com/danielpclark/webpacker-cli/blob/master/CHANGELOG.md
138
71
  rdoc_options: []
139
72
  require_paths:
140
73
  - lib
@@ -142,16 +75,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
75
  requirements:
143
76
  - - ">="
144
77
  - !ruby/object:Gem::Version
145
- version: '2.2'
78
+ version: '3.0'
146
79
  required_rubygems_version: !ruby/object:Gem::Requirement
147
80
  requirements:
148
81
  - - ">="
149
82
  - !ruby/object:Gem::Version
150
83
  version: '0'
151
84
  requirements: []
152
- rubyforge_project:
153
- rubygems_version: 2.7.7
154
- signing_key:
85
+ rubygems_version: 3.6.9
155
86
  specification_version: 4
156
87
  summary: Bringing Webpacker to any framework.
157
88
  test_files: []
data/.travis.yml DELETED
@@ -1,25 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.3.8
4
- - 2.4.5
5
- - 2.5.3
6
-
7
- os:
8
- - linux
9
- - osx
10
-
11
- cache:
12
- bundler: true
13
-
14
- before_install:
15
- - ruby --version
16
- - ruby -e 'require "pp"; pp RbConfig::CONFIG'
17
-
18
- install:
19
- - nvm install node
20
- - node -v
21
- - npm i -g yarn
22
-
23
- script:
24
- - bundle install
25
- - bundle exec rake test
@@ -1,74 +0,0 @@
1
- module WebpackerCli
2
- module RailsTemplate
3
- class << self
4
- def gemfile(test = false)
5
- %Q$source 'https://rubygems.org'
6
- git_source(:github) { |repo| "https://github.com/\#{repo}.git" }
7
-
8
- gem 'rake', '>= 11'
9
- gem 'webpacker_cli', '#{WebpackerCli::VERSION}'#{
10
- if test; ", path: '#{File.expand_path('../', __dir__)}'" end
11
- }
12
- $ + gemspec_deps
13
- end
14
-
15
- def rakefile
16
- %q$require_relative "config/application"
17
-
18
- Rails.application.load_tasks$
19
- end
20
-
21
- def config_application
22
- %q$require "action_controller/railtie"
23
- require "action_view/railtie"
24
- require "webpacker"
25
-
26
- module TestApp
27
- class Application < ::Rails::Application
28
- config.secret_key_base = "abcdef"
29
- config.eager_load = true
30
- config.webpacker.check_yarn_integrity = false
31
- end
32
- end$
33
- end
34
-
35
- def package_json
36
- %q${
37
- "name": "application",
38
- "private": true,
39
- "dependencies": {
40
- "@rails/webpacker": "3.5"
41
- },
42
- "devDependencies": {
43
- "webpack-dev-server": "2.11.2"
44
- }
45
- }$
46
- end
47
-
48
- def bin_rails
49
- %q$#!/usr/bin/env ruby
50
- APP_PATH = File.expand_path('../config/application', __dir__)
51
- require 'rails/commands'$
52
- end
53
-
54
- def gemspec_deps
55
- gemspec.runtime_dependencies.
56
- map {|i| "gem '#{i.name}', '#{i.requirement.requirements.join(' ')}'"}.
57
- join("\n")
58
- end
59
-
60
- def gemspec
61
- begin
62
- Gem::Specification.find_by_name "webpacker_cli"
63
- rescue
64
- local_spec = File.expand_path("../../webpacker_cli.gemspec", __dir__)
65
-
66
- raise "Gemspec not found for 'webpacker_cli'!" unless File.exist? local_spec
67
-
68
- eval File.read(local_spec)
69
- end
70
- end
71
- end
72
- end
73
- end
74
-