1.基础配置 #

1.1 library选项 #

1.2 externals #

1.3 webpack-node-externals #

1.4 mini-css-extract-plugin #

1.5 Sourcemap #

1.6 .npmignore #

1.7 prepublishOnly #

2.开发NPM #

2.1 安装 #

npm install webpack webpack-cli jquery lodash webpack-node-externals  mini-css-extract-plugin css-loader --save

2.1 webpack.config.js #

webpack.config.js

const path = require("path");
const nodeExternals = require('webpack-node-externals');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
  mode: "development",
  devtool: 'source-map',
  entry: "./src/index.js",
  externals: [
    /* {
      jquery: {
        commonjs: 'jquery',
        commonjs2: 'jquery',
        amd: 'jquery',
        root: '$',
      },
      lodash: {
        commonjs: 'lodash',
        commonjs2: 'lodash',
        amd: 'lodash',
        root: '_',
      },
    }, */
    nodeExternals()
  ],  
  module: {
   rules: [
     {
       test: /\.css$/,
       use: [MiniCssExtractPlugin.loader, "css-loader"],
     },
   ],
 },
  output: {
    filename: "[name].js",
    path: path.join(__dirname, "./dist"),
    library: "math",
    libraryExport: 'add',
    libraryTarget: 'umd'
  },
  plugins: [new MiniCssExtractPlugin()],
};

2.2 src\index.js #

src\index.js

import $ from 'jquery';
import _ from 'lodash';
import './index.css';
export { 
 _,
 $
}
export const add = (a, b) => a + b
export const minus = (a, b) => a -b

2.3 src\index.css #

src\index.css

body{
    background-color: green;
}

3.输出多种产物 #

3.1 webpack.config.js #

webpack.config.js

const path = require("path");
const nodeExternals = require('webpack-node-externals');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { merge } = require("webpack-merge");
const baseConfig = {
  mode: "development",
  devtool: 'source-map',
  entry: "./src/index.js",
  externals: [
    nodeExternals()
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"],
      },
    ],
  },
  output: {
    filename: "[name].js",
    path: path.join(__dirname, "./dist"),
    library: "math",
    libraryExport: 'add',
    libraryTarget: 'umd'
  },
  plugins: [new MiniCssExtractPlugin()],
};

+module.exports = [
+  merge(baseConfig, {
+    output: {
+      filename: "[name]-commonjs.js",
+      libraryTarget: 'commonjs'
+    },
+  }),
+  merge(baseConfig, {
+    output: {
+      filename: "[name]-amd.js",
+      libraryTarget: 'amd'
+    },
+  })
+];

4.配置函数 #

webpack --env mode=production --watch
env { WEBPACK_WATCH: true, mode: 'production' }
argv { env: { WEBPACK_WATCH: true, mode: 'production' }, watch: true }

webpack.config.js

const path = require("path");
const nodeExternals = require('webpack-node-externals');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = function (env, argv) {
+ const isProduction = env.mode === 'production';
+ const isWatch = argv.watch;
  return {
+   mode:  isProduction ? 'production' : 'development',
+   devtool: isProduction ? 'source-map' : 'eval-source-map',
    entry: "./src/index.js",
+   watch: isWatch,
    externals: [
      nodeExternals()
    ],
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [MiniCssExtractPlugin.loader, "css-loader"],
        },
      ],
    },
    output: {
      filename: "[name].js",
      path: path.join(__dirname, "./dist"),
      library: "math",
      libraryExport: 'add',
      libraryTarget: 'umd'
    },
    plugins: [new MiniCssExtractPlugin()],
  }
}

5.区分环境 #

5.1 mode #

5.2 区分环境 #

5.2.1 命令行配置 #

5.2.2 mode配置 #

webpack.config.js

module.exports = function (env, argv) {
+ const isProduction = env.mode === 'production';
  const isWatch = argv.watch;
  return {
    mode:  isProduction ? 'production' : 'development'
  }
}

5.2.3 DefinePlugin #

index.js

console.log(NODE_ENV);//production

webpack.config.js

console.log('process.env.NODE_ENV',process.env.NODE_ENV);// undefined

5.2.4 cross-env #

package.json

"scripts": {
  "build": "cross-env NODE_ENV=development webpack"
}

webpack.config.js

console.log('process.env.NODE_ENV',process.env.NODE_ENV);// development
`