Skip to content

Commit

Permalink
feat(webpack): add support for fonts to webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Oct 15, 2019
1 parent e56a7de commit 2148823
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Expand Up @@ -71,6 +71,7 @@
"require-from-string": "^2.0.2",
"ts-node": "^8.0.3",
"typescript": "^3.6.3",
"url-loader": "2.1.0",
"webpack": "^4.29.6",
"webpack-bundle-analyzer": "^3.1.0",
"webpack-dev-middleware": "^3.6.0",
Expand Down
Expand Up @@ -55,6 +55,18 @@ Object {
"test": /\\\\\\.css\\$/,
"use": "raw-loader",
},
Object {
"test": /\\\\\\.\\(woff\\(2\\)\\?\\|ttf\\|eot\\)\\(\\\\\\?v=\\\\d\\+\\\\\\.\\\\d\\+\\\\\\.\\\\d\\+\\)\\?\\$/,
"use": Object {
"loader": "url-loader",
"options": Object {
"emitFile": true,
"limit": 25000,
"name": [Function],
"outputPath": "fonts",
},
},
},
],
},
"name": "client",
Expand Down Expand Up @@ -177,6 +189,18 @@ Object {
"test": /\\\\\\.css\\$/,
"use": "raw-loader",
},
Object {
"test": /\\\\\\.\\(woff\\(2\\)\\?\\|ttf\\|eot\\)\\(\\\\\\?v=\\\\d\\+\\\\\\.\\\\d\\+\\\\\\.\\\\d\\+\\)\\?\\$/,
"use": Object {
"loader": "url-loader",
"options": Object {
"emitFile": true,
"limit": 25000,
"name": [Function],
"outputPath": "fonts",
},
},
},
],
},
"name": "client",
Expand Down Expand Up @@ -287,6 +311,18 @@ Object {
"test": /\\\\\\.css\\$/,
"use": "raw-loader",
},
Object {
"test": /\\\\\\.\\(woff\\(2\\)\\?\\|ttf\\|eot\\)\\(\\\\\\?v=\\\\d\\+\\\\\\.\\\\d\\+\\\\\\.\\\\d\\+\\)\\?\\$/,
"use": Object {
"loader": "url-loader",
"options": Object {
"emitFile": false,
"limit": 25000,
"name": [Function],
"outputPath": "fonts",
},
},
},
],
},
"name": "server",
Expand Down Expand Up @@ -402,6 +438,18 @@ Object {
"test": /\\\\\\.css\\$/,
"use": "raw-loader",
},
Object {
"test": /\\\\\\.\\(woff\\(2\\)\\?\\|ttf\\|eot\\)\\(\\\\\\?v=\\\\d\\+\\\\\\.\\\\d\\+\\\\\\.\\\\d\\+\\)\\?\\$/,
"use": Object {
"loader": "url-loader",
"options": Object {
"emitFile": true,
"limit": 25000,
"name": [Function],
"outputPath": "fonts",
},
},
},
],
},
"name": "client",
Expand Down Expand Up @@ -516,6 +564,18 @@ Object {
"test": /\\\\\\.css\\$/,
"use": "raw-loader",
},
Object {
"test": /\\\\\\.\\(woff\\(2\\)\\?\\|ttf\\|eot\\)\\(\\\\\\?v=\\\\d\\+\\\\\\.\\\\d\\+\\\\\\.\\\\d\\+\\)\\?\\$/,
"use": Object {
"loader": "url-loader",
"options": Object {
"emitFile": true,
"limit": 25000,
"name": [Function],
"outputPath": "fonts",
},
},
},
],
},
"name": "client",
Expand Down Expand Up @@ -620,6 +680,18 @@ Object {
"test": /\\\\\\.css\\$/,
"use": "raw-loader",
},
Object {
"test": /\\\\\\.\\(woff\\(2\\)\\?\\|ttf\\|eot\\)\\(\\\\\\?v=\\\\d\\+\\\\\\.\\\\d\\+\\\\\\.\\\\d\\+\\)\\?\\$/,
"use": Object {
"loader": "url-loader",
"options": Object {
"emitFile": false,
"limit": 25000,
"name": [Function],
"outputPath": "fonts",
},
},
},
],
},
"name": "server",
Expand Down
28 changes: 23 additions & 5 deletions packages/core/src/config/webpack/modules.ts
Expand Up @@ -2,8 +2,7 @@ import hash from "hash-it";
import { Configuration } from "webpack";
import babelCore from "@babel/core/package.json";
import babelLoader from "babel-loader/package.json";
import { Target, BabelConfigs } from "../../../types";
import { Mode } from "../../../types";
import { Target, BabelConfigs, Mode } from "../../../types";

export default ({
target,
Expand Down Expand Up @@ -46,10 +45,10 @@ export default ({
{
loader: "file-loader",
options: {
name(file) {
const filename = /([^\/\\]+)\.(?:png|jpe?g|gif|svg)$/.exec(
name: (file: string): string => {
const filename = /([^/\\]+)\.(?:png|jpe?g|gif|svg)$/.exec(
file
) || [, "image"];
) || ["", "image"];
return mode === "development"
? `${filename[1]}.[ext]`
: `${filename[1]}-[hash].[ext]`;
Expand All @@ -63,6 +62,25 @@ export default ({
{
test: /\.css$/,
use: "raw-loader"
},
{
test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
use: {
loader: "url-loader",
options: {
name: (file: string): string => {
const filename = /([^/\\]+)\.(?:woff(2)?|ttf|eot)$/.exec(
file
) || ["", "font"];
return mode === "development"
? `${filename[1]}.[ext]`
: `${filename[1]}-[hash].[ext]`;
},
outputPath: "fonts",
limit: 25000,
emitFile: target !== "server"
}
}
}
]
};
Expand Down

0 comments on commit 2148823

Please sign in to comment.