如何在heroku上的uglify之后使用webpack部署angularjs应用程序

时间:2016-09-21 15:55:17

标签: javascript angularjs heroku angularjs-directive webpack

我需要部署一个angularjs应用程序。

在我使用webpack来uglify生产应用程序之后。出现以下命令。

当我运行webpack-dev-server

时,该应用程序正常运行
Hash: 06f0e2d056b597c4e43f
Version: webpack 1.13.2
Time: 16533ms
    Asset     Size  Chunks             Chunk Names
bundle.js  4.37 MB       0  [emitted]  main
   [0] multi main 28 bytes {0} [built]
  [13] ./src/index.js 2.69 kB {0} [built]
    + 12 hidden modules

WARNING in bundle.js from UglifyJs
Side effects in initialization of unused variable $element [./~/angular/angular.js:9552,0]
Dropping unreachable code [./~/angular/angular.js:20263,0]
Dropping unused variable expression [./~/angular/angular.js:15347,0]
Dropping unused function includes [./~/angular/angular.js:799,0]
Dropping unused function nodesetLinkingFn [./~/angular/angular.js:10162,0]
Dropping unused function directiveLinkingFn [./~/angular/angular.js:10169,0]
Side effects in initialization of unused variable check [./~/markdown/lib/markdown.js:1044,0]
Dropping unused variable lines [./~/markdown/lib/markdown.js:350,0]
Dropping unused variable number_list [./~/markdown/lib/markdown.js:427,0]
Side effects in initialization of unused variable old_tree [./~/markdown/lib/markdown.js:705,0]
Dropping unused variable lastIndex [./~/markdown/lib/markdown.js:775,0]
Condition always false [./~/markdown/lib/markdown.js:1718,0]
Dropping unreachable code [./~/markdown/lib/markdown.js:1720,0]

但是当我尝试运行uglified production app时,同样的错误会不断出现。 (见下文)

我运行的命令是node ./src/bundle.js(uglified后的输出)

C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:21
O=x||k;else{O=x||S,_=[];for(var V in o)Nr.call(o,V)&&"$"!==V.charAt(0)&&_.push(V)}for(w=_.length,T=new Array(w),f=0;f<w;f++)if(C=o===_?f:_[f],E=o[C],A=O(C,E,f),$[A])M=$[A],delete $[A],I[A]=M,T[f]=M;else{if(I[A])throw r(T,function(e){e&&e.scope&&($[e.id]=e)}),s("dupes","Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}, Duplicate value: {2}",h,A,E);T[f]={id:A,scope:void 0,clone:void 0},I[A]=!0}for(var D in $){if(M=$[D],N=pe(M.clone),i.leave(N),N[0].parentNode)for(f=0,v=N.length;f<v;f++)N[f][a]=!0;M.scope.$destroy()}for(f=0;f<w;f++)if(C=o===_?f:_[f],E=o[C],M=T[f],M.scope){g=j;do g=g.nextSibling;while(g&&g[a]);c(M)!=g&&i.move(pe(M.clone),null,j),j=l(M),u(M.scope,f,y,E,b,C,w)}else d(function(e,t){M.scope=t;var n=p.cloneNode(!1);e[e.length++]=n,i.enter(e,null,j),j=n,M.clone=e,I[M.id]=M,u(M.scope,f,y,E,b,C,w)});$=I})}}}}],Ua="ng-hide",Fa="ng-hide-animate",qa=["$animate"

ReferenceError: window is not defined
    at Object.<anonymous> (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:21:8320)
    at t (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:1:169)
    at Object.<anonymous> (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:21:8704)
    at t (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:1:169)
    at Object.<anonymous> (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:22:1478)
    at t (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:1:169)
    at Object.<anonymous> (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:1:289)
    at t (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:1:169)
    at C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:1:256
    at Object.<anonymous> (C:\Users\User\Desktop\udemy\Website\AngularJS\Leadiq\src\bundle.js:1:261)

我的Github回购是通过此链接Github repo有人可以提供协助和建议吗?

1 个答案:

答案 0 :(得分:0)

如果您的网络应用程序可以在webpack-dev-server上运行,只要您的代码可以在uglification(uglify插件)中存活。您所需要的只是设置自己的迷你服务器,以呈现静态/动态资产。

我将从node.js和npm方法解释。

如果要部署到heroku,则需要使用2个脚本postinstallstartpostinstall基本上意味着您将执行webpack -p任务,根据您的webpack.config.js将所有js文件生成就绪到一个捆绑文件中。 start将触发迷你服务器呈现网络应用。

使用git push heroku master将代码推送到heroku master后,这两个命令将自动在heroku上运行。 npm run postinstall将首先运行npm start

请勿使用webpack-dev-server进行任何形式的部署。

请参阅我的github repo了解更多详情。

Github repo