Webpack解析扩展"未找到模块"

时间:2016-09-05 02:35:25

标签: javascript node.js reactjs webpack

警告:我是webpack的新手,请保持温和

好的,所以我试图建立webpack,我用es2015一切正常,并用webpack导入。我遇到了一个问题,现在我尝试将扩展程序添加到resolve,以便我不必声明文件扩展名,但它总是说它可以&# 39;找到文件或模块。知道为什么会这样吗?这是我的index.jsx和我的webpack.config.js

// index.jsx
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import { Provider } from 'react-redux'

import App from './components/App'
import configureStore from './store/configureStore.js'

const store = configureStore()

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('app')
)

这里是webpack.config

// webpack.config
var path = require('path')
var webpack = require('webpack')

module.exports = {
    devtool: 'eval',
    entry: [
        'webpack-dev-server/client?http://localhost:3000',
        'webpack/hot/only-dev-server',
        './src/javascripts/index.jsx'
    ],
    output: {
        path: path.join(__dirname, '/dist'),
        filename: 'bundle.js',
        publicPath: '/public/'
    },

    plugins: [
        new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      __DEV__: process.env.NODE_ENV !== 'production'
    })
    ],

    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                loaders: [ 'react-hot', 'babel-loader' ],
        include: path.join(__dirname, '/src')
            },
      {
        test: /\.scss$/,
        loader: [ 'style', 'css', 'sass' ]
      },
      {
        test: /\.json$/,
        loader: 'json'
      }
        ],

        preLoaders: [
            { test: /\.js$/, loader: 'source-map-loader' }
        ],

    resolve: {
      root: [
        path.resolve(path.join(__dirname, 'src', 'stylesheets')),
        path.resolve(path.join(__dirname, 'src', 'javascripts'))
      ],
      alias: {
        '@style': path.resolve(path.join(__dirname, 'src', 'stylesheets')),
        '@script': path.resolve(path.join(__dirname, 'src', 'javascripts'))
      },
      extensions: [
        '',
        '.webpack.js',
        '.web.js',
        '.jsx',
        '.js',
        '.scss'
      ]
    }
    }
};

正如我所说的,如果我将index.jsx中的第5行从import App from './components/App'更改为import App from './components/App.jsx',它就可以工作,我不知道为什么。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的module正在绕着您的resolve,这会破坏配置文件。如果resolve不在那里,则Webpack无法解析您的扩展程序,从而强制您添加扩展程序。您应该始终统一缩进以跟踪和防止这些类型的错误。

如果您在没有扩展程序的情况下导入它,Webpack不知道如何解析扩展程序,因此无法正确加载扩展程序。你必须指定.jsx,然后Babel进来并明确地将其作为.jsx导入。