Class: Middleman::Cli::Webpack

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/middleman-webpacked/commands/webpack.rb

Constant Summary collapse

BABEL_TEMPLATE =
File.expand_path('../../template/babelrc.tt', __FILE__)
SHARED_WEBPACK_TEMPLATE =
File.expand_path('../../template/shared.webpack.tt', __FILE__)
DEVELOPMENT_WEBPACK_TEMPLATE =
File.expand_path('../../template/dev.webpack.tt', __FILE__)
PRODUCTION_WEBPACK_TEMPLATE =
File.expand_path('../../template/prod.webpack.tt', __FILE__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Webpack

Returns a new instance of Webpack.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/middleman-webpacked/commands/webpack.rb', line 29

def initialize(*args)
  super

  @app = ::Middleman::Application.new do
    config[ :mode ]              = :config
    config[ :disable_sitemap ]   = true
    config[ :watcher_disable ]   = true
    config[ :exit_before_ready ] = true
  end

  @packages = default_packages
end

Class Method Details

.source_rootObject



25
26
27
# File 'lib/middleman-webpacked/commands/webpack.rb', line 25

def self.source_root
  File.expand_path('../../template', __FILE__)
end

Instance Method Details

#enable_reactObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/middleman-webpacked/commands/webpack.rb', line 61

def enable_react
  @packages.push(
    'babel-preset-react',
    'react',
    'react-dom',
    'react-hot-loader'
  )
  @presets = ['env', 'react']
  @loaders.push({
    test: /\.(js|jsx)$/,
    use: 'babel-loader'
  })
end

#enable_vueObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/middleman-webpacked/commands/webpack.rb', line 75

def enable_vue
  @packages.push(
    'vue',
    'babel-preset-es2015',
    'vue-loader',
    'vue-template-compiler'
  )
  @presets = ['env', 'es2015']
  @loaders.push({
    test: /\.vue$/,
    use: 'vue-loader',
  },{
    test: /\.js$/,
    use: 'babel-loader'
  })
end

#generate_configObject



54
55
56
57
58
59
# File 'lib/middleman-webpacked/commands/webpack.rb', line 54

def generate_config
  template BABEL_TEMPLATE, File.join(@app.root_path, '.babelrc')
  template SHARED_WEBPACK_TEMPLATE, File.join(@app.root_path, 'config', 'webpack', 'shared.js')
  template DEVELOPMENT_WEBPACK_TEMPLATE, File.join(@app.root_path, 'config', 'webpack', 'development.js')
  template PRODUCTION_WEBPACK_TEMPLATE, File.join(@app.root_path, 'config', 'webpack', 'production.js')
end

#webpackObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/middleman-webpacked/commands/webpack.rb', line 42

def webpack
  @presets = ['env']
  @plugins = []
  @loaders = []

  enable_react if options[:react]
  enable_vue if options[:vue]

  generate_config
  run "yarn add #{@packages.join(' ')} --dev"
end