Skip to content

Commit

Permalink
feat: filemeata, tsc loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Sep 10, 2019
1 parent b9d3e61 commit 5752bda
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/loaders/meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable node/no-deprecated-api */

let originJsLoader

function loader (module, filename) {
if (originJsLoader) originJsLoader(...arguments)
if (module.exports) {
if (module.exports.default) {
module.exports.default.__meta = { filename }
} else {
module.exports.__meta = { filename }
}
}
}

function apply (ext) {
originJsLoader = require.extensions[ext]
require.extensions[ext] = loader
}

function restore (ext) {
require.extensions[ext] = originJsLoader
}

module.exports = { apply, restore }
14 changes: 14 additions & 0 deletions src/loaders/tsc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable node/no-deprecated-api */

const originJsLoader = require.extensions['.ts']
const tsNode = require('ts-node')

function apply () {
tsNode.register()
}

function restore () {
require.extensions['.ts'] = originJsLoader
}

module.exports = { apply, restore }
1 change: 1 addition & 0 deletions test/stub/null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = null
1 change: 1 addition & 0 deletions test/stub/obj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { foo: 'FOO' }
3 changes: 3 additions & 0 deletions test/stub/obj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class Foo {
public static foo: 'FOO'
}
25 changes: 25 additions & 0 deletions test/unit/meta-loader.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { apply, restore } = require('../../src/loaders/meta.js')

describe('meta loader', function () {
const objfile = require.resolve('../stub/obj.js')
const nullfile = require.resolve('../stub/null.js')

beforeEach(() => {
apply('.js')
delete require.cache[objfile]
delete require.cache[nullfile]
})

afterEach(() => restore('.js'))

it('should add filename metadata', function () {
expect(require('../stub/obj.js')).toEqual({
foo: 'FOO',
__meta: { filename: objfile }
})
})

it('should not throw if exports null', function () {
expect(require('../stub/null.js')).toEqual(null)
})
})
23 changes: 23 additions & 0 deletions test/unit/mixed-loaders.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const tsc = require('../../src/loaders/tsc.js')
const meta = require('../../src/loaders/meta.js')

describe('mixed loaders', function () {
const filename = require.resolve('../stub/obj.ts')

beforeEach(() => {
delete require.cache[filename]
tsc.apply()
meta.apply('.ts')
})

afterEach(() => {
meta.restore('.ts')
tsc.restore()
})

it('should add meta for ts files', function () {
const clazz = require('../stub/obj.ts').default

expect(clazz.__meta.filename).toEqual(filename)
})
})
20 changes: 20 additions & 0 deletions test/unit/tsc-loader.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { apply, restore } = require('../../src/loaders/tsc.js')

describe('tsc loader', function () {
const filename = require.resolve('../stub/obj.ts')

beforeEach(() => {
apply()
delete require.cache[filename]
})

afterEach(restore)

it('should load simple class', function () {
const fn = require('../stub/obj.ts')

expect(fn.default.toString()).toEqual(
'function Foo() {\n }'
)
})
})

0 comments on commit 5752bda

Please sign in to comment.