Skip to content

Commit

Permalink
Rename process to internalProcess (#270)
Browse files Browse the repository at this point in the history
* Rename process to internalProcess

This fixes GH-268. Webpack has issues with the process variable and by
renaming the internal usage we can ensure that it points to our
representation and not the external one.

* Ensure rollup respects the symbol change
  • Loading branch information
tbranyen committed Apr 14, 2022
1 parent e830996 commit 2ada15a
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 62 deletions.
7 changes: 0 additions & 7 deletions packages/diffhtml-components/lib/util/process.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/diffhtml/lib/node/create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import process from '../util/process';
import internalProcess from '../util/process';
import globalThis from '../util/global';
import {
NodeCache,
Expand All @@ -23,7 +23,7 @@ const namespace = 'http://www.w3.org/2000/svg';
* @return {ValidNode | null} A DOM Node matching the vTree
*/
export default function createNode(vTreeLike, ownerDocument = globalThis.document, isSVG) {
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (!vTreeLike) {
throw new Error('Missing VTree when trying to create DOM Node');
}
Expand Down
6 changes: 3 additions & 3 deletions packages/diffhtml/lib/tasks/sync-trees.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import syncTree from '../tree/sync';
import createNode from '../node/create';
import { StateCache, NODE_TYPE, PATCH_TYPE, EMPTY, Mount } from '../util/types';
import process from '../util/process';
import internalProcess from '../util/process';
import Transaction from '../transaction';

export default function syncTrees(/** @type {Transaction} */ transaction) {
const { state, state: { measure }, oldTree, newTree, mount } = transaction;

measure('sync trees');

if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (!oldTree) {
throw new Error('Missing old tree during synchronization');
}
Expand All @@ -30,7 +30,7 @@ export default function syncTrees(/** @type {Transaction} */ transaction) {
) {
// If there is no `parentNode` for the replace operation, we will need to
// throw an error and prevent the `StateCache` from being updated.
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (!/** @type {HTMLElement} */ (mount).parentNode) {
throw new Error('Unable to replace top level node without a parent');
}
Expand Down
6 changes: 3 additions & 3 deletions packages/diffhtml/lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
EMPTY,
} from './util/types';
import makeMeasure from './util/make-measure';
import process from './util/process';
import internalProcess from './util/process';
import { protectVTree, gc } from './util/memory';
import globalThis from './util/global';
import schedule from './tasks/schedule';
Expand Down Expand Up @@ -82,7 +82,7 @@ export default class Transaction {
* @param {Transaction} transaction
*/
static assert(transaction) {
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (typeof transaction.mount !== 'object' || !transaction.mount) {
throw new Error('Transaction requires a DOM Node mount point');
}
Expand Down Expand Up @@ -157,7 +157,7 @@ export default class Transaction {
* @return {Promise<Transaction> | unknown}
*/
start() {
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
Transaction.assert(this);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/diffhtml/lib/transition.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import process from './util/process';
import internalProcess from './util/process';
import {
TransitionCache,
NodeCache,
Expand All @@ -15,7 +15,7 @@ import {
* @return {void}
*/
export function addTransitionState(stateName, callback) {
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (!TransitionStateNames.includes(stateName)) {
throw new Error(`Invalid state name '${stateName}'`);
}
Expand All @@ -35,7 +35,7 @@ export function addTransitionState(stateName, callback) {
* @return {void}
*/
export function removeTransitionState(stateName, callback) {
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
// Only validate the stateName if the caller provides one.
if (stateName && !TransitionStateNames.includes(stateName)) {
throw new Error(`Invalid state name '${stateName}'`);
Expand Down
4 changes: 2 additions & 2 deletions packages/diffhtml/lib/tree/sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Transaction from '../transaction';
import process from '../util/process';
import internalProcess from '../util/process';
import {
SyncTreeHookCache,
TransactionState,
Expand Down Expand Up @@ -186,7 +186,7 @@ export default function syncTree(
const vTree = nodes[i];

if (vTree.key) {
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (map.has(vTree.key)) {
throw new Error(`Key: ${vTree.key} cannot be duplicated`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/diffhtml/lib/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ParseHookCache,
Middleware,
} from './util/types';
import process from './util/process';
import internalProcess from './util/process';
import Internals from './util/internals';

const { isArray } = Array;
Expand Down Expand Up @@ -45,7 +45,7 @@ export default function use(middleware) {
const isFunction = typeof middleware === 'function';
const isObject = typeof middleware === 'object';

if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (!middleware || (!isFunction && !isObject) || isArray(middleware)) {
throw new Error('Middleware must be a function or plain object');
}
Expand Down
8 changes: 4 additions & 4 deletions packages/diffhtml/lib/util/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import process from './process';
import internalProcess from './process';
import { Config } from './types';
import globalThis from './global';

Expand Down Expand Up @@ -59,7 +59,7 @@ export default function getConfig(name, defaultValue, type = typeof defaultValue
const hasSearchParams = typeof URLSearchParams !== 'undefined';
const hasLocation = typeof location !== 'undefined';
const useSearchParams = hasSearchParams && hasLocation;
const useEnv = typeof process !== 'undefined' && process.env;
const useEnv = internalProcess.env;

// Allow bypassing any lookups if overrides are passed and match the config
// being looked up.
Expand All @@ -84,8 +84,8 @@ export default function getConfig(name, defaultValue, type = typeof defaultValue

// Try environment variables.
const upperKey = keyName.toUpperCase();
if (useEnv && upperKey in process.env) {
return formatValue(process.env[upperKey.toUpperCase()], type);
if (useEnv && upperKey in internalProcess.env) {
return formatValue(internalProcess.env[upperKey.toUpperCase()], type);
}

return defaultValue;
Expand Down
4 changes: 2 additions & 2 deletions packages/diffhtml/lib/util/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import escape from './escape';
import makeMeasure from './make-measure';
import * as memory from './memory';
import Pool from './pool';
import process from './process';
import internalProcess from './process';
import { globalConfig } from './config';
import {
PATCH_TYPE,
Expand Down Expand Up @@ -41,7 +41,7 @@ export default /** @type {Internals} */ ({
makeMeasure,
memory,
Pool,
process,
process: internalProcess,
PATCH_TYPE,
globalConfig,

Expand Down
12 changes: 6 additions & 6 deletions packages/diffhtml/lib/util/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import createTree from '../tree/create';
import getConfig from './config';
import process from './process';
import internalProcess from './process';
import { VTree, Supplemental, TransactionConfig, ParserConfig, EMPTY, NODE_TYPE } from './types';

// Magic token used for interpolation.
Expand Down Expand Up @@ -199,7 +199,7 @@ const HTMLElement = (nodeName, rawAttrs, supplemental, options) => {
assign(attributes, newName);
}
else {
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
throw new Error('Arrays cannot be spread as attributes');
}
}
Expand Down Expand Up @@ -291,7 +291,7 @@ export default function parse(html, supplemental, options = {}) {
let currentParent = root;
let lastTextPos = -1;

if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
const markup = [html];

if (!html.includes('<') && options.parser.strict) {
Expand Down Expand Up @@ -389,7 +389,7 @@ Possibly invalid markup. Opening tag was not properly closed.
const closeMarkup = `</${name}>`;
const index = html.indexOf(closeMarkup, tagEx.lastIndex);

if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (index === -1 && options.parser.strict && !selfClosing.has(name)) {
// Find a subset of the markup passed in to validate.
const markup = html
Expand Down Expand Up @@ -438,7 +438,7 @@ Possibly invalid markup. Opening tag was not properly closed.
}

if (bypassMatch || isClosingMatch || selfClosingMatch || selfClosing.has(name)) {
if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if (currentParent && name !== currentParent.rawNodeName && options.parser.strict) {
const nodeName = currentParent.rawNodeName;

Expand Down Expand Up @@ -518,7 +518,7 @@ Possibly invalid markup. Opening tag was not properly closed.
// Find any last remaining text after the parsing completes over tags.
const remainingText = html.slice(lastTextPos === -1 ? 0 : lastTextPos);

if (process.env.NODE_ENV !== 'production') {
if (internalProcess.env.NODE_ENV !== 'production') {
if ((remainingText.includes('>') || remainingText.includes('<')) && options.parser.strict) {
// Find a subset of the markup passed in to validate.
const markup = [remainingText];
Expand Down
100 changes: 78 additions & 22 deletions packages/diffhtml/package-lock.json

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

2 changes: 1 addition & 1 deletion packages/diffhtml/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-hypothetical": "^2.1.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"@rollup/plugin-replace": "^4.0.0",
"rollup-plugin-visualizer": "^5.6.0",
"rollup-watch": "^4.3.1",
"sinon": "^13.0.1",
Expand Down
Loading

0 comments on commit 2ada15a

Please sign in to comment.