Как я могу создать приложение React с Webpack и импортировать папку ресурсов? - PullRequest
0 голосов
/ 11 ноября 2019

Моя основная проблема заключается в том, что я пытаюсь создать приложение React, но папка ресурсов отсутствует, и я не знаю, как импортировать и настроить его в webpack.conf. Другая проблема - относительный маршрут index.html: я не знаю, будет ли это затронуто в сборке приложения.

Заранее спасибо.

Дерево приложений

enter image description here

index.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>App</title>
    <base href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="shortcut icon" href="src/assets/favicon.ico"/>
</head>

<body>
    <div id="app"></div>
</body>

</html>

Webpack Config:

const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {

  entry: './src/index.tsx',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js'
  },
  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: [".ts", ".tsx", ".js", ".jsx", ".json"]
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: "awesome-typescript-loader"
      },
      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      { enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
      {
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "sass-loader"
        ]
      }
    ]
  },
  plugins: [
    new HTMLWebpackPlugin({
      template: 'public/index.html'
    }),
    new MiniCssExtractPlugin("style.css")
  ],
  // Enable sourcemaps for debugging webpack's output.
  devtool: "source-map",
  devServer: {
    historyApiFallback: true,
    port: 3000
  }
};

1 Ответ

0 голосов
/ 11 ноября 2019

Вы можете использовать webpack-copy-plugin для копирования дополнительных папок / файлов в процессе сборки

new CopyPlugin([{ 
  from: path.resolve(__dirname, 'src', 'assets'), 
  to: path.resolve(__dirname, 'build', 'assets') 
}])
...