forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlin asset support (parcel-bundler#2210)
- Loading branch information
1 parent
3e1c5a3
commit 29dca2f
Showing
8 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/core/integration-tests/test/integration/kotlin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var test = require('./test.kt'); | ||
|
||
module.exports = test.sum(2, 3); |
4 changes: 4 additions & 0 deletions
4
packages/core/integration-tests/test/integration/kotlin/test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@JsName("sum") | ||
fun sum(a: Int, b: Int): Int { | ||
return a + b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const assert = require('assert'); | ||
const {bundle, assertBundleTree, run} = require('./utils'); | ||
|
||
describe('kotlin', function() { | ||
it('should produce a basic kotlin bundle', async function() { | ||
let b = await bundle(__dirname + '/integration/kotlin/index.js'); | ||
|
||
await assertBundleTree(b, { | ||
type: 'js', | ||
assets: ['test.kt', 'index.js', 'browser.js', 'kotlin.js'] | ||
}); | ||
|
||
let output = await run(b); | ||
assert.equal(output, 5); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const Asset = require('../Asset'); | ||
const localRequire = require('../utils/localRequire'); | ||
const path = require('path'); | ||
const fs = require('@parcel/fs'); | ||
const os = require('os'); | ||
|
||
class KotlinAsset extends Asset { | ||
constructor(name, options) { | ||
super(name, options); | ||
this.type = 'js'; | ||
} | ||
|
||
async generate() { | ||
// require kotlin | ||
const kotlinCompiler = await localRequire( | ||
'@jetbrains/kotlinc-js-api', | ||
this.name | ||
); | ||
|
||
let id = Math.random() | ||
.toString(36) | ||
.slice(3); | ||
let dir = path.join(os.tmpdir(), id); | ||
let filename = path.join(dir, id + '.js'); | ||
|
||
await fs.mkdirp(dir); | ||
|
||
await kotlinCompiler.compile({ | ||
output: filename, | ||
sources: [this.name], | ||
moduleKind: 'commonjs', | ||
noStdlib: false, | ||
metaInfo: true, | ||
sourceMaps: this.options.sourceMaps | ||
}); | ||
|
||
let source = await fs.readFile(filename, 'utf8'); | ||
let sourceMap; | ||
if (this.options.sourceMaps) { | ||
sourceMap = await fs.readFile(filename + '.map', 'utf8'); | ||
|
||
sourceMap = JSON.parse(sourceMap); | ||
sourceMap.sources = [this.relativeName]; | ||
sourceMap.sourcesContent = [this.contents]; | ||
|
||
// remove source map url | ||
source = source.substring(0, source.lastIndexOf('//# sourceMappingURL')); | ||
} | ||
|
||
// delete temp directory | ||
await fs.rimraf(dir); | ||
|
||
return [ | ||
{ | ||
type: 'js', | ||
value: source, | ||
sourceMap | ||
} | ||
]; | ||
} | ||
} | ||
|
||
module.exports = KotlinAsset; |