Skip to content

Commit

Permalink
Reorganize webpack rules for proper handling interial and external as…
Browse files Browse the repository at this point in the history
…sets
  • Loading branch information
jedrzejchalubek committed May 9, 2017
1 parent 4df3070 commit e120426
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 75 deletions.
13 changes: 9 additions & 4 deletions build/app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ module.exports = merge({
paths: {
root: path.resolve(__dirname, '../'),
public: path.resolve(__dirname, '../public'),
assets: path.resolve(__dirname, '../resources/assets')
sass: path.resolve(__dirname, '../resources/assets/sass'),
fonts: path.resolve(__dirname, '../resources/assets/fonts'),
images: path.resolve(__dirname, '../resources/assets/images'),
javascript: path.resolve(__dirname, '../resources/assets/javascript'),
relative: '../',
external: /node_modules|bower_components/
},

/**
Expand All @@ -29,10 +34,10 @@ module.exports = merge({
* @type {Object}
*/
outputs: {
stylesheet: { filename: 'css/[name].css' },
javascript: { filename: 'js/[name].js' },
css: { filename: 'css/[name].css' },
font: { filename: 'fonts/[name].[ext]' },
image: { filename: 'images/[name].[ext]' },
font: { filename: 'fonts/[name].[ext]' }
javascript: { filename: 'js/[name].js' }
},

/**
Expand Down
16 changes: 0 additions & 16 deletions build/rules/asset.js

This file was deleted.

11 changes: 11 additions & 0 deletions build/rules/external.fonts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const config = require('../app.config')

module.exports = {
test: /\.(eot|woff|woff2|ttf|svg)(\?\S*)?$/,
include: config.paths.external,
loader: 'file-loader',
options: {
publicPath: config.paths.relative,
name: config.outputs.font.filename
}
}
11 changes: 11 additions & 0 deletions build/rules/external.images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const config = require('../app.config')

module.exports = {
test: /\.(png|jpe?g|gif|svg)$/,
include: config.paths.external,
loader: 'file-loader',
options: {
publicPath: config.paths.relative,
name: config.outputs.font.filename
}
}
5 changes: 3 additions & 2 deletions build/rules/fonts.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const config = require('../app.config')

module.exports = {
test: /\.(eot|ttf|woff|woff2)$/,
include: config.paths.assets,
test: /\.(eot|ttf|woff|woff2|svg)(\?\S*)?$/,
include: config.paths.fonts,
loader: 'file-loader',
options: {
publicPath: config.paths.relative,
name: config.outputs.font.filename,
}
}
27 changes: 8 additions & 19 deletions build/rules/images.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
const isdev = require('isdev')

const config = require('../app.config')

module.exports = {
test: /\.(png|jpe?g|gif|svg)$/,
include: config.paths.assets,
use: [
{
loader: 'file-loader',
options: {
publicPath: '../',
name: config.outputs.image.filename
}
},

{
loader: 'img-loader',
options: {
enabled: !isdev
}
}
]
include: config.paths.images,
loader: 'file-loader',
options: {
publicPath: config.paths.relative,
name: config.outputs.image.filename,
emitFile: false,
limit: 10000
}
}
4 changes: 2 additions & 2 deletions build/rules/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ const config = require('../app.config')

module.exports = {
test: /\.js$/,
include: config.paths.assets,
loader: ['babel-loader']
include: config.paths.javascript,
loader: 'babel-loader'
}
6 changes: 3 additions & 3 deletions build/rules/sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const config = require('../app.config')

module.exports = {
test: /\.s[ac]ss$/,
include: config.paths.assets,
include: config.paths.sass,
loader: ExtractTextPlugin.extract({
use: [
{
Expand All @@ -21,15 +21,15 @@ module.exports = {
{
loader: 'postcss-loader',
options: {
sourceMap: isdev,
sourceMap: true,
plugins: () => [autoprefixer]
}
},

{
loader: 'sass-loader',
options: {
sourceMap: isdev
sourceMap: true
}
}
],
Expand Down
17 changes: 0 additions & 17 deletions build/rules/vendor.js

This file was deleted.

38 changes: 31 additions & 7 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ const isdev = require('isdev')
const webpack = require('webpack')
const autoprefixer = require('autoprefixer')

const CleanWebpackPlugin = require('clean-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const CleanPlugin = require('clean-webpack-plugin')
const StyleLintPlugin = require('stylelint-webpack-plugin')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const BrowserSyncPlugin = require("browser-sync-webpack-plugin")
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const BrowserSyncPlugin = require('browser-sync-webpack-plugin')
const { default: ImageminPlugin } = require('imagemin-webpack-plugin')

const sassRule = require('./rules/sass')
const fontsRule = require('./rules/fonts')
const imagesRule = require('./rules/images')
const javascriptRule = require('./rules/javascript')
const externalFontsRule = require('./rules/external.fonts')
const externalImagesRule = require('./rules/external.images')

const config = require('./app.config')

Expand All @@ -34,16 +38,26 @@ module.exports = {
module: {
rules: [
sassRule,
javascriptRule,
fontsRule,
imagesRule,
fontsRule
javascriptRule,
externalFontsRule,
externalImagesRule,
]
},

plugins: [
new webpack.LoaderOptionsPlugin({ minimize: !isdev }),
new ExtractTextPlugin(config.outputs.stylesheet),
new CleanWebpackPlugin(config.paths.public, { root: config.paths.root }),
new ExtractTextPlugin(config.outputs.css),
new CleanPlugin(config.paths.public, { root: config.paths.root }),
new CopyPlugin([{
from: {
glob: `${config.paths.images}/**/*`,
flatten: true,
dot: false
},
to: config.outputs.image.filename,
}]),
]
}

Expand All @@ -69,4 +83,14 @@ if (! isdev) {
sourceMap: isdev
})
)

module.exports.plugins.push(
new ImageminPlugin({
test: /\.(jpe?g|png|gif|svg)$/i,
optipng: { optimizationLevel: 7 },
gifsicle: { optimizationLevel: 3 },
pngquant: { quality: '65-90', speed: 4 },
svgo: { removeUnknownsAndDefaults: false, cleanupIDs: false }
})
)
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
"browser-sync": "^2.18.8",
"browser-sync-webpack-plugin": "^1.1.4",
"clean-webpack-plugin": "^0.1.16",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.28.0",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.11.1",
"imagemin-webpack-plugin": "^1.4.4",
"img-loader": "^2.0.0",
"isdev": "^1.0.1",
"node-sass": "^4.5.2",
Expand Down
3 changes: 0 additions & 3 deletions resources/assets/images/checkcircle.svg

This file was deleted.

Empty file removed resources/assets/images/img.png
Empty file.
Empty file removed resources/assets/images/svg.svg
Empty file.
2 changes: 0 additions & 2 deletions resources/assets/sass/app.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@charset 'utf-8';

// Basic application includes. Here we should import Foundation
// settings, our own variables, mixins and functions.
@import 'settings';
Expand Down

0 comments on commit e120426

Please sign in to comment.