我有一个 Rails 项目,其中包括一个 pdfmake js插件。我将 pdfmake.min.js
文件放在 assets\javascripts
文件夹中,并将其添加到 application.js
中:
//= require pdfmake.min
在临时服务器中,我有相关的 Assets 设置:
config.serve_static_assets = true
config.assets.js_compressor = :uglifier
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
# Generate digests for assets URLs.
config.assets.digest = true
但是,我在登台服务器上遇到错误:
ReferenceError: Can't find variable: n
这个错误不是在开发环境,所以我猜测是js编译导致的。具体来说,使用uglifier
时导致:
相关源码:
function(module, exports, __webpack_require__) {
/* WEBPACK VAR INJECTION */(function(Buffer) {// Generated by CoffeeScript 1.7.1
(function() {
var DecodeStream, iconv;
try {
iconv = __webpack_require__(87);
} catch (_error) {}
DecodeStream = (function() {
var key;
function DecodeStream(buffer) {
this.buffer = buffer;
this.pos = 0;
this.length = this.buffer.length;
}
......
})();
module.exports = DecodeStream;
}).call(this);
min.js 和本地一样不加丑
function(t, e, n) {
(function(e) {
(function() {
var r,
i;
try {
i = n(84)
} catch (t) {}
r = function() {
function t(t) {
this.buffer = t, this.pos = 0, this.length = this.buffer.length
}
var n;
......
}(), t.exports = r
}).call(this)
application.js 中的代码在 staging 中的 uglifier 之后
function(A, t, e) {
(function(A) {
(function() {
var t,
i;
try {
i = e(84)
} catch (n) {}
t = function() {
function t(A) {
this.buffer = A, this.pos = 0, this.length = this.buffer.length
}
var e;
......
}(), n.exports = t
}).call(this)
根据上面的代码,我们可以发现uglifier
将module
和Buffer
都赋给了A
不正确。我该如何解决这个问题?
我有一些想法:
- 在暂存时禁用
uglifier
。对演出和制作有好处吗? - 将
pdfmake.min.js
替换为pdfmake.js
。 - 还有其他想法吗?
最佳答案
根据以往的经验,我发现之前压缩过的 Javascript 会导致 uglifier 出现一些问题。这可能与 uglifier 中的旧错误有关,如果它在同一代码上多次运行,它将失败。
为了获得可靠的结果,请使用未缩小的 pdfmake.js,以便 uglifier 应该没有问题。
此外,鉴于您的丑化器 gem 版本是 2014 年的(请参阅 https://github.com/lautis/uglifier/releases?after=v2.5.2),您绝对应该考虑升级丑化器 gem 以避免其他问题。
关于javascript - 使用uglifier缩小rails中的min.js文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763320/