Skip to content
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

fix(dev-scripts)!: Fixes, refactoring and simplification of webpack.config.js and 'blockly' imports #2229

Merged
merged 11 commits into from
Mar 15, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {overrideOldBlockDefinitions} = require('../src/index');

const assert = chai.assert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {overrideOldBlockDefinitions} = require('../src/index');

const assert = chai.assert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {overrideOldBlockDefinitions} = require('../src/index');

const assert = chai.assert;
Expand Down
2 changes: 1 addition & 1 deletion plugins/block-plus-minus/test/if.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const {runPlusMinusTestSuite} = require('./test_helpers.mocha');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {dartGenerator} = require('blockly/dart');
const {javascriptGenerator} = require('blockly/javascript');
const {luaGenerator} = require('blockly/lua');
Expand Down
2 changes: 1 addition & 1 deletion plugins/block-plus-minus/test/list_create.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const {runPlusMinusTestSuite} = require('./test_helpers.mocha');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {dartGenerator} = require('blockly/dart');
const {javascriptGenerator} = require('blockly/javascript');
const {luaGenerator} = require('blockly/lua');
Expand Down
2 changes: 1 addition & 1 deletion plugins/block-plus-minus/test/procedures.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const chai = require('chai');
const sinon = require('sinon');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {testHelpers} = require('@blockly/dev-tools');
const procedureTestHelpers = require('./procedures_test_helpers.mocha');
const {dartGenerator} = require('blockly/dart');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const chai = require('chai');
const Blockly = require('blockly/node');
const Blockly = require('blockly');

require('../src/index');
const assert = chai.assert;
Expand Down
2 changes: 1 addition & 1 deletion plugins/block-plus-minus/test/text_join.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const {testHelpers} = require('@blockly/dev-tools');
const {runPlusMinusTestSuite} = require('./test_helpers.mocha');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {dartGenerator} = require('blockly/dart');
const {javascriptGenerator} = require('blockly/javascript');
const {luaGenerator} = require('blockly/lua');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const eventTestHelpers = require('./event_test_helpers');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
const {ObservableParameterModel} = require('../src/observable_parameter_model');
const {ProcedureCreate} = require('../src/events_procedure_create');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const eventTestHelpers = require('./event_test_helpers');
const {testHelpers} = require('@blockly/dev-tools');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {testHelpers} = require('@blockly/dev-tools');
const eventTestHelpers = require('./event_test_helpers');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const eventTestHelpers = require('./event_test_helpers');
const {testHelpers} = require('@blockly/dev-tools');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const eventTestHelpers = require('./event_test_helpers');
const {testHelpers} = require('@blockly/dev-tools');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const eventTestHelpers = require('./event_test_helpers');
const {testHelpers} = require('@blockly/dev-tools');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const eventTestHelpers = require('./event_test_helpers');
const {testHelpers} = require('@blockly/dev-tools');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const eventTestHelpers = require('./event_test_helpers');
const {testHelpers} = require('@blockly/dev-tools');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const chai = require('chai');
const sinon = require('sinon');
const assert = chai.assert;
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const eventTestHelpers = require('./event_test_helpers');
const {testHelpers} = require('@blockly/dev-tools');
const {ObservableProcedureModel} = require('../src/observable_procedure_model');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

const Blockly = require('blockly/node');
const Blockly = require('blockly');
const chai = require('chai');
const assert = chai.assert;
const sinon = require('sinon');
Expand Down
2 changes: 1 addition & 1 deletion plugins/content-highlight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @fileoverview A plugin that highlights the content on the workspace.
*/

import * as Blockly from 'blockly';
import * as Blockly from 'blockly/core';

/**
* List of events that cause a change in content area size.
Expand Down
82 changes: 43 additions & 39 deletions plugins/dev-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const webpack = require('webpack');

const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
const exists = (relativePath) => fs.existsSync(resolveApp(relativePath));

const packageJson = require(resolveApp('package.json'));

Expand All @@ -23,26 +24,32 @@ module.exports = (env) => {
const isDevelopment = mode === 'development';
const isProduction = mode === 'production';
const isTest = mode === 'test';
const isTypescript = fs.existsSync(resolveApp('tsconfig.json'));
const isTypescript = exists('tsconfig.json');

let entry;
let outputFile;
let target = 'web';
const plugins = [
// Use DefinePlugin (https://webpack.js.org/plugins/define-plugin/)
// to pass the name of the package being built to the dev-tools
// playground (via plugins/dev-tools/src/playground/id.js). The
// "process.env." prefix is arbitrary: the stringified value
// gets substituted directly into the source code of that file
// at build time.
new webpack.DefinePlugin({
'process.env.PACKAGE_NAME': JSON.stringify(packageJson.name),
}),
];

if (isProduction) {
// Production.
['js', 'ts']
.filter((ext) => fs.existsSync(resolveApp(`./src/index.${ext}`)))
.forEach((ext) => {
entry = `./src/index.${ext}`;
});
if (exists('./src/index.js')) entry = './src/index.js';
if (exists('./src/index.ts')) entry = './src/index.ts';
outputFile = 'index.js';
} else if (isDevelopment) {
// Development.
['js', 'ts']
.filter((ext) => fs.existsSync(resolveApp(`./test/index.${ext}`)))
.forEach((ext) => {
entry = `./test/index.${ext}`;
});
if (exists('./test/index.js')) entry = './test/index.js';
if (exists('./test/index.ts')) entry = './test/index.ts';
outputFile = 'test_bundle.js';
} else if (isTest) {
// Test.
Expand All @@ -56,16 +63,20 @@ module.exports = (env) => {
});
outputFile = '[name].mocha.js';
target = 'node';
}

// Add 'dist' to the end of the blockly module alias if we have acquired
// blockly from git instead of npm.
let blocklyAliasSuffix = '';
const blocklyDependency =
(packageJson.dependencies && packageJson.dependencies['blockly']) ||
(packageJson.devDependencies && packageJson.devDependencies['blockly']);
if (blocklyDependency && blocklyDependency.indexOf('git://') === 0) {
blocklyAliasSuffix = '/dist';
// Certain optional plugins wanted by dependencies of blockly
// (jsdom want canvas, jsdom depends on ws which wants
// bufferutils and utf-8-validate) are loaded via:
//
// try {/*...*/ = require('package')} catch (e) {/*...*/}
//
// Webpack tries to satisfy the require even though it's in a
// try/catch, and issues a warning if it can't be found.
// IgnorePlugin suppresses this.
plugins.push(
new webpack.IgnorePlugin({
resourceRegExp: /^(canvas|bufferutil|utf-8-validate)$/,
}),
);
}

return {
Expand All @@ -84,12 +95,10 @@ module.exports = (env) => {
clean: true,
},
resolve: {
alias: {
blockly: resolveApp(`node_modules/blockly${blocklyAliasSuffix}`),
},
extensions: ['.ts', '.js'].filter(
(ext) => isTypescript || !ext.includes('ts'),
),
extensions: ['.ts', '.js', '.json', '.wasm'],
// Some deps may require node.js core modules. Tell node.js what
// polyfills to use for them when building for non-node.js targets
// (Or to ignore them if the fallback is false.)
fallback: {
util: false,
},
Expand All @@ -111,20 +120,15 @@ module.exports = (env) => {
// Ignore spurious warnings from source-map-loader
// It can't find source maps for some Closure modules and that is expected
ignoreWarnings: [/Failed to parse source map/],
plugins: [
// Add package name.
new webpack.DefinePlugin({
'process.env.PACKAGE_NAME': JSON.stringify(packageJson.name),
}),
// canvas should only be required by jsdom if the 'canvas' package is
// installed in package.json. Ignoring canvas require errors.
isTest &&
new webpack.IgnorePlugin({
resourceRegExp: /canvas$/,
}),
].filter(Boolean),
plugins,
externals: isProduction
? {
'blockly': {
root: 'Blockly',
commonjs: 'blockly',
commonjs2: 'blockly',
amd: 'blockly',
},
'blockly/core': {
root: 'Blockly',
commonjs: 'blockly/core',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const chai = require('chai');
const sinon = require('sinon');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
require('./field_dependent_dropdown_test_block');

const assert = chai.assert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const chai = require('chai');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {Navigation} = require('../src/navigation');
const assert = chai.assert;
const {testHelpers} = require('@blockly/dev-tools');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
const chai = require('chai');
const sinon = require('sinon');

const Blockly = require('blockly/node');
const Blockly = require('blockly');
const {NavigationController, Constants} = require('../src/index');
const {
createNavigationWorkspace,
Expand Down
2 changes: 1 addition & 1 deletion plugins/keyboard-navigation/test/shortcuts_test.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const sinon = require('sinon');
const chai = require('chai');

const Blockly = require('blockly/node');
const Blockly = require('blockly');

const {NavigationController} = require('../src/index');
const {
Expand Down
2 changes: 1 addition & 1 deletion plugins/modal/test/modal_test.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

const assert = require('assert');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const sinon = require('sinon');

const Modal = require('../src/index.js').Modal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

const chai = require('chai');
const Blockly = require('blockly/node');
const Blockly = require('blockly');

const StrictConnectionChecker =
require('../src/index.js').StrictConnectionChecker;
Expand Down
2 changes: 1 addition & 1 deletion plugins/suggested-blocks/test/suggested_blocks.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const {assert} = require('chai');
const sinon = require('sinon');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const SuggestedBlocks = require('../src/index');

const STANDARD_TEST_CASE = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

const assert = require('assert');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const sinon = require('sinon');

const {TypedVariableModal} = require('../src/index.js');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

const assert = require('assert');
const Blockly = require('blockly/node');
const Blockly = require('blockly');
const sinon = require('sinon');

const {WorkspaceSearch} = require('../src/index');
Expand Down
Loading