Webpack не транспиирует typescript в es5
Собираю приложение на js и ts с помощью webpack. И ts-loader выдаёт ошибку при сборке.
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
Файл typescript.ts
let b: Array<number>;
b = [];
b = [1, 2, 3, 4, 5];
for (const i of b) {
console.log(i);
}
webpack.config.js
module.exports = {
mode: 'production',
entry: ['./typescript.ts'],
output: {
filename: 'main.es5.js',
path: path.resolve(__dirname, 'public'),
},
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
targets: {
browsers: [
'> 1%',
'last 2 versions',
'Firefox ESR',
'ie 11',
],
},
},
],
],
},
},
}, {
test: /\.ts$/,
use: {
loader: 'ts-loader',
options: {
compilerOptions: {
transpileOnly: true,
noImplicitAny: true,
noEmitOnError: true,
module: 'es6',
target: 'es5',
allowJs: true,
checkJs: true,
},
},
},
include: '/',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
};
Ответы (1 шт):
Автор решения: Максим Бондарь
→ Ссылка
Решил проблему переносом параметров для TS в отдельный файл. В файле webpack.config.js указал:
{
test: /\.ts$/,
use: {
loader: 'ts-loader',
options: {
configFile: 'tsconfig.es5.json',
},
},
}
А в файле tsconfig.es5.json:
{
"compilerOptions": {
"noImplicitAny": true,
"noEmitOnError": true,
"module": "es6",
"target": "es5",
"allowJs": true,
"checkJs": true,
}
}
И всё заработало. Подозреваю, что без файла многие параметры по-умолчанию не подключаются.
