简体   繁体   English

如何使用Webpack准备应用程序

[英]how to ready react app with webpack

I'm new to React and Webpack and all this stuff. 我是React和Webpack以及所有这些东西的新手。 I've created a React app with Webpack and I used webpack-dev-server to create and debug my app. 我已经使用Webpack创建了一个React应用,并且使用webpack-dev-server创建和调试了我的应用。

So in my webpack.config.js file I have this code: 所以在我的webpack.config.js文件中,我有以下代码:

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  context: path.join(__dirname, "src"),
  devtool: debug ? "inline-sourcemap" : false,
  entry: "./js/client.js",
  module: {
    loaders: [
      {
        test: /\.js?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0'],
          plugins: ['react-html-attrs', 'transform-decorators-legacy', 'transform-class-properties'],
        }
      },
      {
        test: /\.scss$/,
        use: debug ? [{
            loader: "style-loader" // creates style nodes from JS strings
        }, {
            loader: "css-loader" // translates CSS into CommonJS
        }, {
            loader: "sass-loader" // compiles Sass to CSS
        }] : ExtractTextPlugin.extract({ fallback: "style-loader", use: "css-loader" })
      }
    ]
  },
  output: {
    path: __dirname + "/src/",
    filename: "client.min.js"
  },
  plugins: debug ? [] : [
    new ExtractTextPlugin('style.min.css'),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
  ],
  externals: {
    "jquery": "jQuery",
    "react": "React",
    "react-dom": "ReactDOM",
    "animejs": "anime"
  }
};

When I use webpack-dev-server --content-base src --inline --hot, I see my app working in localhost:8080 but now I want to make the app ready for production. 当我使用webpack-dev-server --content-base src --inline --hot时,我看到我的应用程序可以在localhost:8080上运行,但是现在我想让该应用程序投入生产。 so I ran these codes in my terminal: 所以我在终端中运行了以下代码:

$: NODE_ENV=production
$: webpack

It doesn't change anything! 它什么都不会改变! So first question: what is wrong with NODE_ENV=production ? 所以第一个问题: NODE_ENV=production什么问题吗? When I change the first line of my webpack to var debug = process.env.NODE_ENV === "production"; //false 当我将Webpack的第一行更改为var debug = process.env.NODE_ENV === "production"; //false var debug = process.env.NODE_ENV === "production"; //false it works. var debug = process.env.NODE_ENV === "production"; //false可以正常工作。

There are other problems! 还有其他问题! I'm using sass and When debug === false and I open my index.html file in browser, my styles aren't compiled! 我正在使用sass,并且当debug === false ,我在浏览器中打开index.html文件,我的样式未编译! Just all of my sass code is copied to style.min.css file The problem should be with this part of code: 我所有的style.min.css代码都复制到了style.min.css文件中。问题应该出在这部分代码上:

{
    test: /\.scss$/,
    use: debug ? [{
        loader: "style-loader" // creates style nodes from JS strings
    }, {
        loader: "css-loader" // translates CSS into CommonJS
    }, {
        loader: "sass-loader" // compiles Sass to CSS
    }] : ExtractTextPlugin.extract({ fallback: "style-loader", use: "css-loader" })
}

And the last problem is with absolute paths! 最后一个问题是绝对路径! I have this code in my app: 我的应用程序中包含以下代码:

<img src="/images/avatar.jpg">

It works when I use webpack-dev-server but when I use webpack, the image is not found as it tries to open it from the root of my linux. 当我使用webpack-dev-server时它可以工作,但是当我使用webpack时,找不到图像,因为它试图从linux的根目录打开它。

So these are my questions: 所以这是我的问题:

  1. why NODE_ENV=production doesn't work? 为什么NODE_ENV =生产不起作用?
  2. How should I compile sass and put the css in style.min.css? 我应该如何编译sass并将css放入style.min.css?
  3. How can I use absolute paths in my app? 如何在应用程序中使用绝对路径?

thanks in advance 提前致谢

How should I compile sass and put the css in style.min.css? 我应该如何编译sass并将css放入style.min.css?

Try following config 尝试以下配置

{
    test: /\.scss$/,
    use: debug ? [{
        loader: "style-loader" // creates style nodes from JS strings
    }, {
        loader: "css-loader" // translates CSS into CommonJS
    }, {
        loader: "sass-loader" // compiles Sass to CSS
    }] : ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: [
                        {
                            loader: "css-loader",
                            options: {
                                modules: true,
                            },
                        },
                        {
                            loader: "sass-loader",
                            options: {
                                modules: true,
                            },
                        },
                    ],
                }),
            },

How can I use absolute paths in my app? 如何在应用程序中使用绝对路径?

A better way to import images in react app is using file loader in webpack and then directly importing images in application like you import other modules. 在react应用程序中导入图像的更好方法是在webpack中使用文件加载器,然后像导入其他模块一样直接在应用程序中导入图像。 First add file-loader in webpack like this 首先像这样在webpack中添加文件加载器

{
  test: /\.(jpg|png)$/,
  loader: "file-loader",
},

and then the public path of your server in webpack 然后是webpack中服务器的公共路径

output: {
    path: __dirname + "/src/",
    publicPath: "http://localhost:3000/static",  // Your server public path
    filename: "client.min.js"
  },

Then in directly import image and put in src like this 然后在直接导入图像并像这样放入src

import avatar from "../images/avatar.jpg";

<img src={avatar} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM