Skip to content

Commit

Permalink
Fixed raw-loader + Jest problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Aug 12, 2019
1 parent b5195a5 commit 8001b64
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"globals": {
"__DEV__": "readonly",
"__TEST__": "readonly",
"jasmine": "readonly",
"spyOn": "readonly"
}
Expand Down
1 change: 1 addition & 0 deletions flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare module 'events' {
}

declare var __DEV__: boolean;
declare var __TEST__: boolean;

declare var jasmine: {|
getEnv: () => {|
Expand Down
1 change: 1 addition & 0 deletions packages/react-devtools-core/webpack.backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
plugins: [
new DefinePlugin({
__DEV__: true,
__TEST__: false,
'process.env.DEVTOOLS_VERSION': `"${DEVTOOLS_VERSION}"`,
'process.env.GITHUB_URL': `"${GITHUB_URL}"`,
}),
Expand Down
1 change: 1 addition & 0 deletions packages/react-devtools-core/webpack.standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = {
plugins: [
new DefinePlugin({
__DEV__: false,
__TEST__: false,
'process.env.DEVTOOLS_VERSION': `"${DEVTOOLS_VERSION}"`,
'process.env.GITHUB_URL': `"${GITHUB_URL}"`,
'process.env.NODE_ENV': `"${NODE_ENV}"`,
Expand Down
3 changes: 2 additions & 1 deletion packages/react-devtools-inline/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ module.exports = {
},
plugins: [
new DefinePlugin({
__DEV__: __DEV__,
__DEV__,
__TEST__: false,
'process.env.DEVTOOLS_VERSION': `"${DEVTOOLS_VERSION}"`,
'process.env.GITHUB_URL': `"${GITHUB_URL}"`,
'process.env.NODE_ENV': `"${NODE_ENV}"`,
Expand Down
1 change: 1 addition & 0 deletions shells/browser/shared/webpack.backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
plugins: [
new DefinePlugin({
__DEV__: true,
__TEST__: false,
'process.env.DEVTOOLS_VERSION': `"${DEVTOOLS_VERSION}"`,
'process.env.GITHUB_URL': `"${GITHUB_URL}"`,
}),
Expand Down
1 change: 1 addition & 0 deletions shells/browser/shared/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
plugins: [
new DefinePlugin({
__DEV__: false,
__TEST__: false,
'process.env.DEVTOOLS_VERSION': `"${DEVTOOLS_VERSION}"`,
'process.env.GITHUB_URL': `"${GITHUB_URL}"`,
'process.env.NODE_ENV': `"${NODE_ENV}"`,
Expand Down
3 changes: 2 additions & 1 deletion shells/dev/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const config = {
},
plugins: [
new DefinePlugin({
__DEV__: __DEV__,
__DEV__,
__TEST__: false,
'process.env.GITHUB_URL': `"${GITHUB_URL}"`,
'process.env.DEVTOOLS_VERSION': `"${DEVTOOLS_VERSION}"`,
}),
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/setupEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ if (!global.hasOwnProperty('localStorage')) {

// Mimic the global we set with Webpack's DefinePlugin
global.__DEV__ = process.env.NODE_ENV !== 'production';
global.__TEST__ = process.env.NODE_ENV === 'test';
46 changes: 28 additions & 18 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
// @flow

// $FlowFixMe Cannot resolve module
import rawStyleString from '!!raw-loader!src/devtools/views/root.css'; // eslint-disable-line import/no-webpack-loader-syntax

// Flip this flag to true to enable verbose console debug logging.
export const __DEBUG__ = false;

const extractVar = varName => {
const regExp = new RegExp(`${varName}: ([0-9]+)`);
const match = rawStyleString.match(regExp);
return parseInt(match[1], 10);
};

// TRICKY
// Extracting during build time avoids a temporarily invalid state for the inline target.
// Sometimes the inline target is rendered before root styles are applied,
// which would result in e.g. NaN itemSize being passed to react-window list.
export const COMFORTABLE_LINE_HEIGHT = extractVar(
'comfortable-line-height-data'
);
export const COMPACT_LINE_HEIGHT = extractVar('compact-line-height-data');

export const TREE_OPERATION_ADD = 1;
export const TREE_OPERATION_REMOVE = 2;
export const TREE_OPERATION_REORDER_CHILDREN = 3;
Expand All @@ -45,3 +27,31 @@ export const PROFILER_EXPORT_VERSION = 4;

export const CHANGE_LOG_URL =
'https://github.com/bvaughn/react-devtools-experimental/blob/master/CHANGELOG.md';

// HACK
//
// Extracting during build time avoids a temporarily invalid state for the inline target.
// Sometimes the inline target is rendered before root styles are applied,
// which would result in e.g. NaN itemSize being passed to react-window list.
//
// We can't use the Webpack loader syntax in the context of Jest though,
// so tests need some reasonably meaningful fallback value.
let COMFORTABLE_LINE_HEIGHT = 15;
let COMPACT_LINE_HEIGHT = 10;

if (!__TEST__) {
// $FlowFixMe
const rawStyleString = require('!!raw-loader!src/devtools/views/root.css') // eslint-disable-line import/no-webpack-loader-syntax
.default;

const extractVar = varName => {
const regExp = new RegExp(`${varName}: ([0-9]+)`);
const match = rawStyleString.match(regExp);
return parseInt(match[1], 10);
};

COMFORTABLE_LINE_HEIGHT = extractVar('comfortable-line-height-data');
COMPACT_LINE_HEIGHT = extractVar('compact-line-height-data');
}

export { COMFORTABLE_LINE_HEIGHT, COMPACT_LINE_HEIGHT };

0 comments on commit 8001b64

Please sign in to comment.