在纯 JavaScript 开发人员的目标位置有一个 Scala.js 项目(所有 JSExport 都可以),是否有一种现有的方法可以自动生成可以发布到远程存储库并由这些开发人员使用的 NPM 包?
我有一个 Scala.js 项目,其目标是供纯 JavaScript 开发人员使用。一切都已开发完毕,包括从 javascript 导出以使用库。编译后,我有两个 js 文件:lib 本身(xx_opt.js),以及本地 js 依赖项到 xx_jsdeps.js。
到目前为止一切都很好,但是有没有一种方法可以(几乎)自动生成一个包(NPM?),可以发布到远程存储库并由纯 JavaScript 开发人员像任何其他 JS 库一样使用? 我发现很多东西都使用npm依赖,但是没有逆向操作。 我错过了什么,还是必须“手工”完成? Il 可能很微不足道,但我必须承认我对 JS 和 NPM 世界不太熟悉,因此任何技巧或最佳实践可能会有用。
最佳答案
这是我在 build.sbt 中添加的任务。也许不是最聪明的方法,但它足以满足我的需求。它在目标文件夹(此处为 target/npm/)中创建一些文件夹,并将所需的文件复制到其中。它还使用 build.sbt 中存在的元数据(包名称、版本、作者……)生成 package.json 文件。
val npmTargetDir = s"target/npm/" // where to generate npm
val npmConf = "npm_config" // directory with static files for NPM package
val npmTask = taskKey[Unit](s"Create npm package arborescence in $npmTargetDir")
npmTask := {
// JS libraries must first be generated
(Compile / fastOptJS).value
(Compile / fullOptJS).value
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.Files.copy
import java.nio.file.Paths.get
import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets
implicit def toPath (filename: String) = get(filename)
def copyToDir(filePathName:String, dirName:String) = {
val fileName = new File(filePathName).getName
copy (s"$filePathName", s"$dirName/$fileName", REPLACE_EXISTING)
}
val libName = name.value.toLowerCase()
val inputDir = "target/scala-2.12"
val targetDir = s"$npmTargetDir/$libName"
val sourceDir = "source/js"
val distDir = "dist/js"
val demoCssDir = "demo/css"
// create arborescence
new File(targetDir).mkdirs()
List(demoCssDir, distDir, sourceDir).foreach(d => new File(s"$targetDir/$d").mkdirs())
// copy static files
//copyToDir(s"$npmConf/package.json", targetDir)
copyToDir(s"$npmConf/licence.txt", targetDir)
copyToDir(s"$npmConf/exampleJSFastOpt.html", targetDir)
copyToDir(s"$npmConf/exampleJSFullOpt.html", targetDir)
copyToDir(s"$npmConf/style.css", s"$targetDir/$demoCssDir")
// copy optimized js library
val fileDist = List(s"$libName-opt.js", s"$libName-jsdeps.min.js", s"$libName-opt.js.map")
for(file <- fileDist) {
println(s"copy file $inputDir/$file")
copy(s"$inputDir/$file", s"$targetDir/$distDir/$file", REPLACE_EXISTING)
}
// copy non optimized js library (for debug purpose)
val fileSource = List(s"$libName-fastopt.js", s"$libName-jsdeps.js", s"$libName-fastopt.js.map")
for(file <- fileSource) {
println(s"copy file $file")
copy(s"$inputDir/$file", s"$targetDir/$sourceDir/$file", REPLACE_EXISTING)
}
val packageJson = s"""{
"name": "$libName",
"version": "${version.value.toString}",
"description": "${description.value.toString}",
"scripts": {
"test": "sbt test"
},
"main": "$distDir",
"repository": {
"type": "git",
"url": "git+https://...git"
},
"keywords": [
"scalajs"
],
"author": "${author}",
"license": "SEE LICENSE IN licence.txt",
"bugs": {
"url": "https://..."
},
"homepage": "https://...",
"dependencies": {}
}"""
println(packageJson)
Files.write(Paths.get(s"$targetDir/package.json"), packageJson.getBytes(StandardCharsets.UTF_8))
println(s"NPM arborescence for package created in $npmTargetDir")
}
关于scala - 从 Scala.js 项目生成 NPM 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309661/