From e7f58fc7fa8f2009b0fa274e364a76a067ea93be Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Tue, 14 Jan 2020 18:13:21 +0100 Subject: [PATCH] Convert webpack.config.js to typescript Change-type: patch --- npm-shrinkwrap.json | 61 ++++++++ package.json | 3 +- .../simple-progress-webpack-plugin/index.d.ts | 1 + webpack.config.js | 131 ------------------ webpack.config.ts | 122 ++++++++++++++++ 5 files changed, 186 insertions(+), 132 deletions(-) create mode 100644 typings/simple-progress-webpack-plugin/index.d.ts delete mode 100644 webpack.config.js create mode 100644 webpack.config.ts diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 28f042f0..6abc5757 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1092,6 +1092,12 @@ "defer-to-connect": "^1.0.1" } }, + "@types/anymatch": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/@types/anymatch/-/anymatch-1.3.1.tgz", + "integrity": "sha512-/+CRPXpBDpo2RK9C68N3b2cOvO0Cf5B9aPijHsoDQTHivnGSObdOF2BRQOYjojWTDy6nQvMjmqRXIxH55VjxxA==", + "dev": true + }, "@types/bindings": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/@types/bindings/-/bindings-1.3.0.tgz", @@ -1338,6 +1344,12 @@ "integrity": "sha512-1OzrNb4RuAzIT7wHSsgZRlMBlNsJl+do6UblR7JMW4oB7bbR+uBEYtUh7gEc/jM84GGilh68lSOokyM/zNUlBA==", "dev": true }, + "@types/source-list-map": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/@types/source-list-map/-/source-list-map-0.1.2.tgz", + "integrity": "sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==", + "dev": true + }, "@types/styled-components": { "version": "4.1.8", "resolved": "https://registry.npmjs.org/@types/styled-components/-/styled-components-4.1.8.tgz", @@ -1356,6 +1368,12 @@ "csstype": "^2.6.4" } }, + "@types/tapable": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@types/tapable/-/tapable-1.0.5.tgz", + "integrity": "sha512-/gG2M/Imw7cQFp8PGvz/SwocNrmKFjFsm5Pb8HdbHkZ1K8pmuPzOX4VeVoiEecFCVf4CsN1r3/BRvx+6sNqwtQ==", + "dev": true + }, "@types/tmp": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/@types/tmp/-/tmp-0.1.0.tgz", @@ -1368,6 +1386,15 @@ "integrity": "sha512-wHNBMnkoEBiRAd3s8KTKwIuO9biFtTf0LehITzBhSco+HQI0xkXZbLOD55SW3Aqw3oUkHstkm5SPv58yaAdFPQ==", "dev": true }, + "@types/uglify-js": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/uglify-js/-/uglify-js-3.0.4.tgz", + "integrity": "sha512-SudIN9TRJ+v8g5pTG8RRCqfqTMNqgWCKKd3vtynhGzkIIjxaicNAMuY5TRadJ6tzDu3Dotf3ngaMILtmOdmWEQ==", + "dev": true, + "requires": { + "source-map": "^0.6.1" + } + }, "@types/usb": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/@types/usb/-/usb-1.5.1.tgz", @@ -1384,6 +1411,40 @@ "@types/node": "*" } }, + "@types/webpack": { + "version": "4.41.2", + "resolved": "https://registry.npmjs.org/@types/webpack/-/webpack-4.41.2.tgz", + "integrity": "sha512-DNMQOfEvwzWRRyp6Wy9QVCgJ3gkelZsuBE2KUD318dg95s9DKGiT5CszmmV58hq8jk89I9NClre48AEy1MWAJA==", + "dev": true, + "requires": { + "@types/anymatch": "*", + "@types/node": "*", + "@types/tapable": "*", + "@types/uglify-js": "*", + "@types/webpack-sources": "*", + "source-map": "^0.6.0" + } + }, + "@types/webpack-node-externals": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/@types/webpack-node-externals/-/webpack-node-externals-1.7.0.tgz", + "integrity": "sha512-3EohijquPf5rtTvhAPzqbBWb6nPnUxkRxEHcggQxktyeN0/pLnwtZ9nRR6XBCG0R/5nLnGzFrUwQV+pqJPqHXQ==", + "dev": true, + "requires": { + "@types/webpack": "*" + } + }, + "@types/webpack-sources": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/@types/webpack-sources/-/webpack-sources-0.1.5.tgz", + "integrity": "sha512-zfvjpp7jiafSmrzJ2/i3LqOyTYTuJ7u1KOXlKgDlvsj9Rr0x7ZiYu5lZbXwobL7lmsRNtPXlBfmaUD8eU2Hu8w==", + "dev": true, + "requires": { + "@types/node": "*", + "@types/source-list-map": "*", + "source-map": "^0.6.1" + } + }, "@webassemblyjs/ast": { "version": "1.8.5", "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.8.5.tgz", diff --git a/package.json b/package.json index 60d94ce8..d9c1123f 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ }, "scripts": { "test": "make lint test sanity-checks", - "prettier": "prettier --config ./node_modules/resin-lint/config/.prettierrc --write \"lib/**/*.ts\" \"lib/**/*.tsx\"", + "prettier": "prettier --config ./node_modules/resin-lint/config/.prettierrc --write \"lib/**/*.ts\" \"lib/**/*.tsx\" \"webpack.config.ts\"", "start": "./node_modules/.bin/electron .", "postshrinkwrap": "node ./scripts/clean-shrinkwrap.js", "configure": "node-gyp configure", @@ -102,6 +102,7 @@ "@types/request": "^2.48.4", "@types/semver": "^6.2.0", "@types/tmp": "^0.1.0", + "@types/webpack-node-externals": "^1.7.0", "babel-loader": "^8.0.4", "chalk": "^1.1.3", "electron": "6.1.4", diff --git a/typings/simple-progress-webpack-plugin/index.d.ts b/typings/simple-progress-webpack-plugin/index.d.ts new file mode 100644 index 00000000..e473ffde --- /dev/null +++ b/typings/simple-progress-webpack-plugin/index.d.ts @@ -0,0 +1 @@ +declare module 'simple-progress-webpack-plugin'; diff --git a/webpack.config.js b/webpack.config.js deleted file mode 100644 index 748cca61..00000000 --- a/webpack.config.js +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2017 balena.io - * - * Licensed 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. - */ - -'use strict' - -const _ = require('lodash') -const path = require('path') -const SimpleProgressWebpackPlugin = require('simple-progress-webpack-plugin') -const nodeExternals = require('webpack-node-externals') - -/** - * Don't webpack package.json as mixpanel & sentry tokens - * will be inserted in it after webpacking - * - * @param {*} packageJsonPath - Path for the package.json file - * @returns {void} - * - * @example webpack externals: - * [ - * externalPackageJson('./package.json') - * ] - */ -const externalPackageJson = (packageJsonPath) => { - return (_context, request, callback) => { - if (_.endsWith(request, 'package.json')) { - return callback(null, `commonjs ${packageJsonPath}`) - } - return callback() - } -} - -const commonConfig = { - mode: 'production', - optimization: { - minimize: false - }, - module: { - rules: [ - { - test: /\.jsx?$/, - include: [ path.resolve(__dirname, 'lib', 'gui') ], - use: { - loader: 'babel-loader', - options: { - presets: [ - '@babel/preset-react', - [ '@babel/preset-env', { targets: { electron: '6' } } ] - ], - plugins: [ '@babel/plugin-proposal-function-bind' ], - cacheDirectory: true - } - } - }, - { - test: /\.html$/, - use: 'html-loader' - }, - { - test: /\.tsx?$/, - use: 'ts-loader' - } - ] - }, - resolve: { - extensions: [ '.js', '.jsx', '.json', '.ts', '.tsx' ] - }, - plugins: [ - new SimpleProgressWebpackPlugin({ - format: process.env.WEBPACK_PROGRESS || 'verbose' - }) - ], - output: { - path: path.join(__dirname, 'generated'), - filename: '[name].js' - } -} - -const guiConfig = { - ...commonConfig, - target: 'electron-renderer', - node: { - __dirname: true, - __filename: true - }, - externals: [ - nodeExternals(), - - // '../../../package.json' because we are in 'lib/gui/app/index.html' - externalPackageJson('../../../package.json') - ], - entry: { - gui: path.join(__dirname, 'lib', 'gui', 'app', 'app.ts') - }, - devtool: 'source-map' -} - -const etcherConfig = { - ...commonConfig, - target: 'electron-main', - node: { - __dirname: false, - __filename: true - }, - externals: [ - nodeExternals(), - - // '../package.json' because we are in 'generated/etcher.js' - externalPackageJson('../package.json') - ], - entry: { - etcher: path.join(__dirname, 'lib', 'start.ts') - } -} - -module.exports = [ - guiConfig, - etcherConfig -] diff --git a/webpack.config.ts b/webpack.config.ts new file mode 100644 index 00000000..5e057aad --- /dev/null +++ b/webpack.config.ts @@ -0,0 +1,122 @@ +/* + * Copyright 2017 balena.io + * + * Licensed 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 * as _ from 'lodash'; +import * as path from 'path'; +import * as SimpleProgressWebpackPlugin from 'simple-progress-webpack-plugin'; +import * as nodeExternals from 'webpack-node-externals'; + +/** + * Don't webpack package.json as mixpanel & sentry tokens + * will be inserted in it after webpacking + */ +function externalPackageJson(packageJsonPath: string) { + return ( + _context: string, + request: string, + callback: (error?: Error | null, result?: string) => void, + ) => { + if (_.endsWith(request, 'package.json')) { + return callback(null, `commonjs ${packageJsonPath}`); + } + return callback(); + }; +} + +const commonConfig = { + mode: 'production', + optimization: { + minimize: false, + }, + module: { + rules: [ + { + test: /\.jsx?$/, + include: [path.resolve(__dirname, 'lib', 'gui')], + use: { + loader: 'babel-loader', + options: { + presets: [ + '@babel/preset-react', + ['@babel/preset-env', { targets: { electron: '6' } }], + ], + plugins: ['@babel/plugin-proposal-function-bind'], + cacheDirectory: true, + }, + }, + }, + { + test: /\.html$/, + use: 'html-loader', + }, + { + test: /\.tsx?$/, + use: 'ts-loader', + }, + ], + }, + resolve: { + extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'], + }, + plugins: [ + new SimpleProgressWebpackPlugin({ + format: process.env.WEBPACK_PROGRESS || 'verbose', + }), + ], + output: { + path: path.join(__dirname, 'generated'), + filename: '[name].js', + }, +}; + +const guiConfig = { + ...commonConfig, + target: 'electron-renderer', + node: { + __dirname: true, + __filename: true, + }, + externals: [ + nodeExternals(), + + // '../../../package.json' because we are in 'lib/gui/app/index.html' + externalPackageJson('../../../package.json'), + ], + entry: { + gui: path.join(__dirname, 'lib', 'gui', 'app', 'app.ts'), + }, + devtool: 'source-map', +}; + +const etcherConfig = { + ...commonConfig, + target: 'electron-main', + node: { + __dirname: false, + __filename: true, + }, + externals: [ + nodeExternals(), + + // '../package.json' because we are in 'generated/etcher.js' + externalPackageJson('../package.json'), + ], + entry: { + etcher: path.join(__dirname, 'lib', 'start.ts'), + }, +}; + +module.exports = [guiConfig, etcherConfig];