Javascript 找不到 bundle.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39049576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:09:17  来源:igfitidea点击:

Can't find bundle.js

javascriptreactjstypescriptwebpack

提问by Jay

I know this question has been asked before, but I honestly can't find the answer anywhere-- it appears as if I'm doing everything I should however bundle is not created. So I have this webpack.config.jsfile that's supposed to handle HMR + React, and typescript (with tsx syntax), but it's not creating the bundle. Throws no errors on compilation, and seems to be doing alright, but the bundle returns a 404 when I try to fetch it. Here's my file structure:

我知道以前有人问过这个问题,但老实说,我在任何地方都找不到答案——似乎我正在做我应该做的一切,但是没有创建捆绑包。所以我有这个webpack.config.js文件应该处理 HMR + React 和打字稿(使用 tsx 语法),但它没有创建包。在编译时没有抛出任何错误,并且似乎运行正常,但是当我尝试获取它时,该包返回 404。这是我的文件结构:

someApp/
  src/
    index.tsx
    components/
      Hello.tsx
  dist/
  webpack.config.js
  ...

And here's my webpack config:

这是我的 webpack 配置:

var path = require('path')
var webpack = require('webpack')

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

    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ],

    devtool: 'eval',

    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
    },

    module: {
        loaders: [
            {
                test: /\.tsx?$/,
                loaders: [
          'react-hot-loader',
          'ts-loader'
        ],
        include: path.join(__dirname, '/src')
            }
        ],

        preLoaders: [
            { test: /\.js$/, loader: 'source-map-loader' }
        ]
    },
    externals: {
        'react': 'React',
        'react-dom': 'ReactDOM'
    },
};

Another strange thing is that I think it might have something to do with executing this through node, since when I just run webpackby itself it compiles the bundle. Here's my code for starting up the server:

另一个奇怪的事情是,我认为它可能与通过 node 执行它有关,因为当我webpack自己运行时,它会编译包。这是我启动服务器的代码:

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
  publicPath: config.output.publicPath,
  hot: true,
  historyApiFallback: true
}).listen(3000, 'localhost', function (err, result) {
  if (err) {
    return console.log(err)
  }

  console.log('Listening at http://localhost:3000/')

})

Maybe I'm missing something, but I'm pretty new to webpack so any help would be amazing!

也许我遗漏了一些东西,但我对 webpack 还很陌生,所以任何帮助都会很棒!

回答by David

Thats because webpack-dev-server is serving bundle.js from memory. This is done to make serving bundle.js fast. You can add another webpack config for production that spits out bundle.js to disk

那是因为 webpack-dev-server 从内存中提供 bundle.js。这样做是为了使服务 bundle.js 更快。您可以为生产添加另一个 webpack 配置,将 bundle.js 输出到磁盘

module.exports = {
    entry: './src/index.tsx',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js',
        publicPath: '/public/'
    },
.
.
.

and all your other modules, just don't include your dev-server

和所有其他模块,只是不包括您的开发服务器

if you want to fetch bundle.js like localhost:3000//..../bundle.js

如果你想像 localhost:3000//..../bundle.js 一样获取 bundle.js

try this

尝试这个

var path = require('path')
var webpack = require('webpack')

module.exports = {
    entry:'./src/index.tsx',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js',
        publicPath: '/public/'
    },

    //plugins: [
      //  new webpack.HotModuleReplacementPlugin()
    //],

    devtool: 'eval',

    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
    },

    module: {
        loaders: [
            {
                test: /\.tsx?$/,
                loaders: [
          'react-hot-loader',
          'ts-loader'
        ],
        include: path.join(__dirname, 'src')
            }
        ],

        preLoaders: [
            { test: /\.js$/, loader: 'source-map-loader' }
        ]
    },
    externals: {
        'react': 'React',
        'react-dom': 'ReactDOM'
    },
};

EDIT: If you want to fetch bundle.js

编辑:如果你想获取 bundle.js

you have to use what is defined in the publicPath: '/public/'. so for you the url you can fetch bundle.js from is this localhost:3000/public/bundle.js

您必须使用 publicPath 中定义的内容:'/public/'。所以对你来说,你可以从中获取 bundle.js 的 url 是 localhost:3000/public/bundle.js

回答by alexi2

I think you need to change your output to this:

我认为您需要将输出更改为:

output: {
    path: path.join(__dirname, '/dist'), // <- change last argument
    filename: 'bundle.js',
    publicPath: '/public/'
},