@videinfra/static-website-builder 1.15.1 → 1.15.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/init/test/.env CHANGED
@@ -1,2 +1,5 @@
1
1
  HOST=https://test-global.tld
2
2
  FOO=foo-global
3
+ TYPE_BOOL_TRUE=true
4
+ TYPE_BOOL_FALSE=false
5
+ TYPE_NUMBER=123.456
@@ -38,6 +38,9 @@ exports.env = {
38
38
  'HOST': 'host',
39
39
  'FOO': 'foo',
40
40
  'BAR': 'bar',
41
+ 'TYPE_BOOL_TRUE': 'typeBoolTrue',
42
+ 'TYPE_BOOL_FALSE': 'typeBoolFalse',
43
+ 'TYPE_NUMBER': 'typeNumber',
41
44
  },
42
45
  };
43
46
 
@@ -6,4 +6,5 @@
6
6
  */
7
7
  module.exports = {
8
8
  'world': 'World',
9
+ 'hostFromGlobalJS': process.env.host,
9
10
  };
@@ -3,4 +3,19 @@
3
3
 
4
4
  {% block body %}
5
5
  <p>HOST: {{ host }}</p>
6
+ <p>HOST FROM GLOBAL JS: {{ hostFromGlobalJS }}</p>
7
+
8
+ {% if typeBoolTrue %}
9
+ <p>TYPE_BOOL_TRUE: pass</p>
10
+ {% else %}
11
+ <p>TYPE_BOOL_TRUE: fail</p>
12
+ {% endif %}
13
+
14
+ {% if typeBoolFalse %}
15
+ <p>TYPE_BOOL_FALSE: fail</p>
16
+ {% else %}
17
+ <p>TYPE_BOOL_FALSE: pass</p>
18
+ {% endif %}
19
+
20
+ <p>TYPE_NUMBER: {{ typeNumber }}</p>
6
21
  {% endblock %}
@@ -3,3 +3,6 @@ console.log('Hello from main page!');
3
3
  console.log('env.host ==', process.env.host);
4
4
  console.log('env.foo ==', process.env.foo);
5
5
  console.log('env.bar ==', process.env.bar);
6
+ console.log('env.typeBoolTrue ==', process.env.typeBoolTrue);
7
+ console.log('env.typeBoolFalse ==', process.env.typeBoolFalse);
8
+ console.log('env.typeNumber ==', process.env.typeNumber);
@@ -1,3 +1,27 @@
1
1
  .env-test:before {
2
- content: $env-host;
2
+ content: map-get($env, host);
3
+ }
4
+
5
+ :root {
6
+ #{ --env-test-type-number }: map-get($env, type-number);
7
+ }
8
+
9
+ @if map-get($env, type-bool-true) {
10
+ :root {
11
+ --env-test-bool-true: true;
12
+ }
13
+ } @else {
14
+ :root {
15
+ --env-test-bool-true: false;
16
+ }
17
+ }
18
+
19
+ @if map-get($env, type-bool-false) {
20
+ :root {
21
+ --env-test-bool-false: true;
22
+ }
23
+ } @else {
24
+ :root {
25
+ --env-test-bool-false: false;
26
+ }
3
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@videinfra/static-website-builder",
3
- "version": "1.15.1",
3
+ "version": "1.15.3",
4
4
  "description": "Customizable static site project builder",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -16,6 +16,11 @@ function getData () {
16
16
  const loaders = getConfig.getTaskConfig('data', 'loaders');
17
17
  const ignore = getConfig.getTaskConfig('data', 'ignore');
18
18
  const group = getConfig.getTaskConfig('data', 'groupByFileName');
19
+ const envData = getEnvData();
20
+
21
+ // Merge into process.env before loading data because these may be used
22
+ // in data
23
+ merge(process.env, envData.env);
19
24
 
20
25
  const data = reduce(folders, (data, folder) => {
21
26
  getFileNamesSync(folder).forEach(fileName => {
@@ -62,7 +67,7 @@ function getData () {
62
67
  }, {});
63
68
 
64
69
  // Merge with env variables
65
- return merge(data, getEnvData().twig);
70
+ return merge(data, envData.twig);
66
71
  }
67
72
 
68
73
 
@@ -3,7 +3,7 @@ const dotenv = require('dotenv');
3
3
  const getConfig = require('../../lib/get-config');
4
4
 
5
5
  function escapeJSVariable (value) {
6
- if (value === true || value === false || !isNaN(value)) {
6
+ if (value === 'true' || value === 'false' || value === true || value === false || !isNaN(value)) {
7
7
  return value;
8
8
  } else {
9
9
  // Convert to string
@@ -11,15 +11,27 @@ function escapeJSVariable (value) {
11
11
  }
12
12
  }
13
13
 
14
+ function normalizeTwigVariable (value) {
15
+ if (value === 'true') {
16
+ return true;
17
+ } else if (value === 'false') {
18
+ return false;
19
+ } else if (value !== '' && !isNaN(value)) {
20
+ return parseFloat(value);
21
+ } else {
22
+ return value;
23
+ }
24
+ }
25
+
14
26
  function getEnvData () {
15
27
  const envVariables = {};
16
28
  const twigVariables = {};
17
- const scssVariables = {};
29
+ const scssVariables = { env: {} };
18
30
  const jsVariables = {};
31
+ const envOutVariables = {};
19
32
 
20
33
  const envFiles = paths.getPathConfig().env.map((path) => paths.getProjectPath(path));
21
34
 
22
-
23
35
  dotenv.config({
24
36
  // dotenv file order is reversed, values in first file overwrite all other
25
37
  // file values
@@ -35,9 +47,10 @@ function getEnvData () {
35
47
  const value = envVariables[key];
36
48
  const camelCase = map[key];
37
49
  const kebabCase = map[key].replace(/([a-z])([A-Z])/g, '$1-$2').replace(/_([a-z])/ig, '-$1').toLowerCase();
38
- twigVariables[camelCase] = value;
50
+ twigVariables[camelCase] = normalizeTwigVariable(value);
51
+ envOutVariables[camelCase] = value;
39
52
  jsVariables[`process.env.${ camelCase }`] = escapeJSVariable(value);
40
- scssVariables[`env-${ kebabCase }`] = value;
53
+ scssVariables.env[kebabCase] = value;
41
54
  }
42
55
  });
43
56
 
@@ -45,6 +58,7 @@ function getEnvData () {
45
58
  twig: twigVariables,
46
59
  sass: scssVariables,
47
60
  js: jsVariables,
61
+ env: envOutVariables,
48
62
  }
49
63
  }
50
64
 
@@ -99,16 +99,31 @@ test('.env and .env.local files loaded', () => {
99
99
  expect(js.indexOf('console.log("env.host ==","https://test-local.tld")')).not.toBe(-1);
100
100
  expect(js.indexOf('console.log("env.foo ==","foo-global")')).not.toBe(-1);
101
101
  expect(js.indexOf('console.log("env.bar ==","bar-local")')).not.toBe(-1);
102
+ expect(js.indexOf('console.log("env.typeBoolTrue ==",!0)')).not.toBe(-1);
103
+ expect(js.indexOf('console.log("env.typeBoolFalse ==",!1)')).not.toBe(-1);
104
+ expect(js.indexOf('console.log("env.typeNumber ==",123.456)')).not.toBe(-1);
102
105
  }),
103
106
  fsPromises.readFile(path.resolve(publicPath, 'env.html'), {'encoding': 'utf8'}).then((html) => {
104
107
  expect(html.indexOf('<p>HOST: https://test-local.tld</p>')).not.toBe(-1);
108
+ expect(html.indexOf('<p>TYPE_BOOL_TRUE: pass</p>')).not.toBe(-1);
109
+ expect(html.indexOf('<p>TYPE_BOOL_FALSE: pass</p>')).not.toBe(-1);
110
+ expect(html.indexOf('<p>TYPE_NUMBER: 123.456</p>')).not.toBe(-1);
105
111
  }),
106
112
  fsPromises.readFile(path.resolve(publicPath, 'assets/stylesheets/env-test.css'), {'encoding': 'utf8'}).then((css) => {
107
113
  expect(css.indexOf('.env-test:before{content:"https://test-local.tld"}')).not.toBe(-1);
114
+ expect(css.indexOf('--env-test-bool-true:true')).not.toBe(-1);
115
+ expect(css.indexOf('--env-test-bool-false:false')).not.toBe(-1);
116
+ expect(css.indexOf('--env-test-type-number:123.456')).not.toBe(-1);
108
117
  }),
109
118
  ]);
110
119
  });
111
120
 
121
+ test('process.env available in html/data', () => {
122
+ return fsPromises.readFile(path.resolve(publicPath, 'env.html'), {'encoding': 'utf8'}).then((html) => {
123
+ expect(html.indexOf('<p>HOST FROM GLOBAL JS: https://test-local.tld</p>')).not.toBe(-1);
124
+ });
125
+ });
126
+
112
127
  test('icons generated', () => {
113
128
  return fsPromises.readFile(path.resolve(publicPath, 'assets/images/icons.svg'), {'encoding': 'utf8'}).then((svg) => {
114
129
  expect(svg.indexOf('<symbol id="example-arrow">')).not.toBe(-1);
@@ -0,0 +1,24 @@
1
+ const sassStingify = require('../vendor/gulp-sass/sass-stringify');
2
+
3
+ test('SASS stringify boolean', () => {
4
+ expect(sassStingify(true)).toEqual('true');
5
+ expect(sassStingify(false)).toEqual('false');
6
+ });
7
+
8
+ test('SASS stringify number', () => {
9
+ expect(sassStingify(0)).toEqual('0');
10
+ expect(sassStingify(-123)).toEqual('-123');
11
+ expect(sassStingify(123.456)).toEqual('123.456');
12
+ });
13
+
14
+ test('SASS stringify falsy', () => {
15
+ expect(sassStingify(null)).toEqual('false');
16
+ expect(sassStingify(undefined)).toEqual('false');
17
+ });
18
+
19
+ test('SASS stringify object', () => {
20
+ const obj1 = {env: {key1: 'value1', key2: null, key3: undefined, arr: ['a', 123, true] }};
21
+ const out1 = "$env: (key1: 'value1', key2: false, key3: false, arr: ('a',123,true));";
22
+
23
+ expect(sassStingify(obj1)).toEqual(out1);
24
+ });
@@ -9,6 +9,7 @@ const replaceExtension = require('replace-ext');
9
9
  const stripAnsi = require('strip-ansi');
10
10
  const clonedeep = require('lodash.clonedeep');
11
11
  const applySourceMap = require('vinyl-sourcemaps-apply');
12
+ const sassStingify = require('./sass-stringify')
12
13
 
13
14
  const PLUGIN_NAME = 'gulp-sass';
14
15
 
@@ -129,19 +130,7 @@ const gulpSass = (options, sync) => {
129
130
 
130
131
  // Stringiyfy variables
131
132
  if (options.data) {
132
- const scssVariables = [];
133
-
134
- if (typeof options.data === 'string') {
135
- // Assume it's valid SCSS
136
- scssVariables.push(options.data);
137
- } else {
138
- for (let key in options.data) {
139
- const value = escapeSCSSVariable(options.data[key]);
140
- scssVariables.push(`$${key}: ${value};`);
141
- }
142
- }
143
-
144
- opts.data = scssVariables.join('') + opts.data;
133
+ opts.data = sassStingify(options.data) + opts.data;
145
134
  }
146
135
 
147
136
  // We set the file path here so that libsass can correctly resolve import paths
@@ -0,0 +1,32 @@
1
+ module.exports = function sassStingify (data, isDeep = false) {
2
+ if (data === null || data === undefined) {
3
+ return 'false';
4
+ } else if (data !== '' && (data === 'true' || data === 'false' || data === true || data === false || !isNaN(data))) {
5
+ // Convert to simple value
6
+ return String(data);
7
+ } else if (Array.isArray(data)) {
8
+ const out = data.map((item) => sassStingify(item, true));
9
+ return `(${ out })`;
10
+ } else if (data && typeof data === 'object') {
11
+ const out = [];
12
+ for (let key in data) {
13
+ out.push(`${key}: ${sassStingify(data[key], true)}`);
14
+ }
15
+
16
+ if (out.length) {
17
+ if (isDeep) {
18
+ return `(${ out.join(', ') })`;
19
+ } else {
20
+ return `$${ out.join(';$') };`;
21
+ }
22
+ } else {
23
+ return '';
24
+ }
25
+ } else {
26
+ // Convert to string
27
+ return "'" + data.toString()
28
+ .replace(/\\/g, '\\\\')
29
+ .replace(/'/g, '\\\'')
30
+ .replace(/\n/g, '\\n') + "'";
31
+ }
32
+ };