From 8fefbdf0e6810f2f7139a209a7b9d98f63cf5439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Jastrz=C4=99bski?= Date: Thu, 9 Apr 2020 20:28:02 +0200 Subject: [PATCH 1/7] support file extensions --- src/config/rollup.config.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/config/rollup.config.js b/src/config/rollup.config.js index c293de6e..ec21e66d 100644 --- a/src/config/rollup.config.js +++ b/src/config/rollup.config.js @@ -46,7 +46,7 @@ const deps = Object.keys(pkg.dependencies || {}) const peerDeps = Object.keys(pkg.peerDependencies || {}) const defaultExternal = umd ? peerDeps : deps.concat(peerDeps) -const input = glob.sync(fromRoot(process.env.BUILD_INPUT || 'src/index.js')) +const input = glob.sync(fromRoot(process.env.BUILD_INPUT || 'src/index.{js,ts,tsx}')) const codeSplitting = input.length > 1 if ( @@ -136,6 +136,8 @@ const replacements = Object.entries( return acc }, {}) +const extensions = ['.js', '.ts', '.tsx']; + module.exports = { input: codeSplitting ? input : input[0], output, @@ -146,6 +148,7 @@ module.exports = { nodeResolve({ preferBuiltins: isNode, mainFields: ['module', 'main', 'jsnext', 'browser'], + extensions, }), commonjs({include: 'node_modules/**'}), json(), @@ -153,6 +156,7 @@ module.exports = { presets: babelPresets, babelrc: !useBuiltinConfig, runtimeHelpers: useBuiltinConfig, + extensions, }), replace(replacements), useSizeSnapshot ? sizeSnapshot({printInfo: false}) : null, From 72b29c5b6a8d90819c49239f6b97ca236d71876e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Jastrz=C4=99bski?= Date: Thu, 9 Apr 2020 20:34:55 +0200 Subject: [PATCH 2/7] include preset-typescript and fix runtime helpers --- src/config/babelrc.js | 1 + src/config/rollup.config.js | 9 ++++++--- src/utils.js | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/config/babelrc.js b/src/config/babelrc.js index c2220294..d80178a4 100644 --- a/src/config/babelrc.js +++ b/src/config/babelrc.js @@ -53,6 +53,7 @@ module.exports = () => ({ ], ), ifAnyDep(['flow-bin'], [require.resolve('@babel/preset-flow')]), + ifAnyDep(['typescript'], [require.resolve('@babel/preset-typescript')]), ].filter(Boolean), plugins: [ [ diff --git a/src/config/rollup.config.js b/src/config/rollup.config.js index ec21e66d..c621825e 100644 --- a/src/config/rollup.config.js +++ b/src/config/rollup.config.js @@ -15,6 +15,7 @@ const { pkg, hasFile, hasPkgProp, + hasAnyDep, parseEnv, fromRoot, uniq, @@ -46,7 +47,9 @@ const deps = Object.keys(pkg.dependencies || {}) const peerDeps = Object.keys(pkg.peerDependencies || {}) const defaultExternal = umd ? peerDeps : deps.concat(peerDeps) -const input = glob.sync(fromRoot(process.env.BUILD_INPUT || 'src/index.{js,ts,tsx}')) +const input = glob.sync( + fromRoot(process.env.BUILD_INPUT || 'src/index.{js,ts,tsx}'), +) const codeSplitting = input.length > 1 if ( @@ -136,7 +139,7 @@ const replacements = Object.entries( return acc }, {}) -const extensions = ['.js', '.ts', '.tsx']; +const extensions = ['.js', '.ts', '.tsx'] module.exports = { input: codeSplitting ? input : input[0], @@ -155,7 +158,7 @@ module.exports = { rollupBabel({ presets: babelPresets, babelrc: !useBuiltinConfig, - runtimeHelpers: useBuiltinConfig, + runtimeHelpers: hasAnyDep('@babel/runtime'), extensions, }), replace(replacements), diff --git a/src/utils.js b/src/utils.js index 0fcdd46b..9d3eba99 100644 --- a/src/utils.js +++ b/src/utils.js @@ -168,6 +168,7 @@ module.exports = { hasLocalConfig, hasPkgProp, hasScript, + hasAnyDep, ifAnyDep, ifDep, ifDevDep, From 1dd815d09b1d26f365d375e5277492164d5ff233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Jastrz=C4=99bski?= Date: Thu, 9 Apr 2020 21:03:28 +0200 Subject: [PATCH 3/7] add missing dep and update docs --- README.md | 12 ++++++++++++ package.json | 1 + 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index aac4678b..2c5e52a3 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ for linting, testing, building, and more. - [Usage](#usage) - [Overriding Config](#overriding-config) - [Flow support](#flow-support) + - [TypeScript Support](#typescript-support) - [Inspiration](#inspiration) - [Other Solutions](#other-solutions) - [Issues](#issues) @@ -122,6 +123,17 @@ automatically get loaded when you use the default babel config that comes with `kcd-scripts`. If you customised your `.babelrc`-file you might need to manually add `@babel/preset-flow` to the `presets`-section. +### TypeScript Support + +If the `typescript` is a dependency on the project the +`@babel/preset-typescript` will automatically get loaded when you use the +default babel config that comes with `kcd-scripts`. If you customised your +`.babelrc`-file you might need to manually add `@babel/preset-typescript` to the +`presets`-section. + +`kcd-scripts` will automatically load any `.ts` and `.tsx` files, including the +default entry point, so you don't have to worry about rollup configuration. + ## Inspiration This is inspired by `react-scripts`. diff --git a/package.json b/package.json index d0f54a98..a2d38067 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@babel/preset-env": "^7.9.0", "@babel/preset-flow": "^7.9.0", "@babel/preset-react": "^7.9.1", + "@babel/preset-typescript": "^7.9.0", "@babel/runtime": "^7.9.2", "@rollup/plugin-commonjs": "^11.0.2", "@rollup/plugin-json": "^4.0.2", From 9b13324c66802b46567b7b71279031b07380f325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Jastrz=C4=99bski?= Date: Thu, 9 Apr 2020 21:51:35 +0200 Subject: [PATCH 4/7] detect ts based on tsconfig instead --- README.md | 4 ++-- src/config/babelrc.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2c5e52a3..519fd86a 100644 --- a/README.md +++ b/README.md @@ -125,14 +125,14 @@ add `@babel/preset-flow` to the `presets`-section. ### TypeScript Support -If the `typescript` is a dependency on the project the +If the `tsconfig.json`-file is present in the project root directory the `@babel/preset-typescript` will automatically get loaded when you use the default babel config that comes with `kcd-scripts`. If you customised your `.babelrc`-file you might need to manually add `@babel/preset-typescript` to the `presets`-section. `kcd-scripts` will automatically load any `.ts` and `.tsx` files, including the -default entry point, so you don't have to worry about rollup configuration. +default entry point, so you don't have to worry about any rollup configuration. ## Inspiration diff --git a/src/config/babelrc.js b/src/config/babelrc.js index d80178a4..79a080eb 100644 --- a/src/config/babelrc.js +++ b/src/config/babelrc.js @@ -1,7 +1,7 @@ const browserslist = require('browserslist') const semver = require('semver') -const {ifAnyDep, parseEnv, appDirectory, pkg} = require('../utils') +const {ifAnyDep, parseEnv, appDirectory, pkg, ifFile} = require('../utils') const {BABEL_ENV, NODE_ENV, BUILD_FORMAT} = process.env const isTest = (BABEL_ENV || NODE_ENV) === 'test' @@ -53,7 +53,7 @@ module.exports = () => ({ ], ), ifAnyDep(['flow-bin'], [require.resolve('@babel/preset-flow')]), - ifAnyDep(['typescript'], [require.resolve('@babel/preset-typescript')]), + ifFile(['tsconfig.json'], [require.resolve('@babel/preset-typescript')]), ].filter(Boolean), plugins: [ [ From c0eff257c0e353e04ae5c92aeb644c051508300c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Jastrz=C4=99bski?= Date: Thu, 9 Apr 2020 22:20:25 +0200 Subject: [PATCH 5/7] check for both --- README.md | 10 +++++----- src/config/babelrc.js | 10 ++++++++-- src/config/rollup.config.js | 12 ++++++++++-- src/utils.js | 5 +++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 519fd86a..8ca808e5 100644 --- a/README.md +++ b/README.md @@ -125,11 +125,11 @@ add `@babel/preset-flow` to the `presets`-section. ### TypeScript Support -If the `tsconfig.json`-file is present in the project root directory the -`@babel/preset-typescript` will automatically get loaded when you use the -default babel config that comes with `kcd-scripts`. If you customised your -`.babelrc`-file you might need to manually add `@babel/preset-typescript` to the -`presets`-section. +If the `tsconfig.json`-file is present in the project root directory and the +`typescript` is a dependency the `@babel/preset-typescript` will automatically +get loaded when you use the default babel config that comes with `kcd-scripts`. +If you customised your `.babelrc`-file you might need to manually add +`@babel/preset-typescript` to the `presets`-section. `kcd-scripts` will automatically load any `.ts` and `.tsx` files, including the default entry point, so you don't have to worry about any rollup configuration. diff --git a/src/config/babelrc.js b/src/config/babelrc.js index 79a080eb..a6cdc5a8 100644 --- a/src/config/babelrc.js +++ b/src/config/babelrc.js @@ -1,7 +1,13 @@ const browserslist = require('browserslist') const semver = require('semver') -const {ifAnyDep, parseEnv, appDirectory, pkg, ifFile} = require('../utils') +const { + ifAnyDep, + ifTypescript, + parseEnv, + appDirectory, + pkg, +} = require('../utils') const {BABEL_ENV, NODE_ENV, BUILD_FORMAT} = process.env const isTest = (BABEL_ENV || NODE_ENV) === 'test' @@ -53,7 +59,7 @@ module.exports = () => ({ ], ), ifAnyDep(['flow-bin'], [require.resolve('@babel/preset-flow')]), - ifFile(['tsconfig.json'], [require.resolve('@babel/preset-typescript')]), + ifTypescript([require.resolve('@babel/preset-typescript')]), ].filter(Boolean), plugins: [ [ diff --git a/src/config/rollup.config.js b/src/config/rollup.config.js index c621825e..b25d276e 100644 --- a/src/config/rollup.config.js +++ b/src/config/rollup.config.js @@ -16,6 +16,7 @@ const { hasFile, hasPkgProp, hasAnyDep, + hasTypescript, parseEnv, fromRoot, uniq, @@ -48,7 +49,10 @@ const peerDeps = Object.keys(pkg.peerDependencies || {}) const defaultExternal = umd ? peerDeps : deps.concat(peerDeps) const input = glob.sync( - fromRoot(process.env.BUILD_INPUT || 'src/index.{js,ts,tsx}'), + fromRoot( + process.env.BUILD_INPUT || + (hasTypescript ? 'src/index.{js,ts,tsx}' : 'src/index.js'), + ), ) const codeSplitting = input.length > 1 @@ -139,7 +143,11 @@ const replacements = Object.entries( return acc }, {}) -const extensions = ['.js', '.ts', '.tsx'] +// TODO: reuse `defaults` from `node-resolve` plugin when this issue is resolved https://github.com/rollup/plugins/issues/299 +const defaultExtensions = ['.mjs', '.js', '.json', '.node'] +const extensions = hasTypescript + ? defaultExtensions.concat(['.ts', '.tsx']) + : defaultExtensions module.exports = { input: codeSplitting ? input : input[0], diff --git a/src/utils.js b/src/utils.js index 9d3eba99..c778191a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -72,6 +72,9 @@ const ifDevDep = ifPkgSubProp('devDependencies') const ifAnyDep = (deps, t, f) => (hasAnyDep(arrify(deps)) ? t : f) const ifScript = ifPkgSubProp('scripts') +const hasTypescript = hasAnyDep('typescript') && hasFile('tsconfig.json') +const ifTypescript = (t, f) => (hasTypescript ? t : f) + function parseEnv(name, def) { if (envIsSet(name)) { try { @@ -175,6 +178,8 @@ module.exports = { ifFile, ifPeerDep, ifScript, + hasTypescript, + ifTypescript, parseEnv, pkg, resolveBin, From a87896c0690a594275ad263d97f535f44f429a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Jastrz=C4=99bski?= Date: Fri, 10 Apr 2020 16:49:36 +0200 Subject: [PATCH 6/7] hasAnyDep -> hasDep --- src/config/rollup.config.js | 4 ++-- src/utils.js | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/config/rollup.config.js b/src/config/rollup.config.js index b25d276e..7a5424ca 100644 --- a/src/config/rollup.config.js +++ b/src/config/rollup.config.js @@ -15,7 +15,7 @@ const { pkg, hasFile, hasPkgProp, - hasAnyDep, + hasDep, hasTypescript, parseEnv, fromRoot, @@ -166,7 +166,7 @@ module.exports = { rollupBabel({ presets: babelPresets, babelrc: !useBuiltinConfig, - runtimeHelpers: hasAnyDep('@babel/runtime'), + runtimeHelpers: hasDep('@babel/runtime'), extensions, }), replace(replacements), diff --git a/src/utils.js b/src/utils.js index c778191a..8ecfe924 100644 --- a/src/utils.js +++ b/src/utils.js @@ -171,6 +171,7 @@ module.exports = { hasLocalConfig, hasPkgProp, hasScript, + hasDep, hasAnyDep, ifAnyDep, ifDep, From 81601215da6bd490c6ce1b8bf020aecf664468c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Jastrz=C4=99bski?= Date: Fri, 10 Apr 2020 16:50:47 +0200 Subject: [PATCH 7/7] don't export if not needed --- src/utils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 8ecfe924..46c7b232 100644 --- a/src/utils.js +++ b/src/utils.js @@ -172,7 +172,6 @@ module.exports = { hasPkgProp, hasScript, hasDep, - hasAnyDep, ifAnyDep, ifDep, ifDevDep,