From 444d2bacd045bfda043bc30e073b0b36c3bb2347 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Tue, 21 Apr 2020 10:52:35 -0700 Subject: [PATCH] Standardize build (prod), start (dev) and read dev config --- plugins/block-plus-minus/package.json | 3 +- .../dev-create/templates/block/template.json | 3 +- .../dev-create/templates/field/template.json | 3 +- .../dev-create/templates/plugin/template.json | 3 +- .../dev-create/templates/theme/template.json | 3 +- plugins/dev-scripts/config/webpack.config.js | 80 ++++++------------- .../config/webpackDevServer.config.js | 32 ++++++++ plugins/dev-scripts/package.json | 1 - plugins/dev-scripts/scripts/build.js | 7 +- plugins/dev-scripts/scripts/start.js | 16 ++-- plugins/dev-tools/package.json | 3 +- plugins/field-slider/package.json | 3 +- plugins/theme-modern/package.json | 3 +- plugins/typed-variable-modal/package.json | 3 +- plugins/workspace-search/package.json | 3 +- 15 files changed, 74 insertions(+), 92 deletions(-) create mode 100644 plugins/dev-scripts/config/webpackDevServer.config.js diff --git a/plugins/block-plus-minus/package.json b/plugins/block-plus-minus/package.json index 05422065d2..b53fd11b93 100644 --- a/plugins/block-plus-minus/package.json +++ b/plugins/block-plus-minus/package.json @@ -5,8 +5,7 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", - "prepublishOnly": "npm run clean && npm run prod", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start", "test": "npm run build && npm run test:mocha", "test:mocha": "mocha --ui tdd test/if.js test/list_create.js test/procedures.js test/text_join.js" diff --git a/plugins/dev-create/templates/block/template.json b/plugins/dev-create/templates/block/template.json index 1d9c804c7a..8ce1e88130 100644 --- a/plugins/dev-create/templates/block/template.json +++ b/plugins/dev-create/templates/block/template.json @@ -3,9 +3,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start" }, "devDependencies": { diff --git a/plugins/dev-create/templates/field/template.json b/plugins/dev-create/templates/field/template.json index cd1d606642..10b0675464 100644 --- a/plugins/dev-create/templates/field/template.json +++ b/plugins/dev-create/templates/field/template.json @@ -3,9 +3,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start" }, "devDependencies": { diff --git a/plugins/dev-create/templates/plugin/template.json b/plugins/dev-create/templates/plugin/template.json index 5864b54b24..c1b27d4de5 100644 --- a/plugins/dev-create/templates/plugin/template.json +++ b/plugins/dev-create/templates/plugin/template.json @@ -3,9 +3,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start" }, "devDependencies": { diff --git a/plugins/dev-create/templates/theme/template.json b/plugins/dev-create/templates/theme/template.json index 67b56d0e25..9720f30cba 100644 --- a/plugins/dev-create/templates/theme/template.json +++ b/plugins/dev-create/templates/theme/template.json @@ -3,9 +3,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start" }, "devDependencies": { diff --git a/plugins/dev-scripts/config/webpack.config.js b/plugins/dev-scripts/config/webpack.config.js index cd980b29be..bf739b25e4 100644 --- a/plugins/dev-scripts/config/webpack.config.js +++ b/plugins/dev-scripts/config/webpack.config.js @@ -4,9 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ /** - * @fileoverview Webpack base configuration file. + * @fileoverview Webpack configuration file. * @author samelh@google.com (Sam El-Husseini) */ +'use strict'; const path = require('path'); const fs = require('fs'); @@ -15,30 +16,32 @@ const appDirectory = fs.realpathSync(process.cwd()); const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath); module.exports = (env) => { - const devServer = { - port: 3000, - host: '0.0.0.0', - watchOptions: { - ignored: /node_modules/, - }, - }; - if (env.buildTest) { - devServer.openPage = 'test'; - devServer.open = true; - } + const mode = env.mode; + const isProduction = mode === 'production'; + + const srcEntry = `./src/index.${['js', 'ts'].find((ext) => + fs.existsSync(resolveApp(`./src/index.${ext}`)) + )}`; + const testEntry = `./test/index.${['js', 'ts'].find((ext) => + fs.existsSync(resolveApp(`./test/index.${ext}`)) + )}`; - const src = { - name: 'src', - mode: env.mode, - entry: './src/index.js', + return { + mode, + entry: isProduction ? srcEntry : testEntry, devtool: 'source-map', output: { - path: resolveApp('dist'), - publicPath: '/dist/', - filename: 'index.js', + path: isProduction ? resolveApp('dist') : resolveApp('build'), + publicPath: isProduction ? '/dist/' : '/build/', + filename: isProduction ? 'index.js' : 'test_bundle.js', libraryTarget: 'umd', globalObject: 'this', }, + resolve: { + alias: { + 'blockly': resolveApp('node_modules/blockly'), + }, + }, module: { rules: [{ test: /\.js$/, @@ -51,48 +54,13 @@ module.exports = (env) => { }, }], }, - devServer: devServer, - externals: { + externals: isProduction ? { 'blockly/core': { root: 'Blockly', commonjs: 'blockly/core', commonjs2: 'blockly/core', amd: 'blockly/core', }, - }, + } : {}, }; - const webpackExports = [src]; - - if (env.buildTest) { - const test = { - name: 'test', - mode: 'development', - entry: './test/index.js', - devtool: 'source-map', - output: { - path: resolveApp('build'), - publicPath: '/build/', - filename: 'test_bundle.js', - }, - resolve: { - alias: { - 'blockly': resolveApp('node_modules/blockly'), - }, - }, - module: { - rules: [{ - test: /\.js$/, - exclude: /(node_modules)/, - use: { - loader: require.resolve('babel-loader'), - options: { - presets: [require.resolve('@babel/preset-env')], - }, - }, - }], - }, - }; - webpackExports.push(test); - } - return webpackExports; }; diff --git a/plugins/dev-scripts/config/webpackDevServer.config.js b/plugins/dev-scripts/config/webpackDevServer.config.js new file mode 100644 index 0000000000..386fef2328 --- /dev/null +++ b/plugins/dev-scripts/config/webpackDevServer.config.js @@ -0,0 +1,32 @@ +/** + * @license + * Copyright 2020 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ +/** + * @fileoverview Webpack dev server configuration file. + * @author samelh@google.com (Sam El-Husseini) + */ +'use strict'; + +const path = require('path'); +const fs = require('fs'); + +const appDirectory = fs.realpathSync(process.cwd()); +const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath); + +module.exports = () => { + return { + port: 3000, + host: '0.0.0.0', + hot: true, + quiet: true, + publicPath: resolveApp('build'), + writeToDisk: true, + watchOptions: { + ignored: /node_modules/, + }, + openPage: 'test', + open: true, + }; +}; diff --git a/plugins/dev-scripts/package.json b/plugins/dev-scripts/package.json index 2599b5af27..5d21078f3b 100644 --- a/plugins/dev-scripts/package.json +++ b/plugins/dev-scripts/package.json @@ -31,7 +31,6 @@ "babel-eslint": "^10.1.0", "eslint": "^6.8.0", "mocha": "^7.1.0", - "open": "^7.0.3", "rimraf": "^3.0.2", "webpack": "^4.41.6", "webpack-cli": "^3.3.11", diff --git a/plugins/dev-scripts/scripts/build.js b/plugins/dev-scripts/scripts/build.js index bb11643bbb..877db707e0 100644 --- a/plugins/dev-scripts/scripts/build.js +++ b/plugins/dev-scripts/scripts/build.js @@ -16,7 +16,6 @@ 'use strict'; -const args = process.argv.slice(2); const path = require('path'); const fs = require('fs'); @@ -27,13 +26,11 @@ const appDirectory = fs.realpathSync(process.cwd()); const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath); const packageJson = require(resolveApp('package.json')); -const mode = args.length > 0 && args[0] == 'prod' ? - 'production' : 'development'; -console.log(`Running ${mode} build for ${packageJson.name}`); +console.log(`Running production build for ${packageJson.name}`); // Create the webpack configuration for based on the build environment. const config = webpackConfig({ - mode, + mode: 'production', }); // Create and run the webpack compiler. diff --git a/plugins/dev-scripts/scripts/start.js b/plugins/dev-scripts/scripts/start.js index ca53e0c3b3..0008b5ccee 100644 --- a/plugins/dev-scripts/scripts/start.js +++ b/plugins/dev-scripts/scripts/start.js @@ -18,11 +18,11 @@ const path = require('path'); const fs = require('fs'); -const open = require('open'); const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const webpackConfig = require('../config/webpack.config'); +const webpackDevServerConfig = require('../config/webpackDevServer.config'); const appDirectory = fs.realpathSync(process.cwd()); const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath); @@ -34,23 +34,19 @@ console.log(`Running start for ${packageJson.name}`); // Build the test directory. const config = webpackConfig({ mode: 'development', - buildTest: true, }); const compiler = webpack(config); // Read the webpack devServer configuration. -const devServerConfig = config[0].devServer; -const port = devServerConfig.port; -const host = devServerConfig.host; -const page = devServerConfig.openPage; -const URL = `http://${host}:${port}/${page}`; +const serverConfig = webpackDevServerConfig(); +const port = serverConfig.port; +const host = serverConfig.host; // Start the dev server. -const devServer = new WebpackDevServer(compiler); -devServer.listen(port, host, async (err) => { +const devServer = new WebpackDevServer(compiler, serverConfig); +devServer.listen(port, host, (err) => { if (err) { return console.log(err); } console.log('Starting the development server...\n'); - await open(URL); }); diff --git a/plugins/dev-tools/package.json b/plugins/dev-tools/package.json index 9ae7229daf..38144db039 100644 --- a/plugins/dev-tools/package.json +++ b/plugins/dev-tools/package.json @@ -5,9 +5,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist" + "prepublishOnly": "npm run clean && npm run build" }, "main": "dist/index.js", "module": "./src/index.js", diff --git a/plugins/field-slider/package.json b/plugins/field-slider/package.json index 91a760eee9..2177c19a55 100644 --- a/plugins/field-slider/package.json +++ b/plugins/field-slider/package.json @@ -5,9 +5,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start", "test": "blockly-scripts test" }, diff --git a/plugins/theme-modern/package.json b/plugins/theme-modern/package.json index 54dae4c40f..5d7696ba3b 100644 --- a/plugins/theme-modern/package.json +++ b/plugins/theme-modern/package.json @@ -5,9 +5,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start" }, "main": "./dist/index.js", diff --git a/plugins/typed-variable-modal/package.json b/plugins/typed-variable-modal/package.json index 0cea5a5ded..0eaba1671f 100644 --- a/plugins/typed-variable-modal/package.json +++ b/plugins/typed-variable-modal/package.json @@ -5,9 +5,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start", "test": "npm run build && npm run test:mocha", "test:mocha": "blockly-scripts test" diff --git a/plugins/workspace-search/package.json b/plugins/workspace-search/package.json index 722bff0aab..5e9307a882 100644 --- a/plugins/workspace-search/package.json +++ b/plugins/workspace-search/package.json @@ -5,9 +5,8 @@ "scripts": { "build": "blockly-scripts build", "clean": "blockly-scripts clean", - "dist": "blockly-scripts build prod", "lint": "blockly-scripts lint", - "prepublishOnly": "npm run clean && npm run dist", + "prepublishOnly": "npm run clean && npm run build", "start": "blockly-scripts start", "test": "npm run build && npm run test:mocha", "test:mocha": "blockly-scripts test"