webpack上下文和resolve.modules不解析node_modules

时间:2018-02-12 14:18:56

标签: webpack babel babel-loader

我试图将webpack引入具有以下文件夹结构的旧项目中:

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

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

我将上下文设置为web文件夹,因为它是config和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$/)
  ]
};

package.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"
  }
}

提前致谢!

0 个答案:

没有答案