-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
[interpreter/loadPlugins] avoid deleting globals added by plugins #27171
Changes from all commits
de27ccd
ce9fb02
621deb1
7e8cf2c
bb2fb33
681f322
e497403
5853717
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import '../common/register'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* eslint-disable */ | ||
import util from 'util'; | ||
console.log(util.format('hello world')); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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 { extname } = require('path'); | ||
|
||
const { transform } = require('babel-core'); | ||
|
||
exports.createServerCodeTransformer = (sourceMaps) => { | ||
return (content, path) => { | ||
switch (extname(path)) { | ||
case '.js': | ||
const { code = '' } = transform(content.toString('utf8'), { | ||
filename: path, | ||
ast: false, | ||
code: true, | ||
sourceMaps: sourceMaps ? 'inline' : false, | ||
babelrc: false, | ||
presets: [require.resolve('@kbn/babel-preset/webpack_preset')], | ||
}); | ||
|
||
return code; | ||
|
||
default: | ||
return content.toString('utf8'); | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { readFileSync } from 'fs'; | ||
import { resolve } from 'path'; | ||
import { createServerCodeTransformer } from './server_code_transformer'; | ||
|
||
const JS_FIXTURE_PATH = resolve(__dirname, '__fixtures__/sample.js'); | ||
const JS_FIXTURE = readFileSync(JS_FIXTURE_PATH); | ||
|
||
describe('js support', () => { | ||
it('transpiles js file', () => { | ||
const transformer = createServerCodeTransformer(); | ||
expect(transformer(JS_FIXTURE, JS_FIXTURE_PATH)).toMatchInlineSnapshot(` | ||
"'use strict'; | ||
|
||
var _util = require('util'); | ||
|
||
var _util2 = _interopRequireDefault(_util); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
console.log(_util2.default.format('hello world')); /* eslint-disable */" | ||
`); | ||
}); | ||
|
||
it('throws errors for js syntax errors', () => { | ||
const transformer = createServerCodeTransformer(); | ||
expect(() => transformer(Buffer.from(`export default 'foo`), JS_FIXTURE_PATH)).toThrowError( | ||
/Unterminated string constant/ | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ export function runMochaCli() { | |
|
||
// check that we aren't leaking any globals | ||
process.argv.push('--check-leaks'); | ||
// prevent globals injected from canvas plugins from triggering leak check | ||
process.argv.push('--globals', 'core,regeneratorRuntime,_'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kind of fragile, but at least future globals will break the CI. I do wonder, is there a way to remove that lodash global? |
||
|
||
// ensure that mocha requires the setup_node_env script | ||
process.argv.push('--require', require.resolve('../../setup_node_env')); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,9 +41,8 @@ var ignore = [ | |
// is `x-pack` and `b` is not `node_modules` | ||
/\/node_modules\/(?!x-pack\/(?!node_modules)([^\/]+))([^\/]+\/[^\/]+)/, | ||
|
||
// ignore paths matching `/canvas/canvas_plugin/{a}/{b}` unless | ||
// `a` is `functions` and `b` is `server` | ||
/\/canvas\/canvas_plugin\/(?!functions\/server)([^\/]+\/[^\/]+)/, | ||
// ignore paths matching `/canvas/canvas_plugin/` | ||
/\/canvas\/canvas_plugin\//, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By sharing the server_code_transformer with |
||
]; | ||
|
||
if (global.__BUILT_WITH_BABEL__) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,9 @@ | |
|
||
const path = require('path'); | ||
const CopyWebpackPlugin = require('copy-webpack-plugin'); | ||
const { | ||
createServerCodeTransformer, | ||
} = require('@kbn/interpreter/tasks/build/server_code_transformer'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interpreter seems like a weird place to import build configuration from. It's also weird that it's importing something that wouldn't be part of the published package. Do you think putting this in another package (currently existing or not) would make more sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We really don't want to have hundreds of packages in two years, so no to another package. I think this is fine, we don't publish these packages and if we want to we can figure something out then. |
||
|
||
const sourceDir = path.resolve(__dirname, '../../canvas_plugin_src'); | ||
const buildDir = path.resolve(__dirname, '../../canvas_plugin'); | ||
|
@@ -25,7 +28,6 @@ export function getWebpackConfig({ devtool, watch } = {}) { | |
'uis/datasources/all': path.join(sourceDir, 'uis/datasources/register.js'), | ||
'uis/arguments/all': path.join(sourceDir, 'uis/arguments/register.js'), | ||
'functions/browser/all': path.join(sourceDir, 'functions/browser/register.js'), | ||
'functions/common/all': path.join(sourceDir, 'functions/common/register.js'), | ||
'templates/all': path.join(sourceDir, 'templates/register.js'), | ||
'uis/tags/all': path.join(sourceDir, 'uis/tags/register.js'), | ||
}, | ||
|
@@ -74,8 +76,21 @@ export function getWebpackConfig({ devtool, watch } = {}) { | |
}, | ||
new CopyWebpackPlugin([ | ||
{ | ||
from: `${sourceDir}/functions/server/`, | ||
to: `${buildDir}/functions/server/`, | ||
from: path.resolve(sourceDir, 'functions/server'), | ||
to: path.resolve(buildDir, 'functions/server'), | ||
transform: createServerCodeTransformer(!!devtool), | ||
ignore: '**/__tests__/**', | ||
}, | ||
{ | ||
from: path.resolve(sourceDir, 'functions/common'), | ||
to: path.resolve(buildDir, 'functions/common'), | ||
transform: createServerCodeTransformer(!!devtool), | ||
ignore: '**/__tests__/**', | ||
}, | ||
{ | ||
from: path.resolve(sourceDir, 'lib'), | ||
to: path.resolve(buildDir, 'lib'), | ||
transform: createServerCodeTransformer(!!devtool), | ||
ignore: '**/__tests__/**', | ||
}, | ||
]), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some documentation to this logic, to make it clear why we're adding, registering and then deleting the global?
Are there any other globals we should consider deleting? Forgive the terminology, but perhaps a registry of globals to delete would help, even if it's a bit derivative...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We add the global for plugins to use. They all use
canvas.register
, so the global needs to exist when the code is run (ie. whenrequire(path)
is run).My understanding is that it's removed to avoid complaints from our test runners, which check for added globals in the node runtime and fail if any are unexpectedly added.