webpacker-legacy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +15 -0
  2. data/.eslintrc.js +14 -0
  3. data/.gitignore +4 -0
  4. data/.rubocop.yml +124 -0
  5. data/.travis.yml +22 -0
  6. data/CHANGELOG.md +148 -0
  7. data/Gemfile +11 -0
  8. data/Gemfile.lock +137 -0
  9. data/MIT-LICENSE +20 -0
  10. data/README.md +1118 -0
  11. data/Rakefile +12 -0
  12. data/lib/install/angular.rb +22 -0
  13. data/lib/install/bin/webpack-dev-server.tt +43 -0
  14. data/lib/install/bin/webpack.tt +27 -0
  15. data/lib/install/config/.babelrc +17 -0
  16. data/lib/install/config/.postcssrc.yml +3 -0
  17. data/lib/install/config/loaders/core/assets.js +12 -0
  18. data/lib/install/config/loaders/core/babel.js +5 -0
  19. data/lib/install/config/loaders/core/coffee.js +4 -0
  20. data/lib/install/config/loaders/core/erb.js +9 -0
  21. data/lib/install/config/loaders/core/sass.js +15 -0
  22. data/lib/install/config/loaders/installers/angular.js +4 -0
  23. data/lib/install/config/loaders/installers/elm.js +20 -0
  24. data/lib/install/config/loaders/installers/react.js +5 -0
  25. data/lib/install/config/loaders/installers/vue.js +41 -0
  26. data/lib/install/config/webpack/configuration.js +35 -0
  27. data/lib/install/config/webpack/development.js +32 -0
  28. data/lib/install/config/webpack/production.js +35 -0
  29. data/lib/install/config/webpack/shared.js +58 -0
  30. data/lib/install/config/webpack/test.js +6 -0
  31. data/lib/install/config/webpacker.yml +38 -0
  32. data/lib/install/elm.rb +33 -0
  33. data/lib/install/examples/angular/hello_angular.js +7 -0
  34. data/lib/install/examples/angular/hello_angular/app/app.component.ts +9 -0
  35. data/lib/install/examples/angular/hello_angular/app/app.module.ts +16 -0
  36. data/lib/install/examples/angular/hello_angular/index.ts +6 -0
  37. data/lib/install/examples/angular/hello_angular/polyfills.ts +19 -0
  38. data/lib/install/examples/angular/tsconfig.json +19 -0
  39. data/lib/install/examples/elm/Main.elm +54 -0
  40. data/lib/install/examples/elm/hello_elm.js +11 -0
  41. data/lib/install/examples/react/.babelrc +6 -0
  42. data/lib/install/examples/react/hello_react.jsx +26 -0
  43. data/lib/install/examples/vue/app.vue +22 -0
  44. data/lib/install/examples/vue/hello_vue.js +15 -0
  45. data/lib/install/javascript/packs/application.js +10 -0
  46. data/lib/install/react.rb +36 -0
  47. data/lib/install/template.rb +44 -0
  48. data/lib/install/vue.rb +19 -0
  49. data/lib/tasks/installers.rake +22 -0
  50. data/lib/tasks/webpacker.rake +19 -0
  51. data/lib/tasks/webpacker/check_node.rake +20 -0
  52. data/lib/tasks/webpacker/check_webpack_binstubs.rake +11 -0
  53. data/lib/tasks/webpacker/check_yarn.rake +15 -0
  54. data/lib/tasks/webpacker/clobber.rake +17 -0
  55. data/lib/tasks/webpacker/compile.rake +38 -0
  56. data/lib/tasks/webpacker/install.rake +23 -0
  57. data/lib/tasks/webpacker/verify_install.rake +18 -0
  58. data/lib/tasks/webpacker/yarn_install.rake +6 -0
  59. data/lib/webpacker-legacy.rb +2 -0
  60. data/lib/webpacker.rb +40 -0
  61. data/lib/webpacker/compiler.rb +20 -0
  62. data/lib/webpacker/configuration.rb +60 -0
  63. data/lib/webpacker/env.rb +23 -0
  64. data/lib/webpacker/file_loader.rb +24 -0
  65. data/lib/webpacker/helper.rb +62 -0
  66. data/lib/webpacker/manifest.rb +51 -0
  67. data/lib/webpacker/railtie.rb +18 -0
  68. data/lib/webpacker/version.rb +3 -0
  69. data/package.json +31 -0
  70. data/test/configuration_test.rb +32 -0
  71. data/test/env_test.rb +14 -0
  72. data/test/helper_test.rb +23 -0
  73. data/test/manifest_test.rb +30 -0
  74. data/test/test_app/config/secrets.yml +5 -0
  75. data/test/test_app/public/packs/manifest.json +4 -0
  76. data/test/webpacker_test.rb +15 -0
  77. data/webpacker.gemspec +23 -0
  78. data/yarn.lock +1014 -0
  79. metadata +200 -0
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ t.verbose = true
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+
3
+ require "webpacker/configuration"
4
+
5
+ current_dir = File.dirname(__FILE__)
6
+
7
+ puts "Copying angular loader to config/webpack/loaders"
8
+ copy_file "#{current_dir}/config/loaders/installers/angular.js", "config/webpack/loaders/angular.js"
9
+
10
+ puts "Copying angular example entry file to #{Webpacker::Configuration.entry_path}"
11
+ copy_file "#{current_dir}/examples/angular/hello_angular.js", "#{Webpacker::Configuration.entry_path}/hello_angular.js"
12
+
13
+ puts "Copying hello_angular app to #{Webpacker::Configuration.source_path}"
14
+ directory "#{current_dir}/examples/angular/hello_angular", "#{Webpacker::Configuration.source_path}/hello_angular"
15
+
16
+ puts "Copying tsconfig.json to the Rails root directory for typescript"
17
+ copy_file "#{current_dir}/examples/angular/tsconfig.json", "tsconfig.json"
18
+
19
+ puts "Installing all angular dependencies"
20
+ run "yarn add typescript ts-loader core-js zone.js rxjs @angular/core @angular/common @angular/compiler @angular/platform-browser @angular/platform-browser-dynamic"
21
+
22
+ puts "Webpacker now supports angular and typescript 🎉"
@@ -0,0 +1,43 @@
1
+ <%= shebang %>
2
+ $stdout.sync = true
3
+
4
+ require "shellwords"
5
+ require "yaml"
6
+
7
+ ENV["RAILS_ENV"] ||= "development"
8
+ RAILS_ENV = ENV["RAILS_ENV"]
9
+
10
+ ENV["NODE_ENV"] ||= RAILS_ENV
11
+ NODE_ENV = ENV["NODE_ENV"]
12
+
13
+ APP_PATH = File.expand_path("../", File.dirname(__FILE__))
14
+ CONFIG_FILE = File.join(APP_PATH, "config/webpacker.yml")
15
+ NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
16
+ WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/development.js")
17
+
18
+ def args(key)
19
+ index = ARGV.index(key)
20
+ index ? ARGV[index + 1] : nil
21
+ end
22
+
23
+ begin
24
+ dev_server = YAML.load_file(CONFIG_FILE)["development"]["dev_server"]
25
+
26
+ DEV_SERVER_HOST = "http#{"s" if args('--https') || dev_server["https"]}://#{args('--host') || dev_server["host"]}:#{args('--port') || dev_server["port"]}"
27
+
28
+ rescue Errno::ENOENT, NoMethodError
29
+ puts "Webpack dev_server configuration not found in #{CONFIG_FILE}."
30
+ puts "Please run bundle exec rails webpacker:install to install webpacker"
31
+ exit!
32
+ end
33
+
34
+ newenv = {
35
+ "NODE_PATH" => NODE_MODULES_PATH.shellescape,
36
+ "ASSET_HOST" => DEV_SERVER_HOST.shellescape
37
+ }.freeze
38
+
39
+ cmdline = ["yarn", "run", "webpack-dev-server", "--", "--progress", "--color", "--config", WEBPACK_CONFIG] + ARGV
40
+
41
+ Dir.chdir(APP_PATH) do
42
+ exec newenv, *cmdline
43
+ end
@@ -0,0 +1,27 @@
1
+ <%= shebang %>
2
+ $stdout.sync = true
3
+
4
+ require "shellwords"
5
+
6
+ ENV["RAILS_ENV"] ||= "development"
7
+ RAILS_ENV = ENV["RAILS_ENV"]
8
+
9
+ ENV["NODE_ENV"] ||= RAILS_ENV
10
+ NODE_ENV = ENV["NODE_ENV"]
11
+
12
+ APP_PATH = File.expand_path("../", File.dirname(__FILE__))
13
+ NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
14
+ WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js")
15
+
16
+ unless File.exist?(WEBPACK_CONFIG)
17
+ puts "Webpack configuration not found."
18
+ puts "Please run bundle exec rails webpacker:install to install webpacker"
19
+ exit!
20
+ end
21
+
22
+ newenv = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
23
+ cmdline = ["yarn", "run", "webpack", "--", "--config", WEBPACK_CONFIG] + ARGV
24
+
25
+ Dir.chdir(APP_PATH) do
26
+ exec newenv, *cmdline
27
+ end
@@ -0,0 +1,17 @@
1
+ {
2
+ "presets": [
3
+ ["env", {
4
+ "modules": false,
5
+ "targets": {
6
+ "browsers": "> 1%",
7
+ "uglify": true
8
+ },
9
+ "useBuiltIns": true
10
+ }]
11
+ ],
12
+
13
+ "plugins": [
14
+ "syntax-dynamic-import",
15
+ ["transform-class-properties", { "spec": true }]
16
+ ]
17
+ }
@@ -0,0 +1,3 @@
1
+ plugins:
2
+ postcss-smart-import: {}
3
+ postcss-cssnext: {}
@@ -0,0 +1,12 @@
1
+ const { env, output } = require('../configuration.js')
2
+
3
+ module.exports = {
4
+ test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
5
+ use: [{
6
+ loader: 'file-loader',
7
+ options: {
8
+ publicPath: output.publicPath,
9
+ name: env.NODE_ENV === 'production' ? '[name]-[hash].[ext]' : '[name].[ext]'
10
+ }
11
+ }]
12
+ }
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ test: /\.js(\.erb)?$/,
3
+ exclude: /node_modules/,
4
+ loader: 'babel-loader'
5
+ }
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ test: /\.coffee(\.erb)?$/,
3
+ loader: 'coffee-loader'
4
+ }
@@ -0,0 +1,9 @@
1
+ module.exports = {
2
+ test: /\.erb$/,
3
+ enforce: 'pre',
4
+ exclude: /node_modules/,
5
+ loader: 'rails-erb-loader',
6
+ options: {
7
+ runner: 'bin/rails runner'
8
+ }
9
+ }
@@ -0,0 +1,15 @@
1
+ const ExtractTextPlugin = require('extract-text-webpack-plugin')
2
+ const { env } = require('../configuration.js')
3
+
4
+ module.exports = {
5
+ test: /\.(scss|sass|css)$/i,
6
+ use: ExtractTextPlugin.extract({
7
+ fallback: 'style-loader',
8
+ use: [
9
+ { loader: 'css-loader', options: { minimize: env.NODE_ENV === 'production' } },
10
+ { loader: 'postcss-loader', options: { sourceMap: true } },
11
+ 'resolve-url-loader',
12
+ { loader: 'sass-loader', options: { sourceMap: true } }
13
+ ]
14
+ })
15
+ }
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ test: /.ts$/,
3
+ loader: 'ts-loader'
4
+ }
@@ -0,0 +1,20 @@
1
+ const path = require('path')
2
+ const { env } = require('../configuration.js')
3
+
4
+ const elmSource = path.resolve(process.cwd())
5
+ const elmMake = `${elmSource}/node_modules/.bin/elm-make`
6
+ const elmDefaultOptions = `cwd=${elmSource}&pathToMake=${elmMake}`
7
+
8
+ const loaderOptions = () => {
9
+ if (env.NODE_ENV === 'production') {
10
+ return `elm-webpack-loader?${elmDefaultOptions}`
11
+ }
12
+
13
+ return `elm-hot-loader!elm-webpack-loader?${elmDefaultOptions}&verbose=true&warn=true&debug=true`
14
+ }
15
+
16
+ module.exports = {
17
+ test: /\.elm$/,
18
+ exclude: [/elm-stuff/, /node_modules/],
19
+ loader: loaderOptions()
20
+ }
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ test: /\.(js|jsx)?(\.erb)?$/,
3
+ exclude: /node_modules/,
4
+ loader: 'babel-loader'
5
+ }
@@ -0,0 +1,41 @@
1
+ const ExtractTextPlugin = require('extract-text-webpack-plugin')
2
+ const { env } = require('../configuration.js')
3
+
4
+ // Change it to false if you prefer Vue styles to be inlined by javascript in runtime
5
+ const extractStyles = false
6
+
7
+ const cssLoader = [
8
+ { loader: 'css-loader', options: { minimize: env.NODE_ENV === 'production' } },
9
+ { loader: 'postcss-loader', options: { sourceMap: true } },
10
+ 'resolve-url-loader'
11
+ ]
12
+ const sassLoader = cssLoader.concat([
13
+ { loader: 'sass-loader', options: { sourceMap: true, indentedSyntax: true } }
14
+ ])
15
+ const scssLoader = cssLoader.concat([
16
+ { loader: 'sass-loader', options: { sourceMap: true } }
17
+ ])
18
+
19
+ function vueStyleLoader(loader) {
20
+ if (extractStyles) {
21
+ return ExtractTextPlugin.extract({
22
+ fallback: 'vue-style-loader',
23
+ use: loader
24
+ })
25
+ }
26
+ return ['vue-style-loader'].concat(loader)
27
+ }
28
+
29
+ module.exports = {
30
+ test: /.vue$/,
31
+ loader: 'vue-loader',
32
+ options: {
33
+ loaders: {
34
+ js: 'babel-loader',
35
+ file: 'file-loader',
36
+ css: vueStyleLoader(cssLoader),
37
+ scss: vueStyleLoader(scssLoader),
38
+ sass: vueStyleLoader(sassLoader)
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,35 @@
1
+ // Common configuration for webpacker loaded from config/webpacker.yml
2
+
3
+ const { join, resolve } = require('path')
4
+ const { env } = require('process')
5
+ const { safeLoad } = require('js-yaml')
6
+ const { readFileSync } = require('fs')
7
+
8
+ const configPath = resolve('config', 'webpacker.yml')
9
+ const loadersDir = join(__dirname, 'loaders')
10
+ const settings = safeLoad(readFileSync(configPath), 'utf8')[env.NODE_ENV]
11
+
12
+ function removeOuterSlashes(string) {
13
+ return string.replace(/^\/*/, '').replace(/\/*$/, '')
14
+ }
15
+
16
+ function formatPublicPath(host = '', path = '') {
17
+ let formattedHost = removeOuterSlashes(host)
18
+ if (formattedHost && !/^http/i.test(formattedHost)) {
19
+ formattedHost = `//${formattedHost}`
20
+ }
21
+ const formattedPath = removeOuterSlashes(path)
22
+ return `${formattedHost}/${formattedPath}/`
23
+ }
24
+
25
+ const output = {
26
+ path: resolve('public', settings.public_output_path),
27
+ publicPath: formatPublicPath(env.ASSET_HOST, settings.public_output_path)
28
+ }
29
+
30
+ module.exports = {
31
+ settings,
32
+ env,
33
+ loadersDir,
34
+ output
35
+ }
@@ -0,0 +1,32 @@
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ const merge = require('webpack-merge')
4
+ const sharedConfig = require('./shared.js')
5
+ const { settings, output } = require('./configuration.js')
6
+
7
+ module.exports = merge(sharedConfig, {
8
+ devtool: 'cheap-eval-source-map',
9
+
10
+ stats: {
11
+ errorDetails: true
12
+ },
13
+
14
+ output: {
15
+ pathinfo: true
16
+ },
17
+
18
+ devServer: {
19
+ clientLogLevel: 'none',
20
+ https: settings.dev_server.https,
21
+ host: settings.dev_server.host,
22
+ port: settings.dev_server.port,
23
+ contentBase: output.path,
24
+ publicPath: output.publicPath,
25
+ compress: true,
26
+ headers: { 'Access-Control-Allow-Origin': '*' },
27
+ historyApiFallback: true,
28
+ watchOptions: {
29
+ ignored: /node_modules/
30
+ }
31
+ }
32
+ })
@@ -0,0 +1,35 @@
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ /* eslint global-require: 0 */
4
+
5
+ const webpack = require('webpack')
6
+ const merge = require('webpack-merge')
7
+ const CompressionPlugin = require('compression-webpack-plugin')
8
+ const sharedConfig = require('./shared.js')
9
+
10
+ module.exports = merge(sharedConfig, {
11
+ output: { filename: '[name]-[chunkhash].js' },
12
+ devtool: 'source-map',
13
+ stats: 'normal',
14
+
15
+ plugins: [
16
+ new webpack.optimize.UglifyJsPlugin({
17
+ minimize: true,
18
+ sourceMap: true,
19
+
20
+ compress: {
21
+ warnings: false
22
+ },
23
+
24
+ output: {
25
+ comments: false
26
+ }
27
+ }),
28
+
29
+ new CompressionPlugin({
30
+ asset: '[path].gz[query]',
31
+ algorithm: 'gzip',
32
+ test: /\.(js|css|html|json|ico|svg|eot|otf|ttf)$/
33
+ })
34
+ ]
35
+ })
@@ -0,0 +1,58 @@
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ /* eslint global-require: 0 */
4
+ /* eslint import/no-dynamic-require: 0 */
5
+
6
+ const webpack = require('webpack')
7
+ const { basename, dirname, join, relative, resolve } = require('path')
8
+ const { sync } = require('glob')
9
+ const ExtractTextPlugin = require('extract-text-webpack-plugin')
10
+ const ManifestPlugin = require('webpack-manifest-plugin')
11
+ const extname = require('path-complete-extname')
12
+ const { env, settings, output, loadersDir } = require('./configuration.js')
13
+
14
+ const extensionGlob = `**/*{${settings.extensions.join(',')}}*`
15
+ const entryPath = join(settings.source_path, settings.source_entry_path)
16
+ const packPaths = sync(join(entryPath, extensionGlob))
17
+
18
+ module.exports = {
19
+ entry: packPaths.reduce(
20
+ (map, entry) => {
21
+ const localMap = map
22
+ const namespace = relative(join(entryPath), dirname(entry))
23
+ localMap[join(namespace, basename(entry, extname(entry)))] = resolve(entry)
24
+ return localMap
25
+ }, {}
26
+ ),
27
+
28
+ output: {
29
+ filename: '[name].js',
30
+ path: output.path,
31
+ publicPath: output.publicPath
32
+ },
33
+
34
+ module: {
35
+ rules: sync(join(loadersDir, '*.js')).map(loader => require(loader))
36
+ },
37
+
38
+ plugins: [
39
+ new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
40
+ new ExtractTextPlugin(env.NODE_ENV === 'production' ? '[name]-[hash].css' : '[name].css'),
41
+ new ManifestPlugin({
42
+ publicPath: output.publicPath,
43
+ writeToFileEmit: true
44
+ })
45
+ ],
46
+
47
+ resolve: {
48
+ extensions: settings.extensions,
49
+ modules: [
50
+ resolve(settings.source_path),
51
+ 'node_modules'
52
+ ]
53
+ },
54
+
55
+ resolveLoader: {
56
+ modules: ['node_modules']
57
+ }
58
+ }
@@ -0,0 +1,6 @@
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ const merge = require('webpack-merge')
4
+ const sharedConfig = require('./shared.js')
5
+
6
+ module.exports = merge(sharedConfig, {})
@@ -0,0 +1,38 @@
1
+ # Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ default: &default
4
+ source_path: app/javascript
5
+ source_entry_path: packs
6
+ public_output_path: packs
7
+
8
+ extensions:
9
+ - .coffee
10
+ - .erb
11
+ - .js
12
+ - .jsx
13
+ - .ts
14
+ - .vue
15
+ - .sass
16
+ - .scss
17
+ - .css
18
+ - .png
19
+ - .svg
20
+ - .gif
21
+ - .jpeg
22
+ - .jpg
23
+
24
+ development:
25
+ <<: *default
26
+
27
+ dev_server:
28
+ host: 0.0.0.0
29
+ port: 8080
30
+ https: false
31
+
32
+ test:
33
+ <<: *default
34
+
35
+ public_output_path: packs-test
36
+
37
+ production:
38
+ <<: *default