如何使用 webpack 将 css 链接移动到 html 文档的底部? [英] How to move css links to the bottom of the html document with webpack?

查看:27
本文介绍了如何使用 webpack 将 css 链接移动到 html 文档的底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 webpack 当前设置的方式是将 css 资源链接放在文档的头部,这会阻止我的初始内容的呈现.

The way my webpack is currently being set up, it places the css resources links in the head of the document, which blocks the rendering of my initial content.

示例:

<head>
   <link href="main.28396255e7678a2cdf3c.css" rel="stylesheet">
</head>

因此,我想将它们移到文档底部,就在 之前.

Therefore, I'd like to move them at the bottom of the document, right before </body>.

有没有办法配置我已有的东西来实现这一点,或者是否有我找不到的插件?

Is there a way to configure what I already have, to achieve this, or is there maybe a plugin I wasn't able to find?

这是我的一些 webpack 配置:

Here's some of my webpack config:

{{
  entry: {
    app: [
      './styles/main.scss',
    ]
  },
  module: {
    rules: [
      {
        test: /main.scss$/,
        use: [
          {loader: MiniCssExtractPlugin.loader, options: {hmr: isActiveDevelopment}},
          'css-loader?sourceMap',
          {loader: 'postcss-loader', options: {sourceMap: true, ident: 'postcss', plugins: () => [postcssPresetEnv({browsers: 'last 20 versions'})]}},
          'sass-loader?sourceMap',
          'import-glob',
        ],
      },
    ]
  },
  plugins: removeEmpty([
    new MiniCssExtractPlugin({
      filename: ifOptimized('[name].[chunkhash].css', '[name].css'),
    }),
    new HtmlWebpackPlugin({
      template: './app.ejs',
      filename: indexHtml,
      chunksSortMode: (a, b) => scriptsOrder.indexOf(a.names[0]) > scriptsOrder.indexOf(b.names[0]),
      env: {isProd, isWeb, isHybrid},
      config: createConfig(ifWeb, ifHybrid, ifProd, ifDev)
    })
  ])
};


这是我的 html(ejs) 模板,以防万一:

Here's my html(ejs) template, just in case:

<!DOCTYPE html>
<html lang="en" class="noOverflow">
<head>
    <meta charset="UTF-8">
    <title>title</title>
    <meta name="description" content="description">
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="format-detection" content="telephone=no">
    <% if (htmlWebpackPlugin.options.env.isWeb) { %>
    <link rel="manifest" href="/manifest.json">
    <base href="/">
    <% } else if (htmlWebpackPlugin.options.env.isHybrid) { %>
    <script type="text/javascript" src="cordova.js"></script>
    <% } %>
</head>
<body>
<noscript>How do we put this.. Your browser does not support JavaScript o_O</noscript>
<% if (htmlWebpackPlugin.options.env.isWeb) { %>
<%- require('!html-loader!./splash/splash.html') %>
<% } %>
<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700&subset=cyrillic" rel="stylesheet">
<link rel="preload" href="<%- htmlWebpackPlugin.options.config.staticUrl %>/common/mk" as="fetch" type="application/json" crossorigin>
<link href="<%- htmlWebpackPlugin.files.chunks.app.css[0] %>" rel="stylesheet">
</body>
</html>

推荐答案

首先,我很好奇你的意图.根据提供的示例代码,您似乎主要关心的是转译 SCSS 文件,并能够将结果放入 HTML 文件中.如果您只关心这些,我建议您阅读 这篇文章.那里可能有更好的文章,但它在 Google 上排名最高,所以你去吧.

First off, I'm curious about your intent. Based on the example code provided it looks like you're mainly concerned with just transpiling an SCSS file, and being able to place the result in an HTML file. If that's all you care about I'd suggest reading this article. There may be better articles out there, but it was ranked highest on Google, so there yuh go.

如果您的示例代码为简洁起见被删减了 - 或者您真的想使用 Webpack 来转译单个 SCSS 文件 - 我建议您阅读 关于插件选项的文档.

If your example code was trimmed down for brevity - or you just really want to use Webpack to transpile a single SCSS file - I'd suggest you read the documentation for the options of the plugin.

但基于 您在此处发表评论,以及您打开此问题的事实,我感觉您不想阅读文档 - 所以,inject 属性似乎是您需要设置为 false 的内容.

But based on your comment here, and the fact that you opened this issue, I'm getting the feeling you don't want to read the documentation - so, the inject property seems to be what you need to set to false.

true ||'头'||'身体' ||假

将所有资产注入给定的 templatetemplateContent.当传递 true'body' 时,所有 javascript 资源都将放置在 body 元素的底部.'head' 将脚本放在 head 元素中

Inject all assets into the given template or templateContent. When passing true or 'body' all javascript resources will be placed at the bottom of the body element. 'head' will place the scripts in the head element

在将 inject 设置为 false 时如何访问文件的示例可以在 这里,在他们的示例文件夹中.

An example of how to access the files when setting inject to false can be found here, in their examples folder.

这篇关于如何使用 webpack 将 css 链接移动到 html 文档的底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆