Skip to content
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

Custom tsconfig.json for angular apps. #2669

Merged
merged 9 commits into from
Jan 10, 2018
4 changes: 4 additions & 0 deletions app/angular/src/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import fs from 'fs';
import path from 'path';
import loadBabelConfig from './babel_config';
import loadTsConfig from './ts_config';

// avoid ESLint errors
const logger = console;
Expand All @@ -15,6 +16,9 @@ export default function(configType, baseConfig, configDir) {
const babelConfig = loadBabelConfig(configDir);
config.module.rules[0].query = babelConfig;

const tsOptions = loadTsConfig(configDir);
config.module.rules[1].loaders[0].options = tsOptions;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if it looks ugly, IMO need some work moving webpack configurations to the core.


// Check whether a config.js file exists inside the storybook
// config directory and throw an error if it's not.
const storybookConfigPath = path.resolve(configDir, 'config.js');
Expand Down
7 changes: 6 additions & 1 deletion app/angular/src/server/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ export default function(configDir) {
},
{
test: /\.ts?$/,
loaders: [require.resolve('ts-loader'), require.resolve('angular2-template-loader')],
loaders: [
{
loader: require.resolve('ts-loader'),
},
require.resolve('angular2-template-loader'),
],
},
{
test: /\.(html|css)$/,
Expand Down
7 changes: 6 additions & 1 deletion app/angular/src/server/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ export default function(configDir) {
},
{
test: /\.ts?$/,
loaders: [require.resolve('ts-loader'), require.resolve('angular2-template-loader')],
loaders: [
{
loader: require.resolve('ts-loader'),
},
require.resolve('angular2-template-loader'),
],
},
{
test: /\.(html|css)$/,
Expand Down
2 changes: 0 additions & 2 deletions app/angular/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ if (!hasCustomFavicon) {
app.use(favicon(path.resolve(__dirname, 'public/favicon.ico')));
}

// Build the webpack configuration using the `baseConfig`
// custom `.babelrc` file and `webpack.config.js` files
const configDir = program.configDir || './.storybook';

// The repository info is sent to the storybook while running on
Expand Down
27 changes: 27 additions & 0 deletions app/angular/src/server/ts_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fs from 'fs';
import path from 'path';

// avoid ESLint errors
const logger = console;

function resolveTsConfig(tsConfigPath) {
if (!fs.existsSync(tsConfigPath)) {
return null;
}

logger.info('=> Found custom tsconfig.json');

return tsConfigPath;
}

export default function(configDir) {
const configFile = resolveTsConfig(path.resolve(configDir, 'tsconfig.json'));

if (!configFile) {
return {};
}

return {
configFile,
};
}
32 changes: 32 additions & 0 deletions app/angular/src/server/ts_config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import loadTsConfig from './ts_config';

// eslint-disable-next-line global-require
jest.mock('fs', () => require('../../../../__mocks__/fs'));
jest.mock('path', () => ({
resolve: () => 'tsconfig.json',
}));

const setupFiles = files => {
// eslint-disable-next-line no-underscore-dangle, global-require
require('fs').__setMockFiles(files);
};

describe('ts_config', () => {
it('should return the config with the path to the tsconfig.json', () => {
setupFiles({ 'tsconfig.json': '{}' });

const config = loadTsConfig('.foo');

expect(config).toEqual({
configFile: 'tsconfig.json',
});
});

it('should return empty object when there is no tsconfig.json', () => {
setupFiles({});

const config = loadTsConfig('.foo');

expect(config).toEqual({});
});
});
10 changes: 10 additions & 0 deletions examples/angular-cli/.storybook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"exclude": [
"../src/test.ts",
"../src/**/*.spec.ts"
],
"include": [
"../src/**/*"
]
}