我有一个这样的目录结构:
在 node_modules 内部:
 >node_modules
  >./bin
   >webpack.config.js
  >bootstrap
   >bootstrap.css
   >bootstrap.js
我需要像这样生成单独的 CSS 和 JS 包:
custom-styles.css, custom-js.js, style-libs.css, js-libs.js
其中style-libs和js-libs应该包含所有库(如引导程序和 jQuery)的 syles 和 js 文件。这是我到目前为止所做的:
webpack.config.js:
const path = require('path');
const basedir = path.join(__dirname, '../../client');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const stylesPath = path.join(__dirname, '../bootstrap/dist/css');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
  watch: true,
  // Script to bundle using webpack
  entry: path.join(basedir, 'src', 'Client.js'),
  // Output directory and bundled file
  output: {
    path: path.join(basedir, 'dist'),
    filename: 'app.js'
  },
  // Configure module loaders (for JS ES6, JSX, etc.)
  module: {
    // Babel loader for JS(X) files, presets configured in .babelrc
    loaders: [
        {
            test: /\.jsx?$/,
            loader: 'babel',
            babelrc: false,
            query: {
                presets: ["es2015", "stage-0", "react"],
                cacheDirectory: true // TODO: only on development
            }
        },
        {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract("style-loader", "css-loader")
        },
    ]
  },
  // Set plugins (for index.html, optimizations, etc.)
  plugins: [
     // Generate index.html
     new HtmlWebpackPlugin({
        template: path.join(basedir, 'src', 'index.html'),
        filename: 'index.html'
     }),
     new ExtractTextPlugin(stylesPath + "/bootstrap.css", {
        allChunks: true,
     })
  ]
};
客户端.js
import * as p from 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';
ReactDOM.render(<App />, document.getElementById('app'));
我能够运行应用程序并正确呈现所有组件,除了使用webpack.
我对 webpack 没有太多经验,发现它真的很难让我听到它。有几个简单的问题:
1-此配置是否正确?如果是,那么如何使用 ES6 在组件中包含我的 CSS 和 JS 文件。类似import关键字的东西。
2-我什至应该为 CSS 文件使用 webpack 吗?
3-如何在 webpack 中指定单独的输入目录及其各自的输出文件?all-custom.js应该为custom1.jsand输出类似的东西custom2.js?
我知道这些是一些非常基本的问题,我试过谷歌,但没有找到一个简单的针对初学者的 Webpack 教程。
