乐闻世界logo
搜索文章和话题

How to copy static files to build directory with webpack

5个答案

1
2
3
4
5

In CSS, the display property is crucial as it determines how an element is displayed and laid out on a page. Here are some common values of the display property and their effects:

  1. none:

    • Effect: Completely hides the element and does not reserve space for it.
    • Example: When you want to hide certain elements under specific conditions, such as dynamically hiding or showing content with JavaScript.
  2. block:

    • Effect: Makes the element behave as a block-level element, occupying the full width of its line, and subsequent elements appear on a new line.
    • Example: Used for layout, such as creating self-contained content blocks, like paragraphs, headings, and containers.
  3. inline:

    • Effect: Makes the element display inline, not occupying a full line, with its width determined solely by its content.
    • Example: Used for formatting text, such as <span> or <a> elements, to display them inline within paragraphs.
  4. inline-block:

    • Effect: Combines the characteristics of inline and block, not occupying a full line but allowing width and height to be set.
    • Example: When you need to display multiple blocks in a single line and control their size, such as each item in a navigation menu.
  5. flex:

    • Effect: Makes the element a flex container, allowing its child elements to utilize the powerful features of flex layout.
    • Example: Used to create responsive layouts where child elements' sizes and order can be flexibly adjusted.
  6. grid:

    • Effect: Makes the element a grid container, allowing you to define rows and columns for creating complex two-dimensional layouts.
    • Example: Used for designing complex page layouts, such as magazine or newspaper-style layouts.
  7. table, table-row, table-cell, etc.:

    • Effect: These values mimic the behavior of HTML table tags, allowing page content to be laid out in a table format.
    • Example: When you want to present table data using CSS, you can choose these values.
  8. list-item:

    • Effect: Makes the element behave as a list item, typically displayed with list markers.
    • Example: Used to customize the appearance of lists, such as custom list item symbols or layouts.

These are some commonly used values of the display property. Additionally, many other values and property combinations can be used to meet specific layout requirements. As web technologies evolve, the CSS specification continues to introduce new display types to address more complex design challenges. Continuing to explain more values of the display property:

  1. inline-flex:

    • Effect: Makes the element an inline flex container, meaning it can be laid out within a text line like an inline element, while its child elements can use the flexbox model.
    • Example: If you want a small layout unit to be laid out within a text line while using flexbox layout internally, such as a small card within a paragraph.
  2. inline-grid:

    • Effect: Makes the element an inline grid container, combining the characteristics of inline and grid.
    • Example: When you need to embed a small grid layout within a text flow, such as a complex mathematical formula or chart.
  3. contents:

    • Effect: Makes the child elements appear as if directly placed at the position of the parent element, with the parent itself not rendered as any box model, but its child elements are displayed normally.
    • Example: When you need a container for semantic organization without creating a new layout level.
  4. run-in:

    • Effect: Depending on the context, the element may behave as a block or inline element.
    • Example: This value is relatively rare and can be used for layout between headings and paragraphs in certain cases.
  5. flex-start, flex-end, center, space-between, space-around, space-evenly:

    • Effect: These values are primarily used with flex container properties like align-items, align-content, and justify-content, not the display property, to define alignment of flex items along the main or cross axis.
    • Example: When you need to align or distribute child items within a flex container.
  6. grid-auto-columns, grid-auto-rows:

    • Effect: These values are used on grid containers to define the size of implicitly created rows or columns.
    • Example: When you have a dynamic number of grid items and need automatic row or column sizes.
  7. grid-template-columns, grid-template-rows:

    • Effect: These values are used on grid containers to define the size and number of explicitly created rows or columns.
    • Example: When designing a specific grid layout where you need to specify the size of each column or row.
  8. grid-column-start, grid-column-end, grid-row-start, grid-row-end:

    • Effect: These values are used on grid items to define their position and span across columns or rows.
    • Example: When you need to place an element in the grid that spans multiple columns or rows.

The CSS display property is a complex and powerful tool capable of handling various layout requirements. As the CSS specification continues to evolve, new display values and layout models like Flexbox and Grid provide unprecedented flexibility and control.

2024年6月29日 12:07 回复

Using the file loader module to request resources is a common approach in Webpack (source). However, if you require greater flexibility or a more straightforward interface, you can use the copy-webpack-plugin (npm, GitHub) to directly copy static files. For example, to copy the static directory to the build directory:

javascript
const CopyWebpackPlugin = require('copy-webpack-plugin'); module.exports = { context: path.join(__dirname, 'your-app'), plugins: [ new CopyWebpackPlugin({ patterns: [ { from: 'static' } ] }) ] };

Compatibility Note: If you are using an older version of Webpack, such as webpack@4.x.x, use copy-webpack-plugin@6.x.x. Otherwise, use the latest version.

2024年6月29日 12:07 回复

你不需要复制东西,webpack 的工作方式与 gulp 不同。Webpack 是一个模块捆绑器,您在文件中引用的所有内容都将包含在内。您只需要为此指定一个加载程序。

所以如果你写:

shell
var myImage = require("./static/myImage.jpg");

Webpack 首先尝试将引用的文件解析为 JavaScript(因为这是默认的)。当然,那会失败。这就是为什么您需要为该文件类型指定一个加载器。例如,文件或url -loader获取引用的文件,将其放入 webpack 的输出文件夹(应该build在您的情况下)并返回该文件的哈希 url。

shell
var myImage = require("./static/myImage.jpg"); console.log(myImage); // '/build/12as7f9asfasgasg.jpg'

通常加载器是通过 webpack 配置应用的:

shell
// webpack.config.js module.exports = { ... module: { loaders: [ { test: /\.(jpe?g|gif|png|svg|woff|ttf|wav|mp3)$/, loader: "file" } ] } };

当然,您需要先安装文件加载器才能使其工作。

2024年6月29日 12:07 回复

如果您想复制静态文件,可以通过以下方式使用文件加载器:

对于 html 文件:

在 webpack.config.js 中:

shell
module.exports = { ... module: { loaders: [ { test: /\.(html)$/, loader: "file?name=[path][name].[ext]&context=./app/static" } ] } };

在你的 js 文件中:

shell
require.context("./static/", true, /^\.\/.*\.html/);

./static/ 是相对于你的js文件所在的位置。

您可以对图像或其他内容执行相同的操作。上下文是一种强有力的探索方法!

2024年6月29日 12:07 回复

Webpack 5 添加了资产模块,这些模块本质上是常见文件加载器的替代品。我复制了以下文档的相关部分:

  • asset/resource发出一个单独的文件并导出 URL。以前可以通过使用file-loader.
  • asset/inline导出资产的数据 URI。以前可以通过使用url-loader.
  • asset/source导出资产的源代码。以前可以通过使用raw-loader.
  • asset自动选择导出数据 URI 和发出单独的文件。以前可以通过使用url-loader资产规模限制来实现。

要添加一个,您可以使您的配置如下所示:

shell
// webpack.config.js module.exports = { ... module: { rules: [ { test: /\.(jpe?g|gif|png|svg|woff|ttf|wav|mp3)$/, type: "asset/resource" } ] } };

要控制文件的输出方式,您可以使用模板化路径

在配置中,您可以在此处设置全局模板:

shell
// webpack.config.js module.exports = { ... output: { ... assetModuleFilename: '[path][name].[hash][ext][query]' } }

要覆盖一组特定的资产,您可以执行以下操作:

shell
// webpack.config.js module.exports = { ... module: { rules: [ { test: /\.(jpe?g|gif|png|svg|woff|ttf|wav|mp3)$/, type: "asset/resource" generator: { filename: '[path][name].[hash][ext][query]' } } ] } };

提供的模板将生成类似于build/images/img.151cfcfa1bd74779aadb.png. 哈希对于缓存清除等很有用。您应该根据您的需要进行修改。

2024年6月29日 12:07 回复

你的答案