Skip to content

Commit 484732d

Browse files
authored
Do a commonjs dist file for node, when using require plus example (#1287)
1 parent e3bd8c6 commit 484732d

File tree

9 files changed

+4805
-29
lines changed

9 files changed

+4805
-29
lines changed

dist/rollbar.cjs

Lines changed: 4696 additions & 0 deletions
Large diffs are not rendered by default.

dist/rollbar.min.cjs

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

dist/rollbar.min.cjs.LICENSE.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */

examples/node-require/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const http = require('http');
2+
const Rollbar = require('rollbar');
3+
const PORT = process.env.PORT || 3000;
4+
5+
// Initialize Rollbar with your configuration
6+
const rollbar = new Rollbar({
7+
accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN',
8+
environment: 'development',
9+
captureUncaught: true,
10+
captureUnhandledRejections: true,
11+
});
12+
13+
const server = http.createServer((_req, res) => {
14+
res.writeHead(200, { 'Content-Type': 'text/plain' });
15+
res.end('👋 Hello, Rollbar!\n');
16+
});
17+
18+
if (require.main === module) {
19+
server.listen(PORT, () => {
20+
console.log(`🚀 Server running on http://localhost:${PORT}`);
21+
rollbar.info('Server started!');
22+
});
23+
}

examples/node-require/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "require",
3+
"version": "1.0.0",
4+
"private": true,
5+
"main": "index.js",
6+
"dependencies": {
7+
"rollbar": "file:../rollbar.tgz"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1",
11+
"build": "node -e \"require('./index.js')\"",
12+
"start": "node index.js"
13+
},
14+
"type": "commonjs"
15+
}

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"javascript"
1616
],
1717
"license": "MIT",
18-
"main": "src/server/rollbar.cjs",
18+
"main": "dist/rollbar.cjs",
1919
"browser": "dist/rollbar.umd.min.js",
2020
"module": "src/browser/rollbar.js",
2121
"types": "./index.d.ts",
@@ -24,7 +24,7 @@
2424
"types": "./index.d.ts",
2525
"node": {
2626
"import": "./src/server/rollbar.js",
27-
"require": "./src/server/rollbar.cjs"
27+
"require": "./dist/rollbar.cjs"
2828
},
2929
"browser": {
3030
"import": "./src/browser/rollbar.js",
@@ -137,7 +137,8 @@
137137
"stackframe": "^0.2.2",
138138
"time-grunt": "^1.0.0",
139139
"webpack": "^5.98.0",
140-
"webpack-cli": "^6.0.1"
140+
"webpack-cli": "^6.0.1",
141+
"webpack-node-externals": "^3.0.0"
141142
},
142143
"scripts": {
143144
"build": "webpack --mode production",

src/server/rollbar.cjs

Lines changed: 0 additions & 5 deletions
This file was deleted.

webpack.config.cjs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
var extend = require('util')._extend;
2-
var path = require('path');
3-
var webpack = require('webpack');
4-
var TerserPlugin = require('terser-webpack-plugin');
1+
const extend = require('util')._extend;
2+
const path = require('path');
3+
const nodeExternals = require('webpack-node-externals');
4+
const TerserPlugin = require('terser-webpack-plugin');
55

6-
var outputPath = path.resolve(__dirname, 'dist');
6+
const outputPath = path.resolve(__dirname, 'dist');
77

88
// Packages that need to be transpiled to ES5
9-
var needToTranspile = ['@rrweb', 'error-stack-parser-es'].join('|');
10-
var excludePattern = new RegExp('node_modules/(?!(' + needToTranspile + ')/)');
9+
const needToTranspile = ['@rrweb', 'error-stack-parser-es'].join('|');
10+
const excludePattern = new RegExp(
11+
'node_modules/(?!(' + needToTranspile + ')/)',
12+
);
1113

12-
var uglifyPlugin = new TerserPlugin({
14+
const uglifyPlugin = new TerserPlugin({
1315
parallel: true,
1416
});
1517

16-
var snippetConfig = {
18+
const snippetConfig = {
1719
name: 'snippet',
1820
entry: {
1921
'rollbar.snippet': './src/browser/bundles/rollbar.snippet.js',
@@ -26,15 +28,15 @@ var snippetConfig = {
2628
module: {
2729
rules: [
2830
{
29-
test: /\.js$/,
31+
test: /\.m?js$/,
3032
loader: 'babel-loader',
3133
exclude: [excludePattern, /vendor/],
3234
},
3335
],
3436
},
3537
};
3638

37-
var pluginConfig = {
39+
const pluginConfig = {
3840
name: 'plugins',
3941
entry: {
4042
jquery: './src/browser/plugins/jquery.js',
@@ -55,7 +57,7 @@ var pluginConfig = {
5557
},
5658
};
5759

58-
var vanillaConfigBase = {
60+
const vanillaConfigBase = {
5961
entry: {
6062
rollbar: './src/browser/bundles/rollbar.js',
6163
},
@@ -75,7 +77,7 @@ var vanillaConfigBase = {
7577
},
7678
};
7779

78-
var UMDConfigBase = {
80+
const UMDConfigBase = {
7981
entry: {
8082
'rollbar.umd': ['./src/browser/bundles/rollbar.js'],
8183
},
@@ -99,20 +101,42 @@ var UMDConfigBase = {
99101
},
100102
};
101103

102-
var noConflictConfigBase = extend({}, UMDConfigBase);
104+
const serverCJSConfigBase = {
105+
entry: {
106+
rollbar: './src/server/rollbar.js',
107+
},
108+
output: {
109+
path: outputPath,
110+
libraryTarget: 'commonjs2',
111+
libraryExport: 'default',
112+
},
113+
target: 'node',
114+
externals: [nodeExternals()],
115+
module: {
116+
rules: [
117+
{
118+
test: /\.m?js$/,
119+
loader: 'babel-loader',
120+
exclude: [excludePattern, /vendor/],
121+
},
122+
],
123+
},
124+
};
125+
126+
const noConflictConfigBase = extend({}, UMDConfigBase);
103127
noConflictConfigBase.entry = {
104128
'rollbar.noconflict.umd': ['./src/browser/bundles/rollbar.noconflict.js'],
105129
};
106130

107-
var namedAMDConfigBase = extend({}, UMDConfigBase);
131+
const namedAMDConfigBase = extend({}, UMDConfigBase);
108132
namedAMDConfigBase.entry = {
109133
'rollbar.named-amd': namedAMDConfigBase.entry['rollbar.umd'],
110134
};
111135
namedAMDConfigBase.output = extend({}, namedAMDConfigBase.output);
112136
namedAMDConfigBase.output.library = 'rollbar';
113137
namedAMDConfigBase.output.libraryTarget = 'amd';
114138

115-
var config = [snippetConfig, pluginConfig];
139+
const config = [snippetConfig, pluginConfig];
116140

117141
function optimizationConfig(minimizer) {
118142
return {
@@ -122,7 +146,7 @@ function optimizationConfig(minimizer) {
122146
}
123147

124148
function addVanillaToConfig(webpackConfig, filename, extraPlugins, minimizer) {
125-
var vanillaConfig = extend({}, vanillaConfigBase);
149+
const vanillaConfig = extend({}, vanillaConfigBase);
126150
vanillaConfig.name = filename;
127151

128152
vanillaConfig.plugins = extraPlugins;
@@ -135,7 +159,7 @@ function addVanillaToConfig(webpackConfig, filename, extraPlugins, minimizer) {
135159
}
136160

137161
function addUMDToConfig(webpackConfig, filename, extraPlugins, minimizer) {
138-
var UMDConfig = extend({}, UMDConfigBase);
162+
const UMDConfig = extend({}, UMDConfigBase);
139163

140164
UMDConfig.plugins = extraPlugins;
141165

@@ -152,7 +176,7 @@ function addNoConflictToConfig(
152176
extraPlugins,
153177
minimizer,
154178
) {
155-
var noConflictConfig = extend({}, noConflictConfigBase);
179+
const noConflictConfig = extend({}, noConflictConfigBase);
156180

157181
noConflictConfig.plugins = extraPlugins;
158182

@@ -167,7 +191,7 @@ function addNoConflictToConfig(
167191
}
168192

169193
function addNamedAMDToConfig(webpackConfig, filename, extraPlugins, minimizer) {
170-
var AMDConfig = extend({}, namedAMDConfigBase);
194+
const AMDConfig = extend({}, namedAMDConfigBase);
171195

172196
AMDConfig.plugins = extraPlugins;
173197

@@ -178,11 +202,19 @@ function addNamedAMDToConfig(webpackConfig, filename, extraPlugins, minimizer) {
178202
webpackConfig.push(AMDConfig);
179203
}
180204

205+
function addServerCJSConfigBase(webpackConfig, filename, minimizer) {
206+
const serverConfig = extend({}, serverCJSConfigBase);
207+
serverConfig.optimization = optimizationConfig(minimizer);
208+
serverConfig.output = extend({ filename }, serverCJSConfigBase.output);
209+
webpackConfig.push(serverConfig);
210+
}
211+
181212
function generateBuildConfig(name, plugins, minimizer) {
182213
addVanillaToConfig(config, name, plugins, minimizer);
183214
addUMDToConfig(config, name, plugins, minimizer);
184215
addNamedAMDToConfig(config, name, plugins, minimizer);
185216
addNoConflictToConfig(config, name, plugins, minimizer);
217+
addServerCJSConfigBase(config, name.replace('.js', '.cjs'), minimizer);
186218
}
187219

188220
generateBuildConfig('[name].js', []);

0 commit comments

Comments
 (0)