Skip to content

Commit

Permalink
fix: typescript config file
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 9, 2021
1 parent 451e2bd commit e576853
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 35 additions & 7 deletions core/core/src/modules.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}
Expand Down
57 changes: 57 additions & 0 deletions core/core/test/dynamic-import.test.ts
Original file line number Diff line number Diff line change
@@ -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'],
},
});
});
});
11 changes: 11 additions & 0 deletions core/core/test/fixtures/buildtime.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions core/core/test/fixtures/buildtime.ts
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit e576853

Please sign in to comment.