1. <tfoot id='pWBR4'></tfoot>
    2. <small id='pWBR4'></small><noframes id='pWBR4'>

    3. <i id='pWBR4'><tr id='pWBR4'><dt id='pWBR4'><q id='pWBR4'><span id='pWBR4'><b id='pWBR4'><form id='pWBR4'><ins id='pWBR4'></ins><ul id='pWBR4'></ul><sub id='pWBR4'></sub></form><legend id='pWBR4'></legend><bdo id='pWBR4'><pre id='pWBR4'><center id='pWBR4'></center></pre></bdo></b><th id='pWBR4'></th></span></q></dt></tr></i><div id='pWBR4'><tfoot id='pWBR4'></tfoot><dl id='pWBR4'><fieldset id='pWBR4'></fieldset></dl></div>
      <legend id='pWBR4'><style id='pWBR4'><dir id='pWBR4'><q id='pWBR4'></q></dir></style></legend>

          <bdo id='pWBR4'></bdo><ul id='pWBR4'></ul>
      1. 使用 Gulp 使 browserify 模块外部化

        时间:2023-05-28

          <tbody id='r9NZ9'></tbody>
          • <bdo id='r9NZ9'></bdo><ul id='r9NZ9'></ul>

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

                  <tfoot id='r9NZ9'></tfoot>
                  <i id='r9NZ9'><tr id='r9NZ9'><dt id='r9NZ9'><q id='r9NZ9'><span id='r9NZ9'><b id='r9NZ9'><form id='r9NZ9'><ins id='r9NZ9'></ins><ul id='r9NZ9'></ul><sub id='r9NZ9'></sub></form><legend id='r9NZ9'></legend><bdo id='r9NZ9'><pre id='r9NZ9'><center id='r9NZ9'></center></pre></bdo></b><th id='r9NZ9'></th></span></q></dt></tr></i><div id='r9NZ9'><tfoot id='r9NZ9'></tfoot><dl id='r9NZ9'><fieldset id='r9NZ9'></fieldset></dl></div>
                  <legend id='r9NZ9'><style id='r9NZ9'><dir id='r9NZ9'><q id='r9NZ9'></q></dir></style></legend>
                  本文介绍了使用 Gulp 使 browserify 模块外部化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个库 lib.js 我想从 lib/a.jslib/b.js 创建并能够使用 var a = require('lib/a.js'); 从脚本 client.js 使用它,并且当我只包含已编译的lib.js 库在 client.js 之前(因此,lib.js 必须声明一个知道的 require 函数关于 lib/a.js)

                  I have a library lib.js that I want to create from lib/a.js and lib/b.js and to be able to use it from a script client.js using var a = require('lib/a.js'); and that it works when I just include the compiled lib.js library before client.js (therefore, lib.js has to declare a require function that knows about lib/a.js)

                  我想我必须使用 externalalias 但我不确定什么是正确的方法

                  I guess I have to use external and alias but I am not sure what is the proper way to do it

                  另外,是否有可能有一个 Gulp 文件为我的库中的文件夹自动创建所有别名?例如.为 lib/ 目录中的所有文件创建别名?

                  Also, is it possible to have a Gulp file that creates all the alias automatically for the folders in my library? eg. creates an alias for all the files in the lib/ dir?

                  推荐答案

                  这里有几个 gulp 任务可以帮助你分别构建通用的 lib.js 和 client.js 包.

                  Here are a couple of gulp tasks that would help to build your common lib.js and the client.js bundles separately.

                  请注意,捆绑 lib.js 时必须将 browserify 告知 b.require() lib/*.js,并且必须告知 b.external() 捆绑客户端时将单独加载的库.js

                  Note that you have to tell browserify to b.require() lib/*.js when bundling lib.js, and you have to tell it to b.external() the libraries that will be loaded separately when bundling client.js

                  var path = require('path');
                  var gulp = require('gulp');
                  var browserify = require('browserify');
                  var concat = require('gulp-concat');
                  var transform = require('vinyl-transform');
                  
                  gulp.task('build-lib', function () {
                  
                    // use `vinyl-transform` to wrap around the regular ReadableStream returned by b.bundle();
                    // so that we can use it down a vinyl pipeline as a vinyl file object.
                    // `vinyl-transform` takes care of creating both streaming and buffered vinyl file objects.
                    var browserified = transform(function(filename) {
                  
                      // basename, for eg: 'a.js'
                      var basename = path.basename(filename);
                  
                      // define the exposed name that your client.js would use to require();
                      // for eg: require('lib/a.js'); // -> exposed name should be 'lib/a.js'
                      var expose = 'lib/' + basename;
                  
                      return browserify(filename)
                        .require(filename, { expose: expose})
                        .bundle();
                    });
                  
                    return gulp.src(['./lib/*.js'])
                      .pipe(browserified)
                      .pipe(concat('lib.js'))
                      .pipe(gulp.dest('./dist'));
                  });
                  
                  gulp.task('build-client', function () {
                  
                    var browserified = transform(function(filename) {
                      // filename = './client.js'
                  
                      // let browserify know that lib/a.js and and lib/b.js are external files
                      // and will be loaded externally (in your case, by loading the bundled lib.js 
                      // for eg: <script src='dist/lib.js'>)
                      return browserify(filename)
                        .external('lib/a.js')
                        .external('lib/b.js')
                        .bundle();
                    });
                  
                    return gulp.src(['./client.js'])
                      .pipe(browserified)
                      .pipe(gulp.dest('./dist'));
                  });
                  
                  gulp.task('default', ['build-lib', 'build-client']);
                  

                  这篇关于使用 Gulp 使 browserify 模块外部化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 LESS 文件中出现错误后,Gulp.js 停止编译 LESS 下一篇:在没有构建工具(如 grunt 和 gulp)的情况下使用 J

                  相关文章

                  最新文章

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

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

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

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

                      <tfoot id='zV6k2'></tfoot>