From fbcfd5962b66968ff567946ffaa8d447e42c4239 Mon Sep 17 00:00:00 2001 From: Luca Spinazzola Date: Wed, 16 Aug 2023 12:48:06 -0400 Subject: [PATCH] fix packing test-resources for library js tests --- gradle/pack-library-test-resources.gradle.kts | 35 +++++++++++++++++++ library/build.gradle.kts | 4 ++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 gradle/pack-library-test-resources.gradle.kts diff --git a/gradle/pack-library-test-resources.gradle.kts b/gradle/pack-library-test-resources.gradle.kts new file mode 100644 index 00000000..d6e5c9a3 --- /dev/null +++ b/gradle/pack-library-test-resources.gradle.kts @@ -0,0 +1,35 @@ +val rootProjectAbsPath = rootProject.projectDir.absolutePath +val path = """"$rootProjectAbsPath/library/build/generated/moko/jsMain/iomichaelrockslibphonenumber/res"""" +val webpackConfig = File(projectDir, "webpack.config.d/pack-library-resources-generated.js") + +fun createWebpackConfig() { + val configText = """const path = require('path'); + +const mokoResourcePath = path.resolve($path); + +config.module.rules.push({ + test: /(ShortNumberMetadataProto_|PhoneNumberMetadataProto_|PhoneNumberMetadataProtoForTesting_|PhoneNumberAlternateFormatsProto_)[^.]+${'$'}/, + include: [ + path.resolve(mokoResourcePath, "files"), + ], + type: 'asset/inline', + generator: { + dataUrl: content => { + // Convert buffer content to base64 and prepend it with mimetype + return `${'$'}{content.toString('base64')}` + } + } +});""" + webpackConfig.writeText(configText) +} + +tasks.create("createPackResourcesWebpackConfig") { + // Define the task's output + outputs.file(webpackConfig) + + doFirst { + createWebpackConfig() + } +} + +tasks.getByName("compileKotlinJs").dependsOn("createPackResourcesWebpackConfig") diff --git a/library/build.gradle.kts b/library/build.gradle.kts index 32c2a630..ed089b28 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -65,7 +65,7 @@ kotlin { } val jvmCommonTest by creating { dependsOn(commonTest) - dependencies{ + dependencies { implementation(libs.junit) implementation(libs.mockito.core) } @@ -215,3 +215,5 @@ afterEvaluate { } } */ + +apply(from = "$rootDir/gradle/pack-library-test-resources.gradle.kts") \ No newline at end of file