Fix event action in automation editor (#2686)

* Fix event action in automation editor

* Fix webpack resolve

* Update ha-automation-editor.js
This commit is contained in:
Paulus Schoutsen 2019-02-06 11:13:00 -08:00 committed by GitHub
parent f00de454d1
commit 504e4987b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 101 additions and 76 deletions

View File

@ -3,7 +3,7 @@ module.exports.babelLoaderConfig = ({ latestBuild }) => {
throw Error("latestBuild not defined for babel loader config"); throw Error("latestBuild not defined for babel loader config");
} }
return { return {
test: /\.m?js$|\.ts$/, test: /\.m?js$|\.tsx?$/,
use: { use: {
loader: "babel-loader", loader: "babel-loader",
options: { options: {
@ -12,7 +12,12 @@ module.exports.babelLoaderConfig = ({ latestBuild }) => {
require("@babel/preset-env").default, require("@babel/preset-env").default,
{ modules: false }, { modules: false },
], ],
require("@babel/preset-typescript").default, [
require("@babel/preset-typescript").default,
{
jsxPragma: "h",
},
],
].filter(Boolean), ].filter(Boolean),
plugins: [ plugins: [
// Part of ES2018. Converts {...a, b: 2} to Object.assign({}, a, {b: 2}) // Part of ES2018. Converts {...a, b: 2} to Object.assign({}, a, {b: 2})

View File

@ -2,6 +2,18 @@ const webpack = require("webpack");
const path = require("path"); const path = require("path");
const BabelMinifyPlugin = require("babel-minify-webpack-plugin"); const BabelMinifyPlugin = require("babel-minify-webpack-plugin");
module.exports.resolve = {
extensions: [".ts", ".js", ".json", ".tsx"],
alias: {
react: "preact-compat",
"react-dom": "preact-compat",
// Not necessary unless you consume a module using `createClass`
"create-react-class": "preact-compat/lib/create-react-class",
// Not necessary unless you consume a module requiring `react-dom-factories`
"react-dom-factories": "preact-compat/lib/react-dom-factories",
},
};
module.exports.plugins = [ module.exports.plugins = [
// Ignore moment.js locales // Ignore moment.js locales
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
@ -17,30 +29,32 @@ module.exports.plugins = [
), ),
]; ];
module.exports.minimizer = [ module.exports.optimization = {
// Took options from Polymer build tool minimizer: [
// https://github.com/Polymer/tools/blob/master/packages/build/src/js-transform.ts // Took options from Polymer build tool
new BabelMinifyPlugin( // https://github.com/Polymer/tools/blob/master/packages/build/src/js-transform.ts
{ new BabelMinifyPlugin(
// Disable the minify-constant-folding plugin because it has a bug relating {
// to invalid substitution of constant values into export specifiers: // Disable the minify-constant-folding plugin because it has a bug relating
// https://github.com/babel/minify/issues/820 // to invalid substitution of constant values into export specifiers:
evaluate: false, // https://github.com/babel/minify/issues/820
evaluate: false,
// TODO(aomarks) Find out why we disabled this plugin. // TODO(aomarks) Find out why we disabled this plugin.
simplifyComparisons: false, simplifyComparisons: false,
// Prevent removal of things that babel thinks are unreachable, but sometimes // Prevent removal of things that babel thinks are unreachable, but sometimes
// gets wrong: https://github.com/Polymer/tools/issues/724 // gets wrong: https://github.com/Polymer/tools/issues/724
deadcode: false, deadcode: false,
// Disable the simplify plugin because it can eat some statements preceeding // Disable the simplify plugin because it can eat some statements preceeding
// loops. https://github.com/babel/minify/issues/824 // loops. https://github.com/babel/minify/issues/824
simplify: false, simplify: false,
// This is breaking ES6 output. https://github.com/Polymer/tools/issues/261 // This is breaking ES6 output. https://github.com/Polymer/tools/issues/261
mangle: false, mangle: false,
}, },
{} {}
), ),
]; ],
};

View File

@ -39,9 +39,7 @@ module.exports = {
}, },
], ],
}, },
optimization: { optimization: webpackBase.optimization,
minimizer: webpackBase.minimizer,
},
plugins: [ plugins: [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
__DEV__: false, __DEV__: false,
@ -78,17 +76,7 @@ module.exports = {
importWorkboxFrom: "local", importWorkboxFrom: "local",
}), }),
].filter(Boolean), ].filter(Boolean),
resolve: { resolve: webpackBase.resolve,
extensions: [".ts", ".js", ".json"],
alias: {
react: "preact-compat",
"react-dom": "preact-compat",
// Not necessary unless you consume a module using `createClass`
"create-react-class": "preact-compat/lib/create-react-class",
// Not necessary unless you consume a module requiring `react-dom-factories`
"react-dom-factories": "preact-compat/lib/react-dom-factories",
},
},
output: { output: {
filename: "[name].js", filename: "[name].js",
chunkFilename: chunkFilename, chunkFilename: chunkFilename,

View File

@ -1,7 +1,7 @@
const path = require("path"); const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin"); const CopyWebpackPlugin = require("copy-webpack-plugin");
const { babelLoaderConfig } = require("../config/babel.js"); const { babelLoaderConfig } = require("../config/babel.js");
const { minimizer } = require("../config/babel.js"); const webpackBase = require("../config/babel.js");
const isProd = process.env.NODE_ENV === "production"; const isProd = process.env.NODE_ENV === "production";
const chunkFilename = isProd ? "chunk.[chunkhash].js" : "[name].chunk.js"; const chunkFilename = isProd ? "chunk.[chunkhash].js" : "[name].chunk.js";
@ -32,9 +32,7 @@ module.exports = {
}, },
], ],
}, },
optimization: { optimization: webpackBase.optimization,
minimizer,
},
plugins: [ plugins: [
new CopyWebpackPlugin([ new CopyWebpackPlugin([
"public", "public",
@ -63,9 +61,7 @@ module.exports = {
}, },
}), }),
].filter(Boolean), ].filter(Boolean),
resolve: { resolve: webpackBase.resolve,
extensions: [".ts", ".js", ".json"],
},
output: { output: {
filename: "[name].js", filename: "[name].js",
chunkFilename: chunkFilename, chunkFilename: chunkFilename,

5
src/data/script.ts Normal file
View File

@ -0,0 +1,5 @@
export interface EventAction {
event: string;
event_data?: { [key: string]: any };
event_data_template?: { [key: string]: any };
}

View File

@ -0,0 +1,12 @@
import { PaperInputElement } from "@polymer/paper-input/paper-input";
// Force file to be a module to augment global scope.
export {};
declare global {
namespace JSX {
interface IntrinsicElements {
"paper-input": Partial<PaperInputElement>;
}
}
}

View File

@ -3,8 +3,26 @@ import "@polymer/paper-input/paper-input";
import JSONTextArea from "../json_textarea"; import JSONTextArea from "../json_textarea";
import { onChangeEvent } from "../../../../common/preact/event"; import { onChangeEvent } from "../../../../common/preact/event";
import { LocalizeFunc } from "../../../../common/translations/localize";
import { EventAction } from "../../../../data/script";
interface Props {
index: number;
action: EventAction;
localize: LocalizeFunc;
onChange: (index: number, action: EventAction) => void;
}
export default class EventActionForm extends Component<Props> {
private onChange: (event: Event) => void;
static get defaultConfig(): EventAction {
return {
event: "",
event_data: {},
};
}
export default class EventAction extends Component {
constructor() { constructor() {
super(); super();
@ -12,16 +30,11 @@ export default class EventAction extends Component {
this.serviceDataChanged = this.serviceDataChanged.bind(this); this.serviceDataChanged = this.serviceDataChanged.bind(this);
} }
serviceDataChanged(data) { public render() {
this.props.onChange( const {
this.props.index, action: { event, event_data },
Object.assign({}, this.props.action, { data }) localize,
); } = this.props;
}
render({ action, localize }) {
/* eslint-disable camelcase */
const { event, event_data } = action;
return ( return (
<div> <div>
<paper-input <paper-input
@ -42,9 +55,11 @@ export default class EventAction extends Component {
</div> </div>
); );
} }
}
EventAction.defaultConfig = { private serviceDataChanged(eventData) {
event: "", this.props.onChange(this.props.index, {
event_data: {}, ...this.props.action,
}; event_data: eventData,
});
}
}

View File

@ -1,5 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"jsx": "react",
"jsxFactory": "h",
"target": "es2017", "target": "es2017",
"module": "esnext", "module": "esnext",
"moduleResolution": "node", "moduleResolution": "node",

View File

@ -76,9 +76,7 @@ function createConfig(isProdBuild, latestBuild) {
}, },
], ],
}, },
optimization: { optimization: webpackBase.optimization,
minimizer: webpackBase.minimizer,
},
plugins: [ plugins: [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
__DEV__: JSON.stringify(!isProdBuild), __DEV__: JSON.stringify(!isProdBuild),
@ -196,17 +194,7 @@ function createConfig(isProdBuild, latestBuild) {
path: path.resolve(__dirname, buildPath), path: path.resolve(__dirname, buildPath),
publicPath, publicPath,
}, },
resolve: { resolve: webpackBase.resolve,
extensions: [".ts", ".js", ".json"],
alias: {
react: "preact-compat",
"react-dom": "preact-compat",
// Not necessary unless you consume a module using `createClass`
"create-react-class": "preact-compat/lib/create-react-class",
// Not necessary unless you consume a module requiring `react-dom-factories`
"react-dom-factories": "preact-compat/lib/react-dom-factories",
},
},
}; };
} }