Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Encore.configureUrlLoader() method to the public API #296

Merged
merged 4 commits into from
Apr 14, 2018

Conversation

Lyrkan
Copy link
Collaborator

@Lyrkan Lyrkan commented Apr 11, 2018

This PR adds a configureUrlLoader() method to the public API in order to allow the use of the url-loader instead of the file-loader for images and fonts (closes #295).

Usage:

// Enable the url-loader for both images and fonts
Encore.configureUrlLoader({
    images: {
        limit: 8192,
        mimetype: 'image/png'
    },
    fonts: {
        limit: 4096
    }
});

// Enable the url-loader for images and implicitly
// disable it for fonts (by omitting the "fonts" key)
Encore.configureUrlLoader({
    images: {
        limit: 8192,
        mimetype: 'image/png'
    }
});

// Enable the url-loader for fonts and explicitly
// disable it for images (by using a falsy value
// for the "images" key).
Encore.configureUrlLoader({
    images: false,
    fonts: {
        limit: 4096
    }
});

@Lyrkan Lyrkan added the Feature New Feature label Apr 11, 2018

if (this.webpackConfig.urlLoaderOptions.images) {
loaderName = 'url-loader';
loaderOptions = this.webpackConfig.urlLoaderOptions.images;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the file-loader options must be combined with the url-loader options. Otherwise, we won't have the right config in case the url-loader fallbacks to the file-loader for bigger files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, should be good now!

package.json Outdated
@@ -43,6 +43,7 @@
"pretty-error": "^2.1.1",
"resolve-url-loader": "^2.0.2",
"style-loader": "^0.13.2",
"url-loader": "^1.0.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it actually be a requirement for everyone using Encore ? This is an opt-in feature.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I hesitated a bit on that one but ended-up adding it as a main dependency without any good reason.

I moved it to dev dependencies and added the usual ensurePackageExist checks.

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also create PR on the docs? Because, this will definitely be merged :)

if (validKeys.indexOf(key) === -1) {
throw new Error(`"${key}" is not a valid key for configureUrlLoader(). Valid keys: ${validKeys.join(', ')}.`);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay for validation :)

@Koc
Copy link

Koc commented Apr 12, 2018

@Lyrkan thank you so much for this PR

@weaverryan
Copy link
Member

Thank you @Lyrkan! I've wanted this feature for awhile :)

@weaverryan weaverryan merged commit 92ea9c4 into symfony:master Apr 14, 2018
weaverryan added a commit that referenced this pull request Apr 14, 2018
… (Lyrkan)

This PR was squashed before being merged into the master branch (closes #296).

Discussion
----------

Add Encore.configureUrlLoader() method to the public API

This PR adds a `configureUrlLoader()` method to the public API in order to allow the use of the `url-loader` instead of the `file-loader` for images and fonts (closes #295).

Usage:

```js
// Enable the url-loader for both images and fonts
Encore.configureUrlLoader({
    images: {
        limit: 8192,
        mimetype: 'image/png'
    },
    fonts: {
        limit: 4096
    }
});

// Enable the url-loader for images and implicitly
// disable it for fonts (by omitting the "fonts" key)
Encore.configureUrlLoader({
    images: {
        limit: 8192,
        mimetype: 'image/png'
    }
});

// Enable the url-loader for fonts and explicitly
// disable it for images (by using a falsy value
// for the "images" key).
Encore.configureUrlLoader({
    images: false,
    fonts: {
        limit: 4096
    }
});
```

Commits
-------

92ea9c4 Encore.configureUrlLoader() - Replace some 'let' by 'const'
9ca96a5 Encore.configureUrlLoader() - Move url-loader to dev dependencies
69e2cfe Encore.configureUrlLoader() - Merge options instead of replacing them
0ba2677 Add Encore.configureUrlLoader() method to the public API
@Lyrkan
Copy link
Collaborator Author

Lyrkan commented Apr 14, 2018

I'll try to add some doc for it next week!

@simensen
Copy link

👍 so awesome this made my night. :)

@javiereguiluz
Copy link
Member

@Lyrkan thanks for volunteering to write some docs about this. Reading the examples of the descriptions, these are my main doubts:

  • Is the mimetype of image optional? If I don't define it, would it inline all image formats? Can I define multiple formats in an array or is this option always a string? How is mimetype detected and what about edge cases where the mimetype is wrongly detected? (I didn't follow the discussion, but using the file extension instead of the mimetype would be simpler for all: extension: 'svg', extension: 'png', etc.)
  • The size limit in font ... is it per font family or per font file? If a font defines 4 variants (normal, bold, italic, font italic) would it be 4KB for all of them ... or 4KB per file (4 * 4KB = 16KB in total).

Thanks!

@Lyrkan
Copy link
Collaborator Author

Lyrkan commented Apr 16, 2018

@javiereguiluz These options are the ones provided by the URL loader, not something we added on our side:

  • The mimetype is optional and isn't a filter. You can use it if you want to force the mimetype in the generated data URL (data:<mimetype>;base64,<data>). By default the URL loader will look at the file extension to guess it.

  • The size limit is per require call (for instance an url(...) in a CSS file).

javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Apr 19, 2018
…n, javiereguiluz)

This PR was merged into the 3.4 branch.

Discussion
----------

[Encore] Encore.configureUrlLoader() documentation

This PR adds the documentation for the `Encore.configureUrlLoader()` method (symfony/webpack-encore#296).

Not sure if it should be more detailed since the other options of the URL loader are typically not needed...

Commits
-------

5c87901 Use the default article title
4d8700f Reword
6820867 Encore.configureUrlLoader() documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support of the url-loader
6 participants