Browserify & React NODE_ENV for Production



我想用 react 和其他代码构建我的 js 代码并将其缩小到一个文件。

它运行良好,但我得到了反应的开发版本

It looks like you're using a minified copy of the development build of React

现在我知道我需要添加NODE_ENV = production

但我尝试了很多方法,但仍然,构建保持不变......

我尝试了环境,如下所示,并像这样对其进行硬编码:

process.env.NODE_ENV = 'production';

但是,仍然不好。

当我尝试添加环境转换时,有了它:

.transform(envify({
'NODE_ENV': 'production'
}))

我在构建中收到此错误:

TypeError Path must be a string.

有什么想法吗?

function bundleJs() {
const _browserify = browserify({
entries: [config.entry],
debug : false,
cache: {},
packageCache: {},
fullPaths: true,
extensions: ['.js']
});
_browserify.plugin(resolutions, ['*'])
.transform('envify', {global: true, _: 'purge', NODE_ENV: 'production'})
.transform(hbsfy)
.transform(babelify, {
only: /(app)|(frontend-app)/,
presets: ['es2015-without-strict', 'react']
})
.on('update', () => {
bundle();
gutil.log('Rebundle...');
})
.on('log', gutil.log);
function bundle() {
return bundler.bundle()
.on('error', handleError)
.pipe(source('init.js'))
.pipe(rename('bundle.js'))
.pipe(buffer())
.pipe(gulpif(env === 'production', uglify()))
.pipe(gulpif(env !== 'production', sourcemaps.init({ loadMaps: true })))
.pipe(gulpif(env !== 'production', sourcemaps.write('.')))
.pipe(gulp.dest(config.dist))
.pipe(browserSync.reload({stream:true}));
}
// run it once the first time buildJs is called
return bundle();
}

好的,所以在浪费了我 3 个小时的生命在那段代码上之后。

我注意到 react 的构建是从 bower 而不是从 npm 使用的。

在 composer.json 中,我们在"浏览器"下有标识符,即:

"react": "./bower_components/react/react.js",
"react-dom": "./bower_components/react/react-dom.js"

我认为这直接指向反应开发构建,所以这就是问题所在。

我只是用 npm 安装,一切都运行良好。

最新更新