Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set root directory for webpack

I am configuring webpack for react storybook and I am having issues with the import statements paths. When I normally run my app, I have a path like this:

import { constructURLName } from "src/shared/utilities/stringManipulation";

I am using the reactQL boilerplate and it is configured so that it will return the proper module based on the src folder. How can I replicate that in storybook? Storybook currently tries to look for that stringManipulation file here:

/Users/me/development/reactQL/node_modules/src/shared/utilities/stringManipulation

So it needs to go up one directory based on my layout:

node_modules
src
  pages
  ...
  shared
    utilities
      stringManipulation

This is what my rule looks like for js/jsx files:

      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "babel-loader",
            query: {
              babelrc: false,
              presets: [
                ["env", { modules: false, exclude: ["transform-regenerator"] }],
                "react"
              ],
              plugins: [
                "transform-object-rest-spread",
                "syntax-dynamic-import",
                "transform-regenerator",
                "transform-class-properties",
                "transform-decorators-legacy"
              ]
            }
          }
        ]
      }, 

How can I properly set the src directory? Thanks!

like image 580
Coherent Avatar asked Jul 12 '17 12:07

Coherent


People also ask

What is webpack public path?

publicPath specifies the virtual directory in web server from where bundled file, app. js is going to get served up from. Keep in mind, the word server when using publicPath can be either webpack-dev-server or express server or other server that you can use with webpack.

Where do I put webpack config?

The easiest way to get your webpack. config. js file to work is to place it at the top level of your project's folder structure along with your package.

What is webpack entry point?

The entry object is where webpack looks to start building the bundle. The context is an absolute string to the directory that contains the entry files.


Video Answer


1 Answers

You can use resolve.modules.

const path = require('path');
const config = {
    ...
    resolve: {
        modules: [
            path.resolve(__dirname, 'node_modules'),
            path.resolve(__dirname, './'),
        ]
    }
like image 146
Stanislav Mayorov Avatar answered Nov 09 '22 19:11

Stanislav Mayorov