Skip to content

Commit

Permalink
cherry pick, part one
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Dec 13, 2019
1 parent 6c1f248 commit daf9fcd
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/kbn-babel-preset/common_preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

const plugins = [
require.resolve('babel-plugin-add-module-exports'),
require.resolve('./transforms/rewrite_absolute_imports'),

// The class properties proposal was merged with the private fields proposal
// into the "class fields" proposal. Babel doesn't support this combined
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-babel-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"babel-plugin-filter-imports": "^3.0.0",
"babel-plugin-styled-components": "^1.10.6",
"babel-plugin-transform-define": "^1.3.1",
"babel-plugin-typescript-strip-namespaces": "^1.1.1"
"babel-plugin-typescript-strip-namespaces": "^1.1.1",
"babel-types": "^6.26.0"
}
}
77 changes: 77 additions & 0 deletions packages/kbn-babel-preset/transforms/rewrite_absolute_imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const Path = require('path');
const { readFileSync } = require('fs');
const t = require('babel-types');

// we have to determine the root of the Kibana repo to properly resolve imports
// and since this package runs from source, and from the node_modules directory
// we are breaking the rules and embedding that info here.
const KIBANA_PACKAGE_JSON = __dirname.includes('node_modules')
? Path.resolve(__dirname, '../../../../package.json')
: Path.resolve(__dirname, '../../../package.json');

try {
const pkg = JSON.parse(readFileSync(KIBANA_PACKAGE_JSON, 'utf8'));
if (pkg.name !== 'kibana') {
throw true;
}
} catch (error) {
throw new Error(`
Unable to determine absolute path to Kibana repo, @kbn/babel-preset expects to either run
from the packages/ directory in source, or from the root of the node_modules directory
in the distributable.
`);
}

const REPO_ROOT = Path.dirname(KIBANA_PACKAGE_JSON);

module.exports = () => ({
name: '@kbn/babel-preset/transform/rewrite_absolute_imports',
visitor: {
ImportDeclaration(path) {
const source = path.get('source');
const importPath = t.isStringLiteral(source.node) && source.node.value;

if (!importPath || !(importPath.startsWith('src/') || importPath.startsWith('x-pack/'))) {
return;
}

const { root, sourceFileName, filename } = path.hub.file.opts;

let absPath;
if (sourceFileName && Path.isAbsolute(sourceFileName)) {
absPath = sourceFileName;
} else if (filename && Path.isAbsolute(filename)) {
absPath = filename;
} else if (!absPath && root) {
absPath = Path.resolve(root, sourceFileName || filename);
} else {
throw new Error(`
Unable to determine absolute path of file, either sourceFileName or filename opt
must be defined for the file and be absolute or combined with the root file opt.
`);
}

const targetPath = Path.resolve(REPO_ROOT, source.node.value);
source.replaceWith(t.stringLiteral(Path.relative(Path.dirname(absPath), targetPath)));
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ exports.getWebpackConfig = function(kibanaPath, projectRoot, config) {
const fromKibana = (...path) => resolve(kibanaPath, ...path);

const alias = {
src: fromKibana('src'),
'x-pack': fromKibana('x-pack'),

// Kibana defaults https://github.com/elastic/kibana/blob/6998f074542e8c7b32955db159d15661aca253d7/src/legacy/ui/ui_bundler_env.js#L30-L36
ui: fromKibana('src/legacy/ui/public'),
test_harness: fromKibana('src/test_harness/public'),
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-eslint-plugin-eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"babel-eslint": "^10.0.3"
},
"dependencies": {
"@kbn/dev-utils": "1.0.0",
"micromatch": "3.1.10",
"dedent": "^0.7.0",
"eslint-module-utils": "2.4.1"
Expand Down
2 changes: 2 additions & 0 deletions src/legacy/ui/ui_exports/ui_export_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const UI_EXPORT_DEFAULTS = {
],

webpackAliases: {
src: resolve(ROOT, 'src'),
'x-pack': resolve(ROOT, 'x-pack'),
ui: resolve(ROOT, 'src/legacy/ui/public'),
'__kibanaCore__$': resolve(ROOT, 'src/core/public'),
test_harness: resolve(ROOT, 'src/test_harness/public'),
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"baseUrl": ".",
"paths": {
// Allows for importing from `kibana` package for the exported types.
"src/*": ["src/*"],
"x-pack/*": ["x-pack/*"],
"kibana": ["./kibana"],
"kibana/public": ["src/core/public"],
"kibana/server": ["src/core/server"],
Expand Down

0 comments on commit daf9fcd

Please sign in to comment.