Skip to content

Commit 1637534

Browse files
hugueschabotgoto-bus-stop
authored andcommitted
Support Typescript synthetic default imports (#153)
* Support Typescript synthetic default imports * Add test for Typescript synthetic default imports
1 parent 26b73fb commit 1637534

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/browserify-transform.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ module.exports = function yoYoify (file, opts) {
6565
}
6666
if (node.parent.type === 'VariableDeclarator') {
6767
viewVariables.push(node.parent.id.name)
68+
// Typescript Synthetic Default Imports
69+
} else if (node.parent.type === 'CallExpression' && node.parent.callee.name === '__importDefault' && node.parent.parent.type === 'VariableDeclarator') {
70+
viewVariables.push(node.parent.parent.id.name)
6871
}
6972
}
7073

tests/transform/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ test('works', function (t) {
2525
})
2626
})
2727

28+
test('works with code transpiled from Typescript with esModuleInterop', function (t) {
29+
t.plan(5)
30+
var src = '"use strict";\n var __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { "default": mod };\n };\n Object.defineProperty(exports, "__esModule", { value: true });\n const nanohtml_1 = __importDefault(require("nanohtml"));\n module.exports = function (data) {\n const className = \'test\';\n return nanohtml_1.default `<div class="${className}">\n <h1>${data}</h1>\n <div is="my-div"></div>\n </div>`;\n };' // eslint-disable-line
31+
fs.writeFileSync(FIXTURE, src)
32+
var b = browserify(FIXTURE, {
33+
browserField: false,
34+
transform: path.join(__dirname, '../../')
35+
})
36+
b.bundle(function (err, src) {
37+
fs.unlinkSync(FIXTURE)
38+
t.ifError(err, 'no error')
39+
var result = src.toString()
40+
t.ok(result.indexOf('const nanohtml_1 = __importDefault({})') !== -1, 'replaced html dependency with {}')
41+
t.ok(result.indexOf('document.createElement("h1")') !== -1, 'created an h1 tag')
42+
t.ok(result.indexOf('document.createElement("div", { is: "my-div" })') !== -1, 'created an extended build-in element')
43+
t.ok(result.indexOf('setAttribute("class", arguments[1])') !== -1, 'set a class attribute')
44+
t.end()
45+
})
46+
})
47+
2848
test('strings + template expressions', function (t) {
2949
t.plan(2)
3050
var src = 'var html = require(\'nanohtml\')\n var className = \'test\'\n var el = html`<div class="before ${className} after"><div>`' // eslint-disable-line

0 commit comments

Comments
 (0)