Skip to content

Commit

Permalink
feat: debug mode for CLI, Vite, and Webpack (#1298)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Jul 23, 2023
1 parent c90ba49 commit 520ba8d
Show file tree
Hide file tree
Showing 17 changed files with 435 additions and 139 deletions.
11 changes: 11 additions & 0 deletions .changeset/stale-brooms-kick.md
@@ -0,0 +1,11 @@
---
'@linaria/babel-preset': patch
'@linaria/cli': patch
'@linaria/testkit': patch
'@linaria/utils': patch
'@linaria/vite': patch
'@linaria/webpack5-loader': patch
'linaria-website': patch
---

Debug mode for CLI, Webpack 5 and Vite. When enabled, prints brief perf report to console and information about processed dependency tree to the specified file.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,6 +10,7 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
**/linaria-debug.json

# Runtime data
pids
Expand Down
26 changes: 23 additions & 3 deletions packages/babel/src/plugins/preeval.ts
Expand Up @@ -7,6 +7,7 @@ import type { BabelFile, PluginObj } from '@babel/core';
import { createCustomDebug } from '@linaria/logger';
import type { StrictOptions } from '@linaria/utils';
import {
EventEmitter,
getFileIdx,
addIdentifierToLinariaPreval,
removeDangerousCode,
Expand All @@ -20,12 +21,14 @@ import { processTemplateExpression } from '../utils/processTemplateExpression';
export type PreevalOptions = Pick<
StrictOptions,
'classNameSlug' | 'displayName' | 'evaluate' | 'features'
>;
> & { eventEmitter: EventEmitter };

const onFinishCallbacks = new WeakMap<object, () => void>();

export default function preeval(
babel: Core,
options: PreevalOptions
): PluginObj<IPluginState> {
{ eventEmitter = EventEmitter.dummy, ...options }: PreevalOptions
): PluginObj<IPluginState & { onFinish: () => void }> {
const { types: t } = babel;
return {
name: '@linaria/babel/preeval',
Expand All @@ -38,6 +41,10 @@ export default function preeval(
const rootScope = file.scope;
this.processors = [];

const onProcessTemplateFinished = eventEmitter.pair({
method: 'preeval:processTemplate',
});

file.path.traverse({
Identifier: (p) => {
processTemplateExpression(p, file.opts, options, (processor) => {
Expand All @@ -53,15 +60,28 @@ export default function preeval(
},
});

onProcessTemplateFinished();

if (
isFeatureEnabled(options.features, 'dangerousCodeRemover', filename)
) {
log('start', 'Strip all JSX and browser related stuff');
const onCodeRemovingFinished = eventEmitter.pair({
method: 'preeval:removeDangerousCode',
});
removeDangerousCode(file.path);
onCodeRemovingFinished();
}

onFinishCallbacks.set(
this,
eventEmitter.pair({ method: 'preeval:rest-transformations' })
);
},
visitor: {},
post(file: BabelFile) {
onFinishCallbacks.get(this)?.();

const log = createCustomDebug('preeval', getFileIdx(file.opts.filename!));

if (this.processors.length === 0) {
Expand Down

0 comments on commit 520ba8d

Please sign in to comment.