You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
3.2 KiB
82 lines
3.2 KiB
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getTypescriptLoader = exports.getBabelLoader = exports.LangType = void 0;
|
|
var LangType;
|
|
(function (LangType) {
|
|
LangType["ES6"] = "ES6";
|
|
LangType["Typescript"] = "Typescript";
|
|
})(LangType = exports.LangType || (exports.LangType = {}));
|
|
const replaceExt = (path, ext) => path.substr(0, path.lastIndexOf('.')) + `${ext}'`;
|
|
function updateEntryExt(self, newExt) {
|
|
const jsEntryOption = self.entryOption;
|
|
let tsEntryOption = {};
|
|
if (typeof jsEntryOption === 'string') {
|
|
tsEntryOption = replaceExt(jsEntryOption, newExt);
|
|
}
|
|
else if (typeof jsEntryOption === 'object') {
|
|
Object.keys(jsEntryOption).forEach((entry) => {
|
|
tsEntryOption[entry] = replaceExt(jsEntryOption[entry], newExt);
|
|
});
|
|
}
|
|
self.configuration.config.webpackOptions.entry = tsEntryOption;
|
|
}
|
|
const getFolder = (path) => path.replace("'./", '').split('/').slice(0, -1).join('/');
|
|
function getEntryFolders(self) {
|
|
const entryOption = self.entryOption;
|
|
const entryFolders = {};
|
|
if (typeof entryOption === 'string') {
|
|
const folder = getFolder(entryOption);
|
|
if (folder.length > 0) {
|
|
entryFolders[folder] = true;
|
|
}
|
|
}
|
|
else if (typeof entryOption === 'object') {
|
|
Object.keys(entryOption).forEach((entry) => {
|
|
const folder = getFolder(entryOption[entry]);
|
|
if (folder.length > 0) {
|
|
entryFolders[folder] = true;
|
|
}
|
|
});
|
|
}
|
|
return Object.keys(entryFolders);
|
|
}
|
|
/**
|
|
*
|
|
* Returns an module.rule object for the babel loader
|
|
* @param {string[]} includeFolders An array of folders to include
|
|
* @returns {Rule} A configuration containing the babel-loader with env preset
|
|
*/
|
|
function getBabelLoader(includeFolders) {
|
|
const include = includeFolders.map((folder) => `path.resolve(__dirname, '${folder}')`);
|
|
return { test: '/\\.(js|jsx)$/', include, loader: "'babel-loader'" };
|
|
}
|
|
exports.getBabelLoader = getBabelLoader;
|
|
/**
|
|
*
|
|
* Returns an module.rule object for the typescript loader
|
|
* @param {string[]} includeFolders An array of folders to include
|
|
* @returns {Rule} A configuration containing the ts-loader
|
|
*/
|
|
function getTypescriptLoader(includeFolders) {
|
|
const include = includeFolders.map((folder) => `path.resolve(__dirname, '${folder}')`);
|
|
return { test: '/\\.(ts|tsx)$/', loader: "'ts-loader'", include, exclude: ['/node_modules/'] };
|
|
}
|
|
exports.getTypescriptLoader = getTypescriptLoader;
|
|
function language(self, langType) {
|
|
const entryFolders = getEntryFolders(self);
|
|
switch (langType) {
|
|
case LangType.ES6:
|
|
self.dependencies.push('babel-loader', '@babel/core', '@babel/preset-env');
|
|
self.configuration.config.webpackOptions.module.rules.push(getBabelLoader(entryFolders));
|
|
break;
|
|
case LangType.Typescript:
|
|
self.dependencies.push('typescript', 'ts-loader');
|
|
self.configuration.config.webpackOptions.module.rules.push(getTypescriptLoader(entryFolders));
|
|
self.configuration.config.webpackOptions.resolve = {
|
|
extensions: ["'.tsx'", "'.ts'", "'.js'"],
|
|
};
|
|
updateEntryExt(self, '.ts');
|
|
break;
|
|
}
|
|
}
|
|
exports.default = language;
|
|
|