Skip to content

Commit 89d2499

Browse files
committed
updated to 0.1.6
1 parent c7ac603 commit 89d2499

File tree

11 files changed

+538
-176
lines changed

11 files changed

+538
-176
lines changed

README.md

Lines changed: 405 additions & 118 deletions
Large diffs are not rendered by default.

lib/helpers/lib/cmd.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Cmd
22
def self.run(cmd)
3-
puts cmd
3+
STDERR.puts cmd
44
system(cmd)
55
end
66
end

lib/helpers/scripts/vue_command.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require_relative '../lib'
2+
3+
class VueCommand
4+
SASS = %w[sass-loader node-sass].freeze
5+
6+
SUPPORED_FORMATS = {
7+
'pug' => %w[pug-plain-loader pug],
8+
'sass' => SASS,
9+
'scss' => SASS,
10+
'less' => %w[less-loader less],
11+
'stylus' => %w[stylus-loader stylus],
12+
}.freeze
13+
14+
def initialize
15+
@pm = VueCli::Rails::Configuration.instance.node_env
16+
end
17+
18+
def install_format_support(formats)
19+
pkgs, unknown = group_formats(formats)
20+
if pkgs.empty?
21+
msg = unknown.empty? ? 'No formats supplied' : "Unsupported formats #{unknown}"
22+
raise(ArgumentError, msg)
23+
end
24+
25+
STDERR.puts "Unsupported formats #{unknown}" if unknown.any?
26+
@pm.add "-D #{pkgs.join(' ')}"
27+
end
28+
29+
def install_node_dev
30+
pack_json = ::Rails.root.join('package.json')
31+
abort('Not found package.json!') unless pack_json.exist? && pack_json.file?
32+
33+
add_deps(pack_json, %w[cross-env npm-run-all])
34+
add_scripts(pack_json,
35+
dev: 'run-p rails-s serve',
36+
prod: 'cross-env RAILS_ENV=production vue-cli-service build',
37+
serve: 'vue-cli-service serve',
38+
'rails-s' => 'cross-env NO_WEBPACK_DEV_SERVER=1 rails s')
39+
puts 'Dependencies and scripts have been installed successfully'
40+
cmd = @pm.package_manager == :npm ? 'npm run' : 'yarn'
41+
puts " Please use `#{cmd} dev` to start dev server"
42+
end
43+
44+
private
45+
46+
def add_deps(package_json, *packages, dev: true)
47+
json = JSON.parse(package_json.read)
48+
deps = json[dev ? 'devDependencies' : 'dependencies']
49+
pkgs = [packages].flatten.find_all do |dep|
50+
!(dep.blank? || deps.key?(dep))
51+
end
52+
@pm.add "#{dev ? '-D ' : ''}#{pkgs.join(' ')}" if pkgs.any?
53+
end
54+
55+
def add_scripts(package_json, commands = {})
56+
json = JSON.parse(package_json.read)
57+
scripts = json['scripts']
58+
commands.stringify_keys.each do |key, cmd|
59+
scripts[key] = cmd unless scripts.key? key
60+
end
61+
package_json.write(JSON.pretty_generate(json))
62+
end
63+
64+
def group_formats(formats)
65+
formats.each_with_object([[], []]) do |result, fmt|
66+
fmts = SUPPORED_FORMATS[fmt.downcase]
67+
if fmts
68+
result[0] += fmts
69+
else
70+
result[1] << fmt
71+
end
72+
end
73+
end
74+
end

lib/source/vue.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = {
5454
/* [DO NOT EDIT!] end */
5555

5656
/* put your custom code here
57-
// Example: yarn -D compression-webpack-plugin webpack-bundle-analyzer
57+
// Example: npm/yarn add -D compression-webpack-plugin webpack-bundle-analyzer
5858
if (isProd) {
5959
config
6060
.plugin('compression')

lib/source/vue.rails.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (() => {
1313
const railsEnv = env.RAILS_ENV || 'development';
1414
const config = yaml.safeLoad(readFileSync(resolve('config/vue.yml'), 'utf8'))[railsEnv];
1515
const root = resolve(__dirname);
16-
const po = (config.public_output_path || 'vue_assets').replace(/(^\/+|\/+$)/g, '');
16+
const pop = (config.public_output_path || 'vue_assets').replace(/(^\/+|\/+$)/g, '');
1717
const {
1818
manifest_output: manifestOutput,
1919
js_output: output,
@@ -55,17 +55,17 @@ module.exports = (() => {
5555
root,
5656
manifestOutput: resolve(root, manifestOutput),
5757

58-
outputDir: resolve(root, 'public', po),
59-
publicPath: `/${po}/`,
58+
outputDir: resolve(root, 'public', pop),
59+
publicPath: `/${pop}/`,
6060
configureWebpack: {
6161
entry,
62+
output,
6263
resolve: {
6364
alias: Object.keys(alias).reduce((obj, key) => ({
6465
...obj,
6566
[key]: resolve(root, alias[key]),
6667
}), {}),
6768
},
68-
output,
6969
},
7070

7171
devServer,
@@ -89,27 +89,25 @@ module.exports = (() => {
8989
encoding: 'utf8',
9090
}));
9191
}
92-
const isProd = settings.env === 'production';
93-
const manifest = settings.manifestOutput ? {
94-
/* eslint-disable-next-line global-require,import/no-extraneous-dependencies */
95-
plugin: require('webpack-assets-manifest'),
96-
options: {
97-
entrypoints: true,
98-
writeToDisk: true,
99-
publicPath: true,
100-
output: settings.manifestOutput,
101-
},
102-
} : null;
10392

104-
const getSettingsFromKeys = keys => keys.filter(s => s)
93+
const getSettingsFromKeys = keys => [].concat(keys).filter(s => s)
10594
.reduce((cfg, k) => {
10695
const v = settings[k];
10796
return v === undefined ? cfg : { ...cfg, [k]: v };
10897
}, {});
10998

11099
return {
111-
manifest,
112-
isProd,
100+
isProd: settings.env === 'production',
101+
manifest: {
102+
/* eslint-disable-next-line global-require,import/no-extraneous-dependencies */
103+
plugin: require('webpack-assets-manifest'),
104+
options: {
105+
entrypoints: true,
106+
writeToDisk: true,
107+
publicPath: true,
108+
output: settings.manifestOutput,
109+
},
110+
},
113111
getSettings: (keys = Object.keys(settings)) => getSettingsFromKeys(keys),
114112
pickUpSettings: ([lines]) => getSettingsFromKeys(lines.split('\n').map(s => s.trim())),
115113
};

lib/source/vue.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
default: &default
22
package_manager: ${PACKAGE_MANAGER}
3-
manifest_output: false
3+
manifest_output: tmp/manifest.json
44
public_output_path: vue_assets
55
# js_output:
66
# filename: 'js/[name].[hash:8].js'
@@ -29,5 +29,4 @@ production:
2929
test:
3030
<<: *default
3131
# launch_dev_service: vue-cli-service serve
32-
# manifest_output: tmp/manifest.json
3332
# filenameHashing: false

lib/tasks/vue.rake

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,33 @@ namespace :vue do
55
VueCreate.run!
66
end
77

8-
desc 'Add pug template support: formats=pug,sass,less,stylus'
8+
desc 'Add template/style support: formats=pug,sass,less,stylus'
99
task :support, [:formats] do |_t, args|
10-
pkgs = []
11-
args.formats.split(/\W/).each do |fmt|
12-
pkgs += case fmt
13-
when 'pug'
14-
%w[pug-plain-loader pug]
15-
when 'sass', 'scss'
16-
%w[sass-loader node-sass]
17-
when 'less'
18-
%w[less-loader less]
19-
when 'stylus'
20-
%w[stylus-loader stylus]
21-
else
22-
[]
23-
end
24-
end
25-
throw(StandardError, '') if pkgs.empty?
26-
27-
pm = VueCli::Rails::Configuration.instance.node_env
28-
pm.add "-D #{pkgs.join(' ')}"
10+
require_relative '../helpers/scripts/vue_command'
11+
VueCommand.new.install_format_support(args.formats&.split(/\W/))
2912
end
3013

3114
desc 'Dump config/vue.yml to JSON: set [js] to get result from vue.rails.js'
3215
task :json_config, [:from] => :environment do |_t, args|
3316
if args.from == 'js'
34-
cmd = <<~CMD
35-
node -e "console.log(JSON.stringify(require('./vue.rails.js').getSettings(), null, 2))"
36-
CMD
37-
puts "RUN: #{cmd}"
38-
system(cmd)
17+
require_relative '../helpers/lib/cmd'
18+
Cmd.run VueCli::Rails::Configuration::JS_CONFIG_CMD
3919
else
4020
config = VueCli::Rails::Configuration.new
4121
puts config.to_json
4222
end
4323
end
4424

45-
desc 'Build assets'
46-
task compile: :environment do
25+
desc 'Build assets: set [with_rails_assets] to invoke assets:precompile as well'
26+
task :compile, [:with_rails_assets] => :environment do |_t, args|
4727
pm = VueCli::Rails::Configuration.instance.node_env
4828
pm.exec('vue-cli-service build', env: { 'RAILS_ENV' => ::Rails.env })
29+
::Rake::Task['assets:precompile'].invoke if args.with_rails_assets
30+
end
31+
32+
desc 'Install Node way to run Rails dev server alongside webpack-dev-server'
33+
task node_dev: :environment do
34+
require_relative '../helpers/scripts/vue_command'
35+
VueCommand.new.install_node_dev
4936
end
5037
end

lib/vue_cli/rails/configuration.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def node_env
1414
end
1515
end
1616

17+
JS_CONFIG_CMD = %{
18+
node -e "console.log(JSON.stringify(require('./vue.rails.js').getSettings(), null, 2))"
19+
}.strip.freeze
20+
1721
def load_config(config)
1822
config = config[::Rails.env]
1923
c = {
@@ -29,7 +33,7 @@ def load_config(config)
2933
c['root'] = @root.to_s
3034
cw['output'] = config['js_output'] if config['js_output'].present?
3135
c['manifestOutput'] = config['manifest_output']
32-
if ::Rails.env.production? && c['manifestOutput'].blank?
36+
unless c['manifestOutput'].presence
3337
raise(Error, 'Incorrect manifest_output in config/vue.yml')
3438
end
3539

lib/vue_cli/rails/engine.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
module VueCli
22
module Rails
33
require 'vue_cli/rails/helper'
4-
USE_PROXY_MIDDLEWARE = ::Rails.env.development? && defined?(::Rails::Server)
5-
require 'vue_cli/rails/dev_server_proxy' if USE_PROXY_MIDDLEWARE
64

75
class Engine < ::Rails::Engine
86
initializer 'vue_cli' do |app|
9-
if USE_PROXY_MIDDLEWARE
7+
if ::Rails.env.development? && defined?(::Rails::Server)
8+
require 'vue_cli/rails/dev_server_proxy'
109
app.middleware.insert_before 0, DevServerProxy
11-
fork do
12-
config = Configuration.instance
13-
config.node_env.exec(config['launch_dev_service'] || 'vue-cli-service serve')
14-
end
10+
Engine.start_wds! if ENV['NO_WEBPACK_DEV_SERVER'].blank?
1511
end
1612

1713
::ActiveSupport.on_load :action_controller do
@@ -22,6 +18,19 @@ class Engine < ::Rails::Engine
2218
include Helper
2319
end
2420
end
21+
22+
def self.start_wds!
23+
fork do
24+
config = Configuration.instance
25+
port = config['devServer']&.dig('port')
26+
if port
27+
running = `lsof -i:#{port} -sTCP:LISTEN -Pn`&.chop.presence&.split("\n")
28+
pid = running&.dig(1)&.split(/\s+/, 3)&.dig(1)
29+
Process.kill('INT', pid.to_i) if pid.present?
30+
end
31+
config.node_env.exec(config['launch_dev_service'] || 'vue-cli-service serve')
32+
end
33+
end
2534
end
2635
end
2736
end

lib/vue_cli/rails/node_env.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def exec(command, args = nil, extra = nil, env: {})
5151
end
5252

5353
COMMAND_LINE = {
54+
add: {
55+
yarn: 'yarn add',
56+
npm: 'npm i',
57+
},
5458
global_add: {
5559
yarn: 'yarn global add',
5660
npm: 'npm i -g',

0 commit comments

Comments
 (0)