【Webpack--016】config文件include和exclude配置

🤓😍Sam9029的CSDN博客主页:Sam9029的博客_CSDN博客-前端领域博主
🐱‍🐉若此文你认为写的不错,不要吝啬你的赞扬,求收藏,求评论,求一个大大的赞!👍*
🐞🐞文章若有错误或某个内容有更优的解决方案,还望指正!👍*
更多webpack入门知识,详细解读webpack的基本使用、进阶优化等;请阅读专栏:sam9029–CSDN博客—Webpack入门笔记
或者访问 作者个人博客网站sam9029.asia—Webpack入门笔记


Include/Exclude

目的:提升打包构建的速度—用于大型项目

为什么

开发时我们需要使用第三方的库或插件,所有文件都下载到 node_modules 中了。而这些文件是不需要编译可以直接使用的。

比如:我们在对 js 文件处理时,要排除 node_modules 下面的文件。

是什么

  • include

包含,只处理 xxx 文件

  • exclude

排除,除了 xxx 文件以外其他文件都处理

怎么用

const path = require("path");
const ESLintWebpackPlugin = require("eslint-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./src/main.js",
  output: {
    // 其他配置
  },
  module: {
    rules: [
      {
        oneOf: [
          // 其他配置
          {
            test: /\.js$/,
            // exclude: /node_modules/, // 排除node_modules代码不编译
            include: path.resolve(__dirname, "../src"), // 也可以用包含
            loader: "babel-loader",
          },
        ],
      },
    ],
  },
  plugins: [
    new ESLintWebpackPlugin({
      // 指定检查文件的根目录
      context: path.resolve(__dirname, "../src"),
      exclude: "node_modules", // 默认值
    }),
    // 其他配置
  ],
  // 开发服务器
  devServer: {
    // 其他配置
  },
  mode: "development",
};

生产模式也是如此配置。

如果你使用的是 UmiJS 框架,你可以在配置文件中使用 `monaco-editor-webpack-plugin` 插件。 首先,你需要安装 `monaco-editor-webpack-plugin`,可以使用以下命令进行安装: ``` npm install monaco-editor-webpack-plugin --save-dev ``` 接下来,在 UmiJS 的配置文件 `config/config.js` 中,你可以添加以下代码来配置 `monaco-editor-webpack-plugin` 插件: ```javascript import MonacoWebpackPlugin from 'monaco-editor-webpack-plugin'; export default { // ... chainWebpack(config, { webpack }) { config.plugin('monaco-editor').use(MonacoWebpackPlugin, [ { // Languages are loaded on demand at runtime languages: ['json'], // Optional: includes will be used to determine which files to include includes: ['**/*.json'], // Optional: excludes will be used to determine which files to exclude excludes: ['node_modules'], }, ]); }, // ... }; ``` 这个配置将会加载 `monaco-editor-webpack-plugin` 插件,并且添加了一个 `MonacoWebpackPlugin` 插件实例。这个插件可以根据你的配置来打包和加载 `monaco-editor` 的语言包,这里示例中只加载了 `json` 语言包。 最后,你需要在你的组件中导入 `monaco-editor`,并且进行使用: ```javascript import React from 'react'; import MonacoEditor from 'react-monaco-editor'; class MyComponent extends React.Component { render() { return ( <MonacoEditor language="json" theme="vs-dark" defaultValue="{}" onChange={this.onChange} editorDidMount={this.editorDidMount} /> ); } } ``` 在这个组件中,我们使用了 `react-monaco-editor` 库来创建一个 `MonacoEditor` 组件,并且指定了语言为 `json`,主题为 `vs-dark`,默认值为 `{}`。你可以根据你的需求来调整这些参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值