Giter VIP home page Giter VIP logo

new-url-loader's Introduction

A tiny alternative to url-loader and file-loader for webpack 5.

The url-loader and file-loader are deprecated in webpack 5 and replaced by asset modules. Loaders that are used with url-loader or file-loader (example: @svgr/webpack in Create React App) might still need them. new-url-loader provides the functionality of both url-loader and file-loader using asset modules and URL assets.

Installation

npm install new-url-loader --save-dev

Usage

If you are using url-loader or file-loader with another loader (example: @svgr/webpack), you can replace them with new-url-loader. The following examples show how to configure webpack to load SVGs using @svgr/webpack.

Replacing url-loader

Old

{
  test: /\.svg$/,
  use: ['@svgr/webpack', 'url-loader'],
}

New

{
  test: /\.svg$/,
  oneOf: [
    {
      dependency: { not: ['url'] }, // exclude new URL calls
      use: ['@svgr/webpack', 'new-url-loader'],
    },
    {
      type: 'asset', // export a data URI or emit a separate file
    },
  ],
}

By default, a file with size less than 8kb will be inlined as a data URI and emitted as a separate file otherwise. To change the file size limit, use:

{
  type: 'asset',
  parser: {
    dataUrlCondition: {
      maxSize: 4 * 1024, // 4kb
    },
  },
}

You can also specify a function to decide whether to inline a file or not. Learn more

Replacing file-loader

Old

{
  test: /\.svg$/,
  use: ['@svgr/webpack', 'file-loader'],
}

New

{
  test: /\.svg$/,
  oneOf: [
    {
      dependency: { not: ['url'] }, // exclude new URL calls
      use: ['@svgr/webpack', 'new-url-loader'],
    },
    {
      type: 'asset/resource', // emit a separate file
    },
  ],
}

By default, files are emitted with [hash][ext][query] name to output directory. To customize the output filename, use:

{
  type: 'asset/resource',
  generator: {
    filename: 'static/media/[name].[hash][ext]',
  },
}

It also works with asset module type. Learn more

Server Side Rendering

For Server Side Rendering (SSR) when base URL is not known by server, set module.parser.javascript.url to 'relative' to avoid generating URLs like file:///path/to/project/dist/file.svg. See #1.

Examples

See examples.

new-url-loader's People

Contributors

marella avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

new-url-loader's Issues

URLs are broken after SSR if publicPath is relative

URL objects always represent absolute URLs. It's not a problem if the code is only executed in a browser, since we will simply use the current domain if necessary. We're also good if publicPath is absolute because URLs are naturally absolute in this case.

The problem arises when publicPath is relative and the code is executed on the server side. In this case, URLs will look like this (at least on Windows):

file:///C:/public-path/file.svg

I was able to resolve this by omitting everything before __webpack_public_path__:

module.exports = function () {
    const path = JSON.stringify(this.resourcePath);
    return `export default __webpack_public_path__ + new URL(${path}, import.meta.url).toString().split(__webpack_public_path__)[1]`;
};

It looks extremely hacky though, so I'll probably stick to file-loader instead ๐Ÿ˜….

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.