diff --git a/jest.config.js b/jest.config.js index 6a0461ed..cd36e86d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -13,12 +13,6 @@ module.exports = { statements: 100 } }, - moduleNameMapper: { - '^~/(.*)$': '/lib/$1', - '^~~$': '', - '^@@$': '', - '^@/(.*)$': '/lib/$1' - }, transform: { '^.+\\.js$': 'babel-jest' } diff --git a/packages/typescript-build/lib/module.js b/packages/typescript-build/lib/module.js index b135e897..3de6d48e 100644 --- a/packages/typescript-build/lib/module.js +++ b/packages/typescript-build/lib/module.js @@ -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'] diff --git a/packages/typescript-build/test/fixture/nuxt.config.js b/packages/typescript-build/test/fixture/nuxt.config.js index 2588c57f..8c0436d3 100644 --- a/packages/typescript-build/test/fixture/nuxt.config.js +++ b/packages/typescript-build/test/fixture/nuxt.config.js @@ -1,5 +1,5 @@ export default { - modules: [ - '../../../packages/typescript-build' + buildModules: [ + '@nuxt/typescript-build' ] } diff --git a/packages/typescript-build/test/module.test.js b/packages/typescript-build/test/module.test.js index 4dd3f0f7..61e68a37 100644 --- a/packages/typescript-build/test/module.test.js +++ b/packages/typescript-build/test/module.test.js @@ -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']) diff --git a/packages/typescript-runtime/bin/nuxt-ts.js b/packages/typescript-runtime/bin/nuxt-ts.js index ee6f26ea..713b50e7 100755 --- a/packages/typescript-runtime/bin/nuxt-ts.js +++ b/packages/typescript-runtime/bin/nuxt-ts.js @@ -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) + }) diff --git a/packages/typescript-runtime/lib/args.js b/packages/typescript-runtime/lib/args.js deleted file mode 100644 index 40643f6c..00000000 --- a/packages/typescript-runtime/lib/args.js +++ /dev/null @@ -1,37 +0,0 @@ -const path = require('path') -const { tryResolve } = require('./resolve') - -function getCliOptions () { - const cli = ['@nuxt/cli', '@nuxt/cli-edge'].find(cli => tryResolve(cli)) - return Object.entries(require(cli).options) - .reduce((options, [_key, value]) => { - return { ...options, ...value } - }, {}) -} - -function getCliOptionsWithValue () { - return Object.entries(getCliOptions()) - .filter(([_name, { type }]) => type !== 'boolean') - .reduce((optionsWithValue, [name, { alias }]) => { - return [...optionsWithValue, `--${name}`, ...alias ? [`-${alias}`] : []] - }, []) -} - -function getRootdirFromArgv () { - const args = process.argv.slice(2) - const optionsWithValue = getCliOptionsWithValue() - - const isCliOption = (previousArg, currentArg) => { - return ['dev', 'build', 'start', 'generate'].includes(currentArg) || - currentArg[0] === '-' || - (previousArg && previousArg[0] === '-' && optionsWithValue.includes(previousArg)) - } - - const rootDir = args.find((arg, i) => !isCliOption(args[i - 1], arg)) || '.' - - return path.resolve(process.cwd(), rootDir) -} - -module.exports = { - getRootdirFromArgv -} diff --git a/packages/typescript-runtime/lib/index.js b/packages/typescript-runtime/lib/index.js index 5351ee34..b59fc55b 100644 --- a/packages/typescript-runtime/lib/index.js +++ b/packages/typescript-runtime/lib/index.js @@ -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 } diff --git a/packages/typescript-runtime/lib/register.js b/packages/typescript-runtime/lib/register.js deleted file mode 100644 index 55c432d9..00000000 --- a/packages/typescript-runtime/lib/register.js +++ /dev/null @@ -1,13 +0,0 @@ -const { register } = require('ts-node') - -function registerTSNode (tsconfigPath) { - // https://github.com/TypeStrong/ts-node - register({ - project: tsconfigPath, - compilerOptions: { - module: 'commonjs' - } - }) -} - -module.exports = registerTSNode diff --git a/packages/typescript-runtime/lib/resolve.js b/packages/typescript-runtime/lib/resolve.js deleted file mode 100644 index a1c8c67b..00000000 --- a/packages/typescript-runtime/lib/resolve.js +++ /dev/null @@ -1,31 +0,0 @@ -const path = require('path') - -function tryResolve (path) { - try { - return require.resolve(path) - } catch (err) { - return null - } -} - -function resolveNuxtFile (filePath, packages = ['nuxt', 'nuxt-start']) { - packages.push(...packages.map(pkg => pkg + '-edge')) - - for (const pkg of packages) { - const resolvedFile = tryResolve(path.join(pkg, filePath)) - - if (resolvedFile) { - return resolvedFile - } - } -} - -function resolveNuxtBin () { - return resolveNuxtFile('bin/nuxt.js') -} - -module.exports = { - resolveNuxtFile, - resolveNuxtBin, - tryResolve -} diff --git a/packages/typescript-runtime/test/args.test.js b/packages/typescript-runtime/test/args.test.js deleted file mode 100644 index 03b54125..00000000 --- a/packages/typescript-runtime/test/args.test.js +++ /dev/null @@ -1,47 +0,0 @@ -import { resolve } from 'path' -import { getRootdirFromArgv } from '..' - -jest.mock('@nuxt/cli-edge', () => ({ - options: { - ...jest.requireActual('@nuxt/cli-edge').options, - custom: { - withoutAlias: { - type: 'string' - } - } - } -})) - -const argv = process.argv - -describe('getRootdirFromArgv', () => { - let setupArgs - - beforeEach(() => { - setupArgs = (commandLine) => { process.argv = [...argv, ...commandLine.split(' ')] } - }) - - test('nuxt-ts', () => { - expect(getRootdirFromArgv()).toEqual(process.cwd()) - }) - test('nuxt-ts project', () => { - setupArgs('project') - expect(getRootdirFromArgv()).toEqual(resolve(process.cwd(), 'project')) - }) - test('nuxt-ts dev project', () => { - setupArgs('dev project') - expect(getRootdirFromArgv()).toEqual(resolve(process.cwd(), 'project')) - }) - test('nuxt-ts dev -p 3001 project', () => { - setupArgs('dev -p 30001 project') - expect(getRootdirFromArgv()).toEqual(resolve(process.cwd(), 'project')) - }) - test('nuxt-ts dev -p 3001 --analyze project', () => { - setupArgs('dev -p 30001 --analyze project') - expect(getRootdirFromArgv()).toEqual(resolve(process.cwd(), 'project')) - }) - test('nuxt-ts dev --withoutAlias test project', () => { - setupArgs('dev --withoutAlias test project') - expect(getRootdirFromArgv()).toEqual(resolve(process.cwd(), 'project')) - }) -}) diff --git a/packages/typescript-runtime/test/hooks.test.js b/packages/typescript-runtime/test/hooks.test.js new file mode 100644 index 00000000..c56c86e6 --- /dev/null +++ b/packages/typescript-runtime/test/hooks.test.js @@ -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') + }) +}) diff --git a/packages/typescript-runtime/test/register.test.js b/packages/typescript-runtime/test/register.test.js deleted file mode 100644 index fc6850d5..00000000 --- a/packages/typescript-runtime/test/register.test.js +++ /dev/null @@ -1,17 +0,0 @@ -import { register } from 'ts-node' -import { registerTSNode } from '..' - -jest.mock('ts-node') - -test('registerTSNode', () => { - const tsConfigPath = 'path/tsconfig.json' - - registerTSNode(tsConfigPath) - - expect(register).toHaveBeenCalledWith({ - project: tsConfigPath, - compilerOptions: { - module: 'commonjs' - } - }) -}) diff --git a/packages/typescript-runtime/test/resolve.test.js b/packages/typescript-runtime/test/resolve.test.js deleted file mode 100644 index 3b6f0fd1..00000000 --- a/packages/typescript-runtime/test/resolve.test.js +++ /dev/null @@ -1,5 +0,0 @@ -import { resolveNuxtBin } from '..' - -test('resolveNuxtBin', () => { - expect(resolveNuxtBin()).toEqual(require.resolve('nuxt-edge/bin/nuxt.js')) -}) diff --git a/test/setup.js b/test/setup.js deleted file mode 100644 index b532703e..00000000 --- a/test/setup.js +++ /dev/null @@ -1,12 +0,0 @@ -import consola from 'consola' -import exit from 'exit' - -consola.mockTypes(() => jest.fn()) - -function errorTrap (error) { - process.stderr.write('\n' + error.stack + '\n') - exit(1) -} - -process.on('unhandledRejection', errorTrap) -process.on('uncaughtException', errorTrap)