Skip to content

refactor: use hooks #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ module.exports = {
statements: 100
}
},
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/lib/$1',
'^~~$': '<rootDir>',
'^@@$': '<rootDir>',
'^@/(.*)$': '<rootDir>/lib/$1'
},
transform: {
'^.+\\.js$': 'babel-jest'
}
Expand Down
3 changes: 0 additions & 3 deletions packages/typescript-build/lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ function tsModule (_moduleOptions) {
_moduleOptions
)

// Allow TypeScript extension for severMiddlewares (`nuxt.resolver.resolvePath` uses `options.extensions`)
this.options.extensions.push('ts')

// Extend Builder to handle .ts/.tsx files as routes and watch them
this.options.build.additionalExtensions = ['ts', 'tsx']

Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-build/test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
modules: [
'../../../packages/typescript-build'
buildModules: [
'@nuxt/typescript-build'
]
}
3 changes: 0 additions & 3 deletions packages/typescript-build/test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ describe('module', () => {
test('with default options', async () => {
builder = await buildWithTsModule()

expect(builder.nuxt.options.extensions).toHaveLength(3)
expect(builder.nuxt.options.extensions).toEqual(['js', 'mjs', 'ts'])

expect(builder.nuxt.options.build.additionalExtensions).toHaveLength(2)
expect(builder.nuxt.options.build.additionalExtensions).toEqual(['ts', 'tsx'])

Expand Down
23 changes: 7 additions & 16 deletions packages/typescript-runtime/bin/nuxt-ts.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
#!/usr/bin/env node

const path = require('path')
const { resolveNuxtBin, getRootdirFromArgv, registerTSNode } = require('..')
const cli = (() => { try { return require('@nuxt/cli') } catch { return require('@nuxt/cli-edge') } })()

function main () {
const rootDir = getRootdirFromArgv()
const tsConfigPath = path.resolve(rootDir, 'tsconfig.json')
const { hooks } = require('..')

registerTSNode(tsConfigPath)

require(resolveNuxtBin())
}

try {
main()
} catch (error) {
require('consola').error(error)
process.exit(1)
}
cli.run(null, hooks)
.catch((error) => {
require('consola').fatal(error)
require('exit')(2)
})
37 changes: 0 additions & 37 deletions packages/typescript-runtime/lib/args.js

This file was deleted.

25 changes: 22 additions & 3 deletions packages/typescript-runtime/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
const path = require('path')
const { register } = require('ts-node')

const hooks = {
'run:before' ({ argv, rootDir }) {
const customPath = argv.find((_arg, index) => index > 0 && argv[index - 1] === '--tsconfig')
const tsConfigPath = path.resolve(customPath || rootDir, customPath && customPath.endsWith('.json') ? '' : 'tsconfig.json')

register({
project: tsConfigPath,
compilerOptions: {
module: 'commonjs'
}
})
},

config (config) {
config.extensions = [...(config.extensions || []), 'ts']
}
}

module.exports = {
...require('./resolve'),
...require('./args'),
registerTSNode: require('./register')
hooks
}
13 changes: 0 additions & 13 deletions packages/typescript-runtime/lib/register.js

This file was deleted.

31 changes: 0 additions & 31 deletions packages/typescript-runtime/lib/resolve.js

This file was deleted.

47 changes: 0 additions & 47 deletions packages/typescript-runtime/test/args.test.js

This file was deleted.

62 changes: 62 additions & 0 deletions packages/typescript-runtime/test/hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import path from 'path'
import { register } from 'ts-node'
import { hooks } from '..'

jest.mock('ts-node')

describe('run:before hook', () => {
beforeEach(() => {
register.mockClear()
})

test('registers ts-node', () => {
hooks['run:before']({ argv: [], rootDir: 'path' })

expect(register).toHaveBeenCalledWith({
project: path.resolve('path', 'tsconfig.json'),
compilerOptions: {
module: 'commonjs'
}
})
})

test('registers ts-node (custom tsconfig.json path)', () => {
hooks['run:before']({ argv: ['--tsconfig', 'custom/tsconfig.json'], rootDir: 'path' })

expect(register).toHaveBeenCalledWith({
project: path.resolve('custom/tsconfig.json'),
compilerOptions: {
module: 'commonjs'
}
})
})

test('registers ts-node (custom tsconfig.json dir path)', () => {
hooks['run:before']({ argv: ['--tsconfig', 'custom'], rootDir: 'path' })

expect(register).toHaveBeenCalledWith({
project: path.resolve('custom/tsconfig.json'),
compilerOptions: {
module: 'commonjs'
}
})
})
})

describe('config hook', () => {
test('adds ts extension (config.extensions is: defined)', () => {
const config = { extensions: [] }

hooks.config(config)

expect(config.extensions).toContain('ts')
})

test('adds ts extension (config.extensions is: undefined)', () => {
const config = {}

hooks.config(config)

expect(config.extensions).toContain('ts')
})
})
17 changes: 0 additions & 17 deletions packages/typescript-runtime/test/register.test.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/typescript-runtime/test/resolve.test.js

This file was deleted.

12 changes: 0 additions & 12 deletions test/setup.js

This file was deleted.