2

I'd like to load modules using just a folder name, without specifying file name in that specific folder. Like it's done here in react-starter-kit: enter image description here

I imagine that this is needed to be done in Webpack configuration, but I cannot wrap my head around that yet (I'm fresh to javascript).

My app is structured this way atm and I have to reference both folder and file name in order to import it.

enter image description here That's my current webpack.config.js:

const webpack = require('webpack');
const path = require('path');
const autoprefixer = require('autoprefixer');
const htmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  devtool: '#cheap-module-eval-source-map',
  entry: [
    'webpack-hot-middleware/client',
    'tether',
    'font-awesome-loader',
    'bootstrap-loader',
    './app/App',
  ],
  output: {
    path: path.join(__dirname, 'public'),
    filename: 'app.js',
    publicPath: '/',
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [{
      test: /\.jsx?$/,
      loader: 'babel-loader',
      exclude: /node_modules/,
      query: {
        presets: ['es2015', 'react', 'stage-0']
      }
    }, {
      test: /\.css$/,
      loaders: [
        'style',
        'css?modules&importLoaders=1&localIdentName=[name]__[local]__[hash:base64:5]',
        'postcss'
      ]
    }, {
      test: /\.scss$/,
      loaders: [
        'style',
        'css?modules&importLoaders=2&localIdentName=[name]__[local]__[hash:base64:5]',
        'postcss',
        'sass'
      ]
    }, {
      test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
      loader: "url?limit=10000"
    }, {
      test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
      loader: 'file'
    }]
  },
  postcss: [autoprefixer],
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new htmlWebpackPlugin({
      title: 'MoiApp'
    }),
    new webpack.ProvidePlugin({
      "window.Tether": "tether"
    })
  ]
};

However I could not find anything useful on Google. Perhaps I'm making my query not correct enough. Please advice on how to achieve that.

1 Answer 1

3

There are different ways to achieve this. Without using some special plugins, you can get this behaviour by putting a pretty small package.json file into the folder with an entry main pointing to the file to be used like it is done in the react starter kit.

{
  "name": "Header",
  "version": "0.0.0",
  "private": true,
  "main": "./Header.js"
}

Another option is to rename the main in every folder to index.js. For details and resolving orders please see the docs at:

https://webpack.github.io/docs/resolving.html

There is also a plugin that helps you avoiding to rename all your files to index.js. I don't have experience with that:

https://www.npmjs.com/package/directory-named-webpack-plugin

Sign up to request clarification or add additional context in comments.

1 Comment

This looks exactly it. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.