javascript 使用我系统中文件夹路径的浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21993073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:10:44  来源:igfitidea点击:

Browserify with paths to folders in my system

javascriptnode.jsbrowserify

提问by Vinz243

When I compile markdown-symbols using Browserify 3.30.2 (browserify file.js -o bundle.js), I get something like that :

当我使用 Browserify 3.30.2 ( browserify file.js -o bundle.js)编译 markdown-symbols 时,我得到了类似的东西:

!function(e){if("object"==typeof exports...[function(_dereq_,module,exports){

},{}],2:[function(_dereq_,module,exports){
...
...
[on line 8000] 
    : function (str, start, len) {
        if (start < 0) start = str.length + start;
        return str.substr(start, len);
    }
;

}).call(this,_dereq_("C:\Users\Me\AppData\Roaming\npm\node_modules\browserify\node_modules\insert-module-globals\node_modules\process\browser.js"))
},{"C:\Users\Me\AppData\Roaming\npm\node_modules\browserify\node_modules\insert-module-globals\node_modules\process\browser.js":11}],14:[function(_dereq_,module,exports){
module.exports=_dereq_(3)
},{}],15:[function(_dereq_,module,exports){
module.exports=_dereq_(4)
},{"./support/isBuffer":14,"C:\Users\ME\AppData\Roaming\npm\node_modules\browserify\node_modules\insert-module-globals\node_modules\process\browser.js":11,"inherits":10}],16:[function(_dereq_,module,exports){
var frep = _dereq_('frep');
var file = _dereq_('fs-utils');
var delims = _dereq_('delims');
var _ = _dereq_('lodash');
...

As you can see, there are absolute paths to my files here. Why ? How can I remove them ?

如您所见,这里有我的文件的绝对路径。为什么 ?我怎样才能删除它们?

EDIT: here is my build.jsfile

编辑:这是我的build.js文件

 var browserify = require('browserify-middleware')
fs = require('fs');
var b = browserify('./index.js', {
    'opts.basedir': './'
});
b({
    // Mocks up express req and res
    headers: []
}, {
    getHeader: function () {},
    setHeader: function () {},
    send: function (a) {
        console.log('send', a);
    },
    end: function (a) {
        //console.log('end', a.constructor.name);
        //  fs.write('bundle.js', a, undefined, undefined, function (err) {
        console.log(a.toString());
        //});
        //  a.pipe(fs.createWriteStream('bundle.js'));
    },

});

And to run node build > bundle.js. same problem. If I replace basedir value by for instancee ihatebrowserifythere is an error about something not resolved.

并运行node build > bundle.js。同样的问题。如果我用 instancee 替换 basedir 值,ihatebrowserify则会出现未解决的错误。

回答by Song Gao

This came across to me as well today. It turns out there's a boolean option --full-path[0] now that solves the problem.

今天我也遇到了这个问题。事实证明,现在有一个布尔选项--full-path[0] 可以解决问题。

For example:

例如:

browserify -o bundle.js --full-path=false index.js

[0] https://github.com/substack/node-browserify/blob/master/bin/args.js

[0] https://github.com/substack/node-browserify/blob/master/bin/args.js

回答by Bruno Reis

Almost 6 months later, and I've seen the same issue. Now I found a workaround that suited me, and others might benefit from it as well.

差不多 6 个月后,我看到了同样的问题。现在我找到了一种适合我的解决方法,其他人也可能从中受益。

Google has given me this issue report: https://github.com/thlorenz/browserify-shim/issues/43-- which reports this local system path disclosure on "browserify-shim", although it is a "browserify" issue from what I understood.

谷歌给了我这个问题报告:https: //github.com/thlorenz/browserify-shim/issues/43——它报告了关于“browserify-shim”的本地系统路径披露,尽管它是一个来自我所理解的。

That issue suggests an workaround:

该问题提出了一种解决方法:

$ npm install -g intreq browser-pack browser-unpack
$ browserify example/main.js -t browserify-shim | browser-unpack | intreq | browser-pack

I've tested this and was satisfied with the results. However I wanted to go further, and integrate this into my Gruntfile.js. The solution I found, tested and that made me happy is to use unpathify:

我已经对此进行了测试,并对结果感到满意。但是我想更进一步,并将其集成到我的 Gruntfile.js 中。我发现、测试并让我高兴的解决方案是使用unpathify

Compress browserify require paths for better minification i.e. require('some/long/path') => require(1)

压缩 browserify 需要路径以更好地缩小即 require('some/long/path') => require(1)

To use it, just install unpathify (npm install --save-dev unpathify) and add it to your build:

要使用它,只需安装 unpathify ( npm install --save-dev unpathify) 并将其添加到您的构建中:

grunt.initConfig({
  browserify: {
    all: {
      files: {
        'build/all.js': ['some/file.js']
      }
    }
  },
  unpathify: {
    all: {
      src: ['build/all.js']
    }
  }
  // ...
}

回答by Hüseyin BABAL

Browserify resolves absolute path as default. If you do not want to see absolute paths, you can set options.basediras you want. For example;

Browserify 默认解析绝对路径。如果不想看到绝对路径,可以随意设置options.basedir。例如;

var browserify = require('browserify-middleware');
var b = browserify({'opts.basedir': './'});

回答by Scimonster

My best guess is that somewhere, there are modules being included from absolute paths.

我最好的猜测是在某个地方,绝对路径中包含了模块。

It doesn't happen to me when using browserify.

使用 browserify 时不会发生这种情况。



Actually it's more than a guess.

实际上,这不仅仅是一种猜测。

},{}],2:[function(_dereq_,module,exports){
...
...
}).call(this,_dereq_("C:\Users\Me\AppData\Roaming\npm\node_modules\browserify\node_modules\insert-module-globals\node_modules\process\browser.js"))
},{"C:\Users\Me\AppData\Roaming\npm\node_modules\browserify\node_modules\insert-module-globals\node_modules\process\browser.js":11}],14:[function(_dereq_,module,exports){

You have _dereq_("C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules\\browserify\\node_modules\\insert-module-globals\\node_modules\\process\\browser.js"); therefore, it must include that module.

你有_dereq_("C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules\\browserify\\node_modules\\insert-module-globals\\node_modules\\process\\browser.js");因此,它必须包含该模块。

If you set some Node global (no idea how on Windows) to look in C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules, you might not have to include the full path, but i can't be sure.

如果您设置一些 Node 全局(不知道如何在 Windows 上查看)C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules,您可能不必包含完整路径,但我不能确定。

回答by jonbo

Not entirely sure if this is an answer to the original question, but I was having a similar problem. The bundled js file and the external sourcemap (using exorcist) being generated was containing absolute paths to folders on my system. After a headache figuring out why, it seems the culprit was babelify (previously 6to5ify). https://github.com/substack/node-browserify/issues/663& https://github.com/babel/babelify/issues/19

不完全确定这是否是原始问题的答案,但我遇到了类似的问题。生成的捆绑 js 文件和外部源映射(使用 exorcist)包含到我系统上文件夹的绝对路径。在头疼地弄清楚原因之后,罪魁祸首似乎是 babelify(以前是 6to5ify)。https://github.com/substack/node-browserify/issues/663& https://github.com/babel/babelify/issues/19

The solution is simple though:

不过解决方法很简单:

browserify --debug -t [ babelify --sourceMapRelative .]

browserify --debug -t [ babelify --sourceMapRelative 。]

or

或者

browserify({ debug: true }) .transform(babelify.configure({sourceMapRelative: '.'}))

browserify({ debug: true }) .transform(babelify.configure({ sourceMapRelative: '.'}))

回答by Ryan

There is a new Browserify plugin available from the author of Browserify. It's called Bundle-Collapser. It'll replace folder paths with numerical indices.

Browserify 的作者提供了一个新的 Browserify 插件。它被称为Bundle-Collapser。它将用数字索引替换文件夹路径。