1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.setupRules = exports.normalizeIncludePath = exports.checkReact = exports.checkTypescript = exports.unneededBabelPlugins = exports.minimumSupportedBrowsers = void 0;
|
7 | const globby_1 = __importDefault(require("globby"));
|
8 | const path_1 = require("path");
|
9 | const babel_remove_function_1 = require("./babel-remove-function");
|
10 | const environment_1 = require("./environment");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | exports.minimumSupportedBrowsers = {
|
20 | chrome: '80',
|
21 | edge: '80',
|
22 | firefox: '75',
|
23 | ios: '12',
|
24 | opera: '67',
|
25 | safari: '12',
|
26 | samsung: '10.1'
|
27 | };
|
28 | exports.unneededBabelPlugins = [
|
29 | '@babel/plugin-transform-regenerator',
|
30 | '@babel/transform-template-literals',
|
31 | '@babel/plugin-transform-function-name',
|
32 | '@babel/proposal-async-generator-functions',
|
33 | '@babel/proposal-object-rest-spread'
|
34 | ];
|
35 | async function checkTypescript(rulesOptions, srcFolder) {
|
36 | if (typeof rulesOptions.typescript === 'boolean') {
|
37 | return rulesOptions.typescript;
|
38 | }
|
39 | return (await globby_1.default(path_1.resolve(srcFolder, './**/*.ts'))).length > 0;
|
40 | }
|
41 | exports.checkTypescript = checkTypescript;
|
42 | async function checkReact(rulesOptions, srcFolder) {
|
43 | if (typeof rulesOptions.react === 'boolean') {
|
44 | return rulesOptions.react;
|
45 | }
|
46 | return (await globby_1.default(path_1.resolve(srcFolder, './**/*.(jsx|tsx)'))).length > 0;
|
47 | }
|
48 | exports.checkReact = checkReact;
|
49 | function normalizeIncludePath(path) {
|
50 | const components = path.split(path_1.sep);
|
51 | if (components[0] === 'src') {
|
52 | components.shift();
|
53 | }
|
54 | else if (components[0] === 'node_modules') {
|
55 | components.splice(0, components[1][0] === '@' ? 3 : 2);
|
56 | }
|
57 | return components.join(path_1.sep);
|
58 | }
|
59 | exports.normalizeIncludePath = normalizeIncludePath;
|
60 | async function setupRules(options) {
|
61 | var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
62 | const rulesOptions = (_a = options.rules) !== null && _a !== void 0 ? _a : {};
|
63 | const babelOptions = (_b = options.babel) !== null && _b !== void 0 ? _b : {};
|
64 | const useBabel = (_c = rulesOptions.babel) !== null && _c !== void 0 ? _c : true;
|
65 | const useTypescript = await checkTypescript(rulesOptions, options.srcFolder);
|
66 | const useReact = await checkReact(rulesOptions, options.srcFolder);
|
67 | const babelPresets = [
|
68 | [
|
69 | '@babel/preset-env',
|
70 | {
|
71 | targets: (_d = babelOptions.browsersWhiteList) !== null && _d !== void 0 ? _d : exports.minimumSupportedBrowsers,
|
72 | exclude: (_e = babelOptions.exclude) !== null && _e !== void 0 ? _e : exports.unneededBabelPlugins,
|
73 | modules: (_f = babelOptions.modules) !== null && _f !== void 0 ? _f : false,
|
74 | debug: (_g = babelOptions.envDebug) !== null && _g !== void 0 ? _g : false
|
75 | }
|
76 | ]
|
77 | ];
|
78 | const babelPlugins = [
|
79 | ['@babel/plugin-proposal-class-properties', { loose: false }],
|
80 | '@babel/plugin-proposal-optional-catch-binding'
|
81 | ];
|
82 | if (options.environment === 'production') {
|
83 | const removeFunctions = (_h = babelOptions.removeFunctions) !== null && _h !== void 0 ? _h : ['debugClassName'];
|
84 | if (removeFunctions.length) {
|
85 | for (const name of removeFunctions) {
|
86 | babelPlugins.unshift(babel_remove_function_1.babelRemoveFunction({ name }));
|
87 | }
|
88 | }
|
89 | }
|
90 | const babelConfiguration = (_j = babelOptions.configuration) !== null && _j !== void 0 ? _j : {};
|
91 | let rules = [];
|
92 | if (useBabel) {
|
93 | rules.push({
|
94 | test: /\.js$/,
|
95 | exclude: /node_modules/,
|
96 | use: {
|
97 | loader: 'babel-loader',
|
98 | options: { presets: babelPresets, plugins: babelPlugins, ...babelConfiguration }
|
99 | }
|
100 | });
|
101 | }
|
102 | if (useTypescript) {
|
103 | rules.push({
|
104 | test: /\.ts$/,
|
105 | exclude: /node_modules/,
|
106 | use: {
|
107 | loader: 'babel-loader',
|
108 | options: { presets: babelPresets.concat('@babel/typescript'), plugins: babelPlugins, ...babelConfiguration }
|
109 | }
|
110 | });
|
111 | }
|
112 | if (useReact) {
|
113 | rules.push({
|
114 | test: /\.jsx$/,
|
115 | exclude: /node_modules/,
|
116 | use: {
|
117 | loader: 'babel-loader',
|
118 | options: { presets: babelPresets.concat('@babel/react'), plugins: babelPlugins, ...babelConfiguration }
|
119 | }
|
120 | });
|
121 | if (useTypescript) {
|
122 | rules.push({
|
123 | test: /\.tsx$/,
|
124 | exclude: /node_modules/,
|
125 | use: {
|
126 | loader: 'babel-loader',
|
127 | options: {
|
128 | presets: babelPresets.concat('@babel/react', '@babel/typescript'),
|
129 | plugins: babelPlugins,
|
130 | ...babelConfiguration
|
131 | }
|
132 | }
|
133 | });
|
134 | }
|
135 | }
|
136 | if ((_k = rulesOptions.images) !== null && _k !== void 0 ? _k : true) {
|
137 | rules.push({
|
138 | test: /\.(?:bmp|png|jpg|jpeg|gif|svg|webp)$/,
|
139 | use: [
|
140 | {
|
141 | loader: 'file-loader',
|
142 | options: {
|
143 | name: '[path][name]-[hash].[ext]',
|
144 | outputPath: normalizeIncludePath,
|
145 | publicPath: normalizeIncludePath
|
146 | }
|
147 | }
|
148 | ]
|
149 | });
|
150 | }
|
151 | if (rulesOptions.additional) {
|
152 | rules = rules.concat(rulesOptions.additional);
|
153 | }
|
154 | return environment_1.runHook(rules, rulesOptions.afterHook);
|
155 | }
|
156 | exports.setupRules = setupRules;
|