Skip to content

Commit

Permalink
build: use esbuild for nodejs bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
owenpearson committed Mar 20, 2024
1 parent af0f3f1 commit 7c00f59
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 64 deletions.
61 changes: 18 additions & 43 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ var fs = require('fs');
var path = require('path');
var webpackConfig = require('./webpack.config');
var esbuild = require('esbuild');
var umdWrapper = require('esbuild-plugin-umd-wrapper');
var banner = require('./src/fragments/license');
var process = require('process');
var stripLogsPlugin = require('./grunt/esbuild/strip-logs').default;
var MochaServer = require('./test/web_server');
var esbuildConfig = require('./grunt/esbuild/build');

module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-webpack');
Expand Down Expand Up @@ -49,7 +47,6 @@ module.exports = function (grunt) {
dirs: dirs,
webpack: {
all: Object.values(webpackConfig),
node: [webpackConfig.node],
browser: [webpackConfig.browser, webpackConfig.browserMin, webpackConfig.mochaJUnitReporterBrowser],
},
};
Expand All @@ -76,11 +73,7 @@ module.exports = function (grunt) {
});
});

grunt.registerTask('build', ['checkGitSubmodules', 'webpack:all', 'build:browser']);

grunt.registerTask('build:node', ['checkGitSubmodules', 'webpack:node']);

grunt.registerTask('build:browser', ['checkGitSubmodules', 'webpack:browser']);
grunt.registerTask('build', ['checkGitSubmodules', 'webpack:all', 'build:browser', 'build:node']);

grunt.registerTask('all', ['build', 'requirejs']);

Expand All @@ -99,44 +92,26 @@ module.exports = function (grunt) {
});
});

grunt.registerTask('build:node', function () {
const done = this.async();

esbuild
.build(esbuildConfig.nodeConfig)
.then(() => {
done(true);
})
.catch((err) => {
done(err);
});
});

grunt.registerTask('build:browser', function () {
var done = this.async();

function createBaseConfig() {
return {
entryPoints: ['src/platform/web/index.ts'],
outfile: 'build/ably.js',
bundle: true,
sourcemap: true,
format: 'umd',
banner: { js: '/*' + banner + '*/' },
plugins: [umdWrapper.default({ libraryName: 'Ably', amdNamedModule: false })],
target: 'es2017',
};
}

function createModularConfig() {
return {
// We need to create a new copy of the base config, because calling
// esbuild.build() with the base config causes it to mutate the passed
// config’s `banner.js` property to add some weird modules shim code,
// which we don’t want here.
...createBaseConfig(),
entryPoints: ['src/platform/web/modular.ts'],
outfile: 'build/modular/index.mjs',
format: 'esm',
plugins: [stripLogsPlugin],
};
}

Promise.all([
esbuild.build(createBaseConfig()),
esbuild.build({
...createBaseConfig(),
outfile: 'build/ably.min.js',
minify: true,
}),
esbuild.build(createModularConfig()),
esbuild.build(esbuildConfig.webConfig),
esbuild.build(esbuildConfig.minifiedWebConfig),
esbuild.build(esbuildConfig.modularConfig),
]).then(() => {
console.log('esbuild succeeded');
done(true);
Expand Down
61 changes: 61 additions & 0 deletions grunt/esbuild/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const banner = require('../../src/fragments/license');
const umdWrapper = require('esbuild-plugin-umd-wrapper');
const stripLogsPlugin = require('./strip-logs').default;

// We need to create a new copy of the base config each time, because calling
// esbuild.build() with the base config causes it to mutate the passed
// config’s `banner.js` property to add some weird modules shim code,
// which we don’t want here.
function createBaseConfig() {
return {
bundle: true,
sourcemap: true,
format: 'umd',
banner: { js: '/*' + banner + '*/' },
plugins: [umdWrapper.default({ libraryName: 'Ably', amdNamedModule: false })],
target: 'es2017',
};
}

const webConfig = {
...createBaseConfig(),
entryPoints: ['src/platform/web/index.ts'],
outfile: 'build/ably.js',
};

const minifiedWebConfig = {
...createBaseConfig(),
outfile: 'build/ably.min.js',
minify: true,
};

const modularConfig = {
...createBaseConfig(),
entryPoints: ['src/platform/web/modular.ts'],
outfile: 'build/modular/index.js',
format: 'esm',
plugins: [stripLogsPlugin],
};

const nodeConfig = {
...createBaseConfig(),
platform: 'node',
entryPoints: ['src/platform/nodejs/index.ts'],
outfile: 'build/ably-node.js',
/*
* in order to support both named, and default exports in commonjs, esbuild
* will export named exports by name on module.exports and default exports on
* module.exports.default. Since we export everything on the default export
* object anyway, we can just ignore named exports and only export the default.
* Without this footer, you would need to require('ably').default.Realtime to
* access client constructors.
*/
footer: { js: 'module.exports = module.exports.default;' },
};

module.exports = {
webConfig,
minifiedWebConfig,
modularConfig,
nodeConfig,
};
21 changes: 0 additions & 21 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,6 @@ function platformPath(platform, ...dir) {
return path.resolve(__dirname, 'src', 'platform', platform, ...dir);
}

const nodeConfig = {
...baseConfig,
entry: {
index: platformPath('nodejs'),
},
output: {
...baseConfig.output,
filename: 'ably-node.js',
},
target: ['node', 'es2017'],
externals: {
got: true,
ws: true,
},
optimization: {
minimize: false,
},
devtool: 'source-map',
};

const nativeScriptConfig = {
...baseConfig,
output: {
Expand Down Expand Up @@ -157,7 +137,6 @@ function createMochaJUnitReporterConfig() {
}

module.exports = {
node: nodeConfig,
nativeScript: nativeScriptConfig,
reactNative: reactNativeConfig,
mochaJUnitReporterBrowser: createMochaJUnitReporterConfig(),
Expand Down

0 comments on commit 7c00f59

Please sign in to comment.