Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit c6bacf0

Browse files
committed
fix(build): close #341
issue - rollup doesn't correctly handle mix modules (import and require) files which results in inline require files not getting included in final build. - log4js uses dynamic requires fix - replace all require with import in code - remove log4js dependency. (use same Logger as used in flowLSP code) other changes - add eslint rules to prevent this from happening in future.
1 parent 7bd24ac commit c6bacf0

25 files changed

Lines changed: 293 additions & 750 deletions

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
flow-libs
2-
lib/flowNonLSP
32
build
43
rollup.config.js
54
babel.config.js

.eslintrc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
root: true,
33

4-
plugins: ['eslint-plugin-playlyfe'],
4+
plugins: ['eslint-plugin-playlyfe', 'eslint-plugin-import'],
55

66
extends: [
77
'plugin:playlyfe/js',
@@ -17,5 +17,10 @@ module.exports = {
1717
rules: {
1818
'no-negated-condition': 'off',
1919
'no-undefined': 'off',
20+
21+
/* Rules to prevent use of require in code: with rollup it's not safe to use */
22+
'global-require': 'error',
23+
'import/no-commonjs': 'error',
24+
'import/no-dynamic-require': 'error',
2025
},
2126
};

lib/common/Logger.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* @flow */
2+
import * as vscode from 'vscode';
3+
4+
const LOG_LEVEL = Object.freeze({
5+
error: 3,
6+
warn: 2,
7+
info: 1,
8+
trace: 0,
9+
debug: 0,
10+
});
11+
12+
export type LogLevel = $Keys<typeof LOG_LEVEL>;
13+
14+
// max of keys LOG_LEVEL
15+
const MAX_LEVEL_LENGTH = Object.keys(LOG_LEVEL).reduce((maxLength, level) => {
16+
if (level.length > maxLength) {
17+
return level.length;
18+
}
19+
return maxLength;
20+
}, 0);
21+
22+
export default class Logger {
23+
_outputChannel: vscode.OutputChannel;
24+
_level: LogLevel;
25+
_context: string;
26+
27+
constructor(
28+
context: string,
29+
outputChannel: vscode.OutputChannel,
30+
level: LogLevel,
31+
) {
32+
this._outputChannel = outputChannel;
33+
this._level = level;
34+
this._context = context;
35+
}
36+
37+
error(message: string, data?: mixed) {
38+
if (this._getLevelVal() <= LOG_LEVEL.error) {
39+
this._write('Error', message, data);
40+
}
41+
}
42+
43+
warn(message: string, data?: mixed) {
44+
if (this._getLevelVal() <= LOG_LEVEL.warn) {
45+
this._write('Warn', message, data);
46+
}
47+
}
48+
49+
info(message: string, data?: mixed) {
50+
if (this._getLevelVal() <= LOG_LEVEL.info) {
51+
this._write('Info', message, data);
52+
}
53+
}
54+
55+
debug(message: string, data?: mixed) {
56+
if (this._getLevelVal() <= LOG_LEVEL.debug) {
57+
this._write('Debug', message, data);
58+
}
59+
}
60+
61+
trace(message: string, data?: mixed) {
62+
if (this._getLevelVal() <= LOG_LEVEL.trace) {
63+
this._write('Trace', message, data);
64+
}
65+
}
66+
67+
_getLevelVal() {
68+
return LOG_LEVEL[this._level];
69+
}
70+
71+
_write(level: string, message: string, data?: mixed) {
72+
const levelStr = level.padEnd(MAX_LEVEL_LENGTH);
73+
const tag = [
74+
levelStr,
75+
new Date().toLocaleTimeString(),
76+
this._context ? this._context : null,
77+
]
78+
.filter(Boolean)
79+
.join(' - ');
80+
81+
let output = `[${tag}] ${message}`;
82+
if (data) {
83+
output += ' ${JSON.stringify(data)';
84+
}
85+
86+
this._outputChannel.appendLine(output);
87+
}
88+
}

lib/flowLSP/utils/Logger.js

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,3 @@
11
/* @flow */
2-
import * as vscode from 'vscode';
3-
4-
const LOG_LEVEL = Object.freeze({
5-
error: 3,
6-
warn: 2,
7-
info: 1,
8-
trace: 0,
9-
});
10-
11-
export type LogLevel = $Keys<typeof LOG_LEVEL>;
12-
13-
// max of keys LOG_LEVEL
14-
const MAX_LEVEL_LENGTH = Object.keys(LOG_LEVEL).reduce((maxLength, level) => {
15-
if (level.length > maxLength) {
16-
return level.length;
17-
}
18-
return maxLength;
19-
}, 0);
20-
21-
export default class Logger {
22-
_outputChannel: vscode.OutputChannel;
23-
_level: LogLevel;
24-
_context: string;
25-
26-
constructor(
27-
context: string,
28-
outputChannel: vscode.OutputChannel,
29-
level: LogLevel,
30-
) {
31-
this._outputChannel = outputChannel;
32-
this._level = level;
33-
this._context = context;
34-
}
35-
36-
error(message: string) {
37-
if (this._getLevelVal() <= LOG_LEVEL.error) {
38-
this._write(message, 'Error');
39-
}
40-
}
41-
42-
warn(message: string) {
43-
if (this._getLevelVal() <= LOG_LEVEL.warn) {
44-
this._write(message, 'Warn');
45-
}
46-
}
47-
48-
info(message: string) {
49-
if (this._getLevelVal() <= LOG_LEVEL.info) {
50-
this._write(message, 'Info');
51-
}
52-
}
53-
54-
trace(message: string) {
55-
if (this._getLevelVal() <= LOG_LEVEL.trace) {
56-
this._write(message, 'Trace');
57-
}
58-
}
59-
60-
_getLevelVal() {
61-
return LOG_LEVEL[this._level];
62-
}
63-
64-
_write(message: string, level: string) {
65-
const levelStr = level.padEnd(MAX_LEVEL_LENGTH);
66-
const tag = [
67-
levelStr,
68-
new Date().toLocaleTimeString(),
69-
this._context ? this._context : null,
70-
]
71-
.filter(Boolean)
72-
.join(' - ');
73-
74-
this._outputChannel.appendLine(`[${tag}] ${message}`);
75-
}
76-
}
2+
export { default } from '../../common/Logger';
3+
export type { LogLevel } from '../../common/Logger';

lib/flowLSP/utils/importFresh.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ export default function importFresh(moduleId: string) {
1313
delete require.cache[filePath];
1414
}
1515
});
16+
// NOTE: safe to use dynamic require here
17+
// eslint-disable-next-line global-require, import/no-dynamic-require
1618
return require(moduleId);
1719
}

lib/flowNonLSP/.eslintrc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
root: true,
3+
4+
parser: 'babel-eslint',
5+
6+
plugins: ['eslint-plugin-import'],
7+
8+
env: {
9+
node: true,
10+
},
11+
12+
rules: {
13+
/* Rules to prevent use of require in code: with rollup it's not safe to use */
14+
'global-require': 'error',
15+
'import/no-commonjs': 'error',
16+
'import/no-dynamic-require': 'error',
17+
},
18+
};

lib/flowNonLSP/flowLogging.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
/**
2-
* Adds a gloabl that is used inside consoleAppender.js to output console messages
3-
* to the user, instead of to the developer console.
4-
*/
1+
/* @flow */
52
import * as vscode from 'vscode';
3+
import getOutputChannel from './utils/getOutputChannel';
64

7-
export function setupLogging(context): void {
8-
const channel = vscode.window.createOutputChannel('Flow');
9-
vscode.commands.registerCommand('flow.show-output', () => channel.show());
10-
global.flowOutputChannel = channel;
5+
export function setupLogging(context: vscode.ExtensionContext): void {
6+
const channel = getOutputChannel();
117
context.subscriptions.push(channel);
128
}

lib/flowNonLSP/pkg/flow-base/lib/FlowHelpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import invariant from 'assert';
2020
import path from 'path';
2121
import os from 'os';
2222

23-
import {getLogger} from '../../nuclide-logging/lib/main';
23+
import getLogger from '../../../utils/getLogger';
2424
const logger = getLogger();
2525

2626
const flowConfigDirCache: LRUCache<string, Promise<?string>> = LRU({

lib/flowNonLSP/pkg/flow-base/lib/FlowProcess.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import os from 'os';
1616

1717
import {BehaviorSubject, Observable} from 'rxjs';
1818

19-
import {getLogger} from '../../nuclide-logging/lib/main';
19+
import getLogger from '../../../utils/getLogger';
2020
const logger = getLogger();
2121

2222
// import {track} from '../../nuclide-analytics';

lib/flowNonLSP/pkg/flow-base/lib/FlowRoot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
import {filter} from 'fuzzaldrin';
2222
import semver from 'semver';
2323

24-
import {getLogger} from '../../nuclide-logging/lib/main';
24+
import getLogger from '../../../utils/getLogger';
2525
const logger = getLogger();
2626

2727
import {

0 commit comments

Comments
 (0)