在纯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世界不是很熟悉,所以任何技巧或最佳实践都可能是有用的。
发布于 2019-08-08 00:28:15
这是我在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")
}https://stackoverflow.com/questions/57309661
复制相似问题