yarn global add webpack
yarn init
yarn add webpack -D
module.exports = {
entry: './app/index.js',
output: {
filename: './dist/bundle.js'
}
}
var moduleA = require('./moduleA')
var mainEl = document.getElementById('main')
mainEl.innerHTML = moduleA
var name = 'moduleA'
module.exports = name
webpack
webpack --watch
webpack -p
//package.json
scripts: {
"dev": "webpack --watch",
"build": "webpack -p"
}
yarn run dev //for dev
yarn run build //for prod
yarn add jquery
yarn add bootstrap-sass
"dependencies": {
"jquery": "^3.1.0",
"bootstrap-sass": "^3.3.6"
}
jquery@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/jq...
bootstrap-sass@^3.3.6:
version "3.3.7"
resolved "https://registry.yarnpkg.com/boot...
yarn global add npm-check-updates
$ ncu
express 4.12.x → 4.13.x
react-bootstrap ^0.22.6 → ^0.24.0
webpack ~1.9.10 → ~1.10.5
Run with -u to upgrade your package.json
# update all package
$ ncu -u
express 4.12.x → 4.13.x
# update specific package
$ ncu -u react-bootstrap
resolve: {
alias: {
'spin': 'spin.js',
'bootstrap-collapse':
'bootstrap-sass/assets/javascripts/bootstrap/collapse.js',
'bootstrap-dropdown':
'bootstrap-sass/assets/javascripts/bootstrap/dropdown.js',
'bootstrap-transition':
'bootstrap-sass/assets/javascripts/bootstrap/transition.js',
'bootstrap-modal':
'bootstrap-sass/assets/javascripts/bootstrap/modal.js'
}
},
plugins: [
new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery',
'window.jQuery': 'jquery',
'd3': 'd3'
})
]
module: {
loaders: [
{
test: /[\/\\]node_modules[\/\\]some[\/\\]index\.js$/,
loader: "imports?this=>window"
}
]
}
module: {
loaders: [
{
test: /[\/\\]node_modules[\/\\]some-module[\/\\]index\.js$/,
loader: "imports?define=>false"
}
]
}
npm install script-loader --save-dev
{
module: {
loaders: [
{ test: /\some-module\.js$/, loader: "script" },
]
}
}
module: {
noParse: [
/[\/\\]node_modules[\/\\]some-module[\/\\]some\.js$/
]
}
const webpack = require("webpack")
module.exports = {
// ...
plugins: [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/,
/(ko|ja)/)
]
};
//moment.js
...
require('./locale/' + name)
...
yarn add babel-core -D
yarn add babel-preset-es2015 -D
yarn add babel-loader -D
yarn add babel-polyfill -D
(babel-plugin-transform-runtime)
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015'],
cacheDirectory: true
}
}
]
},
resolve: {
extensions: ['', '.js', '.json']
}
{
entry: {
'dist/p1': './app/p1/main.js',
'dist/p2': './app/p2/main.js',
},
output: {
filename: '[name].js'
}
}
const CommonsChunkPlugin =
require("webpack/lib/optimize/CommonsChunkPlugin")
module.exports = {
entry: {
'vendor': ['jquery', 'moment'],
'p1': './app/p1/main.js',
'p2': './app/p2/main.js',
},
output: {
path: './dist/'
filename: '[name].js'
},
plugins: [
new CommonsChunkPlugin({
name: "vendor",
filename: "dist/lib/vendor.js",
minChunks: Infinity,
})
]
}
const CommonsChunkPlugin =
require("webpack/lib/optimize/CommonsChunkPlugin")
module.exports = {
entry: {
'vendor': ['jquery', 'moment'],
'p1': './app/p1/main.js',
'p2': './app/p2/main.js',
},
output: {
path: './dist/',
filename: '[name].js'
},
plugins: [
new CommonsChunkPlugin('common.js')
]
}
yarn add html-webpack-plugin -D
const HtmlWebpackPlugin =
require('html-webpack-plugin')
module.exports = {
...
plugins: [
new HtmlWebpackPlugin({
filename: "app/p1/index.html",
template: './app/index.html',
chunks: ['vendor', 'dist/app/p1/app']
}),
new HtmlWebpackPlugin({
filename: "app/p2/index.html",
template: './app/index.html',
chunks: ['vendor', 'dist/app/p2/app']
}),
]
}
yarn add webpack-dev-server -D
const path = require('path')
const dir_build = path.resolve(__dirname, 'dist')
module.exports = {
devServer: {
contentBase: dir_build,
inline: true
}
}
plugins: [
new CleanWebpackPlugin(['dist/*'], {
verbose: true,
dry: false
}),
// Avoid publishing files when compilation fails
new webpack.NoErrorsPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.ExtendedAPIPlugin(),
new webpack.optimize.UglifyJsPlugin()
]
yarn add clean-webpack-plugin -D
yarn add webpack-merge -D
const webpackMerge = require('webpack-merge')
const devConfig = require('./webpack.dev.config.js')
const prodConfig = require('./webpack.prod.config.js')
const target = process.env.npm_lifecycle_event
const common = {
...
}
if(target === 'build') {
config = webpackMerge(common, prodConfig)
} else {
config = webpackMerge(common, devConfig)
}
devtool: 'eval' //eval-source-map
devtool: 'source-map'
output: {
path: './dist/',
filename: "[name]-[chunkhash].js",
pathinfo: true,
publicPath: '/public'
}
plugins: [
new CommonsChunkPlugin({
name: "vendor",
filename: "libs/vendor-[hash].js",
minChunks: Infinity,
})
]
new webpack.DefinePlugin({
NODE_ENV: JSON.stringify("production")
})
yarn add node-sass -D
yarn add sass-loader -D
yarn add style-loader -D
yarn add url-loader -D
yarn add resolve-url-loader -D
module: {
loaders: [
{
test: /\.(png|gif)$/,
loaders: ["file?name=images/[name].[ext]"]
},
{
test: /\.(eot|woff|woff2|ttf|eot|svg)$/,
loaders: ["file?name=[path][name].[ext]"]
}
]
}
module: {
loaders: [
{
test: /\.scss$/,
loaders: ["style", "css",
"resolve-url", "sass?sourceMap"]
}
]
}
module: {
loaders: [
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract
('style',
'css!resolve-url!sass?sourceMap')
}
]
}
@import "~angular-ui-select/select";
@import "~font-awesome/scss/font-awesome";
@import "~bootstrap-sass/assets/stylesheets/bootstrap";
@import "variables";
@import "mixins";
import '../stylesheet/main.scss'
import '../moduleA'
@import "~angular-ui-select/select";
@import "~font-awesome/scss/font-awesome";
@import "~bootstrap-sass/assets/stylesheets/bootstrap";
@import "variables";
@import "mixins";
resolve: {
alias: {
'angular-ui-select': 'angular-ui-select/select.js',
}
}
resolve: {
alias: {
'angular-ui-select-js': 'angular-ui-select/select.js',
}
}
plugins: [
new CopyWebpackPlugin([
{
context: 'src',
from: '**/*.tpl.html'
}
])
]
npm install copy-webpack-plugin --save-dev
yarn add handlebars-loader -D
{
...
module: {
loaders: [
...
{ test: /\.handlebars$/, loader: "handlebars-loader" }
]
}
}
const template = require("./some.hbs")
"scripts": {
"start": "npm run dev",
"dev": "webpack-dev-server",
"build":
"NODE_ENV=production webpack --progress"
}
webpack --profile --json >> hint.json
yarn add webpack-dashboard -D
const DashboardPlugin =
require('webpack-dashboard/plugin')
module.exports = {
...
plugins: [
new DashboardPlugin()
]
}
yarn add webpack-bundle-analyzer -D
const wba = require('webpack-bundle-analyzer')
const BundleAnalyzerPlugin = wba.BundleAnalyzerPlugin
module.exports = {
plugins: [
...
new BundleAnalyzerPlugin({
analyzerMode: 'server',
analyzerPort: 8888
})
]
}
yarn add babel-plugin-lodash -D
import _ from 'lodash'
import { add } from 'lodash/fp'
const addOne = add(1)
_.map([1, 2, 3], addOne)
import _add from 'lodash/fp/add'
import _map from 'lodash/map'
const addOne = _add(1)
_map([1, 2, 3], addOne)
yarn add lodash-webpack-plugin -D
yarn add lodash-webpack-plugin -D
node --inspect --debug-brk $(which webpack)
yarn add webpack@v2.1.0-beta.25 -D
yarn add webpack-dev-server@v2.1.0-beta.10 -D
"presets": [
["es2015", { "modules": false }]
]
//module A
export const add = (a, b) => a + b
export const minus = (a, b) => a - b
import {add} from './moduleA'
const result = add(1,4)
System.import('./components/Home')
.then(loadRoute(cb))
.catch(errorLoading);