Add patched kotlin-source-map-loader

This commit is contained in:
Ilya Goncharov
2019-10-03 15:15:19 +03:00
parent dad9958adf
commit e8efe5407d
13 changed files with 1758 additions and 23 deletions
+1
View File
@@ -52,4 +52,5 @@ kotlin-ultimate/
node_modules/
.rpt2_cache/
libraries/tools/kotlin-test-js-runner/lib/
libraries/tools/kotlin-source-map-loader/lib/
local.properties
@@ -111,7 +111,8 @@ tasks.withType<Test> {
":kotlin-scripting-common:install",
":kotlin-scripting-jvm:install",
":kotlin-scripting-compiler-embeddable:install",
":kotlin-test-js-runner:install"
":kotlin-test-js-runner:install",
":kotlin-source-map-loader:install"
)
executable = "${rootProject.extra["JDK_18"]!!}/bin/java"
@@ -20,6 +20,9 @@ class NpmVersions {
val webpackBundleAnalyzer = NpmPackageVersion("webpack-bundle-analyzer", "3.3.2")
val webpackDevServer = NpmPackageVersion("webpack-dev-server", "3.3.1")
// Temporarily use our own source-map-loader, while original source-map-loader is not updated
// https://github.com/webpack-contrib/source-map-loader/pull/91
val kotlinSourceMapLoader = KotlinGradleNpmPackage("source-map-loader")
val sourceMapLoader = NpmPackageVersion("source-map-loader", "0.2.4")
val sourceMapSupport = NpmPackageVersion("source-map-support", "0.5.12")
@@ -35,7 +35,7 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) : KotlinJsTestF
private val versions = nodeJs.versions
private val config: KarmaConfig = KarmaConfig()
private val requiredDependencies = mutableSetOf<NpmPackageVersion>()
private val requiredDependencies = mutableSetOf<RequiredKotlinJsDependency>()
private val configurators = mutableListOf<(KotlinJsTest) -> Unit>()
private val envJsCollector = mutableMapOf<String, String>()
@@ -44,15 +44,13 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) : KotlinJsTestF
private var configDirectory: File? = project.projectDir.resolve("karma.config.d").takeIf { it.isDirectory }
override val requiredNpmDependencies: Collection<RequiredKotlinJsDependency>
get() = mutableListOf<RequiredKotlinJsDependency>().also {
it.add(versions.kotlinJsTestRunner)
it.addAll(requiredDependencies)
}
get() = requiredDependencies.toList()
override val settingsState: String
get() = "KotlinKarma($config)"
init {
requiredDependencies.add(versions.kotlinJsTestRunner)
requiredDependencies.add(versions.karma)
useKotlinReporter()
@@ -176,7 +174,7 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) : KotlinJsTestF
requiredDependencies.add(versions.webpack)
requiredDependencies.add(versions.webpackCli)
requiredDependencies.add(versions.sourceMapLoader)
requiredDependencies.add(versions.kotlinSourceMapLoader)
requiredDependencies.add(versions.sourceMapSupport)
}
@@ -16,7 +16,7 @@ import org.gradle.deployment.internal.DeploymentRegistry
import org.gradle.process.internal.ExecHandle
import org.gradle.process.internal.ExecHandleFactory
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.targets.js.NpmPackageVersion
import org.jetbrains.kotlin.gradle.targets.js.RequiredKotlinJsDependency
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.targets.js.npm.RequiresNpmDependencies
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
@@ -124,7 +124,7 @@ open class KotlinWebpack : DefaultTask(), RequiresNpmDependencies {
override val nodeModulesRequired: Boolean
@Internal get() = true
override val requiredNpmDependencies: Collection<NpmPackageVersion>
override val requiredNpmDependencies: Collection<RequiredKotlinJsDependency>
@Internal get() = createRunner().config.getRequiredDependencies(versions)
@TaskAction
@@ -8,8 +8,8 @@
package org.jetbrains.kotlin.gradle.targets.js.webpack
import com.google.gson.GsonBuilder
import org.jetbrains.kotlin.gradle.targets.js.NpmPackageVersion
import org.jetbrains.kotlin.gradle.targets.js.NpmVersions
import org.jetbrains.kotlin.gradle.targets.js.RequiredKotlinJsDependency
import org.jetbrains.kotlin.gradle.targets.js.appendConfigsFromDir
import org.jetbrains.kotlin.gradle.targets.js.jsQuoted
import java.io.File
@@ -33,22 +33,23 @@ data class KotlinWebpackConfig(
val progressReporter: Boolean = false,
val progressReporterPathFilter: String? = null
) {
fun getRequiredDependencies(versions: NpmVersions) = mutableListOf<NpmPackageVersion>().also {
it.add(versions.webpack)
it.add(versions.webpackCli)
fun getRequiredDependencies(versions: NpmVersions) =
mutableListOf<RequiredKotlinJsDependency>().also {
it.add(versions.webpack)
it.add(versions.webpackCli)
if (bundleAnalyzerReportDir != null) {
it.add(versions.webpackBundleAnalyzer)
}
if (bundleAnalyzerReportDir != null) {
it.add(versions.webpackBundleAnalyzer)
}
if (sourceMaps) {
it.add(versions.sourceMapLoader)
}
if (sourceMaps) {
it.add(versions.kotlinSourceMapLoader)
}
if (devServer != null) {
it.add(versions.webpackDevServer)
if (devServer != null) {
it.add(versions.webpackDevServer)
}
}
}
enum class Mode(val code: String) {
DEVELOPMENT("development"),
@@ -193,7 +194,7 @@ data class KotlinWebpackConfig(
// source maps
config.module.rules.push({
test: /\.js${'$'}/,
use: ["source-map-loader"],
use: ["kotlin-source-map-loader"],
enforce: "pre"
});
config.devtool = '${devtool.code}';
@@ -0,0 +1,45 @@
# test directories
__tests__
test
tests
powered-test
# asset directories
docs
doc
website
images
assets
# examples
example
examples
# code coverage directories
coverage
.nyc_output
# build scripts
Makefile
Gulpfile.js
Gruntfile.js
# configs
appveyor.yml
circle.yml
codeship-services.yml
codeship-steps.yml
wercker.yml
.tern-project
.gitattributes
.editorconfig
.*ignore
.eslintrc
.jshintrc
.flowconfig
.documentup.json
.yarn-metadata.json
.travis.yml
# misc
*.md
@@ -0,0 +1,78 @@
import com.moowork.gradle.node.yarn.YarnTask
description = "Source Map Loader for Webpack"
plugins {
id("base")
id("com.moowork.node") version "1.2.0"
}
val default = configurations.getByName(Dependency.DEFAULT_CONFIGURATION)
val archives = configurations.getByName(Dependency.ARCHIVES_CONFIGURATION)
default.extendsFrom(archives)
plugins.apply("maven")
convention.getPlugin(MavenPluginConvention::class.java).also {
it.conf2ScopeMappings.addMapping(MavenPlugin.RUNTIME_PRIORITY, archives, Conf2ScopeMappingContainer.RUNTIME)
}
node {
version = "11.9.0"
download = true
nodeModulesDir = projectDir
}
tasks {
named("yarn") {
outputs.upToDateWhen {
projectDir.resolve("node_modules").isDirectory
}
}
register<YarnTask>("yarnBuild") {
group = "build"
dependsOn("yarn")
setWorkingDir(projectDir)
args = listOf("build")
inputs.files(
"kotlin-source-map-loader.js",
"package.json",
"yarn.lock"
)
outputs.dir("lib")
}
register<Delete>("cleanYarn") {
group = "build"
delete = setOf(
"node_modules",
"lib",
".rpt2_cache"
)
}
named("clean") {
dependsOn("cleanYarn")
}
}
val jar by tasks.creating(Jar::class) {
dependsOn(tasks.named("yarnBuild"))
from(projectDir.resolve("lib"))
}
artifacts {
add(
"archives",
jar.archiveFile.get().asFile
) {
builtBy(jar)
}
}
publish()
@@ -0,0 +1,141 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var fs = require("fs");
var path = require("path");
var async = require("async");
var loaderUtils = require("loader-utils");
// Matches only the last occurrence of sourceMappingURL
var baseRegex = "\\s*[@#]\\s*sourceMappingURL\\s*=\\s*([^\\s]*)(?![\\S\\s]*sourceMappingURL)",
// Matches /* ... */ comments
regex1 = new RegExp("/\\*" + baseRegex + "\\s*\\*/"),
// Matches // .... comments
regex2 = new RegExp("//" + baseRegex + "($|\n|\r\n?)"),
// Matches DataUrls
regexDataUrl = /data:[^;\n]+(?:;charset=[^;\n]+)?;base64,([a-zA-Z0-9+/]+={0,2})/;
module.exports = function (input, inputMap) {
this.cacheable && this.cacheable();
var resolve = this.resolve;
var addDependency = this.addDependency;
var emitWarning = this.emitWarning || function () {
};
var match = input.match(regex1) || input.match(regex2);
if (match) {
var url = match[1];
var dataUrlMatch = regexDataUrl.exec(url);
var callback = this.async();
if (dataUrlMatch) {
var mapBase64 = dataUrlMatch[1];
var mapStr = (new Buffer(mapBase64, "base64")).toString();
var map;
try {
map = JSON.parse(mapStr)
}
catch (e) {
emitWarning("Cannot parse inline SourceMap '" + mapBase64.substr(0, 50) + "': " + e);
return untouched();
}
processMap(map, this.context, callback);
}
else {
resolve(this.context, loaderUtils.urlToRequest(url, true), function (err, result) {
if (err) {
emitWarning("Cannot find SourceMap '" + url + "': " + err);
return untouched();
}
addDependency(result);
fs.readFile(result, "utf-8", function (err, content) {
if (err) {
emitWarning("Cannot open SourceMap '" + result + "': " + err);
return untouched();
}
var map;
try {
map = JSON.parse(content);
}
catch (e) {
emitWarning("Cannot parse SourceMap '" + url + "': " + e);
return untouched();
}
processMap(map, path.dirname(result), callback);
});
}.bind(this));
}
}
else {
var callback = this.callback;
return untouched();
}
function untouched() {
callback(null, input, inputMap);
}
function resize(arr, size, defval) {
while (arr.length > size) {
arr.pop();
}
while (arr.length < size) {
arr.push(defval);
}
}
function processMap(map, context, callback) {
function setResult(map) {
callback(null, input.replace(match[0], ''), map);
}
var sourcesWithoutContent = [];
map.sourcesContent = map.sourcesContent || [];
resize(map.sourcesContent, map.sources.length, null);
map.sourcesContent.forEach(function (sourceContent, i) {
if (!sourceContent) {
sourcesWithoutContent.push({source: map.sources[i], index: i})
}
});
if (sourcesWithoutContent.length == 0) {
setResult(map)
}
else {
var sourcePrefix = map.sourceRoot ? map.sourceRoot + "/" : "";
async.map(sourcesWithoutContent, function (item, callback) {
var source = sourcePrefix + item.source;
map.sources = map.sources.map(function (s) {
return sourcePrefix + s;
});
delete map.sourceRoot;
resolve(context, loaderUtils.urlToRequest(source, true), function (err, result) {
if (err) {
emitWarning("Cannot find source file '" + source + "': " + err);
return callback(null, null);
}
addDependency(result);
fs.readFile(result, "utf-8", function (err, content) {
if (err) {
emitWarning("Cannot open source file '" + result + "': " + err);
return callback(null, null);
}
callback(null, {
index: item.index,
source: result,
content: content
});
});
});
}, function (err, info) {
info.forEach(function (item) {
if (item) {
map.sources[item.index] = item.source;
map.sourcesContent[item.index] = item.content;
}
});
setResult(map);
});
}
}
};
@@ -0,0 +1,17 @@
{
"name": "@kotlin/source-map-loader",
"version": "0.0.1",
"description": "Modern source-map-loader with additional features",
"main": "lib/kotlin-source-map-loader.js",
"files": [
"lib/**/*"
],
"scripts": {
"build": "cpx \"kotlin-source-map-loader.js\" lib"
},
"dependencies": {
},
"devDependencies": {
"cpx": "^1.5.0"
}
}
File diff suppressed because it is too large Load Diff
+20
View File
@@ -0,0 +1,20 @@
Copyright JS Foundation and other contributors
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+2
View File
@@ -150,6 +150,7 @@ include ":kotlin-build-common",
":kotlin-test:kotlin-test-js",
":kotlin-test:kotlin-test-js:kotlin-test-js-it",
":kotlin-test-js-runner",
":kotlin-source-map-loader",
":kotlinx-metadata",
":kotlinx-metadata-jvm",
":prepare:build.version",
@@ -307,6 +308,7 @@ project(':kotlin-test:kotlin-test-testng').projectDir = "$rootDir/libraries/kotl
project(':kotlin-test:kotlin-test-js').projectDir = "$rootDir/libraries/kotlin.test/js" as File
project(':kotlin-test:kotlin-test-js:kotlin-test-js-it').projectDir = "$rootDir/libraries/kotlin.test/js/it" as File
project(':kotlin-test-js-runner').projectDir = "$rootDir/libraries/tools/kotlin-test-js-runner" as File
project(':kotlin-source-map-loader').projectDir = "$rootDir/libraries/tools/kotlin-source-map-loader" as File
project(':kotlin-reflect').projectDir = "$rootDir/libraries/reflect" as File
project(':kotlin-reflect-api').projectDir = "$rootDir/libraries/reflect/api" as File
project(':kotlinx-metadata').projectDir = "$rootDir/libraries/kotlinx-metadata" as File