From e576853e268e673240dc9de29b3760e3adad0566 Mon Sep 17 00:00:00 2001 From: atanasster Date: Sun, 9 May 2021 14:08:31 +0300 Subject: [PATCH] fix: typescript config file --- .vscode/launch.json | 2 +- core/core/src/modules.ts | 42 +++++++++++--- core/core/test/dynamic-import.test.ts | 57 +++++++++++++++++++ core/core/test/fixtures/buildtime.js | 11 ++++ core/core/test/fixtures/buildtime.ts | 13 +++++ .../.config/{buildtime.js => buildtime.ts} | 3 +- 6 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 core/core/test/dynamic-import.test.ts create mode 100644 core/core/test/fixtures/buildtime.js create mode 100644 core/core/test/fixtures/buildtime.ts rename examples/gatsby/.config/{buildtime.js => buildtime.ts} (97%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 61b7cbadf..d2deb3975 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -98,7 +98,7 @@ "name": "jest core", "program": "${workspaceFolder}/node_modules/.bin/jest", "cwd": "${workspaceFolder}/core/core", - "args": ["deepmerge"], + "args": ["dynamic-import"], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "disableOptimisticBPs": true, diff --git a/core/core/src/modules.ts b/core/core/src/modules.ts index e6f8074c2..8d00517ed 100644 --- a/core/core/src/modules.ts +++ b/core/core/src/modules.ts @@ -1,7 +1,8 @@ import path from 'path'; import fs from 'fs'; import * as ts from 'typescript'; -import { dirSync } from 'tmp'; +import { createHash } from 'crypto'; +import { error } from '@component-controls/logger'; import { deepMerge } from './deepMerge'; /** @@ -17,7 +18,10 @@ const esmRequire = (filePath: string): any => { const result = require('esm')(module)(filePath); if ( !result || - (typeof result === 'object' && Object.keys(result).length === 0) + (typeof result === 'object' && Object.keys(result).length === 0) || + (result.default && + typeof result.default === 'object' && + Object.keys(result.default).length === 0) ) { return require(filePath); } @@ -47,17 +51,40 @@ export const dynamicRequire = (filePath: string): any => { config.config, ts.readConfigFile(configPath, ts.sys.readFile).config, ); + if (config.config.extends) { + const extendsPath = ts.findConfigFile( + path.dirname(path.resolve(configPath, config.config.extends)), + ts.sys.fileExists, + ); + if (extendsPath) { + config.config = deepMerge( + ts.readConfigFile(extendsPath, ts.sys.readFile).config, + config.config, + ); + } + } + } + const tmpFolder = path.resolve( + path.dirname(filePath), + createHash('md5') + .update(filePath) + .digest('hex'), + ); + if (!fs.existsSync(tmpFolder)) { + fs.mkdirSync(tmpFolder); } - const tmpDir = dirSync(); - config.config.compilerOptions.outDir = tmpDir.name; try { + config.config.compilerOptions.outDir = tmpFolder; + // moduleResolution option not working + delete config.config.compilerOptions['moduleResolution']; + delete config.config.compilerOptions['noEmit']; const program = ts.createProgram( [filePath], config.config.compilerOptions, ); const nakedFile = nakedFileName(filePath); // by default output file name same but with .js extension - let jsFilePath = path.resolve(tmpDir.name, nakedFile + '.js'); + let jsFilePath = path.resolve(tmpFolder, nakedFile + '.js'); program.emit(undefined, (fileName: string, data: string) => { if (nakedFileName(fileName) === nakedFile) { jsFilePath = fileName; @@ -68,10 +95,11 @@ export const dynamicRequire = (filePath: string): any => { const result = esmRequire(jsFilePath); return result; } catch (e) { - throw new Error(`error compiling file ${filePath}`); + error(filePath, e); + return esmRequire(filePath); } } finally { - fs.rmdirSync(tmpDir.name, { recursive: true }); + fs.rmdirSync(tmpFolder, { recursive: true }); // tmpDir.removeCallback(); } } diff --git a/core/core/test/dynamic-import.test.ts b/core/core/test/dynamic-import.test.ts new file mode 100644 index 000000000..bcacd1f5e --- /dev/null +++ b/core/core/test/dynamic-import.test.ts @@ -0,0 +1,57 @@ +import path from 'path'; +import { dynamicRequire } from '../src/modules'; +describe('dynamic-import', () => { + it('typescript', () => { + const config = dynamicRequire( + path.resolve(__dirname, './fixtures/buildtime.ts'), + ); + expect(config).toMatchObject({ + default: { + stories: ['../../stories/src/blogs/*.mdx'], + }, + }); + }); + it('typescript config', () => { + const config = dynamicRequire( + path.resolve(__dirname, '../../../examples/gatsby/.config/buildtime.ts'), + ); + expect(config).toMatchObject({ + default: { + search: { + indexingModule: + '/Users/atanasster/component-controls/search/algolia/indexing.js', + searchingModule: + '/Users/atanasster/component-controls/search/algolia/dist/index.js', + fields: ['title', 'description', 'source', 'tags', 'components'], + emptySearchDocuments: [ + 'Getting started/Site generators/Gatsby', + 'Getting started/Site generators/Nextjs', + 'Getting started/Site generators/Webpack', + 'Getting started/Documentation site', + 'Writing Documentation/ESM Stories', + 'Getting started/UI customization', + 'Writing Documentation/MDX Documentation', + 'Writing Documentation/MDX Stories', + ], + options: { + saveIndex: true, + indexName: undefined, + appID: undefined, + searchAPIKey: undefined, + adminAPIKey: undefined, + }, + }, + }, + }); + }); + it('esm', () => { + const config = dynamicRequire( + path.resolve(__dirname, './fixtures/buildtime.js'), + ); + expect(config).toMatchObject({ + default: { + stories: ['../../stories/src/blogs/*.mdx'], + }, + }); + }); +}); diff --git a/core/core/test/fixtures/buildtime.js b/core/core/test/fixtures/buildtime.js new file mode 100644 index 000000000..872146d58 --- /dev/null +++ b/core/core/test/fixtures/buildtime.js @@ -0,0 +1,11 @@ +const config = { + stories: ['../../stories/src/blogs/*.mdx'], + search: { + indexingModule: require.resolve( + '@component-controls/search-algolia/indexing', + ), + searchingModule: require.resolve('@component-controls/search-algolia'), + }, +}; + +export default config; diff --git a/core/core/test/fixtures/buildtime.ts b/core/core/test/fixtures/buildtime.ts new file mode 100644 index 000000000..eff2be4be --- /dev/null +++ b/core/core/test/fixtures/buildtime.ts @@ -0,0 +1,13 @@ +import { BuildConfiguration } from '@component-controls/core'; + +const config: BuildConfiguration = { + stories: ['../../stories/src/blogs/*.mdx'], + search: { + indexingModule: require.resolve( + '@component-controls/search-algolia/indexing', + ), + searchingModule: require.resolve('@component-controls/search-algolia'), + }, +}; + +export default config; diff --git a/examples/gatsby/.config/buildtime.js b/examples/gatsby/.config/buildtime.ts similarity index 97% rename from examples/gatsby/.config/buildtime.js rename to examples/gatsby/.config/buildtime.ts index a63e508d7..8df97ebc8 100644 --- a/examples/gatsby/.config/buildtime.js +++ b/examples/gatsby/.config/buildtime.ts @@ -1,9 +1,10 @@ import path from 'path'; +import { BuildConfiguration } from '@component-controls/core'; require('dotenv').config(); //const BundleAnalyzerPlugin = require('webpack-bundle-analyzer') // .BundleAnalyzerPlugin; -const config = { +const config: BuildConfiguration = { stories: [ '../../stories/src/blogs/*.mdx', '../../stories/src/showcase/*.mdx',