javascript - Webpack - 应用程序文件夹外部的文件夹访问

标签 javascript reactjs webpack configuration

我有代码库,其文件夹结构如下所示:

|- build\
|- node_modules\
|- apps\
|--- app_no_1\
|----- index.js
|- src\
|--- modules\
|----- form-login\
|------- Form.jsx
|- package.json
|- webpack.config.js
....

app_no_1\ 文件夹保存其 React 应用程序的索引文件。但是,这些模块位于 src\ 文件夹中。当我将组件从 src 目录导入应用程序时,出现错误:

bundle.js:41448 Uncaught Error: Module parse failed: Unexpected token (15:18)
You may need an appropriate loader to handle this file type.
| // );

我是否缺少一些访问应用程序文件夹之外的文件所需的 webpack 配置选项?我的 webpack.config.js 是这样的:

const path = require('path');
const merge = require('merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const PATHS = {
  app: path.join(__dirname, 'apps'),
  appAthenaTrader: path.join(__dirname, 'apps/athenaTrader'),
  appAthenaFinancier: path.join(__dirname, 'apps/athenaFinancier'),
  build: path.join(__dirname, 'build'),
  modules: path.join(__dirname, 'src/modules')
};

const common = {
  output: {
    path: PATHS.build,
    publicPath: '/',
    filename: 'bundle.js'
  },
  resolve: {
    alias: {
      modules: PATHS.modules
    },
    extensions: ['.js', '.jsx', '.json'],
    modules: [PATHS.modules, 'node_modules']
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        include: PATHS.app,
        use: [
          {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true
            }
          }
        ]
      },
      {
        test: /\.scss$/,
        include: PATHS.app,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'sass-loader'
          }
        ]
      },
      {
        test: /\.(jpg|png)$/,
        include: PATHS.app,
        use: {
          loader: 'file-loader?name=[name].[ext]'
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Satoshi Ltd - Athena'
    })
  ]
};

const generateEntry = env => {
  const entryVariable = {
    entry: {
      app: ''
    }
  };
  if (env.app === 'athena-trader') {
    entryVariable.entry.app = PATHS.appAthenaTrader;
  } else if (env.app === 'athena-financier') {
    entryVariable.entry.app = PATHS.appAthenaFinancier;
    // } else ...
  }
  return entryVariable;
};

const devServer = {
  devServer: {
    stats: 'errors-only',
    host: process.env.HOST || 'localhost',
    port: process.env.PORT || 3000
  },
  devtool: 'inline-source-map'
};

const generateConfig = env => {
  const entry = generateEntry(env);
  if (env.profile === 'development') {
    return merge(common, entry, devServer);
  }
  return merge(common, entry);
};

module.exports = generateConfig(process.env);

我应该注意到,当该文件夹被放入 app_no_1 中时,应用程序功能正常,即它能够执行该组件并显示它。但是,应用程序不接受上述文件夹结构。

最佳答案

问题出在你的 babel-loader 配置中。 Webpack 提示它不知道如何解析你的文件(我假设它是 JSX)。

在您的配置中,您有:

module: {
    rules: [
      {
        test: /\.jsx?$/,
        include: PATHS.app,
        use: [
          {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true
            }
          }
        ]
      },
      // ...
    ]

include 告诉 webpack 在位于 PATHS.app 内的任何文件上使用 babel-loader。当它查看位于 PATHS.modules 中的文件时,它不会使用 babel-loader。此时 webpack 显示 Module parse failed 错误。

要解决此问题,您可以将 include 值更新为如下所示:

include: [PATHS.app, PATHS.modules]

另一种方法是使用exclude 而不是include

// assuming you want to only ignore node_modules
exclude: /node_modules/ 

我还在 Github 上做了一个简单的例子.

关于javascript - Webpack - 应用程序文件夹外部的文件夹访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48868289/

相关文章:

javascript - 如何仅使用源 URL 而不是原始文件来调整图像大小?

javascript - 如何在 Vue 中加载 cdn 并访问它的对象?

webpack - 我无法将 ipfs-core 与 webpack 捆绑在一起。找不到模块 : Error: Can’t resolve ‘util’

javascript - 无法从 TypeScript 中的模块/命名空间导出/导入类

reactjs - 测试库 React 在异步后不会触发点击

Webpack 构建成功,但没有退出就挂了

javascript - Dart 中的引用错误 : function is not defined when pressing a button

javascript - 使用 vanilla JS 添加 CSS 规则的最短方法

javascript - 根据Route显示不同的内容 - Node.js/React

android - 在 React Native Android 应用程序中显示图像