简体   繁体   English

无法获取 webpack --watch 或开发服务器以使用 Lando 运行本地 Drupal 环境

[英]Cannot get webpack --watch or dev server to work using Lando to run a local Drupal environment

I've scoured the inte.net and have bits and pieces but nothing is coming together for me.我已经搜索了 inte.net 并且有一些零碎的东西,但没有任何东西适合我。 I have a local Drupal environment running with Lando.我有一个运行 Lando 的本地 Drupal 环境。 I've successfully installed and configured webpack. Everything is working except when I try to watch or hot reload.我已经成功安装并配置了 webpack。除了我尝试观看或热重载之外,一切正常。

When I run lando npm run build-dev (that currently uses webpack --watch I can see my changes compiled successfully into the correct folder. However, when I refresh my Drupal site, I do not see that changes. The only time I see my updated JS changes are when I run lando drush cr to clear cache. Same things are happening when I try to configure the webpack-dev-server. I can get everything to watch for changes and compile correctly but I cannot get my browser to reload my files, they stay cached. I'm at a loss.当我运行lando npm run build-dev (当前使用webpack --watch我可以看到我的更改已成功编译到正确的文件夹中。但是,当我刷新我的 Drupal 站点时,我没有看到该更改。我唯一一次看到我更新的 JS 更改是在我运行lando drush cr以清除缓存时发生的。当我尝试配置webpack-dev-server.我可以得到所有的东西来观察变化并正确编译,但我无法让我的浏览器重新加载我的文件,他们保持缓存。我很茫然。

I've tried configuring a proxy in my.lando.yml, and have tried different things with the config options for devServer.我尝试在 my.lando.yml 中配置代理,并尝试使用 devServer 的配置选项进行不同的操作。 I'm just not getting a concise answer, and I just don't have the knowledge to understand exactly what is happening.我只是没有得到一个简明的答案,而且我只是没有知识来准确理解正在发生的事情。 I believe it has to do with Docker containers not being exposed to webpack (??) but I don't understand how to configure this properly.我相信这与 Docker 容器没有暴露给 webpack (??) 有关,但我不明白如何正确配置它。

These are the scripts I have set up in my package.json , build outputs my production ready files into i_screamz/js/dist , build-dev starts a watch and compiles non-minified versions to i_screamz/js/dist-dev - start I have in here from trying to get the devServer to work.这些是我在我的package.json中设置的脚本,将我的生产就绪文件build输出到i_screamz/js/distbuild-dev开始监视并将非缩小版本编译到i_screamz/js/dist-dev - start我有在这里试图让 devServer 工作。 I'd like to get webpack-dev-server running as I'd love to have reloading working.我想让webpack-dev-server运行,因为我希望重新加载工作。

  "scripts": {
    "start": "npm run build:dev",
    "build:dev": "webpack --watch --progress --config webpack.config.js",
    "build": "NODE_ENV=production webpack --progress --config webpack.config.js"
  },

This is my webpack.config.js - no sass yet, this is just a working modular js build at this point.这是我的webpack.config.js - 还没有 sass,此时这只是一个有效的模块化 js 构建。

const path = require("path");
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');

const isDevMode = process.env.NODE_ENV !== 'production';

module.exports = {
  mode: isDevMode ? 'development' : 'production',
  devtool: isDevMode ? 'source-map' : false,
  entry: {
    main: ['./src/index.js']
  },
  output: {
    filename: isDevMode ? 'main-dev.js' : 'main.js',
    path: isDevMode ? path.resolve(__dirname, 'js/dist-dev') : path.resolve(__dirname, 'js/dist'),
    publicPath: '/web/themes/custom/[MYSITE]/js/dist-dev'

  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  },
  plugins: [
    new BrowserSyncPlugin({
      proxy: {
        target: 'http://[MYSITE].lndo.site/',
        proxyReq: [
          function(proxyReq) {
            proxyReq.setHeader('Cache-Control', 'no-cache, no-store');
          }
        ]
      },
      open: false,
      https: false,
      files: [
        {
          match: ['**/*.css', '**/*.js'],
          fn: (event, file) => {
            if (event == 'change') {
              const bs = require("browser-sync").get("bs-webpack-plugin");
              if (file.split('.').pop()=='js') {
                bs.reload();
              } else {
                bs.stream();
              }
            }
          }
        }
      ]
    }, {
      // prevent BrowserSync from reloading the page
      // and let Webpack Dev Server take care of this
      reload: false,
      injectCss: true,
      name: 'bs-webpack-plugin'
    }),
  ],
  watchOptions: {
    aggregateTimeout: 300,
    ignored: ['**/*.woff', '**/*.json', '**/*.woff2', '**/*.jpg', '**/*.png', '**/*.svg', 'node_modules'],
  }
};

And here is the config I have setup in my .lando.yml - I did have the proxy key in here but it's been removed as I couldn't get it setup right.这是我在我的.lando.yml中设置的配置——我确实在这里有代理密钥,但它已被删除,因为我无法正确设置它。

name: [MYSITE]
recipe: pantheon
config:
  framework: drupal8
  site: [MYPANTHEONSITE]
services:
  node:
    type: node
    build:
      - npm install
tooling:
  drush:
    service: appserver
    env:
      DRUSH_OPTIONS_URI: "http://[MYSITE].lndo.site"
  npm:
    service: node

settings.local.php设置.local.php

<?php

/**
 * Disable CSS and JS aggregation.
 */
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;

I've updated my code files above to reflect reflect a final working setup with webpack. The main answer was a setting in /web/sites/default/settings.local.php我已经更新了上面的代码文件以反映 webpack 的最终工作设置。主要答案是/web/sites/default/settings.local.php中的设置

**Disable CSS & JS aggregation. **禁用 CSS 和 JS 聚合。 ** **

$config['system.performance']['css']['preprocess'] = FALSE; $config['system.performance']['js']['preprocess'] = FALSE;

I found a working setup from saschaeggi and just tinkered around until I found this setting.我从saschaeggi找到了一个有效的设置,然后四处修改直到找到这个设置。 So thank you!所以谢谢! I also found more about what this means here .我还在这里找到了更多关于这意味着什么的信息。 This issue took me way longer than I want to admit and it was so simple.这个问题让我花了比我想承认的时间更长的时间,而且它是如此简单。 I don't know why the 'Disabling Caching css/js aggregation' page never came up when I was furiously googling a caching issue.我不知道为什么当我疯狂地谷歌搜索缓存问题时,“禁用缓存 css/js 聚合”页面从未出现。 Hopefully this answer helps anyone else in this very edge case predicament.希望这个答案可以帮助处于这种极端情况困境中的其他人。

I have webpack setup within my theme root folder with my Drupal theme files.我的主题根文件夹中有 webpack 设置和我的 Drupal 主题文件。 I run everything with Lando, including NPM. I found a nifty trick to switch the dist-dev and dist libraries for development / production builds from thinkshout .我用 Lando 运行所有东西,包括 NPM。我发现了一个巧妙的技巧,可以从thinkshout切换 dist-dev 和 dist 库以进行开发/生产构建。

I should note my setup does not include hot-reloading but I can at least compile my files and refresh immediately and see my changes.我应该注意我的设置不包括热重载,但我至少可以编译我的文件并立即刷新并查看我的更改。 The issue I was having before is that I would have to stop my watches to drush cr and that workflow was ridiculous.我之前遇到的问题是,我必须停下手表才能drush cr ,而且这个工作流程很荒谬。 I've never gotten hot reloading to work with with either BrowserSync or Webpack Dev Server and I might try to again but I need to move on with my life at this point.我从来没有使用 BrowserSync 或 Webpack Dev Server 进行热重载,我可能会再次尝试,但此时我需要继续我的生活。

I've also note included sass yet, so these files paths will change to include compilation and output for both.scss and.js files but this is the basic bare min setup working.我还注意到包括 sass,所以这些文件路径将更改为包括编译和 output 用于 .scss 和 .js 文件,但这是基本的最小设置工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM