"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cli_core_1 = require("@bluebase/cli-core");
const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
const webpack_merge_1 = __importDefault(require("webpack-merge"));
const useOwn_1 = __importDefault(require("../../scripts/useOwn"));
// tslint:disable-next-line:no-var-requires
const HappyPack = require('happypack');
const removeNil = cli_core_1.Utils.removeNil;
/**
 * Javascript loader
 * @param config
 * @param builder
 */
const LoaderTypescript = () => (config, builder) => {
    const newConfig = webpack_merge_1.default(config, {
        plugins: removeNil([
            // HappyPack 'typescript' instance.
            new HappyPack({
                id: 'happypack-typescript',
                verbose: false,
                // tslint:disable-next-line:object-literal-sort-keys
                threads: 4,
                loaders: [
                    {
                        loader: useOwn_1.default('babel-loader'),
                        options: {
                            babelrc: false,
                            plugins: [useOwn_1.default('react-hot-loader/babel')],
                        },
                    },
                    {
                        loader: useOwn_1.default('ts-loader'),
                        options: {
                            transpileOnly: true,
                            // IMPORTANT! use happyPackMode mode to speed-up compilation and reduce errors reported to webpack
                            happyPackMode: true,
                        }
                    }
                ],
            }),
            // Typescript
            new fork_ts_checker_webpack_plugin_1.default({ checkSyntacticErrors: true }),
        ]),
    });
    newConfig.module.rules[0].oneOf = [
        // Typescript
        {
            test: /\.tsx?$/,
            exclude: /node_modules/,
            include: removeNil([
                ...builder.configs.includePaths,
            ]),
            loader: useOwn_1.default('happypack/loader?id=happypack-typescript')
        },
    ].concat(newConfig.module.rules[0].oneOf);
    return newConfig;
};
exports.default = LoaderTypescript;
//# sourceMappingURL=LoaderTypescript.js.map