2016-08-10 8 views
0

웹 응용 프로그램에서 webpack + react + redux를 사용하고 있습니다. 그리고 dev 웹 서버를 시작하려면 webpack-dev-server을 사용하고 있습니다. 내가 브라우저 내 응용 프로그램에 액세스 할 때 콘솔에 오류 메시지가 아래에 있습니다 :왜 webpack에서 약 NODE_ENV에 대한 오류가 발생합니까?

Warning: It looks like you're using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. 

You are currently using minified code outside of NODE_ENV === 'production'. This means that you are running a slower development build of Redux. You can use loose-envify to ensure you have the correct code for your production build. 

아래는 내 webpack.config.js 파일입니다. 나는 생산 모드를 지정하지 않았는데, 왜 webpack이 나에게 그런 경고 메시지를 주는지? 어떻게 내가 그걸 없앨 수 있니?

const webpack = require('webpack'); 
const path = require('path'); 
const NpmInstallPlugin = require('npm-install-webpack-plugin'); 
const WebpackShellPlugin = require('webpack-shell-plugin'); 
var CompressionPlugin = require("compression-webpack-plugin"); 

const PATHS = { 
    react: path.join(__dirname, 'node_modules/react/dist/react.min.js'), 
    app: path.join(__dirname, 'src'), 
    build: path.join(__dirname, './dist') 
}; 

module.exports = { 
    entry: { 
    app: './app/index.jsx', 
    android: './app/utils/platform_android.js', 
    ios: './app/utils/platform_ios.js', 
    web: './app/utils/platform_web.js', 
    vendor: [ 
     'axios', 
     'react', 
     'react-dom', 
     'react-redux', 
     'react-router', 
     'react-router-redux', 
     'redux', 
     'redux-thunk', 
     'react-alert', 
     'sha1', 
     'moment', 
     'nuka-carousel', 
     'react-cookie', 
     'material-ui', 
     'react-spinkit', 
     'react-tap-event-plugin', 
     'react-tappable', 
    ], 
    }, 
    output: { 
    path: PATHS.build, 
    filename: '[name].bundle.js', 
    }, 
    watch: true, 
    devtool: 'source-map', 
    relativeUrls: true, 
    resolve: { 
    extensions: ['', '.js', '.jsx', '.css', '.less'], 
    modulesDirectories: ['node_modules'], 
    alias: { 
     normalize_css: __dirname + '/node_modules/normalize.css/normalize.css', 
    } 
    }, 
    module: { 
    preLoaders: [ 

     { 
     test: /\.js$/, 
     loader: "source-map-loader" 
     }, 
     // { 
     // test: /\.js$/, 
     // exclude: /node_modules/, 
     // loader: 'jshint-loader' 

     // } 
    ], 
    loaders: [ 

     { 
     test: /\.html$/, 
     loader: 'file?name=[name].[ext]', 
     }, 
     { 
     test: /\.jsx?$/, 
     exclude: /node_modules/, 
     loader: 'babel-loader?presets=es2015', 
     }, 
     { 
     test: /\.less$/, 
     loader: "style!css!less", 
     }, 
     {test: /\.css$/, loader: 'style-loader!css-loader'}, 
     {test: /\.png$/, loader: "url-loader?limit=100000"}, 

     { 
     test: /\.js$/, 
     exclude: /node_modules/, 
     loaders: ['babel-loader?presets=es2015'] 
     }, 
     { 
     test: /\.svg$/, 
     loader: 'svg-sprite', 
     include: /public\/icons/ 
    } 
    ] 
    }, 
    plugins: [ 
    new webpack.optimize.UglifyJsPlugin({ 
     compress: { 
     warnings: false, 
     }, 
     output: { 
     comments: false, 
     }, 
    }), 
    new NpmInstallPlugin({ 
     save: true // --save 
    }), 

    new CompressionPlugin({ 
     asset: "[path].gz[query]", 
     algorithm: "gzip", 
     test: /\.js$|\.html$/, 
     threshold: 10240, 
     minRatio: 0.8 
    }), 
    new webpack.optimize.CommonsChunkPlugin(/* chunkName= */["vendor"], /* filename= */"[name].bundle.js", Infinity), 
    ], 
    devServer: { 
    colors: true, 
    contentBase: __dirname, 
    historyApiFallback: true, 
    hot: true, 
    inline: true, 
    port: 9093, 
    progress: true, 
    stats: { 
     cached: false 
    } 
    } 
} 

EDIT1 : 그럼 난 여전히 같은 경고를 받았습니다

new webpack.DefinePlugin({ 
     "process.env": { 
     NODE_ENV: JSON.stringify("development") 
     } 
    }) 

:

react: path.join(__dirname, 'node_modules/react/dist/react.min.js'), 

그런 다음 아래와 같이 개발에 NODE_ENV 업데이트 :

나는이 줄을 제거 .

warning.js:14You are currently using minified code outside of NODE_ENV === 'production'. This means that you are running a slower development build of Redux. You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) to ensure you have the correct code for your production build. 

답변

0

를 사용하는 방법 당신의 빌드하지만 여전히 NODE_ENV에서 development으로 설정하고 있습니다.

UglifyJsPlugin을 사용하는 경우 항상 NODE_ENVproduction으로 설정해야합니다. 프로덕션을 위해 구축하지 않은 경우 UglifyJsPlugin을 제거하면 경고가 표시되지 않습니다.

Webpack, React 및 Redux 여기에 대한 유용한 힌트를 제공하려고합니다. 개발 모드 (NODE_ENVproduction로 설정되지 않음)에서는 경고가 더 많이 발생하고 성능이 떨어집니다. 당신이 그 (것)들을 소형화 할 때 당신은 당신이 생산 건축을 달리고 있다고 추정한다. 프로덕션 빌드의 경우 실제로는 NODE_ENV이 올바르게 설정 될 것으로 예상됩니다.짧은

그래서 :

  • 생산 빌드는 : productionNODE_ENV을 설정하고 UglifyJsPlugin를 사용합니다.
  • Dev 빌드 : NODE_ENV에서 development으로 설정하고 확장 플러그인을 사용하지 마십시오.
1

나는 프로덕션 빌드 모드에없는

... 더 명확 웹팩가보다 말을하는 방법을 알고 있지만, 반작용의 축소 된 버전을 사용하지 않습니다. 사용 비 축소 된 하나, 그래서 당신은 당신의을 축소 된하지 반응이

react: path.join(__dirname, 'node_modules/react/dist/react.js'), 

또는 어떤 장소

react: path.join(__dirname, 'node_modules/react/dist/react.min.js'), 

를 교체합니다.

이것은 오류가 아닙니다. 경고이므로 계속해서 작업 할 수 있습니다.

다만 다른 점은 : path.join은 각 운영 체제에 적절한 슬래시를 추가합니다. 당신이 가야 할 때 당신이 여기 일은

path.join(__dirname, 'node_modules/react/dist/react.min.js') 

입니다

path.join(__dirname, 'node_modules', 'react', 'dist', 'react.min.js') 

이것은 path가 제대로 지금 얻을 경고 당신이 웹팩이 작게를시키기 때문이다

+0

즉, 두 개의 webpack 구성 파일을 정의해야합니까? 하나는 dev 모드 용이고, 하나는 생산 모드 용입니까? 그리고이 두 파일의 대부분은 다른 몇 줄의 파일과 동일합니다. 맞습니까? –

+0

반응 경로를 설정 파일에서 아무 것도 사용하지 않아서 제거한 것처럼 보입니다. 내 게시물을 편집했지만 여전히 같은 오류가 발생했습니다. –

+0

@ ZhaoYi 잘 2 개의 Webpack 파일은 꽤 일반적인 연습이지만, 코드를 포함하여 모든 것을 축소시키는 비 확장 버전과 UglifyJS 웹팩 플러그인을 사용할 수 있습니다. 두 번째 의견에 관해서 : 오류가 동일합니까? 반응에 대해 ??? – smnbbrv

0

개발 빌드를 위해 webpack.config.js에서 아래 구성을 제거하여 수정했습니다. 이 플러그인은 개발 모드에 적합하지 않은 js 코드를 압축하는 것으로 보입니다.

new webpack.optimize.UglifyJsPlugin({ 
    compress: { 
    warnings: false, 
    }, 
    output: { 
    comments: false, 
    }, 
})