gulp - 通过带有 DDEV-local 的 Gulp 任务使用 Browsersync

标签 gulp browser-sync ddev gulp-browser-sync

我使用 DDEV 作为我的本地托管环境,我的许多项目都通过 Gulp 实现了前端自动化。 Browsersync是我们前端设置的主要组件,需要 DDEV 容器向主机公开端口。手头的问题有两个方面,从容器向主机公开端口的最佳方式是什么,以及在 DDEV 环境中浏览器同步 Gulp 任务的最佳设置是什么?

最佳答案

暴露必要的端口

这种情况的一部分需要使用 Docker Compose 文件将容器的端口公开给软管机器。基于 this Answer您需要在 .ddev 目录中创建一个 docker-compose.browsersync.yaml 文件。

Browser-Sync 文件的示例如下:

# Override the web container's standard HTTP_EXPOSE and HTTPS_EXPOSE
# This is to expose the browsersync port.
version: '3.6'
services:
  web:
    # ports are a list of exposed *container* ports
    expose:
      - '3000'
    environment:
      - HTTP_EXPOSE=${DDEV_ROUTER_HTTP_PORT}:80,${DDEV_MAILHOG_PORT}:8025,3001:3000
      - HTTPS_EXPOSE=${DDEV_ROUTER_HTTPS_PORT}:80,${DDEV_MAILHOG_HTTPS_PORT}:8025,3000:3000

我们在此处公开端口 3000,因为它是 Browser-Sync 的默认设置,但可以更新以反射(reflect)您的项目需求。

注意:将此文件添加到您的 .ddev 目录后,您应该重新启动您的 ddev 项目。

有关使用 docker compose 定义新服务的更多信息,read the DDEV docs .

在 Gulp 中设置浏览器同步

这假设你有一个工作的 gulpfile.js 准备好处理你已经包含的其他必需的包。如果你对 Browser-Sync 和 Gulp 不是很熟悉,请引用 their docs了解详细信息。

const browserSync = require('browser-sync').create();

/*
* Set 'url' (the host and proxy hostnames) to match the canonical url of your ddev project.
* Do not include the http/s protocol in the url. The ddev-router will route the
* host machine's request to the appropriate protocol.
* 
* ex: yoursite.ddev.site
*/
const url = 'yoursite.ddev.site';

/*
* This function only includes the most basic browser-sync settings needed
* to get a watch server running. Please refer to the browser-sync docs for other
* available options.
*/
const startServer = function (done) {
    // Initialize BrowserSync
    browserSync.init({
        proxy: url,
        host: url,
        port: 3000,
    });
    done();
};

exports.startServer = startServer;

您可以在初始设置后使用 gulp startServer 进行测试。 Gulp 将输出一个 http 作为外部 URL 用于测试。然而,多亏了 ddev-router,它可以通过 httphttps 访问。

关于gulp - 通过带有 DDEV-local 的 Gulp 任务使用 Browsersync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66108335/

相关文章:

node.js - 运行 gulp 任务时如何解决 "Using a domain property in MakeCallback is deprecated"警告?

gulp - 将浏览器同步放在 polymer 自定义构建的 gulpfile 中的正确位置在哪里?

node.js - npm WARN 可选 dep 失败,继续 fsevents@0.3.6

docker - ddev + elasticsearch的最新版本

docker - phpstorm Xdebug无法与ddev一起使用

gulp - 有没有办法在 VSCode 中刷新任务列表?

javascript - 用于 JS 文件的类似 Styleint 的 Gulp 插件

javascript - Gulp.js 浏览器同步不重新加载我的浏览器

drupal - DDEV 多站点设置与 Acquia pull

javascript - 运行 gulp 时解析错误