Skip to content

Instantly share code, notes, and snippets.

@mahm
Last active January 22, 2022 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahm/87e4cf74839ada9af00b9ec7223741b5 to your computer and use it in GitHub Desktop.
Save mahm/87e4cf74839ada9af00b9ec7223741b5 to your computer and use it in GitHub Desktop.
jsbundling-rails + webpack + webpack-dev-server
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
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