Skip to content

Instantly share code, notes, and snippets.

@ohgyun
Forked from ayastreb/webpack.config.js
Created February 3, 2018 18:35
Show Gist options
  • Save ohgyun/1cb4c91bf18c68d2a0f2108c7f8d6204 to your computer and use it in GitHub Desktop.
Save ohgyun/1cb4c91bf18c68d2a0f2108c7f8d6204 to your computer and use it in GitHub Desktop.
Build Chrome Extension with React using Webpack
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = {
// Entry files for our popup and background pages
entry: {
popup: './src/popup.js',
background: './src/background.js'
},
// Extension will be built into ./dist folder, which we can then load as unpacked extension in Chrome
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js'
},
// Here we define loaders for different file types
module: {
rules: [
// We use Babel to transpile JSX
{
test: /\.js$/,
include: [
path.resolve(__dirname, './src')
],
use: 'babel-loader'
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
})
},
{
test: /\.(ico|eot|otf|webp|ttf|woff|woff2)(\?.*)?$/,
use: 'file-loader?limit=100000'
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'file-loader?limit=100000',
{
loader: 'img-loader',
options: {
enabled: true,
optipng: true
}
}
]
}
]
},
plugins: [
// create CSS file with all used styles
new ExtractTextPlugin('bundle.css'),
// create popup.html from template and inject styles and script bundles
new HtmlWebpackPlugin({
inject: true,
chunks: ['popup'],
filename: 'popup.html',
template: './src/popup.html'
}),
// copy extension manifest and icons
new CopyWebpackPlugin([
{ from: './src/manifest.json' },
{ context: './src/assets', from: 'icon-**', to: 'assets' }
])
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment