Skip to content

Commit

Permalink
Standardize build (prod), start (dev) and read dev config (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
samelhusseini authored Apr 22, 2020
1 parent 975eb2b commit e7e87fd
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 92 deletions.
3 changes: 1 addition & 2 deletions plugins/block-plus-minus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions plugins/dev-create/templates/block/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions plugins/dev-create/templates/field/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions plugins/dev-create/templates/plugin/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions plugins/dev-create/templates/theme/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
80 changes: 24 additions & 56 deletions plugins/dev-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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$/,
Expand All @@ -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;
};
32 changes: 32 additions & 0 deletions plugins/dev-scripts/config/webpackDevServer.config.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
1 change: 0 additions & 1 deletion plugins/dev-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 2 additions & 5 deletions plugins/dev-scripts/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

'use strict';

const args = process.argv.slice(2);
const path = require('path');
const fs = require('fs');

Expand All @@ -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.
Expand Down
16 changes: 6 additions & 10 deletions plugins/dev-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
3 changes: 1 addition & 2 deletions plugins/dev-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions plugins/field-slider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 1 addition & 2 deletions plugins/theme-modern/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions plugins/typed-variable-modal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions plugins/workspace-search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e7e87fd

Please sign in to comment.