Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow import from root of repo #42965

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions packages/kbn-babel-preset/common_preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,47 @@
* under the License.
*/

module.exports = {
presets: [require.resolve('@babel/preset-typescript'), require.resolve('@babel/preset-react')],
plugins: [
require.resolve('@kbn/elastic-idx/babel'),
require.resolve('babel-plugin-add-module-exports'),
const path = require('path');
const REPO_ROOT = path.resolve(__dirname, '..', '..');

// The class properties proposal was merged with the private fields proposal
// into the "class fields" proposal. Babel doesn't support this combined
// proposal yet, which includes private field, so this transform is
// TECHNICALLY stage 2, but for all intents and purposes it's stage 3
//
// See https://github.com/babel/proposals/issues/12 for progress
require.resolve('@babel/plugin-proposal-class-properties'),
],
overrides: [
{
// Babel 7 don't support the namespace feature on typescript code.
// With namespaces only used for type declarations, we can securely
// strip them off for babel on x-pack infra/siem plugins
exports.createConfig = (rootPath) => {
if (!rootPath) {
rootPath = REPO_ROOT;
}

//console.log('rootPath', rootPath);

return {
presets: [require.resolve('@babel/preset-typescript'), require.resolve('@babel/preset-react')],
plugins: [
require.resolve('@kbn/elastic-idx/babel'),
require.resolve('babel-plugin-add-module-exports'),

// The class properties proposal was merged with the private fields proposal
// into the "class fields" proposal. Babel doesn't support this combined
// proposal yet, which includes private field, so this transform is
// TECHNICALLY stage 2, but for all intents and purposes it's stage 3
//
// See https://github.com/babel/babel/issues/8244#issuecomment-466548733
test: [
/x-pack[\/\\]legacy[\/\\]plugins[\/\\]infra[\/\\].*[\/\\]graphql/,
/x-pack[\/\\]legacy[\/\\]plugins[\/\\]siem[\/\\].*[\/\\]graphql/,
],
plugins: [[require.resolve('babel-plugin-typescript-strip-namespaces')]],
},
],
// See https://github.com/babel/proposals/issues/12 for progress
require.resolve('@babel/plugin-proposal-class-properties'),
[require.resolve('babel-plugin-module-resolver'), {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
root: rootPath,
}],
],
overrides: [
{
// Babel 7 don't support the namespace feature on typescript code.
// With namespaces only used for type declarations, we can securely
// strip them off for babel on x-pack infra/siem plugins
//
// See https://github.com/babel/babel/issues/8244#issuecomment-466548733
test: [
/x-pack[\/\\]legacy[\/\\]plugins[\/\\]infra[\/\\].*[\/\\]graphql/,
/x-pack[\/\\]legacy[\/\\]plugins[\/\\]siem[\/\\].*[\/\\]graphql/,
],
plugins: [[require.resolve('babel-plugin-typescript-strip-namespaces')]],
},
],
};
};
6 changes: 4 additions & 2 deletions packages/kbn-babel-preset/node_preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* under the License.
*/

module.exports = () => {
module.exports = (_, options) => {
//console.log('node_preset', options);

return {
presets: [
[
Expand All @@ -40,7 +42,7 @@ module.exports = () => {
corejs: 2,
},
],
require('./common_preset'),
require('./common_preset').createConfig(options.rootPath),
],
plugins: [
[
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-babel-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@babel/preset-typescript": "7.3.3",
"@kbn/elastic-idx": "1.0.0",
"babel-plugin-add-module-exports": "1.0.2",
"babel-plugin-module-resolver": "3.2.0",
"babel-plugin-transform-define": "1.3.1",
"babel-plugin-typescript-strip-namespaces": "1.1.1"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-babel-preset/webpack_preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = () => {
corejs: 2,
},
],
require('./common_preset'),
require('./common_preset').createConfig(),
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* under the License.
*/

const { join, dirname, extname } = require('path');
const { join, dirname, extname, resolve } = require('path');
const { readdirSync } = require('fs');

const webpackResolver = require('eslint-import-resolver-webpack');
const nodeResolver = require('eslint-import-resolver-node');
Expand All @@ -32,6 +33,11 @@ const {
resolveWebpackAlias,
} = require('./lib');

const REPO_ROOT = resolve(__dirname, '..', '..');
const ROOT_DIRECTORIES = readdirSync(REPO_ROOT, { withFileTypes: true })
.filter(file => file.isDirectory())
.map(file => file.name);

// cache context, it shouldn't change
let context;
function initContext(file, config) {
Expand Down Expand Up @@ -69,6 +75,14 @@ function tryNodeResolver(importRequest, file, config) {
exports.resolve = function resolveKibanaPath(importRequest, file, config) {
config = config || {};

const rootImport = ROOT_DIRECTORIES.find(
dirName => importRequest === dirName || importRequest.startsWith(`${dirName}/`)
);
if (rootImport) {
const path = resolve(REPO_ROOT, importRequest);
return tryNodeResolver(path, file, config);
}

if (config.forceNode) {
return tryNodeResolver(importRequest, file, config);
}
Expand Down
8 changes: 7 additions & 1 deletion src/dev/build/tasks/transpile_babel_task.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import { createPromiseFromStreams } from '../../../legacy/utils';
const transpileWithBabel = async (srcGlobs, build, presets) => {
const buildRoot = build.resolvePath();

//console.log('buildRoot', buildRoot);

//console.log('transpileWithBabel presets', presets);

await createPromiseFromStreams([
vfs.src(
srcGlobs.concat([
Expand Down Expand Up @@ -52,6 +56,8 @@ export const TranspileBabelTask = {
description: 'Transpiling sources with babel',

async run(config, log, build) {
const rootPath = build.resolvePath();

// Transpile server code
await transpileWithBabel(
[
Expand All @@ -60,7 +66,7 @@ export const TranspileBabelTask = {
],
build,
[
require.resolve('@kbn/babel-preset/node_preset')
[require.resolve('@kbn/babel-preset/node_preset'), { rootPath }]
]
);

Expand Down
2 changes: 1 addition & 1 deletion src/legacy/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { constant, once, compact, flatten } from 'lodash';

import { isWorker } from 'cluster';
import { fromRoot, pkg } from '../utils';
import { Config } from './config';
import { Config } from 'src/legacy/server/config';
import loggingConfiguration from './logging/configuration';
import configSetupMixin from './config/setup';
import httpMixin from './http';
Expand Down
28 changes: 27 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6457,6 +6457,17 @@ [email protected]:
resolved "https://registry.yarnpkg.com/babel-plugin-mock-imports/-/babel-plugin-mock-imports-1.0.1.tgz#1476ed4de911347d344fc81caab4beced80804b1"
integrity sha512-Nu4unCGKeqOfLlfnLPnv/pEHancdAGTqFqyArZ27gsKIiKxeZvMr87IHB8BxhMu3Bfc8fA8bx7hWt32aZbEwpQ==

[email protected]:
version "3.2.0"
resolved "https://registry.yarnpkg.com/babel-plugin-module-resolver/-/babel-plugin-module-resolver-3.2.0.tgz#ddfa5e301e3b9aa12d852a9979f18b37881ff5a7"
integrity sha512-tjR0GvSndzPew/Iayf4uICWZqjBwnlMWjSx6brryfQ81F9rxBVqwDJtFCV8oOs0+vJeefK9TmdZtkIFdFe1UnA==
dependencies:
find-babel-config "^1.1.0"
glob "^7.1.2"
pkg-up "^2.0.0"
reselect "^3.0.1"
resolve "^1.4.0"

babel-plugin-named-asset-import@^0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/babel-plugin-named-asset-import/-/babel-plugin-named-asset-import-0.3.1.tgz#5ec13ec446d0a1e5bb6c57a1f94c9cdedb0c50d6"
Expand Down Expand Up @@ -12656,6 +12667,14 @@ finalhandler@~1.1.2:
statuses "~1.5.0"
unpipe "~1.0.0"

find-babel-config@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/find-babel-config/-/find-babel-config-1.2.0.tgz#a9b7b317eb5b9860cda9d54740a8c8337a2283a2"
integrity sha512-jB2CHJeqy6a820ssiqwrKMeyC6nNdmrcgkKWJWmpoxpE8RKciYJXCcXRq1h2AzCo5I5BJeN2tkGEO3hLTuePRA==
dependencies:
json5 "^0.5.1"
path-exists "^3.0.0"

find-cache-dir@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-2.0.0.tgz#4c1faed59f45184530fb9d7fa123a4d04a98472d"
Expand Down Expand Up @@ -17414,7 +17433,7 @@ [email protected], json3@^3.3.2:
resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1"
integrity sha1-PAQ0dD35Pi9cQq7nsZvLSDV19OE=

json5@^0.5.0:
json5@^0.5.0, json5@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
integrity sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=
Expand Down Expand Up @@ -24568,6 +24587,13 @@ resolve@^1.11.1:
dependencies:
path-parse "^1.0.6"

resolve@^1.4.0:
version "1.12.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.12.0.tgz#3fc644a35c84a48554609ff26ec52b66fa577df6"
integrity sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==
dependencies:
path-parse "^1.0.6"

resolve@^1.5.0, resolve@^1.7.1:
version "1.7.1"
resolved "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz#aadd656374fd298aee895bc026b8297418677fd3"
Expand Down