Skip to content

Add TypeScript support & make externals configurable #54

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

Merged
merged 8 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 121 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"postcss-loader": "4.3.0",
"postcss-simple-vars": "^5.0.1",
"style-loader": "4.0.0",
"ts-loader": "^9.5.1",
"url-loader": "4.1.1",
"webpack": "^5.90.3"
}
Expand Down
45 changes: 39 additions & 6 deletions src/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const merge = require('lodash.merge');
const nodeExternals = require('webpack-node-externals');
const webpack = require('webpack');
const TerserPlugin = require("terser-webpack-plugin")

const DEFAULT_CHUNK_FILENAME = 'chunks/[name].[chunkhash].js';
const DEFAULT_ASSET_FILENAME = 'assets/[name].[hash][ext][query]';
Expand Down Expand Up @@ -30,14 +31,15 @@ const toPath = path => {
class ScratchWebpackConfigBuilder {
/**
* @param {object} options Options for the webpack configuration.
* @param {string|URL} options.rootPath The absolute path to the project root.
* @param {string|URL} [options.rootPath] The absolute path to the project root.
* @param {string|URL} [options.distPath] The absolute path to build output. Defaults to `dist` under `rootPath`.
* @param {string|URL} [options.publicPath] The public location where the output assets will be located. Defaults to `/`.
* @param {boolean} [options.enableReact] Whether to enable React and JSX support.
* @param {string} [options.libraryName] The name of the library to build. Shorthand for `output.library.name`.
* @param {string|URL} [options.srcPath] The absolute path to the source files. Defaults to `src` under `rootPath`.
* @param {boolean} [options.shouldSplitChunks] Whether to enable spliting code to chunks.
*/
constructor ({ distPath, enableReact, libraryName, rootPath, srcPath, shouldSplitChunks }) {
constructor ({ distPath, enableReact, enableTs, libraryName, rootPath, srcPath, publicPath = '/', shouldSplitChunks }) {
const isProduction = process.env.NODE_ENV === 'production';
const mode = isProduction ? 'production' : 'development';

Expand All @@ -58,6 +60,14 @@ class ScratchWebpackConfigBuilder {
} : path.resolve(this._srcPath, 'index'),
optimization: {
minimize: isProduction,
minimizer: [
new TerserPlugin({
// Limiting Terser to use only 2 threads. At least for building scratch-gui
// this results in a performance gain (from ~60s to ~36s) on a MacBook with
// M1 Pro and 32GB of RAM and halving the memory usage (from ~11GB at peaks to ~6GB)
parallel: 2
})
],
...(
shouldSplitChunks ? {
splitChunks: {
Expand All @@ -74,6 +84,8 @@ class ScratchWebpackConfigBuilder {
assetModuleFilename: DEFAULT_ASSET_FILENAME,
chunkFilename: DEFAULT_CHUNK_FILENAME,
path: this._distPath,
// See https://github.com/scratchfoundation/scratch-editor/pull/25/files/9bc537f9bce35ee327b74bd6715d6c5140f73937#r1763073684
publicPath,
library: {
name: libraryName,
type: 'umd2'
Expand All @@ -90,6 +102,7 @@ class ScratchWebpackConfigBuilder {
'.jsx'
] : []
),
...(enableTs ? ['.ts', '.tsx'] : []),
// webpack supports '...' to include defaults, but eslint does not
'.js',
'.json'
Expand All @@ -98,12 +111,18 @@ class ScratchWebpackConfigBuilder {
module: {
rules: [
{
test: enableReact ? /\.[cm]?jsx?$/ : /\.[cm]?js$/,
test: enableReact ?
(enableTs ? /\.[cm]?[jt]sx?$/ : /\.[cm]?jsx?$/) :
(enableTs ? /\.[cm]?[jt]s$/ : /\.[cm]?js$/),
loader: 'babel-loader',
exclude: [
{
and: [/node_modules/],
not: [/node_modules[\\/].*scratch/]

// Some scratch pakcages point to their source (as opposed to a pre-built version)
// for their browser or webpack target. So we need to process them (at the minimum
// to resolve the JSX syntax).
not: [/node_modules[\\/]scratch-(paint|render|svg-renderer|vm)[\\/]src[\\/]/]
}
],
options: {
Expand Down Expand Up @@ -196,9 +215,13 @@ class ScratchWebpackConfigBuilder {
]
}
] : []
)
),
...(enableTs ? [{
test: enableReact ? /\.[cm]?tsx?$/ : /\.[cm]?ts$/,
loader: 'ts-loader',
exclude: [/node_modules/]
}] : []),
],

},
plugins: [
new webpack.ProvidePlugin({
Expand Down Expand Up @@ -238,6 +261,16 @@ class ScratchWebpackConfigBuilder {
return this;
}

/**
* Append new externals to the current configuration object.
* @param {string[]} externals Externals to add.
* @returns {this}
*/
addExternals(externals) {
this._config.externals = (this._config.externals ?? []).concat(externals);
return this;
}

/**
* Set the target environment for this configuration.
* @param {string} target The target environment, like `node`, `browserslist`, etc.
Expand Down