Last active
February 2, 2018 22:54
-
-
Save igorbenic/aebafcba9f7b91214243b5b14736a089 to your computer and use it in GitHub Desktop.
Configuring Webpack in WordPress for the First Time | https://www.ibenic.com/configuring-webpack-wordpress
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'); | |
| module.exports = { | |
| entry: './src/index.js', | |
| output: { | |
| filename: 'bundle.js', | |
| path: path.resolve(__dirname, 'dist') | |
| } | |
| }; |
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
| output: { | |
| filename: '[name].bundle.js', | |
| path: path.resolve(__dirname, 'assets/js') | |
| }, | |
| // Adding jQuery as external library | |
| externals: { | |
| jquery: 'jQuery' | |
| }, | |
| module: { |
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'); | |
| module.exports = { | |
| entry: { | |
| admin: './assets/js/admin.js', | |
| public: './assets/js/public.js', | |
| }, | |
| output: { | |
| filename: '[name].min.js', | |
| path: path.resolve(__dirname, 'assets/js') | |
| } | |
| }; |
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'); | |
| module.exports = { | |
| entry: { | |
| admin: './assets/js/admin.js', | |
| public: './assets/js/public.js', | |
| }, | |
| output: { | |
| filename: '[name].min.js', | |
| path: path.resolve(__dirname, 'assets/js') | |
| }, | |
| module: { | |
| rules: [ | |
| { | |
| test: /\.js$/, | |
| exclude: /(node_modules|bower_components)/, | |
| use: { | |
| loader: 'babel-loader', | |
| options: { | |
| presets: ['babel-preset-env'] | |
| } | |
| } | |
| } | |
| ] | |
| } | |
| }; |
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'); | |
| // Including our UglifyJS | |
| const UglifyJSPlugin = require('uglifyjs-webpack-plugin'); | |
| module.exports = { | |
| entry: { | |
| admin: './assets/js/admin.js', | |
| public: './assets/js/public.js', | |
| }, | |
| plugins: [ | |
| // Adding our UglifyJS plugin | |
| new UglifyJSPlugin() | |
| ], | |
| output: { |
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
| // ... | |
| rules: [ | |
| { | |
| test: /\.js$/, | |
| exclude: /(node_modules|bower_components)/, | |
| use: { | |
| loader: 'babel-loader', | |
| options: { | |
| presets: ['babel-preset-env'] | |
| } | |
| } | |
| }, | |
| // Our new rules | |
| { | |
| test: /\.css$/, | |
| loader: 'style-loader', | |
| }, | |
| { | |
| test: /\.css$/, | |
| loader: 'css-loader', | |
| options: { | |
| minimize: true | |
| } | |
| } | |
| ] | |
| // ... |
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
| // JS rule | |
| }, | |
| { | |
| test: /\.scss$/, | |
| use: [{ | |
| loader: "style-loader" // creates style nodes from JS strings | |
| }, { | |
| loader: "css-loader" // translates CSS into CommonJS | |
| }, { | |
| loader: "sass-loader" // compiles Sass to CSS | |
| }] | |
| }, | |
| { | |
| test: /\.css$/, |
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 UglifyJSPlugin = require('uglifyjs-webpack-plugin'); | |
| // Our new plugin | |
| const ExtractTextPlugin = require("extract-text-webpack-plugin"); | |
| const extractSass = new ExtractTextPlugin({ | |
| filename: "../css/[name].min.css", | |
| }); |
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
| plugins: [ | |
| new UglifyJSPlugin(), | |
| // Adding our new plugin in plugins list | |
| extractSass | |
| ], |
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
| // Changing our SCSS rule completely | |
| { | |
| test: /\.scss$/, | |
| use: extractSass.extract({ | |
| use: [{ | |
| loader: "css-loader", | |
| options: { | |
| minimize: true | |
| } | |
| }, { | |
| loader: "sass-loader" | |
| }], | |
| // use style-loader in development | |
| fallback: "style-loader" | |
| }) | |
| }, |
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
| { | |
| "name": "webpack-plugin", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC" | |
| } |
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
| import css from '../css/style.css'; | |
| 'use strict'; | |
| (function($) { | |
| $(function() { | |
| console.log(() => { }); | |
| console.log('Loaded'); | |
| }); | |
| })(jQuery); |
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
| "scripts": { | |
| "build": "webpack", | |
| "watch": "webpack --watch" | |
| }, |
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 UglifyJSPlugin = require('uglifyjs-webpack-plugin'); | |
| const ExtractTextPlugin = require("extract-text-webpack-plugin"); | |
| const extractSass = new ExtractTextPlugin({ | |
| filename: "../css/[name].min.css", | |
| }); | |
| module.exports = { | |
| entry: { | |
| admin: './assets/js/admin.js', | |
| public: './assets/js/public.js' | |
| }, | |
| plugins: [ | |
| new UglifyJSPlugin(), | |
| extractSass | |
| ], | |
| externals: { | |
| jquery: "jQuery" | |
| }, | |
| output: { | |
| filename: '[name].min.js', | |
| path: path.resolve( __dirname, 'assets/js' ) | |
| }, | |
| module: { | |
| rules: [ | |
| { | |
| test: /\.js$/, | |
| exclude: /(node_modules|bower_components)/, | |
| use: { | |
| loader: 'babel-loader', | |
| options: { | |
| presets: ['babel-preset-env'] | |
| } | |
| } | |
| }, | |
| { | |
| test: /\.scss$/, | |
| use: extractSass.extract({ | |
| use: [ | |
| { | |
| loader: "css-loader", | |
| options: { | |
| minimize: true | |
| } | |
| }, | |
| { | |
| loader: "sass-loader" | |
| }], | |
| fallback: "style-loader" | |
| }) | |
| }, | |
| { | |
| test: /\.css$/, | |
| loader: 'style-loader' | |
| }, | |
| { | |
| test: /\.css$/, | |
| loader: 'css-loader', | |
| options: { | |
| minimize: true | |
| } | |
| } | |
| ] | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment