Commit 9005b484 by Steven Vachon Committed by GitHub

Replace ts-loader with Babel (#21587)

* Applied prettier to relevant webpack configs

* Replaced ng-annotate with babel-plugin-angularjs-annotate

… and replaced ts-loader with @preset/typescript

* Removed redundant import

… that is problematic with Babel's module resolver

* Updated lockfile

* Traspile debug package to es5 for PhantomJS support

* Update babel to latest version

* Remove @babel/poolyfill and url search params polyfill

* Add ts-loader to grafana-ui dependencies

* Update prod webpack build to use cor-js 3

* Applied prettier to relevant webpack configs

* Replaced ng-annotate with babel-plugin-angularjs-annotate

… and replaced ts-loader with @preset/typescript

* Updated lockfile

Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
parent a0ad8118
......@@ -9,11 +9,13 @@
"url": "http://github.com/grafana/grafana.git"
},
"devDependencies": {
"@babel/core": "7.6.4",
"@babel/plugin-syntax-dynamic-import": "7.2.0",
"@babel/preset-env": "7.6.3",
"@babel/preset-react": "7.6.3",
"@babel/preset-typescript": "7.6.0",
"@babel/core": "7.8.3",
"@babel/plugin-proposal-nullish-coalescing-operator": "7.8.3",
"@babel/plugin-proposal-optional-chaining": "7.8.3",
"@babel/plugin-syntax-dynamic-import": "7.8.3",
"@babel/preset-env": "7.8.3",
"@babel/preset-react": "7.8.3",
"@babel/preset-typescript": "7.8.3",
"@emotion/core": "10.0.10",
"@rtsao/plugin-proposal-class-properties": "7.0.1-patch.1",
"@testing-library/react-hooks": "^3.2.1",
......@@ -105,8 +107,6 @@
"mocha": "4.1.0",
"module-alias": "2.2.0",
"monaco-editor": "0.15.6",
"ng-annotate-loader": "0.6.1",
"ng-annotate-webpack-plugin": "0.3.0",
"ngtemplate-loader": "2.0.1",
"node-sass": "4.12.0",
"npm": "6.13.4",
......@@ -131,7 +131,6 @@
"style-loader": "0.23.1",
"terser-webpack-plugin": "1.2.3",
"ts-jest": "24.1.0",
"ts-loader": "6.2.1",
"ts-node": "8.5.0",
"tslib": "1.10.0",
"tslint": "5.20.1",
......@@ -199,7 +198,6 @@
"printWidth": 120
},
"dependencies": {
"@babel/polyfill": "7.6.0",
"@braintree/sanitize-url": "4.0.0",
"@grafana/slate-react": "0.22.9-grafana",
"@reduxjs/toolkit": "1.2.1",
......@@ -218,7 +216,7 @@
"calculate-size": "1.1.1",
"classnames": "2.2.6",
"clipboard": "2.0.4",
"core-js": "2.6.9",
"core-js": "3.6.4",
"d3": "5.15.0",
"d3-scale-chromatic": "1.5.0",
"emotion": "10.0.27",
......@@ -257,6 +255,7 @@
"redux": "4.0.4",
"redux-logger": "3.0.6",
"redux-thunk": "2.3.0",
"regenerator-runtime": "0.13.3",
"reselect": "4.0.0",
"rst2html": "github:thoward/rst2html#990cb89",
"rxjs": "6.5.4",
......@@ -267,7 +266,6 @@
"tether-drop": "https://github.com/torkelo/drop/tarball/master",
"tinycolor2": "1.4.1",
"tti-polyfill": "0.2.2",
"url-search-params-polyfill": "7.0.1",
"whatwg-fetch": "3.0.0",
"xss": "1.0.3"
},
......
......@@ -26,8 +26,8 @@
},
"main": "src/index.ts",
"dependencies": {
"@babel/core": "7.6.4",
"@babel/preset-env": "7.6.3",
"@babel/core": "7.8.3",
"@babel/preset-env": "7.8.3",
"@grafana/data": "6.7.0-pre",
"@grafana/ui": "6.7.0-pre",
"@types/command-exists": "^1.2.0",
......
......@@ -91,6 +91,7 @@
"rollup-plugin-terser": "4.0.4",
"rollup-plugin-typescript2": "0.19.3",
"rollup-plugin-visualizer": "0.9.2",
"ts-loader": "6.2.1",
"typescript": "3.7.2"
},
"types": "src/index.ts"
......
import '@babel/polyfill';
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import 'whatwg-fetch'; // fetch polyfill needed for PhantomJs rendering
import 'abortcontroller-polyfill/dist/polyfill-patch-fetch'; // fetch polyfill needed for PhantomJs rendering
import 'url-search-params-polyfill'; // fetch polyfill needed for PhantomJs rendering
// @ts-ignore
import ttiPolyfill from 'tti-polyfill';
import 'file-saver';
import 'lodash';
import 'jquery';
......@@ -19,8 +23,6 @@ import 'vendor/angular-other/angular-strap';
import $ from 'jquery';
import angular from 'angular';
import config from 'app/core/config';
// @ts-ignore
import ttiPolyfill from 'tti-polyfill';
// @ts-ignore ignoring this for now, otherwise we would have to extend _ interface with move
import _ from 'lodash';
import { AppEvents, setLocale, setMarkdownOptions } from '@grafana/data';
......
// const context = require.context('./', true, /_specs\.ts/);
// context.keys().forEach(context);
// module.exports = context;
import '@babel/polyfill';
import 'jquery';
import angular from 'angular';
import 'angular-mocks';
......
// https://github.com/TypeStrong/ts-loader/issues/653#issuecomment-390889335
const ModuleDependencyWarning = require("webpack/lib/ModuleDependencyWarning")
module.exports = class IgnoreNotFoundExportPlugin {
apply(compiler) {
const messageRegExp = /export '.*'( \(reexported as '.*'\))? was not found in/
function doneHook(stats) {
stats.compilation.warnings = stats.compilation.warnings.filter(function(warn) {
if (warn instanceof ModuleDependencyWarning && messageRegExp.test(warn.message)) {
return false
}
return true;
})
}
if (compiler.hooks) {
compiler.hooks.done.tap("IgnoreNotFoundExportPlugin", doneHook)
} else {
compiler.plugin("done", doneHook)
}
}
}
......@@ -2,7 +2,7 @@ const path = require('path');
// https://github.com/visionmedia/debug/issues/701#issuecomment-505487361
function shouldExclude(filename) {
const packagesToProcessbyBabel = ['debug', 'lru-cache', 'yallist', 'apache-arrow', 'react-hook-form'];
const packagesToProcessbyBabel = ['debug', 'lru-cache', 'yallist', 'apache-arrow', 'react-hook-form', 'rc-trigger'];
for (const package of packagesToProcessbyBabel) {
if (filename.indexOf(`node_modules/${package}`) > 0) {
return false;
......@@ -11,6 +11,7 @@ function shouldExclude(filename) {
return true;
}
console.log(path.resolve());
module.exports = {
target: 'web',
entry: {
......@@ -24,7 +25,11 @@ module.exports = {
},
resolve: {
extensions: ['.ts', '.tsx', '.es6', '.js', '.json', '.svg'],
alias: {},
alias: {
// rc-trigger uses babel-runtime which has internal dependency to core-js@2
// this alias maps that dependency to core-js@t3
'core-js/library/fn': 'core-js/stable',
},
modules: [path.resolve('public'), path.resolve('node_modules')],
},
stats: {
......
......@@ -27,73 +27,100 @@ module.exports = (env = {}) =>
},
module: {
rules: [{
// Note: order is bottom-to-top and/or right-to-left
rules: [
{
test: /\.tsx?$/,
enforce: 'pre',
exclude: /node_modules/,
use: {
loader: 'tslint-loader',
use: [
{
loader: 'babel-loader',
options: {
emitErrors: true,
typeCheck: false,
}
}
cacheDirectory: true,
babelrc: false,
// Note: order is top-to-bottom and/or left-to-right
plugins: [
[
require('@rtsao/plugin-proposal-class-properties'),
{
loose: true,
},
],
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
'angularjs-annotate',
],
// Note: order is bottom-to-top and/or right-to-left
presets: [
[
'@babel/preset-env',
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: {
loader: 'ts-loader',
targets: {
browsers: 'last 3 versions',
},
useBuiltIns: 'entry',
corejs: 3,
modules: false,
},
],
'@babel/preset-typescript',
'@babel/preset-react',
],
},
},
{
loader: 'tslint-loader',
options: {
transpileOnly: true
emitErrors: true,
typeCheck: false,
},
},
],
},
require('./sass.rule.js')({
sourceMap: false,
preserveUrl: false
preserveUrl: false,
}),
{
test: /\.(png|jpg|gif|ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
loader: 'file-loader'
loader: 'file-loader',
},
]
],
},
plugins: [
new CleanWebpackPlugin(),
env.noTsCheck ?
new webpack.DefinePlugin({}) // bogus plugin to satisfy webpack API
:
new ForkTsCheckerWebpackPlugin({
env.noTsCheck
? new webpack.DefinePlugin({}) // bogus plugin to satisfy webpack API
: new ForkTsCheckerWebpackPlugin({
checkSyntacticErrors: true,
}),
new MiniCssExtractPlugin({
filename: 'grafana.[name].[hash].css'
filename: 'grafana.[name].[hash].css',
}),
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, '../../public/views/error.html'),
template: path.resolve(__dirname, '../../public/views/error-template.html'),
inject: false,
chunksSortMode: 'none',
excludeChunks: ['dark', 'light']
excludeChunks: ['dark', 'light'],
}),
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, '../../public/views/index.html'),
template: path.resolve(__dirname, '../../public/views/index-template.html'),
inject: false,
chunksSortMode: 'none',
excludeChunks: ['dark', 'light']
excludeChunks: ['dark', 'light'],
}),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development')
}
NODE_ENV: JSON.stringify('development'),
},
}),
// new BundleAnalyzerPlugin({
// analyzerPort: 8889
// })
]
],
});
......@@ -7,7 +7,6 @@ const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const IgnoreNotFoundExportPlugin = require("./IgnoreNotFoundExportPlugin.js");
module.exports = merge(common, {
mode: 'development',
......@@ -36,50 +35,61 @@ module.exports = merge(common, {
'!/public/build': 'http://localhost:3000',
},
watchOptions: {
ignored: /node_modules/
}
ignored: /node_modules/,
},
},
optimization: {
removeAvailableModules: false,
runtimeChunk: false,
removeEmptyChunks: false,
splitChunks: false
splitChunks: false,
},
module: {
rules: [{
// Note: order is bottom-to-top and/or right-to-left
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [{
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
babelrc: false,
// Note: order is top-to-bottom and/or left-to-right
plugins: [
[require('@rtsao/plugin-proposal-class-properties'), {
loose: true
}],
'angularjs-annotate',
[
require('@rtsao/plugin-proposal-class-properties'),
{
loose: true,
},
],
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-syntax-dynamic-import', // needed for `() => import()` in routes.ts
'angularjs-annotate',
'react-hot-loader/babel',
],
// Note: order is bottom-to-top and/or right-to-left
presets: [
[
'@babel/preset-env',
{
targets: {
browsers: 'last 3 versions'
browsers: 'last 3 versions',
},
useBuiltIns: 'entry',
modules: false
modules: false,
},
],
'@babel/preset-typescript',
'@babel/preset-react',
],
},
}, ],
},
],
},
{
test: /\.scss$/,
......@@ -90,13 +100,13 @@ module.exports = merge(common, {
loader: 'postcss-loader',
options: {
config: {
path: __dirname + '/postcss.config.js'
path: __dirname + '/postcss.config.js',
},
},
},
{
loader: 'sass-loader'
}
loader: 'sass-loader',
},
],
},
{
......@@ -113,7 +123,7 @@ module.exports = merge(common, {
template: path.resolve(__dirname, '../../public/views/index-template.html'),
inject: 'body',
alwaysWriteToDisk: true,
chunksSortMode: 'none'
chunksSortMode: 'none',
}),
new HtmlWebpackHarddiskPlugin(),
new webpack.NamedModulesPlugin(),
......@@ -124,6 +134,5 @@ module.exports = merge(common, {
NODE_ENV: JSON.stringify('development'),
},
}),
new IgnoreNotFoundExportPlugin(),
],
});
......@@ -4,15 +4,14 @@ const merge = require('webpack-merge');
const TerserPlugin = require('terser-webpack-plugin');
const common = require('./webpack.common.js');
const path = require('path');
const ngAnnotatePlugin = require('ng-annotate-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = merge(common, {
mode: 'production',
devtool: "source-map",
devtool: 'source-map',
entry: {
dark: './public/sass/grafana.dark.scss',
......@@ -20,33 +19,62 @@ module.exports = merge(common, {
},
module: {
rules: [{
// Note: order is bottom-to-top and/or right-to-left
rules: [
{
test: /\.tsx?$/,
enforce: 'pre',
exclude: /node_modules/,
use: {
loader: 'tslint-loader',
use: [
{
loader: 'babel-loader',
options: {
emitErrors: true,
typeCheck: false,
}
}
cacheDirectory: true,
babelrc: false,
// Note: order is top-to-bottom and/or left-to-right
plugins: [
[
require('@rtsao/plugin-proposal-class-properties'),
{
loose: true,
},
],
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-syntax-dynamic-import', // needed for `() => import()` in routes.ts
'angularjs-annotate',
],
// Note: order is bottom-to-top and/or right-to-left
presets: [
[
'@babel/preset-env',
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: {
loader: 'ts-loader',
targets: {
browsers: 'last 3 versions',
},
useBuiltIns: 'entry',
corejs: 3,
modules: false,
},
],
'@babel/preset-typescript',
'@babel/preset-react',
],
},
},
{
loader: 'tslint-loader',
options: {
transpileOnly: true
emitErrors: true,
typeCheck: false,
},
},
],
},
require('./sass.rule.js')({
sourceMap: false,
preserveUrl: false
})
]
preserveUrl: false,
}),
],
},
optimization: {
nodeEnv: 'production',
......@@ -54,40 +82,39 @@ module.exports = merge(common, {
new TerserPlugin({
cache: false,
parallel: true,
sourceMap: true
sourceMap: true,
}),
new OptimizeCSSAssetsPlugin({})
]
new OptimizeCSSAssetsPlugin({}),
],
},
plugins: [
new ForkTsCheckerWebpackPlugin({
checkSyntacticErrors: true,
}),
new MiniCssExtractPlugin({
filename: "grafana.[name].[hash].css"
filename: 'grafana.[name].[hash].css',
}),
new ngAnnotatePlugin(),
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, '../../public/views/error.html'),
template: path.resolve(__dirname, '../../public/views/error-template.html'),
inject: false,
excludeChunks: ['dark', 'light'],
chunksSortMode: 'none'
chunksSortMode: 'none',
}),
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, '../../public/views/index.html'),
template: path.resolve(__dirname, '../../public/views/index-template.html'),
inject: false,
excludeChunks: ['manifest', 'dark', 'light'],
chunksSortMode: 'none'
chunksSortMode: 'none',
}),
function () {
this.hooks.done.tap('Done', function (stats) {
function() {
this.hooks.done.tap('Done', function(stats) {
if (stats.compilation.errors && stats.compilation.errors.length) {
console.log(stats.compilation.errors);
process.exit(1);
}
});
}
]
},
],
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment