简体   繁体   English

根目录外的 webpack /src

[英]webpack /src outside root directory

I'm trying to specify a webpack entry point that's not located underneath my react project's root directory.我正在尝试指定一个不在我的 React 项目根目录下的 webpack 入口点。 Essentially I want to move my whole /src directory somewhere else so it can be shared.本质上,我想将我的整个/src目录移动到其他地方,以便可以共享。 Here's my directory structure & webpack config:这是我的目录结构和 webpack 配置:

monorepo/
|-- webpack-project/
|-- |-- package.json
|-- |-- webpack.config.json
|-- |-- old-src/
|-- |-- |-- index.js
|-- other-project
|-- |-- src/
|-- |-- |-- index.js
module.exports = {
    entry: {
        app: `${__dirname}/../other-project/src/index.js`
    },
    output: {
        path: path.resolve(`${__dirname}/wwwroot`),
        publicPath: '/',
        filename: '[name].js',
    },
    module: {
        rules: [
            { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
            { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
        ],
    },
}

When I specify ${__dirname}/old-src/index.js , my project builds fine.当我指定${__dirname}/old-src/index.js ,我的项目构建良好。 When I specify the path to the other-project/src it has issues.当我指定其他项目/src 的路径时,它有问题。 The error I'm getting from the babel loader is at the first line of JSX in the entry file: Support for the experimental syntax 'jsx' isn't currently enabled .我从 babel 加载器得到的错误是在入口文件中 JSX 的第一行: Support for the experimental syntax 'jsx' isn't currently enabled

Does anyone know if what I'm trying to do is even possible, or if I should take a different approach?有谁知道我正在尝试做的事情是否可行,或者我是否应该采取不同的方法?

Add path as dev dependency and use it:添加path作为开发依赖项并使用它:

const path = require('path');
...
app: path.join(__dirname, '../other-project/src/index.js'),

Fixed by making 2 changes:通过进行 2 个更改修复:

The first major one, I renamed by .babelrc to babel.config.js.第一个主要的,我通过 .babelrc 重命名为 babel.config.js。 More about why this fixes an issue here .更多关于为什么这在这里解决了问题。

I also added resolve.modules config to add the webpack-project's node_modules.我还添加了 resolve.modules 配置来添加 webpack-project 的 node_modules。

  ...

  resolve: {
      modules: [ path.resolve(__dirname, 'node_modules') ],
  },

  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM