question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack dev server respond 404 if set entry name or output.fileName with prefix ‘/’.

See original GitHub issue

I’m submitting a bug report

Webpack version: 2.1.0-beta.25 webpack-dev-server@2.1.0-beta.9

Please tell us about your environment: Linux

Current behavior: If set entry name or output.fileName with prefix ‘/’,the entry file will be responded as 404.

Expected/desired behavior:

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem along with a gist/jsbin of your webpack configuration.
const webpack = require('webpack');
const path = require('path');

module.exports = {
    entry: {
        '/app': './src/index.js',
    },

    output: {
        path: path.resolve(__dirname, "dist"),
        filename: '[name].js'
    },

    devServer: {
        historyApiFallback: true,
        port: 3000,
        watchOptions: {
            aggregateTimeout: 300,
            poll: 1000
        }
    }
};

OR

output: {
        path: path.resolve(__dirname, "dist"),
        filename: '/[name].js'
 },
  • What is the expected behavior?
  • What is the motivation / use case for changing the behavior?
  • Browser: all
  • Language: all

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dhardtkecommented, Oct 28, 2016

I’m having a similar issue that does not occur with

"webpack-dev-server": "1.14.1",
"webpack-dev-middleware": "1.8.4",

This is my webpack.common.js:

const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const helpers = require("./helpers");

module.exports = {
    cache: true,

    resolve: {
        extensions: [".ts", ".js", ".json"],

        // An array of directory names to be resolved to the current directory
        modules: [
            helpers.root("app"),
            "node_modules"
        ]
    },

    module: {
        loaders: [
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: "file?name=assets/[name].[hash].[ext]"
            },
            {
                // only embed css files that are being required in app/ due to angular2-template-loader
                test: /\.css$/,
                include: helpers.root("app"),
                loader: "to-string-loader!css-loader"
                // alternative: raw
            },
            {
                // embed and load (inject) css files that are everywhere but in app
                test: /\.css$/,
                exclude: helpers.root("app"),
                loader: "to-string-loader!style-loader!css-loader"
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                loaders: ["raw-loader", "sass-loader"] // sass-loader not scss-loader
            },
            {
                test: /\.html$/,
                loader: "html"
                // alternative: raw
            }
        ]
    },

    // see https://github.com/webpack/webpack/issues/1716 regarding trailing slashes
    output: {
        path: helpers.root("dist/"),
        publicPath: "/dist/",
        filename: "[name].[hash].js",
        chunkFilename: "[id].[hash].chunk.js"
    },

    plugins: [
        // correct order of assets inclusion
        new webpack.optimize.CommonsChunkPlugin({
            name: ["polyfills", "vendor"].reverse()
        }),

        new HtmlWebpackPlugin({
            template: "./app/index.tpl.html",
            filename: "../index.html",
            chunksSortMode: "dependency",
            minify: false
        }),

        new webpack.LoaderOptionsPlugin({
            options: {
                minify: true,
                htmlLoader: {
                    removeAttributeQuotes: false,
                    caseSensitive: true
                }
            }
        }),

        // see https://github.com/AngularClass/angular2-webpack-starter/issues/993#issuecomment-246883410
        new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/, __dirname)
    ]
};

and this is my webpack.dev.js:

const webpackMerge = require("webpack-merge");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const commonConfig = require("./webpack.common.js");
const helpers = require("./helpers");

module.exports = webpackMerge(commonConfig, {
    entry: {
        "polyfills": "./app/polyfills.ts",
        "app":       "./app/main.ts",
        "vendor":    "./app/vendor.ts"
    },

    module: {
        loaders: [
            {
                test:    /\.ts$/,
                loaders: [
                    "awesome-typescript-loader",
                    "angular2-template-loader",
                    "angular2-router-loader?aot=false"
                ]
            }
        ]
    },

    //devtool: "cheap-module-eval-source-map",

    output: {
        path:          helpers.root("dist"),
        publicPath:    "/",
        filename:      "[name].js",
        chunkFilename: "[id].chunk.js"
    },

    plugins: [
        new ExtractTextPlugin("[name].css")
    ],

    devServer: {
        historyApiFallback: true,
        stats:              "minimal",
        proxy:              {
            "/rest": {
                target: "http://127.0.0.1:8080",
                secure: true
            }
        }
    }
});

If I use webpack-dev-server@1.14.1 everything is fine, but with webpack-dev-server@2.1.0-beta.9 the resulting index.html is minified (which it shouldn’t be), also the <script> and <link> tag URLs are wrong.

Instead of <script src="/app.js"></script> I get <script src="/dist/app.arandomhash.js"></script>.

Can’t use 2.1.0-beta.9 then 😦

0reactions
ibufucommented, Oct 31, 2016

@SpaceK33z, thank you very much, I will help you as far as I can:).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack dev server respond 404 if set entry name or output ...
Current behavior: If set entry name or output.fileName with prefix '/',the entry file will be responded as 404. If the current behavior is...
Read more >
Output - webpack
The top-level output key contains a set of options instructing webpack on how and where it should output your bundles, assets, and anything...
Read more >
API - esbuild
To join a set of files together with esbuild, import them all into a single entry point file and bundle just that one...
Read more >
webpack-dev-server - npm
Usage: webpack serve|server|s [entries...] [options] Run the webpack dev server. Options: -c, --config <value...> Provide path to a webpack ...
Read more >
DevServer - webpack 3 documentation
With filename , it's possible to only compile when a certain file is requested. If output.filename is set to bundle.js and filename is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found