@jakesidsmith/tsb
Version:
Dead simple TypeScript bundler, watcher, dev server, transpiler, and polyfiller
202 lines • 9.02 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWebpackConfig = void 0;
const path = __importStar(require("path"));
const webpack_1 = require("webpack");
const tsconfig_paths_webpack_plugin_1 = require("tsconfig-paths-webpack-plugin");
const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
const constants_1 = require("./constants");
const tsconfig_1 = require("./tsconfig");
const config_1 = require("./config");
const html_webpack_plugin_1 = __importDefault(require("html-webpack-plugin"));
const html_webpack_harddisk_plugin_1 = __importDefault(require("html-webpack-harddisk-plugin"));
const rimraf_1 = __importDefault(require("rimraf"));
const logger = __importStar(require("./logger"));
const createWebpackConfig = (configPath = constants_1.CONFIG_FILE_NAME, mode, command) => {
var _a, _b;
const fullConfigPath = path.resolve(process.cwd(), configPath);
const fullConfigDir = path.dirname(fullConfigPath);
const {
// Required
main, outDir,
// Base options
clearOutDirBefore = [], mainOutSubDir, mainBundleName = 'bundle', tsconfigPath = path.resolve(process.cwd(), 'tsconfig.json'), indexHTMLPath, indexHTMLEnv = {}, outputIndexHTMLFor = ['build', 'watch'], insertScriptTag = 'body', scriptLoading = 'blocking', reactHotLoading = false, hashFilesFor = ['build', 'watch'], additionalFilesToParse = [], env,
// Dev server options
hotLoading = true, host = '0.0.0.0', port = 8080, publicDir, publicPath, singlePageApp = true, headers, https, extendBabelPresets, extendBabelPlugins, extendWebpackPlugins, extendWebpackModuleRules, } = (0, config_1.getTsbConfig)(fullConfigPath);
const fullTsconfigPath = (0, tsconfig_1.resolveTsconfigPath)(fullConfigDir, tsconfigPath);
const fullOutDir = path.resolve(fullConfigDir, outDir);
const bundleOutSubDirRelative = path.relative(fullOutDir, mainOutSubDir ? path.resolve(fullOutDir, mainOutSubDir) : fullOutDir);
const tsconfig = (0, tsconfig_1.getTsconfig)(fullTsconfigPath);
const isReactAppDev = reactHotLoading && mode === 'development';
const additionalEntries = isReactAppDev
? [require.resolve('react-hot-loader/patch')]
: [];
const babelPlugins = isReactAppDev
? [require.resolve('react-hot-loader/babel')]
: [];
const babelPresets = [
[
require.resolve('@babel/preset-env'),
{
modules: false,
useBuiltIns: 'usage',
corejs: {
version: 3,
proposals: true,
},
},
],
];
const alias = isReactAppDev
? { ['react-dom']: '@hot-loader/react-dom' }
: {};
const shouldOutputHTML = outputIndexHTMLFor.includes(command);
const htmlPlugins = outputIndexHTMLFor.includes(command) || command === 'serve'
? [
new html_webpack_plugin_1.default(indexHTMLPath
? {
template: path.resolve(fullConfigDir, indexHTMLPath),
filename: path.resolve(fullOutDir, 'index.html'),
alwaysWriteToDisk: shouldOutputHTML,
inject: insertScriptTag,
scriptLoading,
templateParameters: indexHTMLEnv,
}
: {
alwaysWriteToDisk: shouldOutputHTML,
inject: insertScriptTag,
scriptLoading,
meta: {
viewport: 'width=device-width, initial-scale=1',
},
templateParameters: indexHTMLEnv,
}),
new html_webpack_harddisk_plugin_1.default(),
]
: [];
if (clearOutDirBefore.includes(command)) {
logger.log(`Clearing out dir...`);
rimraf_1.default.sync(fullOutDir);
logger.log(`Cleared ${fullOutDir}`);
}
const tsconfigInclude = ((_a = tsconfig.include) !== null && _a !== void 0 ? _a : []).map((comp) => comp.replace(constants_1.MATCHES_GLOB, ''));
const additionalInclude = additionalFilesToParse.map((comp) => path.resolve(fullConfigDir, comp));
const webpackModuleRules = [
{
test: constants_1.MATCHES_EXTENSION,
include: [...tsconfigInclude, ...additionalInclude],
use: [
{
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
presets: extendBabelPresets
? extendBabelPresets(babelPresets, mode, command)
: babelPresets,
plugins: extendBabelPlugins
? extendBabelPlugins(babelPlugins, mode, command)
: babelPlugins,
},
},
{
loader: require.resolve('ts-loader'),
options: {
transpileOnly: true,
configFile: fullTsconfigPath,
},
},
],
},
];
const webpackPlugins = [
new webpack_1.EnvironmentPlugin(Object.assign({ NODE_ENV: mode === 'production' ? mode : 'development' }, env)),
new fork_ts_checker_webpack_plugin_1.default({
typescript: {
configFile: fullTsconfigPath,
},
}),
...htmlPlugins,
];
return {
base: {
mode,
devtool: ((_b = tsconfig.compilerOptions) === null || _b === void 0 ? void 0 : _b.sourceMap) ? 'source-map' : undefined,
stats: 'errors-only',
entry: {
[mainBundleName]: [
...additionalEntries,
path.resolve(fullConfigDir, main),
],
},
output: {
path: fullOutDir,
filename: `${bundleOutSubDirRelative ? `${bundleOutSubDirRelative}/` : ''}[name]${hashFilesFor.includes(command) ? '.[contenthash]' : ''}.js`,
publicPath: singlePageApp ? '/' : '',
},
module: {
rules: extendWebpackModuleRules
? extendWebpackModuleRules(webpackModuleRules, mode, command)
: webpackModuleRules,
},
resolve: {
extensions: constants_1.EXTENSIONS,
plugins: [
new tsconfig_paths_webpack_plugin_1.TsconfigPathsPlugin({
configFile: fullTsconfigPath,
extensions: constants_1.EXTENSIONS,
}),
],
alias,
},
plugins: extendWebpackPlugins
? extendWebpackPlugins(webpackPlugins, mode, command)
: webpackPlugins,
},
devServer: Object.assign(Object.assign({ hot: hotLoading, inline: true, https,
host,
port }, (singlePageApp
? { historyApiFallback: true, serveIndex: true }
: null)), { headers, contentBase: typeof publicDir === 'string'
? path.resolve(fullConfigDir, publicDir)
: undefined, contentBasePublicPath: publicPath, stats: {
colors: true,
assets: false,
children: false,
chunks: false,
chunkModules: false,
entrypoints: false,
hash: false,
modules: false,
timings: false,
version: false,
} }),
};
};
exports.createWebpackConfig = createWebpackConfig;
//# sourceMappingURL=webpack-config.js.map