返回介绍

extract-loader

发布于 2019-05-27 04:54:24 字数 7331 浏览 1309 评论 0 收藏 0

webpack loader to extract HTML and CSS from the bundle.

The extract-loader evaluates the given source code on the fly and returns the result as string. Its main use-case is to resolve urls within HTML and CSS coming from their respective loaders. Use the /doc/webpack-loaders-file-loader to emit the extract-loader's result as separate file.

import stylesheetUrl from "/doc/webpack-loaders-file-loader!extract-loader!/doc/webpack-loaders-css-loader!main.css";
// stylesheetUrl will now be the hashed url to the final stylesheet

The extract-loader works similar to the extract-text-webpack-plugin and is meant as a lean alternative to it. When evaluating the source code, it provides a fake context which was especially designed to cope with the code generated by the html- or the /doc/webpack-loaders-css-loader. Thus it might not work in other situations.

Installation

npm install extract-loader

Examples

Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or hot module replacement in development. In production, on the other hand, it's not a good idea to apply your stylesheets depending on JS execution. Rendering may be delayed or even a FOUC might be visible. Thus it's still better to have them as separate files in your final production build.

With the extract-loader, you are able to reference your main.css as regular entry. The following webpack.config.js shows how to load your styles with the /doc/webpack-loaders-style-loader in development and as separate file in production.

const live = process.env.NODE_ENV === "production";
const mainCss = ["/doc/webpack-loaders-css-loader", path.join(__dirname, "app", "main.css")];

if (live) {
    mainCss.unshift("/doc/webpack-loaders-file-loader?name=[name].[ext]", "extract-loader");
} else {
    mainCss.unshift("/doc/webpack-loaders-style-loader");
}

module.exports = {
    entry: [
        path.join(__dirname, "app", "main.js"),
        mainCss.join("!")
    ],
    ...
};

Extracting the index.html

You can even add your index.html as entry and just reference your stylesheets from there. You just need to tell the /doc/webpack-loaders-html-loader to also pick up link:href:

const indexHtml = path.join(__dirname, "app", "index.html");

module.exports = {
    entry: [
        path.join(__dirname, "app", "main.js"),
        indexHtml
    ],
    ...
    module: {
        rules: [
            {
                test: indexHtml,
                use: [
                    {
                        loader: "/doc/webpack-loaders-file-loader",
                        options: {
                            name: "[name]-dist.[ext]",
                        },
                    },
                    {
                        loader: "extract-loader",
                    },
                    {
                        loader: "/doc/webpack-loaders-html-loader",
                        options: {
                            attrs: ["img:src", "link:href"],
                            interpolate: true,
                        },
                    },
                ],
            },
            {
                test: /\.css$/,
                loaders: [
                    {
                        loader: "/doc/webpack-loaders-file-loader",
                    },
                    {
                        loader: "extract-loader",
                    },
                    {
                        loader: "/doc/webpack-loaders-css-loader",
                    },
                ],
            },
            {
                test: /\.jpg$/,
                loaders: [
                    {
                        loader: "/doc/webpack-loaders-file-loader"
                    },
                ],
            },
        ]
    }
};

turns

<html>
<head>
    <link href="main.css" type="text/css" rel="stylesheet">
</head>
<body>
    <img src="hi.jpg">
</body>
</html>

into

<html>
<head>
    <link href="7c57758b88216530ef48069c2a4c685a.css" type="text/css" rel="stylesheet">
</head>
<body>
    <img src="6ac05174ae9b62257ff3aa8be43cf828.jpg">
</body>
</html>

Options

There is currently exactly one option: publicPath.
If you are using a relative publicPath in webpack's output options and extracting to a file with the /doc/webpack-loaders-file-loader, you might need this to account for the location of your extracted file.

Example:

module.exports = {
    output: {
        path: path.resolve("./dist"),
        publicPath: "dist/"
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    {
                        loader: "/doc/webpack-loaders-file-loader",
                        options: {
                            name: "assets/[name].[ext]",
                        },
                    },
                    {
                        loader: "extract-loader",
                        options: {
                            publicPath: "../",
                        }
                    },
                    {
                        loader: "/doc/webpack-loaders-css-loader",
                    },
                ],
            }
        ]
    }
};

You need another option? Then you should think about:

Contributing

From opening a bug report to creating a pull request: every contribution is appreciated and welcome. If you're planing to implement a new feature or change the api please create an issue first. This way we can ensure that your precious work is not in vain.

All pull requests should have 100% test coverage (with notable exceptions) and need to pass all tests.

  • Call npm test to run the unit tests
  • Call npm run coverage to check the test coverage (using istanbul)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文