@zohodesk/react-cli 0.0.1-exp.160.3 → 0.0.1-exp.162.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _webpackSources = require("webpack-sources");
9
+
10
+ var _utils = require("../utils");
11
+
12
+ var _path = _interopRequireDefault(require("path"));
13
+
14
+ var _fs = require("fs");
15
+
16
+ var _getI18nFileUrlPathTemplate = require("./I18nSplitPlugin/utils/getI18nFileUrlPathTemplate");
17
+
18
+ var _I18nKeysIdentifer = _interopRequireDefault(require("./I18nSplitPlugin/I18nKeysIdentifer"));
19
+
20
+ var _replaceCssDirTemplate = require("./RtlSplitPlugin/replaceCssDirTemplate");
21
+
22
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
+
24
+ // import { getShortI18nAssets } from './I18nSplitPlugin/utils/hashUtils';
25
+ const cliOptions = (0, _utils.getOptions)();
26
+ const {
27
+ i18n: {
28
+ chunkSplitEnable
29
+ },
30
+ css: {
31
+ enableRTLSplit
32
+ },
33
+ efc: efcOptios
34
+ } = cliOptions;
35
+ const {
36
+ createSDkFile
37
+ } = efcOptios; // for smap relate changes
38
+ // CdnChangePlugin
39
+ // SourceMapHookPlugin
40
+
41
+ class EFCTemplatePlugin {
42
+ constructor(options = {}) {
43
+ // console.log(' templateFilePath ', process.cwd(), options.templateFilePath, templateFilePath);
44
+ this.templateFilePath = _path.default.join(process.cwd(), options.templateFilePath); // console.log(' templateFilePath ', this.templateFilePath);
45
+
46
+ this.i18nFileNameTemplate = options.i18nFileNameTemplate;
47
+ this.publicPath = options.publicPath; // NOTE: this logic may be needed for i18n splited file name with contenthash cases
48
+ // this.i18nManifestFileName = options.i18nManifestFileName;
49
+ // IMPORTANT: here we mergeing optionds from pacakge.json and options via constructor
50
+ // So when debugging consider this as well
51
+ // this.options = Object.assign({}, efcOptios, options);
52
+
53
+ this.entryPointName = options.entryPointName || 'efc';
54
+ this.outputFile = efcOptios.outputFile.replace('[version]', efcOptios.version);
55
+ } // NOTE: this logic may be needed for i18n splited file name with contenthash cases
56
+ // getI18nManifest(compilation) {
57
+ // const i18nManifestFile =
58
+ // compilation.assets[this.options.i18nManifestFileName];
59
+ // if (i18nManifestFile) {
60
+ // const manifest = JSON.parse(i18nManifestFile.source());
61
+ // return manifest;
62
+ // }
63
+ // return {};
64
+ // }
65
+
66
+
67
+ getI18nAssetsStr(entryPoint, compilation) {
68
+ // NOTE: we have used lang variable inside
69
+ if (!chunkSplitEnable) {
70
+ let i18nAsstes = {};
71
+ let i18nFiles = Object.keys(compilation.assets).filter(assert => assert.indexOf(`i18n${_path.default.sep}`) !== -1); //hook for i18n url contains i18n/ may cause problem
72
+
73
+ i18nAsstes = i18nFiles.reduce((res, i18nFilePath) => {
74
+ let fileName = i18nFilePath.replace(`i18n${_path.default.sep}`, '');
75
+ let splittedFileName = fileName.split('.');
76
+ res[splittedFileName[0]] = i18nFilePath;
77
+ return res;
78
+ }, {});
79
+ return `[${JSON.stringify(i18nAsstes)}[lang]]`;
80
+ }
81
+
82
+ let initalI18nAssets = entryPoint.chunks.filter(chunk => _I18nKeysIdentifer.default.isChunkHasI18n(chunk)).map(chunk => (0, _getI18nFileUrlPathTemplate.getI18nFileUrlPathTemplate)(compilation, chunk, this.i18nFileNameTemplate, '@locale@'));
83
+ return `${JSON.stringify(initalI18nAssets)}.map(urlpath => urlpath.replace(/@locale@/g, lang))`;
84
+ }
85
+
86
+ templateReplacer(entryPoint, compilation) {
87
+ const cssDirTemplate = '@dir@'; // const i18nManifest = this.getI18nManifest(compilation);
88
+
89
+ const initialFiles = entryPoint.getFiles();
90
+ const filteredInitialFiles = initialFiles.filter(file => /\.(css|js)$/.test(file));
91
+ const initialJsFiles = filteredInitialFiles.filter(file => /\.js$/.test(file));
92
+ let initialCssFiles = filteredInitialFiles.filter(file => /\.css$/.test(file));
93
+
94
+ if (enableRTLSplit) {
95
+ initialCssFiles = initialCssFiles.map(filePath => (0, _replaceCssDirTemplate.replaceCssDirTemplate)(filePath, cssDirTemplate));
96
+ } // const i18nAssets = getShortI18nAssets(
97
+ // entryPoint.chunks,
98
+ // i18nManifest,
99
+ // '' // i18nJsPath
100
+ // );
101
+
102
+
103
+ const initalI18nAssets = this.getI18nAssetsStr(entryPoint, compilation, '' // i18nJsPath
104
+ );
105
+ let templateStr = (0, _fs.readFileSync)(this.templateFilePath).toString();
106
+ return templateStr.replace('((\'getInitalAssets\'))', `function getInitalAssets(assetsType, lang) {
107
+ if (assetsType === "js") {
108
+ return ${JSON.stringify(initialJsFiles)}
109
+ }
110
+ if (assetsType === "css") {
111
+ return ${JSON.stringify(initialCssFiles)}
112
+ }
113
+ if (assetsType === "i18n") {
114
+ return ${initalI18nAssets};
115
+ }
116
+ return [];
117
+ }`).replace('((\'@dir@\'))', '\'@dir@\'').replace('((\'publicPath\'))', this.publicPath).replace('((\'@locale@\'))', '\'@locale@\'');
118
+ }
119
+
120
+ apply(compiler) {
121
+ if (!createSDkFile) {
122
+ return;
123
+ }
124
+
125
+ compiler.hooks.emit.tap('EFCTemplatePlugin', compilation => {
126
+ const {
127
+ entryPointName,
128
+ outputFile
129
+ } = this;
130
+ const entryPoint = compilation.entrypoints.get(entryPointName);
131
+
132
+ if (!entryPoint) {
133
+ return;
134
+ }
135
+
136
+ if (!(0, _fs.existsSync)(this.templateFilePath)) {
137
+ console.error(`EFC Template file not exists ${this.templateFilePath}`);
138
+ return;
139
+ }
140
+
141
+ const source = new _webpackSources.RawSource(this.templateReplacer(entryPoint, compilation));
142
+ compilation.assets[outputFile] = source; // console.log('EFCTemplatePlugin working');
143
+ // eslint-disable-next-line no-console
144
+
145
+ console.log('The EFC embedded code was created successfully..!!!');
146
+ });
147
+ }
148
+
149
+ }
150
+
151
+ exports.default = EFCTemplatePlugin;
@@ -21,6 +21,14 @@ let filenameParser = filename => {
21
21
  } = _path.default.parse(hashedName);
22
22
 
23
23
  let nameparts = name.split('.');
24
+
25
+ if (ext === '.css' && /^(rtl|ltr)$/.test(nameparts[nameparts.length - 1])) {
26
+ return {
27
+ name: nameparts.slice(0, nameparts.length - 2).join('.') + ext,
28
+ hashedName
29
+ };
30
+ }
31
+
24
32
  return {
25
33
  name: nameparts.slice(0, nameparts.length - 1).join('.') + ext,
26
34
  hashedName
@@ -18,6 +18,12 @@ var _default = {
18
18
  value: null,
19
19
  cli: 'ssl_cert_url'
20
20
  },
21
+ preprocessor: {
22
+ runner: {
23
+ value: '',
24
+ cli: 'preprocessor'
25
+ }
26
+ },
21
27
  i18n: {
22
28
  chunkSplitEnable: {
23
29
  value: false,
@@ -42,11 +48,14 @@ var _default = {
42
48
  nameScope: 'ZOHODESK',
43
49
  version: 'default',
44
50
  outputFile: 'zohodesk-efc-sdk-[version].js',
51
+ templateFilePath: '',
45
52
  localeAttr: 'data-efc-locale',
46
53
  localeDefaultValue: 'en_US',
47
54
  localeStatement: null,
48
55
  cssDirAttr: 'data-efc-dir',
49
56
  cssDirDefaultValue: 'ltr',
57
+ cdnStatement: '',
58
+ // cdnStatement: "new URL(document.querySelector(\"script[src*='zohodesk-efc-sdk']\").src).origin",
50
59
  cssDirStatement: null
51
60
  },
52
61
  app: {
@@ -40,6 +40,13 @@ var _default = (unique = true, {
40
40
  filenames,
41
41
  packages
42
42
  }, classNamePrefix) => (context, localIdentName, localName) => {
43
+ // NOTE: in build macine we use date as folder path.
44
+ // So every time we create new build there is path will alway different
45
+ // in order to minmaze that problem we try in relative path;
46
+ // console.log('context.resourcePath', context.resourcePath, context);
47
+ // let contextResourcePath = context.resourcePath;
48
+ let relativePath = _path.default.relative(context.rootContext, context.resourcePath);
49
+
43
50
  if (context.resourcePath.endsWith('.plain.css')) {
44
51
  return localName;
45
52
  }
@@ -47,7 +54,7 @@ var _default = (unique = true, {
47
54
 
48
55
 
49
56
  if (unique) {
50
- let h = (0, _getHash.default)(context.resourcePath + localName, 10);
57
+ let h = (0, _getHash.default)(`${relativePath}-${localName}`, 10);
51
58
  return `${classNamePrefix}${h}`;
52
59
  }
53
60
  /* old production mode end*/
@@ -60,7 +67,7 @@ var _default = (unique = true, {
60
67
  //let local = localName.toLowerCase()
61
68
 
62
69
  if (isSelectorPackage(context.resourcePath, packages) || filenames.indexOf(cleanFileName) !== -1) {
63
- let h = (0, _getHash.default)(context.resourcePath + localName, 10);
70
+ let h = (0, _getHash.default)(`${relativePath}-${localName}`, 10);
64
71
  return `${classNamePrefix}${h}`;
65
72
  }
66
73
 
@@ -22,7 +22,7 @@ var _default = (type = 'git', cwd = process.cwd()) => {
22
22
  });
23
23
  }
24
24
 
25
- let [currentBranch] = results.output.filter(d => d);
25
+ let [currentBranch] = results && results.output ? results.output.filter(d => d) : [''];
26
26
  return currentBranch.replace(/(\r\n|\n|\r)/gm, '');
27
27
  };
28
28
 
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
+ var _child_process = require("child_process");
9
+
8
10
  var _fs = _interopRequireDefault(require("fs"));
9
11
 
10
12
  var _path = _interopRequireDefault(require("path"));
@@ -13,6 +15,9 @@ var _schemas = _interopRequireDefault(require("../schemas"));
13
15
 
14
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
17
 
18
+ // import { argv } from 'process';
19
+ let args = process.argv.slice(2); // console.log('argv', argv);
20
+
16
21
  const NPM_CONFIG_PREFIX = 'npm_config_'; // TODO: we have to do option parse logic little better
17
22
  // if user use react-cli stritly without using npm scripts options won't work,
18
23
  // So we have to write our own option parse logic or some other library
@@ -37,6 +42,51 @@ keysWithColon.forEach(key => {
37
42
  processEnv[nkey] = processEnv[key];
38
43
  });
39
44
 
45
+ function getNpmVersion() {
46
+ // require("child_process").
47
+ let vers = '6';
48
+
49
+ try {
50
+ [vers] = (0, _child_process.execSync)('npm -v').toString().trim().split('.');
51
+ } catch (error) {
52
+ console.log(error);
53
+ }
54
+
55
+ return vers;
56
+ }
57
+
58
+ function getCWD() {
59
+ // require("child_process").
60
+ let cwd = process.cwd();
61
+
62
+ try {
63
+ let ress = (0, _child_process.execSync)('npm bin').toString();
64
+ let i = ress.lastIndexOf('node_modules');
65
+ cwd = i === -1 ? cwd : ress.slice(0, ress.lastIndexOf('node_modules'));
66
+ } catch (error) {
67
+ console.log(error);
68
+ }
69
+
70
+ return cwd;
71
+ }
72
+
73
+ args.forEach(option => {
74
+ if (/^--./.test(option)) {
75
+ let equIndex = option.indexOf('='); // equIndex = equIndex === -1 ? option.length : equIndex;
76
+
77
+ let key = option.slice(2, equIndex);
78
+ let value = option.slice(equIndex + 1);
79
+
80
+ if (equIndex === -1) {
81
+ key = option.slice(2);
82
+ value = true;
83
+ }
84
+
85
+ key = key.replace(/-/g, '_').replace(/:/g, '_');
86
+ processEnv[key] = value;
87
+ }
88
+ });
89
+
40
90
  let defaulter = (target, source) => {
41
91
  let defaultObject = {};
42
92
  Object.keys(target).forEach(key => {
@@ -88,6 +138,8 @@ let getOptions = () => {
88
138
  }
89
139
 
90
140
  let options = defaulter(_schemas.default, userSchemas || {});
141
+ options.npmVersion = getNpmVersion();
142
+ options.cwd = getCWD();
91
143
  options.packageVersion = process.env.npm_package_version;
92
144
  global.reactCLIOptions = options;
93
145
  return options;
@@ -39,15 +39,14 @@ let fileHandler = {
39
39
  }
40
40
  },
41
41
  writeFile: (path, data) => {
42
- let jsonStr = JSON.stringify(data);
43
- fs.writeFileSync(path, jsonStr);
44
- return jsonStr;
42
+ fs.writeFileSync(path, JSON.stringify(data));
43
+ return JSON.stringify(data);
45
44
  }
46
45
  };
47
46
  exports.fileHandler = fileHandler;
48
47
 
49
48
  let jsonConcate = (receiverObj, senterObj) => {
50
- Object.keys(senterObj).forEach(key => {
49
+ Object.keys(senterObj).map(key => {
51
50
  if (Object.prototype.hasOwnProperty.call(receiverObj, key)) {
52
51
  if (receiverObj[key].constructor === {}.constructor && senterObj[key].constructor === {}.constructor) {
53
52
  jsonConcate(receiverObj[key], senterObj[key]);
@@ -59,6 +58,8 @@ let jsonConcate = (receiverObj, senterObj) => {
59
58
  } else {
60
59
  receiverObj[key] = senterObj[key];
61
60
  }
61
+
62
+ return key;
62
63
  });
63
64
  return receiverObj;
64
65
  };
@@ -72,10 +73,12 @@ let jsonHelper = (path, pathArray, value) => {
72
73
  pathArray = pathArray.split('.'); //eslint-disable-line no-param-reassign
73
74
  }
74
75
 
75
- pathArray.slice(0).reverse().forEach(key => {
76
+ pathArray.slice(0).reverse().map(key => {
76
77
  tempObj = {};
77
78
  tempObj[key] = value;
78
79
  value = tempObj; // eslint-disable-line no-param-reassign
80
+
81
+ return key;
79
82
  });
80
83
  let overAllObj = jsonConcate(JSON.parse(fileHandler.readFile(path)), tempObj);
81
84
  fileHandler.writeFile(path, overAllObj);