Webpack-尝试将我的CSS和JS文件路由/编译到dist /文件夹中的CSS和JS特定子文件夹中

时间:2018-08-16 23:18:34

标签: npm webpack css-loader sass-loader webpack.config.js

一般来说,我对于webpack还是很陌生的。真的只有几天。在大多数情况下,我觉得在将webpack设置和集成到本地开发环境中方面已经取得了相当不错的进展。在此过程中,有很多错误和命令行错误,但到目前为止,我仍然可以解决其中的大多数错误。在Webpack上大喊MaxmillianSchwärzmuller的简短YT系列,让我入门。

现在,我正在尝试重新利用/扩展我所学到的一些概念,但是,我的iteration count 1 - key1 value1, key2 value2, key3 value3, key4 value4 (这将是生产命令)无法成功编译将我的CSS文件分别放入CSS和JS子文件夹(不在网站根目录中)。

我觉得我尝试在JS和CSS专用测试块中使用npm run build包方面取得了更大的进步,但是现在我想知道我是否不是对{{ 1}}软件包,或者只是配置不正确。

这是我的file-loader文件的内容:

file-loader

这是webpack.config.js文件:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const extractPlugin = new ExtractTextPlugin({
   filename: 'styles.css'
});

module.exports = {
  entry: './src/js/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: ['babel-preset-env']
            }
          }
          // attempted to replicate the same use statement as seen below for the jpg/png test
          // and only app.js (not app.bundle.js) was dropped into dist/js/
          // {
          //   loader: 'file-loader',
          //   options: {
          //     name: '[name].[ext]',
          //     outputPath: 'js/',
          //     publicPath: 'js/'

          //   }
          // }
        ]
      },
      {
        test: /\.scss$/,
        use: extractPlugin.extract({
          use: ['css-loader', 'sass-loader']
        })
        // attempted to modify/replicate the above use statement
          // use: [
            // {
              // loader: 'css-loader',
              // loader: 'sass-loader'
              // loader: 'file-loader',
              // options: {
              //   name: '[name].[ext]',
              //   outputPath: 'css/',
              //   publicPath: 'css/'
              // }
            // }
          // ]
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader'
          }
        ]
      },
      {
        test: /\.(jpg|png)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'images/',
              publicPath: 'images/'
            }
          }
        ]
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]'
            }
          }
        ],
        exclude: path.resolve(__dirname, 'src/index.html')
      }
    ]
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    extractPlugin,
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'src/index.html'
    }),
    new CleanWebpackPlugin(['dist'])
  ]
};

最后还有package.json文件

  {
  "name": "nodomaintoseehere.com",
  "version": "1.0.0",
  "description": "Site repository v1.0",
  "private": true,
  "main": "./src/app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server",
    "build": "webpack -p"
  },
  "author": "Jeremy Wilson",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "clean-webpack-plugin": "^0.1.19",
    "cross-env": "^5.2.0",
    "css-loader": "^1.0.0",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^1.1.11",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "jquery": "^3.3.1",
    "node-sass": "^4.9.3",
    "sass-loader": "^7.1.0",
    "webpack": "^3.12.0",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^2.11.2"
  },
  "dependencies": {
    "bootstrap": "^4.1.3",
    "lodash": "^4.17.10"
  }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

将资产放入不同的子文件夹不会对性能或使用造成任何影响,因为这些资产每次都会部署在您的静态服务器上,直到重新部署为止。

无法将js文件输出到js文件夹,因为babel loader上没有任何选项,并且file-loader无法处理js,它们只是发出文件,在您的情况下,最后将以重复文件结尾。文件加载器不是使用javascript文件的理想选择。

对于CSS,您可以配置extract-text-plugin。

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const extractPlugin = new ExtractTextPlugin({
   filename: 'css/styles.css' // <---
});

module.exports = {
  entry: './src/js/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: ['babel-preset-env']
            }
          }
        ]
      },
      {
        test: /\.scss$/,
        use: extractPlugin.extract({
          use: ['css-loader', 'sass-loader']
        })
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader'
          }
        ]
      },
      {
        test: /\.(jpg|png)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'images/',
              publicPath: 'images/'
            }
          }
        ]
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]'
            }
          }
        ],
        exclude: path.resolve(__dirname, 'src/index.html')
      }
    ]
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    extractPlugin,
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'src/index.html'
    }),
    new CleanWebpackPlugin(['dist'])
  ]
};