Skip to content

Commit

Permalink
Add Kotlin asset support (#2210)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoweb authored and devongovett committed Dec 18, 2018
1 parent 3e1c5a3 commit 29dca2f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/fs/package.json
Expand Up @@ -20,7 +20,8 @@
"prepublish": "yarn build"
},
"dependencies": {
"@parcel/utils": "^1.10.3",
"mkdirp": "^0.5.1",
"@parcel/utils": "^1.10.3"
"rimraf": "^2.6.2"
}
}
2 changes: 2 additions & 0 deletions packages/core/fs/src/fs.js
@@ -1,12 +1,14 @@
const {promisify} = require('@parcel/utils');
const fs = require('fs');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');

exports.readFile = promisify(fs.readFile);
exports.writeFile = promisify(fs.writeFile);
exports.stat = promisify(fs.stat);
exports.readdir = promisify(fs.readdir);
exports.unlink = promisify(fs.unlink);
exports.rimraf = promisify(rimraf);
exports.realpath = async function(path) {
const realpath = promisify(fs.realpath);
try {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/integration-tests/package.json
Expand Up @@ -13,12 +13,14 @@
},
"devDependencies": {
"@babel/core": "^7.2.0",
"@jetbrains/kotlinc-js-api": "^1.2.12",
"@parcel/fs": "^1.10.3",
"@parcel/test-utils": "^1.10.3",
"codecov": "^3.0.0",
"command-exists": "^1.2.6",
"graphql-tag": "^2.6.0",
"json5": "^1.0.1",
"kotlin": "^1.3.11",
"mocha": "^5.1.1",
"mocha-junit-reporter": "^1.18.0",
"mocha-multi-reporters": "^1.1.7",
Expand Down
@@ -0,0 +1,3 @@
var test = require('./test.kt');

module.exports = test.sum(2, 3);
@@ -0,0 +1,4 @@
@JsName("sum")
fun sum(a: Int, b: Int): Int {
return a + b
}
16 changes: 16 additions & 0 deletions packages/core/integration-tests/test/kotlin.js
@@ -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);
});
});
1 change: 1 addition & 0 deletions packages/core/parcel-bundler/src/Parser.js
Expand Up @@ -27,6 +27,7 @@ class Parser {
this.registerExtension('toml', './assets/TOMLAsset');
this.registerExtension('gql', './assets/GraphqlAsset');
this.registerExtension('graphql', './assets/GraphqlAsset');
this.registerExtension('kt', './assets/KotlinAsset');

this.registerExtension('css', './assets/CSSAsset');
this.registerExtension('pcss', './assets/CSSAsset');
Expand Down
63 changes: 63 additions & 0 deletions packages/core/parcel-bundler/src/assets/KotlinAsset.js
@@ -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;

0 comments on commit 29dca2f

Please sign in to comment.