【问题标题】:browserify & react NODE_ENV for productionbrowserify & react NODE_ENV 用于生产
【发布时间】:2017-07-13 04:06:03
【问题描述】:

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

效果很好,但我得到了 react 的开发版本

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

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

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

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

process.env.NODE_ENV = 'production';

但还是不好。

当我尝试添加 envify 变换时:

.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();
}

【问题讨论】:

  • 你在你的应用中使用了 webpack bundler 吗?
  • 没有 webpack,只有通过 gulp 浏览器化:|
  • 哪个版本的反应?
  • 已解决,谢谢! ://

标签: javascript reactjs gulp browserify


【解决方案1】:

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

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2022-07-22
    • 1970-01-01
    • 2018-03-26
    • 2018-05-17
    • 1970-01-01
    • 2018-08-20
    • 2019-11-19
    • 2018-11-09
    相关资源
    最近更新 更多