Text
npm install -g webpack
npm init -y
npm install webpack --save-dev
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
"dependencies": {
"jquery": "~3.1.0",
"bootstrap-sass": "~3.3.6"
}
npm install jquery --save
npm install bootstrap-sass --save
npm install -g 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
const path = require("path")
const webpack = require("webpack")
module.exports = {
resolve: {
modulesDirectories: ["node_modules", "bower_components"]
},
plugins: [
new webpack.ResolverPlugin(
new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin(
".bower.json", ["main"]
)
)
]
}
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'
}
},
module.exports = {
...
resolve: {
alias: {
some: "some-module/src/bla"
}
}
};
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)
...
npm install babel-core --save-dev
npm install babel-preset-es2015 --save-dev
npm install babel-loader --save-dev
npm install babel-polyfill --save-dev
(babel-plugin-transform-runtime)
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules|bower_components)/,
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')
]
}
npm install html-webpack-plugin --save-dev
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']
}),
]
}
npm install webpack-dev-server --save-dev
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()
]
npm install clean-webpack-plugin --save-dev
npm install webpack-merge --save-dev
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")
})
module.exports = function(grunt) {
grunt.loadNpmTasks("grunt-webpack");
grunt.initConfig({
webpack: {
options: {
// configuration for all builds
},
build: {
// configuration for this build
}
},
"webpack-dev-server": {
options: {
webpack: {
// configuration for all builds
},
// server and middleware options for all builds
},
start: {
webpack: {
// configuration for this build
},
// server and middleware options for this build
}
}
});
};
npm install node-sass --save-dev
npm install sass-loader --save-dev
npm install style-loader --save-dev
npm install url-loader --save-dev
npm install resolve-url-loader --save-dev
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
npm install handlebars-loader --save-dev
{
...
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