Skip to content

Commit 76ec09a

Browse files
committed
ThemeProvider, new way to build themes
1 parent 0ac8c46 commit 76ec09a

20 files changed

Lines changed: 208 additions & 41 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/lib
1+
/build
22
/node_modules
33
*.log
44
/public

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/src/opt
66
/src/paragons
77
/src/types
8+
/packages

Makefile

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,42 @@
33
JS_FILES = $(shell find ./src -name "*.js" -o -name "*.jsx")
44
PRETTIER_OPTIONS = --single-quote --trailing-comma es5 --print-width 120
55

6+
.PHONY: setup
67
setup: clean
78
yarn --pure-lockfile
89

10+
.PHONY: build-theme
11+
build-theme:
12+
./node_modules/.bin/gulp
13+
14+
.PHONY: build
915
build:
10-
yarn build
16+
make build-theme
17+
THEME=tpg make build-theme
18+
make prepare-package
1119

20+
.PHONY: clean
1221
clean:
1322
rm -rf ./node_modules
14-
rm -rf ./lib/*
23+
rm -rf ./build
24+
25+
.PHONY: prepare-package
26+
prepare-package:
27+
cp package.json README.md .npmrc postcss.config.js build/
28+
cp src/index.js build/exports.js
1529

30+
.PHONY: test
1631
test:
1732
yarn flow
1833

34+
.PHONY: fmt
1935
fmt:
2036
./node_modules/.bin/prettier $(PRETTIER_OPTIONS) --write $(JS_FILES)
2137

22-
.PHONY: setup build clean test fmt
38+
.PHONY: publish
39+
publish: build
40+
npm publish ./build --access restricted
41+
2342

2443
.PHONY: build-sg
2544
build-sg:

opt/linkfs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const methods = [
6060
const originalFs = fs;
6161
const existsSync = fs.existsSync;
6262

63-
6463
function linkfs(fs, rewrites, finalMethods = methods) {
6564
const resolvedRewrites = Object.create(null);
6665
_.each(rewrites, (value, from) => {

override-fs.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ const themeName = process.env.THEME;
99
const ReadStream = fs.ReadStream;
1010
const WriteStream = fs.WriteStream;
1111

12-
function override(themeName) {
12+
function override(themeName = 'peacock') {
1313
const linkedfs = linkfs(fs, {
1414
[path.resolve('./src')]: path.resolve(`./themes/${themeName}`)
1515
});
1616

17-
exports.linkedgs = linkedfs;
18-
1917
unionfs
2018
.use(fs)
2119
.use(linkedfs)
@@ -25,7 +23,5 @@ function override(themeName) {
2523
fs.WriteStream.prototype = WriteStream.prototype;
2624
}
2725

28-
if (themeName) {
29-
override(themeName);
30-
}
26+
override(themeName);
3127

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@foxcomm/storefront-react",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "FoxCommerce Storefront React UI Library",
55
"main": "lib/index.js",
66
"scripts": {
7-
"build": "gulp",
7+
"build": "make build",
88
"prepublish": "npm run build",
99
"flow": "flow check"
1010
},
@@ -68,6 +68,7 @@
6868
"style-loader": "^0.17.0",
6969
"through2": "^2.0.3",
7070
"unionfs": "^0.0.9",
71+
"vinyl-file": "^3.0.0",
7172
"webpack": "^3.0.0",
7273
"webpack-svgstore-plugin": "^4.0.1"
7374
},

packages/storefront-react-optimize-imports/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@foxcomm/storefront-react-optimize-imports",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Optimize imports for storefront-react package in order to get smaller bundle size",
55
"main": "src/index.js",
66
"publishConfig": {

packages/storefront-react-optimize-imports/src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const rootPackage = require(parentPackagePath);
2626
if (rootPackage.name === pkgName) {
2727
importsEntrypoint = path.join(path.dirname(parentPackagePath), 'src/index.js');
2828
} else {
29-
importsEntrypoint = path.join(path.dirname(parentPackagePath), 'node_modules', pkgName, 'src/index.js');
29+
importsEntrypoint = path.join(path.dirname(parentPackagePath), 'node_modules', pkgName, 'exports.js');
3030
}
3131

3232
const importsCode = fs.readFileSync(importsEntrypoint).toString();
@@ -60,19 +60,21 @@ function transformToComponents(importsAst) {
6060
const components = transformToComponents(importsAst);
6161
exports.components = components;
6262

63+
const pkgRe = /^\@foxcomm\/storefront-react\/[^\/]+$/;
64+
6365
function plugin(_ref) {
6466
const t = _ref.types;
6567

6668
return {
6769
visitor: {
6870
ImportDeclaration(path) {
6971
const { node } = path;
70-
if (node.source.value == pkgName) {
72+
if (pkgRe.test(node.source.value)) {
7173
const newImports = node.specifiers.map(decl => {
7274
const name = decl.imported.name;
7375
if (name in components) {
7476
const entry = components[name];
75-
const source = t.stringLiteral(`${pkgName}/lib${entry.path}`);
77+
const source = t.stringLiteral(`${node.source.value}${entry.path}`);
7678
let specifier;
7779
if (entry.isDefault) {
7880
specifier = t.importDefaultSpecifier(t.identifier(decl.local.name));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React, { Element } from 'react';
2+
// types
3+
import type { LineItem } from '@foxcomm/api-js/types/api/cord/line-items';
4+
5+
export function renderLineItemImage(lineItem: LineItem): Element<*> {
6+
return <img src={lineItem.imagePath} width="50" height="50" />;
7+
}
8+
9+
export type ThemeData = {
10+
renderLineItemImage(lineItem: LineItem): Element<*>,
11+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, { PropTypes, Component } from 'react';
2+
3+
import * as themeData from './theme-data';
4+
5+
type ContextType = {[key: string]: any};
6+
7+
type Props = {
8+
context?: ContextType,
9+
};
10+
11+
type State = {
12+
context: ContextType,
13+
};
14+
15+
class ThemeProvider extends Component {
16+
static childContextTypes = {
17+
renderLineItemImage: PropTypes.func.isRequired,
18+
};
19+
props: Props;
20+
state: State = {
21+
context: this.calcContext(this.props),
22+
};
23+
24+
calcContext(props) {
25+
return Object.assign({}, props.context || {}, themeData);
26+
}
27+
28+
componentWillReceiveProps(nextProps) {
29+
if (nextProps.context != this.props.context) {
30+
this.setState({
31+
context: this.calcContext(nextProps),
32+
});
33+
}
34+
}
35+
36+
getChildContext() {
37+
return this.state.context;
38+
}
39+
40+
render() {
41+
return this.props.children;
42+
}
43+
}
44+
45+
export default ThemeProvider;

0 commit comments

Comments
 (0)