-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
112 additions
and
0 deletions.
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
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 } |
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,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 } |
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 @@ | ||
module.exports = null |
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 @@ | ||
module.exports = { foo: 'FOO' } |
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 @@ | ||
export default class Foo { | ||
public static foo: 'FOO' | ||
} |
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,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) | ||
}) | ||
}) |
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,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) | ||
}) | ||
}) |
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,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 }' | ||
) | ||
}) | ||
}) |