webpack使用方法以及用法优化

2017-09-11  本文已影响0人  size_of
{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack src/index.js dist/bundle.js"//写在这里 在终端运行npm run build
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^3.5.6"
  }
}
const path = require('path');

module.exports = {
  //入口 index.js
  entry: './src/index.js',
  //输出
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};
const path = require('path');

module.exports = {
  //入口 index.js
  entry: './src/index.js',
  //输出
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    loaders:[
      {
        test: /\.css$/,
        use: 'style-loader!css-loader!autoprefixer-loader'
//顺序为从右向左:给属性加前缀、读取css文件到内存、加到head标签中
      }
    ]   
  }
};

上面的css加载器也可以写成以下形式

loaders:[
  {
    test: /\.css$/,
        loaders:[
          'style-loader',
          'css-loader',
          'autoprefixer-loader'
        ]
  }
]
loaders:[
  {
    test: /\.css$/,
    use: 'style-loader!css-loader!autoprefixer-loader'
  },
  {
    test: /\.(png|jpg|jpeg)$/,
    use: 'url-loader'
  }
]
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack src/index.js dist/bundle.js",
    "watch": "webpack --watch"//在终端运行npm run watch
  }
{
    entry: {
        page1: "./page1",
        //支持数组形式,将加载数组中的所有模块,但以最后一个模块作为输出
        page2: ["./entry1", "./entry2"]
    },
    output: {
        path: "dist/js/page",
        filename: "[name].bundle.js"
    }
}
上一篇下一篇

猜你喜欢

热点阅读