Last active
January 22, 2022 02:16
-
-
Save mahm/87e4cf74839ada9af00b9ec7223741b5 to your computer and use it in GitHub Desktop.
jsbundling-rails + webpack + webpack-dev-server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rack/proxy" | |
module Webpack | |
class DevServerProxy < Rack::Proxy | |
def perform_request(env) | |
if Rails.env.development? && env['PATH_INFO'].start_with?('/assets') | |
env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = 'localhost' | |
env["HTTP_X_FORWARDED_SERVER"] = 'localhost:3035' | |
env["HTTP_PORT"] = env["HTTP_X_FORWARDED_PORT"] = '3035' | |
env["HTTP_X_FORWARDED_PROTO"] = env["HTTP_X_FORWARDED_SCHEME"] = 'http' | |
env["HTTPS"] = env["HTTP_X_FORWARDED_SSL"] = 'off' | |
env["SCRIPT_NAME"] = '' | |
super(env) | |
else | |
@app.call(env) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const webpack = require('webpack'); | |
const { VueLoaderPlugin } = require('vue-loader'); | |
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); | |
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin'); | |
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts'); | |
const mode = process.env.NODE_ENV === 'development' ? 'development' : 'production'; | |
module.exports = { | |
mode, | |
optimization: { | |
minimizer: [ | |
new CssMinimizerPlugin(), | |
], | |
}, | |
devServer: { | |
https: false, | |
host: 'localhost', | |
port: 3035, | |
hot: true, | |
compress: true, | |
headers: { | |
'Access-Control-Allow-Origin': '*', | |
}, | |
devMiddleware: { | |
publicPath: '/assets/', | |
}, | |
static: { | |
directory: path.resolve(__dirname, 'app/assets/builds'), | |
}, | |
}, | |
entry: { | |
application: './app/frontend/packs/application.js', | |
}, | |
output: { | |
filename: '[name].js', | |
sourceMapFilename: '[name].js.map', | |
path: path.resolve(__dirname, 'app/assets/builds'), | |
assetModuleFilename: 'images/[name][ext]', | |
}, | |
plugins: [ | |
new webpack.optimize.LimitChunkCountPlugin({ | |
maxChunks: 1, | |
}), | |
new webpack.ProvidePlugin({ | |
$: 'jquery', | |
jQuery: 'jquery', | |
Popper: ['popper.js', 'default'], | |
}), | |
new VueLoaderPlugin(), | |
new MiniCssExtractPlugin(), | |
new RemoveEmptyScriptsPlugin(), | |
], | |
module: { | |
rules: [ | |
{ | |
test: /\.(js|ts)$/, | |
exclude: /node_modules/, | |
use: ['babel-loader'], | |
}, | |
{ | |
test: /\.vue$/, | |
loader: 'vue-loader', | |
}, | |
{ | |
test: /\.s?[ac]ss$/i, | |
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'], | |
}, | |
{ | |
test: /\.(png|jpe?g|gif|eot|woff2|woff|ttf|svg|webp)$/i, | |
type: 'asset/resource', | |
}, | |
], | |
}, | |
resolve: { | |
// Webpackで利用するときの設定 | |
alias: { | |
vue$: 'vue/dist/vue.esm.js', | |
'~': path.resolve(__dirname, 'app/frontend'), | |
}, | |
extensions: ['*', '.js', '.vue', '.json', '.sass', '.scss', '.css'], | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment