This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e83d66
commit 92fd1c2
Showing
5 changed files
with
187 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
const glob = require("glob"); | ||
const path = require("path"); | ||
const ManifestPlugin = require("webpack-manifest-plugin"); | ||
// extract css from bundled javascript | ||
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); | ||
|
||
const bundles = path.join( | ||
__dirname, | ||
"app", | ||
"bundles", | ||
"javascripts", | ||
"entries" | ||
); | ||
|
||
const targets = glob.sync(path.join(bundles, "**/*.{js,jsx,ts,tsx}")); | ||
const entry = targets.reduce((entry, target) => { | ||
const bundle = path.relative(__dirname, target); | ||
const filename = path.relative(bundles, target); | ||
|
||
const ext = path.extname(filename); | ||
|
||
return Object.assign({}, entry, { | ||
// Input: "application.js" | ||
// Output: { "application": "./app/bundles/javascripts/application.js" } | ||
[filename.replace(ext, "")]: "./" + bundle | ||
}); | ||
}, {}); | ||
|
||
module.exports = { | ||
entry: entry, | ||
output: { | ||
filename: "js/[name]-[hash].js", | ||
chunkFilename: "js/[name].bundle-[hash].js", | ||
path: path.resolve(__dirname, "public", "bundles"), | ||
publicPath: "/bundles/" | ||
}, | ||
plugins: [ | ||
new ManifestPlugin({ | ||
fileName: "manifest.json", | ||
publicPath: "/bundles/", | ||
writeToFileEmit: true | ||
}), | ||
new MiniCssExtractPlugin({ | ||
filename: "style/[name]-[hash].css", | ||
chunkFilename: "style/[name].bundle-[hash].css" | ||
}) | ||
], | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
use: [ | ||
{ | ||
loader: "babel-loader", | ||
options: { | ||
presets: [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
modules: false, | ||
targets: { | ||
browsers: "> 0.25%" | ||
}, | ||
forceAllTransforms: true, | ||
useBuiltIns: "usage" | ||
} | ||
] | ||
], | ||
plugins: [] | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
test: /\.scss$/, | ||
use: [ | ||
MiniCssExtractPlugin.loader, | ||
{ | ||
loader: "css-loader", | ||
options: { | ||
minimize: true, | ||
// css-loaderの前に噛ませるloaderの数 | ||
importLoaders: 2 | ||
} | ||
}, | ||
{ | ||
loader: "postcss-loader", | ||
options: { | ||
plugins: [ | ||
require("autoprefixer")({ grid: true }), | ||
require("postcss-flexbugs-fixes") | ||
] | ||
} | ||
}, | ||
{ | ||
loader: "sass-loader", | ||
options: {} | ||
} | ||
] | ||
}, | ||
{ | ||
// 対象となるファイルの拡張子 | ||
test: /\.(gif|png|jpg|eot|wof|woff|ttf|svg)$/, | ||
// 画像をBase64として取り込む | ||
use: [ | ||
{ | ||
loader: "url-loader", | ||
options: { | ||
limit: 100 * 1024, // 100KB以上だったら埋め込まずファイルとして分離する | ||
name: "img/[name]-[hash].[ext]" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
optimization: { | ||
splitChunks: { | ||
cacheGroups: { | ||
vendor: { | ||
test: /node_modules/, | ||
name: "vendor", | ||
chunks: "initial", | ||
enforce: true | ||
} | ||
} | ||
} | ||
}, | ||
resolve: { | ||
alias: { | ||
"@js": path.resolve(__dirname, "app/bundles/javascripts"), | ||
"@css": path.resolve(__dirname, "app/bundles/stylesheets"), | ||
"@image": path.resolve(__dirname, "app/bundles/images") | ||
} | ||
}, | ||
performance: { | ||
hints: "warning", // default value | ||
maxEntrypointSize: 250000, // default value | ||
maxAssetSize: 250000 // default value | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const merge = require("webpack-merge"); | ||
const common = require("./webpack.common.js"); | ||
const path = require("path"); | ||
|
||
module.exports = merge(common, { | ||
mode: "development", | ||
devtool: "source-map", | ||
devServer: { | ||
publicPath: "/bundles/", | ||
contentBase: path.resolve(__dirname, "public", "bundles"), | ||
host: "0.0.0.0", | ||
port: 3035, | ||
disableHostCheck: true, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*" | ||
} | ||
} | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const merge = require("webpack-merge"); | ||
const common = require("./webpack.common.js"); | ||
// minify css when production | ||
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin"); | ||
// minify js when using optimize-css-assets-webpack-plugin | ||
const TerserPlugin = require("terser-webpack-plugin"); | ||
|
||
module.exports = merge(common, { | ||
mode: "production", | ||
devtool: "none", | ||
optimization: { | ||
minimizer: [new TerserPlugin({}), new OptimizeCSSAssetsPlugin({})] | ||
} | ||
}); |
This file contains 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