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 @@ -641,5 +641,5 @@ suite('If block', function () {
},
},
];
testHelpers.runSerializationTestSuite(testCases);
testHelpers.runSerializationTestSuite(testCases, Blockly);
});
Original file line number Diff line number Diff line change
Expand Up @@ -540,5 +540,5 @@ suite('List create block', function () {
},
},
];
testHelpers.runSerializationTestSuite(testCases);
testHelpers.runSerializationTestSuite(testCases, Blockly);
});
Original file line number Diff line number Diff line change
Expand Up @@ -542,5 +542,5 @@ suite('Text join block', function () {
},
},
];
testHelpers.runSerializationTestSuite(testCases);
testHelpers.runSerializationTestSuite(testCases, Blockly);
});
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -172,7 +172,7 @@ suite('If block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('JSON', function () {
Expand Down Expand Up @@ -222,7 +222,7 @@ suite('If block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

runPlusMinusTestSuite('controls_if', 1, 1, 'IF', assertIfBlockStructure);
Expand Down
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -206,7 +206,7 @@ suite('List create block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('Json', function () {
Expand Down Expand Up @@ -299,7 +299,7 @@ suite('List create block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

runPlusMinusTestSuite(
Expand Down
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -339,7 +339,7 @@ suite('Procedure blocks', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('Json', function () {
Expand Down Expand Up @@ -514,7 +514,7 @@ suite('Procedure blocks', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('Adding and removing inputs', function () {
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
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -195,7 +195,7 @@ suite('Text join block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

suite('Json', function () {
Expand Down Expand Up @@ -275,7 +275,7 @@ suite('Text join block', function () {
},
},
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);
});

runPlusMinusTestSuite('text_join', 2, 0, 'ADD', assertTextJoinBlockStructure);
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 Expand Up @@ -2228,7 +2228,7 @@ suite('Procedures', function () {
},
},
];
testHelpers.runSerializationTestSuite(xmlTestCases, globalThis.clock);
testHelpers.runSerializationTestSuite(xmlTestCases, Blockly);

const jsonTestCases = [
{
Expand Down Expand Up @@ -2448,5 +2448,5 @@ suite('Procedures', function () {
},
},
];
testHelpers.runSerializationTestSuite(jsonTestCases, globalThis.clock);
testHelpers.runSerializationTestSuite(jsonTestCases, Blockly);
});
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ suite('BlockTemplate', function () {
},
// TODO add additional test cases.
];
runSerializationTestSuite(testCases);
runSerializationTestSuite(testCases, Blockly);

// TODO add any other relevant tests
});
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
Loading
Loading