Skip to content

Commit 6f65119

Browse files
committed
chore: Build and package the runtime helpers into a separate directory.
1 parent 9a4a329 commit 6f65119

File tree

7 files changed

+182
-16
lines changed

7 files changed

+182
-16
lines changed

packages/jsx-analyzer/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
"files": [
88
"dist"
99
],
10+
"browser": {
11+
"dist/src/index.js": "./runtime/classnames.js"
12+
},
1013
"scripts": {
1114
"pretest": "npm run build && npm run tslint",
1215
"test": "mocha dist/test --recursive --opts test/mocha.opts",
13-
"build": "rm -rf dist && tsc --jsx preserve -p tsconfig.json",
16+
"build-runtime": "tsc -p tsconfig-runtime.json",
17+
"build-buildtime": "rm -rf dist && tsc --jsx preserve -p tsconfig.json",
18+
"build": "npm run build-runtime && npm run build-buildtime",
1419
"watch": "watch 'npm run test' './src' './test' --wait=3",
1520
"tslint": "tslint --project tsconfig.json --type-check"
1621
},
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var e = function (m) { throw new Error(m); };
4+
var number = function (v) { return typeof v[0] === 'number' ? v.shift() : e('not a number: ' + (v[0] || 'undefined')); };
5+
var string = function (v) { return v.shift().toString(); };
6+
var truthyString = function (v) {
7+
var s = v.shift();
8+
if (!s && s !== 0)
9+
return;
10+
return s.toString();
11+
};
12+
var bool = function (v) { return !!v.shift(); };
13+
function c(staticClasses, stack) {
14+
if (Array.isArray(staticClasses)) {
15+
stack = staticClasses;
16+
staticClasses = '';
17+
}
18+
if (!stack) {
19+
return staticClasses;
20+
}
21+
var sources = [];
22+
var classes = [];
23+
var nSources = number(stack);
24+
var nOutputs = number(stack);
25+
var canSetSource = true;
26+
var abort = function () { return canSetSource = false; };
27+
var isSourceSet = function (n) { return sources[n]; };
28+
var setSource = function (n) { if (canSetSource)
29+
sources[n] = true; };
30+
if (staticClasses.length > 0) {
31+
classes.push(staticClasses);
32+
}
33+
while (nSources-- > 0) {
34+
sourceExpr(stack, isSourceSet, setSource, abort);
35+
canSetSource = true;
36+
}
37+
while (nOutputs-- > 0) {
38+
var c_1 = string(stack);
39+
if (boolExpr(stack, isSourceSet))
40+
classes.push(c_1);
41+
}
42+
return classes.join(' ');
43+
}
44+
exports.default = c;
45+
function sourceExpr(stack, isSourceSet, setSource, abort) {
46+
var type = number(stack);
47+
if (type & 1) {
48+
var numDeps = number(stack);
49+
while (numDeps-- > 0) {
50+
var depIndex = number(stack);
51+
if (!isSourceSet(depIndex))
52+
abort();
53+
}
54+
}
55+
if (type & 2) {
56+
if (!bool(stack))
57+
abort();
58+
}
59+
if (type & 4) {
60+
var nValues = number(stack);
61+
var ifFalsy = number(stack);
62+
var value = truthyString(stack);
63+
if (value === undefined) {
64+
switch (ifFalsy) {
65+
case 2:
66+
value = string(stack);
67+
break;
68+
case 0:
69+
e('string expected');
70+
break;
71+
case 1:
72+
abort();
73+
break;
74+
default:
75+
e('wtf');
76+
}
77+
}
78+
while (nValues-- > 0) {
79+
var matchValue = string(stack);
80+
var nSources = number(stack);
81+
while (nSources-- > 0) {
82+
value === matchValue ? setSource(number(stack)) : number(stack);
83+
}
84+
}
85+
}
86+
else if (type === 0) {
87+
var condition = bool(stack);
88+
var nTrue = number(stack);
89+
while (nTrue-- > 0) {
90+
condition ? setSource(number(stack)) : number(stack);
91+
}
92+
var nFalse = number(stack);
93+
while (nFalse-- > 0) {
94+
condition ? number(stack) : setSource(number(stack));
95+
}
96+
}
97+
else {
98+
var nSources = number(stack);
99+
while (nSources-- > 0) {
100+
setSource(number(stack));
101+
}
102+
}
103+
}
104+
function boolExpr(stack, isSourceSet) {
105+
var result;
106+
var type = number(stack);
107+
switch (type) {
108+
case -1:
109+
return !boolExpr(stack, isSourceSet);
110+
case -3:
111+
var nAnds = number(stack);
112+
result = true;
113+
while (nAnds-- > 0) {
114+
var nextResult = boolExpr(stack, isSourceSet);
115+
result = result && nextResult;
116+
}
117+
return result;
118+
case -2:
119+
var nOrs = number(stack);
120+
result = false;
121+
while (nOrs-- > 0) {
122+
var nextResult = boolExpr(stack, isSourceSet);
123+
result = result || nextResult;
124+
}
125+
return result;
126+
default:
127+
return isSourceSet(type);
128+
}
129+
}

packages/jsx-analyzer/src/helpers/classnames.ts renamed to packages/jsx-analyzer/runtime/classnames.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type IsSourceSet = (n: number) => boolean;
105105
type SetSource = (n: number) => void;
106106
type Abort = () => false;
107107

108-
export function c(staticClasses: string | any[], stack?: any[]): string {
108+
export default function c(staticClasses: string | any[], stack?: any[]): string {
109109
if (Array.isArray(staticClasses)) {
110110
stack = staticClasses;
111111
staticClasses = '';

packages/jsx-analyzer/src/transformer/babel.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
identifier,
1010
stringLiteral,
1111
importDeclaration,
12-
importSpecifier,
12+
importDefaultSpecifier,
1313
jSXAttribute,
1414
jSXExpressionContainer,
1515
jSXIdentifier,
@@ -75,7 +75,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
7575
let firstImport = this.importsToRemove.shift()!;
7676
detectStrayReferenceToImport(firstImport, this.filename);
7777
let importDecl = importDeclaration(
78-
[importSpecifier(identifier(HELPER_FN_NAME.localName), identifier(HELPER_FN_NAME.moduleName))],
78+
[importDefaultSpecifier(identifier(HELPER_FN_NAME))],
7979
stringLiteral('@css-blocks/jsx'));
8080
firstImport.replaceWith(importDecl);
8181
}
@@ -105,8 +105,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
105105
if (classMapping.dynamicClasses.length > 0) {
106106
this.dynamicStylesFound = true;
107107
attributeValue = jSXExpressionContainer(
108-
generateClassName(classMapping, elementAnalysis,
109-
HELPER_FN_NAME.localName, true));
108+
generateClassName(classMapping, elementAnalysis, HELPER_FN_NAME, true));
110109
} else if (classMapping.staticClasses.length > 0) {
111110
attributeValue = stringLiteral(classMapping.staticClasses.join(' '));
112111
}

packages/jsx-analyzer/src/transformer/classNameGenerator.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ import {
4343
Expression,
4444
} from 'babel-types';
4545

46-
export const HELPER_FN_NAME = {
47-
moduleName: 'c',
48-
localName: 'c$$',
49-
};
46+
// TODO: detect conflicts and pick an available name.
47+
export const HELPER_FN_NAME ='c$$';
5048

5149
const enum SourceExpression {
5250
ternary,
@@ -75,7 +73,7 @@ const enum BooleanExpr {
7573
and = -3,
7674
}
7775

78-
export function classnamesHelper(rewrite: IndexedClassRewrite<BlockObject>, element: JSXElementAnalysis, helpFnName = HELPER_FN_NAME.localName, includeStaticClasses = false): CallExpression {
76+
export function classnamesHelper(rewrite: IndexedClassRewrite<BlockObject>, element: JSXElementAnalysis, helpFnName = HELPER_FN_NAME, includeStaticClasses = false): CallExpression {
7977
let args: Expression[] = [ arrayExpression(constructArgs(rewrite, element)) ];
8078
let staticClassnames = rewrite.staticClasses;
8179
if (includeStaticClasses && staticClassnames.length > 0) {

packages/jsx-analyzer/test/transformer/transformer-test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as babel from 'babel-core';
44
import { StyleMapping, PluginOptionsReader, CssBlockOptions, BlockCompiler } from 'css-blocks';
55
import * as postcss from 'postcss';
66
import * as prettier from 'prettier';
7-
import { c as c$$ } from '../../src/helpers/classnames';
7+
import c$$ from '../../runtime/classnames';
88

99
import JSXAnalysis, { MetaAnalysis } from '../../src/utils/Analysis';
1010
import Transformer from '../../src';
@@ -114,7 +114,7 @@ export class Test {
114114

115115
return transform(code, analysis.getAnalysis(0)).then(res => {
116116
assert.deepEqual(minify(res.jsx.code!), minify(`
117-
import { c as c$$ } from '@css-blocks/jsx';
117+
import c$$ from '@css-blocks/jsx';
118118
import objstr from 'obj-str';
119119
let isDynamic = true;
120120
<div class={c$$("a", [1, 1, 2, isDynamic, 1, 0, 'b', 0])}></div>;`
@@ -228,7 +228,7 @@ export class Test {
228228

229229
return transform(code, analysis.getAnalysis(0)).then(res => {
230230
assert.deepEqual(minify(res.jsx.code!), minify(`
231-
import { c as c$$ } from "@css-blocks/jsx";
231+
import c$$ from "@css-blocks/jsx";
232232
import objstr from 'obj-str';
233233
234234
let dynamic = 'yellow';
@@ -275,7 +275,7 @@ export class Test {
275275
return parse(code, 'test.tsx').then((analysis: MetaAnalysis) => {
276276
return transform(code, analysis.getAnalysis(0)).then(res => {
277277
assert.equal(minify(res.jsx.code!), minify(`
278-
import { c as c$$ } from "@css-blocks/jsx";
278+
import c$$ from "@css-blocks/jsx";
279279
import objstr from "obj-str";
280280
let dynamic = "yellow";
281281
let leSigh = true;
@@ -331,7 +331,7 @@ export class Test {
331331

332332
return transform(code, analysis.getAnalysis(0)).then(res => {
333333
assert.equal(minify(res.jsx.code!), minify(`
334-
import { c as c$$ } from "@css-blocks/jsx";
334+
import c$$ from "@css-blocks/jsx";
335335
import objstr from "obj-str";
336336
let dynamic = "yellow";
337337
function conditional() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"compilerOptions": {
3+
// Compilation Configuration
4+
"target": "es5",
5+
"inlineSources": false,
6+
"inlineSourceMap": false,
7+
"declaration": false,
8+
"allowSyntheticDefaultImports": false,
9+
"removeComments": true,
10+
11+
// Environment Configuration
12+
"experimentalDecorators": false,
13+
"moduleResolution": "node",
14+
15+
// Enhance Strictness
16+
"noImplicitAny": true,
17+
"suppressImplicitAnyIndexErrors": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": false,
20+
"allowUnreachableCode": false,
21+
"strictNullChecks": true,
22+
"noImplicitReturns": true,
23+
"noImplicitThis": true,
24+
"preserveConstEnums": false,
25+
26+
// output options
27+
"newLine": "LF",
28+
"module": "commonjs",
29+
"traceResolution": false,
30+
"baseUrl": "."
31+
},
32+
"include": [
33+
"runtime/*.ts"
34+
]
35+
}

0 commit comments

Comments
 (0)