Skip to content

Commit

Permalink
Add basic TS support
Browse files Browse the repository at this point in the history
  • Loading branch information
helloitsjoe committed Dec 5, 2020
1 parent ff7a307 commit f80bf85
Show file tree
Hide file tree
Showing 5 changed files with 3,105 additions and 5,202 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
coverage
.DS_Store
yarn-error.log
40 changes: 35 additions & 5 deletions __tests__/index.test.js
Expand Up @@ -8,6 +8,7 @@ const {
defaultCSSUse,
defaultCSSLoaderOptions,
makeJS,
makeTS,
makeCSS,
} = require('../index');

Expand All @@ -17,7 +18,7 @@ const DEFAULT_EXCLUDE_LENGTH = 2;
const customUse = [{ loader: 'other-loader', options: { foo: 'bar' } }];

describe('makeWebpackConfig', () => {
const config = makeWebpackConfig();
const defaultConfig = makeWebpackConfig();
const newRules = [{ test: /\.js$/, use: [{ loader: 'other-loader' }] }];

const originalWarn = console.warn;
Expand All @@ -31,9 +32,20 @@ describe('makeWebpackConfig', () => {
});

it('Rules include js and css by default', () => {
expect(config.module.rules.length).toBe(DEFAULT_RULES_LENGTH);
expect(config.module.rules[0]).toEqual(makeJS());
expect(config.module.rules[1]).toEqual(makeCSS());
expect(defaultConfig.module.rules.length).toBe(DEFAULT_RULES_LENGTH);
expect(defaultConfig.module.rules[0]).toEqual(makeJS());
expect(defaultConfig.module.rules[1]).toEqual(makeCSS());
});

it('Passing ts: true adds default typescript', () => {
const config = makeWebpackConfig({ ts: true });
expect(config.module.rules.find(rule => rule.test.test('.tsx'))).toEqual(makeTS());
});

it('Passing ts object adds custom typescript config', () => {
const ts = { test: /\.tsx?$/, foo: 'bar' };
const config = makeWebpackConfig({ ts });
expect(config.module.rules.find(rule => rule.test.test('.tsx'))).toEqual(ts);
});

it('User can add to `rules` array', () => {
Expand Down Expand Up @@ -72,7 +84,7 @@ describe('makeWebpackConfig', () => {
});

it('default config', () => {
expect(config).toEqual({
expect(defaultConfig).toEqual({
mode: 'development',
target: 'web',
module: {
Expand Down Expand Up @@ -308,3 +320,21 @@ describe('makeCSS', () => {
expect(() => makeCSS(options)).toThrow('SASS loader options provided but no SASS loader found');
});
});

describe('makeTS', () => {
it('basic TS config', () => {
expect(makeTS()).toEqual({
test: /\.tsx?$/,
exclude: [/node_modules/],
use: [{ loader: 'ts-loader' }],
});
});

it('user can override exclude and use', () => {
expect(makeTS({ exclude: [/other-dir/], use: [{ foo: 'bar' }] })).toEqual({
test: /\.tsx?$/,
exclude: [/other-dir/],
use: [{ foo: 'bar' }],
});
});
});
18 changes: 13 additions & 5 deletions index.js
Expand Up @@ -49,11 +49,13 @@ const makeJS = ({
};
};

// const makeTS = () => ({
// test: /\.tsx?$/,
// exclude: [/node_modules/],
// use: [{ loader: 'ts-loader' }],
// });
const defaultTSExclude = [/node_modules/];
const defaultTSUse = [{ loader: 'ts-loader' }];
const makeTS = ({ exclude = defaultTSExclude, use = defaultTSUse } = {}) => ({
test: /\.tsx?$/,
exclude,
use,
});

const defaultCSSLoaderOptions = { modules: true };
const defaultCSSUse = [
Expand Down Expand Up @@ -121,6 +123,7 @@ const defaultWebpackRules = [makeJS(), makeCSS()];
const makeWebpackConfig = (options = {}) => {
const {
js,
ts,
css,
node,
serve,
Expand Down Expand Up @@ -159,6 +162,10 @@ const makeWebpackConfig = (options = {}) => {
customRules = rules.map(rule => (rule.test.test('.css') ? css : rule));
}

if (ts) {
customRules = [...rules, ts === true ? makeTS() : ts];
}

return {
...options,
mode,
Expand All @@ -172,6 +179,7 @@ const makeWebpackConfig = (options = {}) => {
module.exports = {
makeWebpackConfig,
makeJS,
makeTS,
makeCSS,
defaultWebpackRules,
defaultBabelPlugins,
Expand Down

0 comments on commit f80bf85

Please sign in to comment.