在 Travis CI 上构建 webpack

4
我想通过以下命令运行用于生产的 webpack 构建:

"prod": "webpack -p --config webpack.production.config.js"

我的 .travis.yml 文件:

language: node_js
node_js:
  - "6.9.1"

install:
  - npm i -g yarn && yarn

before_script:
  - yarn prod

deploy:
  provider: s3
  local-dir: public
  on:
    branch: cloudfront

但是这个脚本没有加载正确的文件,在生产环境网站上我看到了 ENV=dev

我也尝试了以下命令:

before_deploy

script

但是它没有帮助

webpack.production.config.js:

output: {
  path: path.join(__dirname, 'public'),
  filename: 'index.js',
  publicPath: '/'
},
new webpack.DefinePlugin({
 'process.env': {
   'BASE_URL': JSON.stringify('url'),
   'NODE_ENV': JSON.stringify('production')
 }
})

travis.log:

The command "npm test" exited with 0.
before_deploy
9.54s$ webpack -p --config webpack.production.config.js
Hash: 4aa1139f3ef3381f91ec
Version: webpack 1.13.2
Time: 9133ms
       Asset       Size  Chunks             Chunk Names
    index.js     176 kB       0  [emitted]  main
index.js.map  151 bytes       0  [emitted]  main
   [0] multi main 28 bytes {0} [built]
    + 255 hidden modules
dpl.2
Preparing deploy
dpl.3
Deploying application
uploading "index.html" with {:content_type=>"text/html"}
uploading "index.js.map" with {:content_type=>""}
uploading "index.js" with {:content_type=>"application/javascript"}
# HEAD detached at 8a81ecb
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   public/index.js
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (593621bc433e8223b3b5c32751122c23297b202e)
Done. Your build exited with 0.
2个回答

5
需要将以下行添加到.travis.yml文件中:
skip_cleanup: true

它帮助了我


0
即使您的 Webpack 为生产生成了一个捆绑包,这也不会改变服务器上的环境。ENV 是在哪里设置的?

在 webpack 文件中,在 new webpack.DefinePlugin({ 中。如果我在本地机器上运行此命令,一切都正常工作。 - Pavel
Webpack只是为构建bundle.js时设置“环境”。当在服务器上运行时,环境可能是服务器想要的任何内容。因此,当它提供您的应用程序时,如果将ENV设置为dev,则您的环境将显示为dev。 - Chris Cousins
我正在使用CI进行构建,并带有“-p”参数,因此在生产配置中,我设置了这个变量。 - Pavel
你能展示一下哪里写着 ENV 是 dev 吗?你只是将其记录到控制台或其他地方吗? - Chris Cousins
链接,在控制台中。问题是它需要从仓库中构建,而不是CI执行的内容。 - Pavel
是的,没错。代码库将在服务器上被拉取并构建。如果您正在服务器上进行构建,则必须在服务器上设置环境变量。 - Chris Cousins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接