4

我正在尝试将 webpack 引入具有以下文件夹结构的遗留项目:

-web
    -project
       - webpack.config.js
       - package.json
    -src
       - client.js

每次我在项目文件夹中运行 webpack --config webpack.config.js 时,我都会收到以下错误:找不到入口模块中的错误:错误:无法解析 ......\web 中的'babel-loader\。

我将上下文设置为 web 文件夹,因为它是配置和 src 的父级。我还将 src 和 node 模块都添加到了 resolve.modules 属性中。是否有我缺少的配置?

我的 webpack 配置:

const path = require('path');

const isProduction = process.env.NODE_ENV === 'production';
const webpack = require('webpack');


const babelConfig = {
  "presets": [
    "@babel/preset-env",
    "react"
  ],
  "plugins": [
    "transform-runtime",
    "add-module-exports",
    "transform-export-extensions"
  ],
  "env": {
    "development": {
      "plugins": []
    }
  }
};
const base = path.resolve(__dirname, '..');
const project = path.resolve(base, 'project');
const src = path.resolve(base, 'src');

module.exports = {
  context: base,
  resolve: {
    modules: [path.resolve(project, 'node_modules'), src],
    extensions: ['.js', '.jsx']
  },
  entry: {
    plp: path.resolve(src, 'client.js')
  },
  devtool: '#inline-source-map',
  output: {
    filename     : '[name].js',
    chunkFilename: '[name].js',
    path: __dirname + "/../dist"
  },
  stats: {
    colors: true,
    modules: true,
    reasons: true,
    errorDetails: true
  },
  module: {
    rules: [{
      test: /\.jsx?$/,
      include: [src],
      use: [{
        loader: 'babel-loader',
        query: babelConfig
      }]
    }]
  },
  plugins: [
    new webpack.DefinePlugin({
      __DEVELOPMENT__: !isProduction,
      __DEVTOOLS__: !isProduction
    }),

    new webpack.IgnorePlugin(/webpack-stats\.json$/)
  ]
};

包.json:

{
  "dependencies": {
    "babel-cli": "^6.22.2",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.22.0",
    "babel-preset-es2016": "https://registry.npmjs.org/babel-preset-es2016/-/babel-preset-es2016-6.22.0.tgz",
    "babel-preset-react": "^6.24.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2017": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "webpack": "^3.8.1"
  }
}

提前致谢!

4

0 回答 0