Skip to content

Commit

Permalink
#154 fix js fonts works
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 committed Mar 12, 2023
1 parent f22d282 commit 855e079
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 152 deletions.
205 changes: 164 additions & 41 deletions kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class JsFontsGenerator(

@Suppress("StringTemplate")
override fun getPropertyInitializer(fontFile: File): CodeBlock {
return CodeBlock.of("FontResource(" +
"fileUrl = js(\"require(\\\"$FONTS_DIR/${fontFile.name}\\\")\") as String, " +
"fontFamily = %S)", fontFile.nameWithoutExtension)
return CodeBlock.of(
"FontResource(" +
"fileUrl = js(\"require(\\\"$FONTS_DIR/${fontFile.name}\\\")\") as String, " +
"fontFamily = %S)", fontFile.nameWithoutExtension
)
}

override fun beforeGenerateResources(objectBuilder: TypeSpec.Builder, files: List<FontFile>) {
Expand All @@ -57,12 +59,12 @@ class JsFontsGenerator(

if (files.isEmpty()) return

objectBuilder.addSuperinterface(ClassName("dev.icerock.moko.resources", "CssDeclarationsUriHolder"))

objectBuilder.addProperty(
PropertySpec.builder("cssDeclarationsUri", STRING, KModifier.OVERRIDE)
.initializer("js(%S) as String", """require("$FONTS_DIR/$cssDeclarationsFileName")""")
.build()
objectBuilder.addFunction(
FunSpec.builder("addFontsToPage")
.addCode(
"js(%S)",
"""require("$FONTS_DIR/$cssDeclarationsFileName")"""
).build()
)
}

Expand All @@ -82,7 +84,7 @@ class JsFontsGenerator(
"""
@font-face {
font-family: "$family";
src: url("$FONTS_DIR/${file.name}");
src: url("${file.name}");
}
""".trimIndent()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package dev.icerock.gradle.generator.js

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.STRING
Expand Down Expand Up @@ -48,7 +49,6 @@ class JsMRGenerator(
.build()
)

@OptIn(ExperimentalStdlibApi::class)
val stringsLoaderInitializer = buildList {
val stringsObjectLoader = mrClass
.typeSpecs
Expand Down Expand Up @@ -147,41 +147,54 @@ class JsMRGenerator(
webpackDir.mkdirs()

val webpackConfig: File = File(webpackDir, "moko-resources-generated.js")
val webpackResourcesDir: String = resourcesOutput.absolutePath
.replace("\\","\\\\")

webpackConfig.writeText(
// language=js
"""
const path = require('path');
const mokoResourcePath = path.resolve("${resourcesOutput.absolutePath.replace("\\","\\\\")}");
config.module.rules.push(
{
test: /\.(.*)/,
include: [
path.resolve(mokoResourcePath)
],
type: 'asset/resource'
}
);
config.module.rules.push(
{
test: /\.(otf|ttf)?${'$'}/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
}
]
}
)
config.resolve.modules.push(
path.resolve(mokoResourcePath)
);
// noinspection JSUnnecessarySemicolon
;(function(config) {
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const mokoResourcePath = path.resolve("$webpackResourcesDir");
config.module.rules.push(
{
test: /\.(.*)/,
resource: [
path.resolve(mokoResourcePath, "files"),
path.resolve(mokoResourcePath, "images"),
path.resolve(mokoResourcePath, "localization"),
],
type: 'asset/resource'
}
);
config.plugins.push(new MiniCssExtractPlugin())
config.module.rules.push(
{
test: /\.css${'$'}/,
resource: [
path.resolve(mokoResourcePath, "fonts"),
],
use: ['style-loader', 'css-loader']
}
)
config.module.rules.push(
{
test: /\.(otf|ttf)?${'$'}/,
resource: [
path.resolve(mokoResourcePath, "fonts"),
],
type: 'asset/resource',
}
)
config.resolve.modules.push(mokoResourcePath);
})(config);
""".trimIndent()
)
}
Expand Down
5 changes: 3 additions & 2 deletions resources/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ kotlin {
dependencies {
api(npm("bcp-47", "1.0.8"))
api(npm("@messageformat/core", "3.0.0"))
implementation(npm("url-loader", "4.1.1"))
implementation(npm("file-loader", "6.2.0"))
api(npm("mini-css-extract-plugin", "2.6.1"))
api(npm("css-loader", "6.7.3"))
api(npm("style-loader", "3.3.1"))

implementation(libs.kotlinxCoroutines)
}
Expand Down

This file was deleted.

Loading

0 comments on commit 855e079

Please sign in to comment.