<tfoot id='syPx8'></tfoot><legend id='syPx8'><style id='syPx8'><dir id='syPx8'><q id='syPx8'></q></dir></style></legend>

      <bdo id='syPx8'></bdo><ul id='syPx8'></ul>

    <small id='syPx8'></small><noframes id='syPx8'>

    <i id='syPx8'><tr id='syPx8'><dt id='syPx8'><q id='syPx8'><span id='syPx8'><b id='syPx8'><form id='syPx8'><ins id='syPx8'></ins><ul id='syPx8'></ul><sub id='syPx8'></sub></form><legend id='syPx8'></legend><bdo id='syPx8'><pre id='syPx8'><center id='syPx8'></center></pre></bdo></b><th id='syPx8'></th></span></q></dt></tr></i><div id='syPx8'><tfoot id='syPx8'></tfoot><dl id='syPx8'><fieldset id='syPx8'></fieldset></dl></div>
    1. 从 gulp 中使用 browserify 时如何向浏览器公开“要

      时间:2023-05-29

          <tbody id='gWKXA'></tbody>
      • <small id='gWKXA'></small><noframes id='gWKXA'>

      • <i id='gWKXA'><tr id='gWKXA'><dt id='gWKXA'><q id='gWKXA'><span id='gWKXA'><b id='gWKXA'><form id='gWKXA'><ins id='gWKXA'></ins><ul id='gWKXA'></ul><sub id='gWKXA'></sub></form><legend id='gWKXA'></legend><bdo id='gWKXA'><pre id='gWKXA'><center id='gWKXA'></center></pre></bdo></b><th id='gWKXA'></th></span></q></dt></tr></i><div id='gWKXA'><tfoot id='gWKXA'></tfoot><dl id='gWKXA'><fieldset id='gWKXA'></fieldset></dl></div>

        <legend id='gWKXA'><style id='gWKXA'><dir id='gWKXA'><q id='gWKXA'></q></dir></style></legend>

              <bdo id='gWKXA'></bdo><ul id='gWKXA'></ul>

              <tfoot id='gWKXA'></tfoot>
              • 本文介绍了从 gulp 中使用 browserify 时如何向浏览器公开“要求"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                当我有一个看起来像这样的文件 x.js 时:

                When I have a file x.js that looks like this:

                x.js

                module.exports = function (n) { return n * 111 }
                

                然后我像这样从命令行运行 browserify:

                and I run browserify from the command line like so:

                browserify -r ./x.js > bundle.js
                

                我得到一个看起来像这样的输出文件(大致):

                I get an output file that looks like this (roughly):

                require=(function e(t,n,r){function ......
                ./App.jsx":[function(require,module,exports){
                module.exports=require('0+DPR/');
                },{}]},{},[])
                

                然后在我的浏览器代码中我可以这样做:

                Then in my browser code I can do this:

                <html>
                  <head>
                    <title>React server rendering example</title>
                    <script src="static/bundle.js"></script>
                  </head>
                  <body>
                    Welcome to the React server rendering example. Here is a server-rendered React component:
                    <div id="53a442ff8b39d"></div><script>
                    var x = require('./x.js');
                    console.log(x(3))
                </script>  </body>
                </html>
                

                我其实有两个问题:

                1) 这在浏览器中不太有效我收到错误:未捕获的错误:找不到模块'./x.js'".为什么会这样?

                1) This doesn't quite work in the browser I get the error: "Uncaught Error: Cannot find module './x.js'". Why is that happening?

                2) 我实际上想使用乙烯基源流在 gulp 中运行它.我试过在我的 gulpfile 中做这样的事情,但它不起作用.有任何想法吗?我收到错误未定义要求"

                2) I actually want to run this in gulp using vinyl-source-stream. I've tried doing something like this in my gulpfile but it doesn't work. Any ideas? I get the error 'require is not defined'

                var   gulp = require('gulp'),
                  browserify = require('browserify'),
                  source = require('vinyl-source-stream');
                
                var b = browserify({
                    entries: ['./x.js'],
                  });
                   b.bundle({debug: false})
                   .pipe(source('bundle.js'))
                   .pipe(gulp.dest('./build'));
                

                推荐答案

                更新:您可以在 -r 开关中引用别名

                Update: You can reference an alias in your -r switch

                试试:browserify -r ./x.js:my-module >bundle.js

                在你的页面中:var x = require('my-module');

                注意:如果您在 fsthrough 等节点模块上使用 -r 开关,则不需要为这些模块设置别名,因为它们通常有名称在他们的 package.json 文件中.

                NOTE: if you used an -r switch on a node module like fs or through, you don't need an alias for these because they usually have a name in their package.json file.

                参见node-browserify -- 外部要求 了解更多信息.

                See node-browserify -- external requires for more info.

                如果你打算像这样(使用 -r 开关)捆绑你的 x.js,有几个选项

                If you are going to bundle your x.js like that (with -r switch) there are a couple of options

                1) 将脚本放入您的 html 页面并单独捆绑.

                1) Take the script in your html page and bundle it separately.

                      创建一个 ma​​in.js 文件并将你的 JS 放入其中.

                      Create a main.js file and put your JS in it.

                      使用browserify -x ./x.js >main.js

                      Use browserify -x ./x.js > main.js

                      通过使用 -x 开关,Browserify 会将您的 bundle.js 作为依赖项链接.

                      By using the -x switch, Browserify will link your bundle.js in as a dependancy.

                      然后在你的页面中引用这两个JS文件.

                      Then reference both JS files in your page.

                2) 使用 Browserify 生成的名称.

                2) Use name generated by Browserify.

                      var x = require('0+DPR/');

                      var x = require('0+DPR/');

                      请参阅上面的更新以创建别名.

                      See Update above to create an alias.

                下面是很好的资源,因为您希望通过 Gulp 走得更远

                Good resource below since you are looking to go further with Gulp

                • Browserify - 将 Nodejs 模块带到浏览器

                更多 Gulp + Browserify(使用 Watchify 以及 livereload)查看 Viget 上的博客文章

                For more Gulp + Browserify (uses Watchify as well for livereload) Check out blog post on Viget

                • Gulp + Browserify:所有帖子

                这篇关于从 gulp 中使用 browserify 时如何向浏览器公开“要求"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:大型 Web 项目中 Browserify 的最佳实践 - Gulp 下一篇:如何在 Polymer 组件中使用 Sass

                相关文章

                最新文章

              • <tfoot id='9RdAq'></tfoot>

                <legend id='9RdAq'><style id='9RdAq'><dir id='9RdAq'><q id='9RdAq'></q></dir></style></legend>

                  <small id='9RdAq'></small><noframes id='9RdAq'>

                  <i id='9RdAq'><tr id='9RdAq'><dt id='9RdAq'><q id='9RdAq'><span id='9RdAq'><b id='9RdAq'><form id='9RdAq'><ins id='9RdAq'></ins><ul id='9RdAq'></ul><sub id='9RdAq'></sub></form><legend id='9RdAq'></legend><bdo id='9RdAq'><pre id='9RdAq'><center id='9RdAq'></center></pre></bdo></b><th id='9RdAq'></th></span></q></dt></tr></i><div id='9RdAq'><tfoot id='9RdAq'></tfoot><dl id='9RdAq'><fieldset id='9RdAq'></fieldset></dl></div>
                    <bdo id='9RdAq'></bdo><ul id='9RdAq'></ul>