首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack-dev-server在publicPath:'/‘时使用"webpack-dev-server“作为路径

Webpack-dev-server在publicPath:'/‘时使用"webpack-dev-server“作为路径
EN

Stack Overflow用户
提问于 2019-02-04 17:58:29
回答 1查看 1.1K关注 0票数 0

试图配置webpack开发服务器,使其工作在一个现有的项目上,而webpack以前是在没有开发服务器的情况下配置的。我在webpack配置中添加了以下内容:

我以这样的方式创建了一个webpack.dev.config.js文件:

代码语言:javascript
复制
const path = require('path');
const config = require('./webpack.config');
const Webpack = require('webpack');

config.devServer = {
  contentBase: path.join(__dirname, 'public'),
  disableHostCheck: true,
  historyApiFallback: true,
  host: '0.0.0.0',
  hot: true,
  port: process.env.PORT || 7031,
  publicPath: '/',
  watchOptions: {
    poll: true,
    ignored: /node_modules/,
  },
};

config.plugins.push(new Webpack.NamedModulesPlugin(), new Webpack.HotModuleReplacementPlugin());

module.exports = config;

它使用package.json中的npm命令执行:

代码语言:javascript
复制
webpack-dev-server --watch-poll --inline --config webpack.dev.config.js --colors --progress -d

webpack包装正在使用:

代码语言:javascript
复制
    "webpack": "^3.12.0",
    "webpack-dev-server": "^2.5.0"

webpack.config.js:

代码语言:javascript
复制
const autoprefixer = require('autoprefixer');
const precss = require('precss');
const shell = require('child_process').exec;
const path = require('path');
const Webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const target = process.env.TARGET || 'dev';
process.env.TARGET = target;
console.info(`Building for target [${target}]`);

const javascriptPath = path.resolve('./src/javascripts')

module.exports = {
  module: {
    rules: [
      {
        test: /\.jsx$/,
        include: javascriptPath,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: ['es2015', 'react', 'stage-0'],
            },
          },
        ],
      },
      {
        test: /\.js$/,
        include: javascriptPath,
        use: [
          {
           loader: 'babel-loader',
            options: {
              presets: ['es2015', 'react', 'stage-0'],
            },
          },
        ],
      },
      {
        test: /\.coffee$/,
        include: javascriptPath,
        use: [
          {
            loader: 'coffee-loader',
          },
        ],
      },
      {
        test: /\.s(a|c)ss$/,
        include: path.resolve('./src'),
        use: [
          {
            loader: 'style-loader',
            options: {
              singleton: true,
            },
          },
          {
            loader: 'css-loader',
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: [
                require('autoprefixer'),
                require('precss'),
              ],
            },
          },
          {
            loader: 'resolve-url-loader',
          },
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true,
            },
          },
        ],
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader',
            options: {
              singleton: true,
            },
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: [
                require('autoprefixer'),
                require('precss'),
              ],
            },
          },
        ],
      },
      {
        test: /\.(ico|png|jpg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              name: 'images/img-[hash:6].[ext]',
            },
          },
        ],
      },
      {
        test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
        use: [
          {
            loader: 'svg-inline-loader',
          },
        ],
      },
      {
        test: /\.json$/,
        use: [
          {
            loader: 'json-loader',
          },
        ],
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'raw-loader',
          },
        ],
      },
    /*
      {
        test: /index\.hamlc$/,
        use: [
          {
            loader: 'haml-loader',
            options: {
              target: target + '!' + path.resolve('./src/views/index.hamlc'),
              filename: path.resolve('./public/index.html'),
            },
          },
        ],
      },
    */
    ],
  },
  entry: {
    'configuration': './build_data/config.js',
    'application': './src/javascripts/index.js.coffee',
    'settings/client': './src/javascripts/settings_client.js',
  },
  output: {
    path: path.resolve('./public/dist'),
    filename: '[name]-[hash:6].js'
  },
  resolve: {
    extensions: ['*', '.js', '.jsx', '.scss'],
    alias: {
      images: './public/images',
    },
  },
  plugins: [
    new CleanWebpackPlugin(['public/dist', 'public/index.html'], {
      root: path.resolve('.'),
      verbose: true,
      dry: false
    }),
    new HtmlWebpackPlugin({
      template: 'haml-loader?target=' + target +
        '!' + path.resolve('./src/views/index.hamlc'),
      filename: path.resolve('./public/index.html'),
    }),
    new Webpack.EnvironmentPlugin(['TARGET']),
    new Webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production'),
      },
    }),
  ],
};

当我运行webpack时,我得到了这个(产出的上半部分):

代码语言:javascript
复制
File build_data/config.js created
Building for target [dev]
clean-webpack-plugin: /Users/xxxxx/src/xxxxxx/xxxxxxx/public/dist has been removed.
clean-webpack-plugin: /Users/xxxxx/src/xxxxxx/xxxxxxx/public/index.html has been removed.
 10% building modules 3/3 modules 0 active                                  Project is running at http://0.0.0.0:7031/
webpack output is served from /
Content not from webpack is served from /Users/xxxxx/src/xxxxxx/xxxxxxx/public
404s will fallback to /index.html
Hash: e26133d6d376997a1da1                                                  r Version: webpack 3.12.0
Time: 28385ms
                                                  Asset     Size  Chunks                    Chunk Names
./images/loading.gif?h=de93ac1f9c3e69e58a5e877e73f1e9e2   3.7 MB          [emitted]  [big]
                                  application-e26133.js  23.1 MB       0  [emitted]  [big]  application
                              settings/client-e26133.js   912 kB       1  [emitted]  [big]  settings/client
                                configuration-e26133.js   912 kB       2  [emitted]  [big]  configuration
                                          ../index.html  10.2 kB          [emitted]

然而,即使上面写着Project is running at http://0.0.0.0:7031/,您也只能通过http://127.0.0.1:7031/webpack-dev-server获得它。

我需要它以http://127.0.0.1:7031/作为它的根,而不是添加的/webpack-dev-server,我不知道是什么导致了这种情况。

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2019-02-04 18:35:33

我可能已经解决了我的问题..。

webpack.config.js,中,我做了以下工作:

将这一行添加到顶部:

代码语言:javascript
复制
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');

编辑输出并添加publicPath

代码语言:javascript
复制
   output: {
     path: path.resolve('./public/dist'),
     filename: '[name]-[hash:6].js',
     publicPath: '/'

然后通过添加硬盘插件来编辑插件:

代码语言:javascript
复制
    new HtmlWebpackPlugin({
      alwaysWriteToDisk: true,
      template: 'haml-loader?target=' + target +
        '!' + path.resolve('./src/views/index.hamlc'),
      filename: path.resolve('./public/index.html'),
    }),
    new HtmlWebpackHarddiskPlugin(),

添加到package.json

代码语言:javascript
复制
"html-webpack-harddisk-plugin": "^1.0.1",

到目前为止的行为和预期的一样..。我需要做更多的测试,以确保一切都符合开发所需的方式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54521785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档