oc-template-elm-compiler 0.0.11 → 0.1.0-beta.1

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.
Files changed (42) hide show
  1. package/dist/index.d.ts +2 -0
  2. package/dist/index.js +18 -0
  3. package/dist/lib/compile.d.ts +3 -0
  4. package/dist/lib/compile.js +36 -0
  5. package/dist/lib/compileServer.d.ts +7 -0
  6. package/dist/lib/compileServer.js +73 -0
  7. package/dist/lib/compileView.d.ts +12 -0
  8. package/dist/lib/compileView.js +102 -0
  9. package/dist/lib/elmOCProviderTemplate.d.ts +4 -0
  10. package/{lib → dist/lib}/elmOCProviderTemplate.js +13 -6
  11. package/dist/lib/higherOrderServerTemplate.d.ts +7 -0
  12. package/{lib → dist/lib}/higherOrderServerTemplate.js +2 -8
  13. package/dist/lib/htmlTemplate.d.ts +6 -0
  14. package/dist/lib/htmlTemplate.js +37 -0
  15. package/dist/lib/to-abstract-base-template-utils/font-family-unicode-parser.d.ts +5 -0
  16. package/dist/lib/to-abstract-base-template-utils/font-family-unicode-parser.js +23 -0
  17. package/dist/lib/verifyConfig.d.ts +8 -0
  18. package/dist/lib/verifyConfig.js +146 -0
  19. package/dist/lib/viewTemplate.d.ts +6 -0
  20. package/{lib → dist/lib}/viewTemplate.js +2 -7
  21. package/package.json +19 -54
  22. package/scaffold/src/tsconfig.json +18 -18
  23. package/index.js +0 -11
  24. package/lib/compile.js +0 -33
  25. package/lib/compileServer.js +0 -95
  26. package/lib/compileView.js +0 -125
  27. package/lib/oc-app.d.ts +0 -81
  28. package/lib/to-abstract-base-template-utils/font-family-unicode-parser.js +0 -23
  29. package/lib/to-abstract-base-template-utils/oc-webpack/index.js +0 -9
  30. package/lib/to-abstract-base-template-utils/oc-webpack/lib/compiler/index.js +0 -43
  31. package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/client/index.js +0 -55
  32. package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/createExcludeRegex.js +0 -6
  33. package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/eslintFormatter.js +0 -110
  34. package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/index.js +0 -9
  35. package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/pnpTs.js +0 -33
  36. package/lib/to-abstract-base-template-utils/oc-webpack/lib/configurator/server/index.js +0 -156
  37. package/lib/verifyConfig.js +0 -199
  38. package/scaffold/src/elm-stuff/0.19.1/Main.elmi +0 -0
  39. package/scaffold/src/elm-stuff/0.19.1/Main.elmo +0 -0
  40. package/scaffold/src/elm-stuff/0.19.1/d.dat +0 -0
  41. package/scaffold/src/elm-stuff/0.19.1/i.dat +0 -0
  42. package/scaffold/src/elm-stuff/0.19.1/o.dat +0 -0
@@ -0,0 +1,2 @@
1
+ export * from './lib/compile';
2
+ export * from 'oc-template-elm';
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./lib/compile"), exports);
18
+ __exportStar(require("oc-template-elm"), exports);
@@ -0,0 +1,3 @@
1
+ import { createCompile } from 'oc-generic-template-compiler';
2
+ export type CompilerOptions = ReturnType<typeof createCompile>;
3
+ export declare const compile: CompilerOptions;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.compile = void 0;
7
+ const oc_generic_template_compiler_1 = require("oc-generic-template-compiler");
8
+ const oc_statics_compiler_1 = __importDefault(require("oc-statics-compiler"));
9
+ const oc_template_elm_1 = require("oc-template-elm");
10
+ const oc_vite_compiler_1 = require("oc-vite-compiler");
11
+ const vite_plugin_elm_1 = __importDefault(require("vite-plugin-elm"));
12
+ const elmOCProviderTemplate_1 = __importDefault(require("./elmOCProviderTemplate"));
13
+ const htmlTemplate_1 = __importDefault(require("./htmlTemplate"));
14
+ exports.compile = (0, oc_generic_template_compiler_1.createCompile)({
15
+ compileView: (options, cb) => (0, oc_vite_compiler_1.viteView)({
16
+ ...options,
17
+ plugins: [(0, vite_plugin_elm_1.default)()],
18
+ viewWrapper: ({ viewPath }) => (0, elmOCProviderTemplate_1.default)({ viewPath, jsPath: undefined }),
19
+ htmlTemplate: (props) => (0, htmlTemplate_1.default)({ ...props, bundleName: 'dontknow' }),
20
+ externals: (0, oc_template_elm_1.getInfo)().externals
21
+ }, cb),
22
+ compileServer: oc_vite_compiler_1.viteServer,
23
+ compileStatics: oc_statics_compiler_1.default,
24
+ getInfo: oc_template_elm_1.getInfo
25
+ });
26
+ // OPTIONS
27
+ // =======
28
+ // componentPath
29
+ // componentPackage,
30
+ // logger,
31
+ // minify
32
+ // ocPackage
33
+ // publishPath
34
+ // verbose,
35
+ // watch,
36
+ // production
@@ -0,0 +1,7 @@
1
+ declare const async: any;
2
+ declare const fs: any;
3
+ declare const hashBuilder: any;
4
+ declare const MemoryFS: any;
5
+ declare const path: any;
6
+ declare const compiler: any, webpackConfigurator: any;
7
+ declare const higherOrderServerTemplate: any;
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+ const async = require('async');
3
+ const fs = require('fs-extra');
4
+ const hashBuilder = require('oc-hash-builder');
5
+ const MemoryFS = require('memory-fs');
6
+ const path = require('path');
7
+ const { compiler, configurator: { server: webpackConfigurator } } = require('./to-abstract-base-template-utils/oc-webpack');
8
+ const higherOrderServerTemplate = require('./higherOrderServerTemplate');
9
+ module.exports = (options, callback) => {
10
+ const componentPath = options.componentPath;
11
+ const serverFileName = options.componentPackage.oc.files.data;
12
+ let serverPath = path.join(options.componentPath, serverFileName);
13
+ if (process.platform === 'win32') {
14
+ serverPath = serverPath.split('\\').join('\\\\');
15
+ }
16
+ const publishFileName = options.publishFileName || 'server.js';
17
+ const publishPath = options.publishPath;
18
+ const stats = options.verbose ? 'verbose' : 'errors-only';
19
+ const dependencies = options.componentPackage.dependencies || {};
20
+ const componentName = options.componentPackage.name;
21
+ const componentVersion = options.componentPackage.version;
22
+ const production = options.production;
23
+ const higherOrderServerContent = higherOrderServerTemplate({
24
+ serverPath,
25
+ componentName,
26
+ componentVersion,
27
+ bundleHashKey: options.compiledViewInfo.bundle.hashKey
28
+ });
29
+ const tempFolder = path.join(serverPath, '../_package/temp');
30
+ const higherOrderServerPath = path.join(tempFolder, '__oc_higherOrderServer.ts');
31
+ const config = webpackConfigurator({
32
+ componentPath,
33
+ serverPath: higherOrderServerPath,
34
+ publishFileName,
35
+ dependencies,
36
+ stats,
37
+ production
38
+ });
39
+ async.waterfall([
40
+ (next) => fs.outputFile(higherOrderServerPath, higherOrderServerContent, next),
41
+ (next) => {
42
+ return compiler(config, next);
43
+ },
44
+ (data, next) => {
45
+ const basePath = path.join(tempFolder, 'build');
46
+ const memory = new MemoryFS(data);
47
+ const getCompiled = (fileName) => memory.readFileSync(`${basePath}/${fileName}`, 'UTF8');
48
+ return fs.ensureDir(publishPath, (err) => {
49
+ if (err)
50
+ return next(err);
51
+ const result = { 'server.js': getCompiled(config.output.filename) };
52
+ if (!production) {
53
+ try {
54
+ result['server.js.map'] = getCompiled(`${config.output.filename}.map`);
55
+ }
56
+ catch (e) {
57
+ // skip sourcemap if it doesn't exist
58
+ }
59
+ }
60
+ next(null, result);
61
+ });
62
+ },
63
+ (compiledFiles, next) => {
64
+ return async.eachOf(compiledFiles, (fileContent, fileName, next) => fs.writeFile(path.join(publishPath, fileName), fileContent, next), (err) => next(err, err
65
+ ? null
66
+ : {
67
+ type: 'node.js',
68
+ hashKey: hashBuilder.fromString(compiledFiles[publishFileName]),
69
+ src: publishFileName
70
+ }));
71
+ }
72
+ ], (err, data) => fs.remove(tempFolder, (_err2) => callback(err, data)));
73
+ };
@@ -0,0 +1,12 @@
1
+ declare const async: any;
2
+ declare const fs: any;
3
+ declare const hashBuilder: any;
4
+ declare const MemoryFS: any;
5
+ declare const ocViewWrapper: any;
6
+ declare const path: any;
7
+ declare const strings: any;
8
+ declare const elmComponentWrapper: (hash: any, content: any, nameSpace: any) => string;
9
+ declare const compiler: any, webpackConfigurator: any;
10
+ declare const fontFamilyUnicodeParser: any;
11
+ declare const elmOCProviderTemplate: any;
12
+ declare const viewTemplate: any;
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+ const async = require('async');
3
+ const fs = require('fs-extra');
4
+ const hashBuilder = require('oc-hash-builder');
5
+ const MemoryFS = require('memory-fs');
6
+ const ocViewWrapper = require('oc-view-wrapper');
7
+ const path = require('path');
8
+ const strings = require('oc-templates-messages');
9
+ const elmComponentWrapper = (hash, content, nameSpace) => {
10
+ nameSpace = nameSpace || 'oc';
11
+ return `var ${nameSpace}=${nameSpace}||{};${nameSpace}.elmComponents=${nameSpace}.elmComponents||{};${nameSpace}.elmComponents['${hash}']=${nameSpace}.elmComponents['${hash}']||(function(){var ${content}
12
+ ; return module.default}())`;
13
+ };
14
+ const { compiler, configurator: { client: webpackConfigurator } } = require('./to-abstract-base-template-utils/oc-webpack');
15
+ const fontFamilyUnicodeParser = require('./to-abstract-base-template-utils/font-family-unicode-parser');
16
+ const elmOCProviderTemplate = require('./elmOCProviderTemplate');
17
+ const viewTemplate = require('./viewTemplate');
18
+ module.exports = (options, callback) => {
19
+ const viewFileName = options.componentPackage.oc.files.template.src;
20
+ const jsFileName = options.componentPackage.oc.files.template.js;
21
+ const componentPath = options.componentPath;
22
+ let viewPath = path.join(options.componentPath, viewFileName);
23
+ let jsPath = jsFileName && path.join(options.componentPath, viewFileName);
24
+ if (process.platform === 'win32') {
25
+ viewPath = viewPath.split('\\').join('\\\\');
26
+ jsPath = jsPath && jsPath.split('\\').join('\\\\');
27
+ }
28
+ const publishPath = options.publishPath;
29
+ const tempPath = path.join(publishPath, 'temp');
30
+ const publishFileName = options.publishFileName || 'template.js';
31
+ const componentPackage = options.componentPackage;
32
+ const production = options.production;
33
+ const elmOCProviderContent = elmOCProviderTemplate({ viewPath, jsPath });
34
+ const elmOCProviderName = 'elmOCProvider.js';
35
+ const elmOCProviderPath = path.join(tempPath, elmOCProviderName);
36
+ const compile = (options, cb) => {
37
+ const config = webpackConfigurator({
38
+ componentPath,
39
+ viewPath: options.viewPath,
40
+ publishFileName,
41
+ production,
42
+ buildIncludes: componentPackage.oc.files.template.buildIncludes || []
43
+ });
44
+ compiler(config, (err, data) => {
45
+ if (err) {
46
+ return cb(err);
47
+ }
48
+ const memoryFs = new MemoryFS(data);
49
+ const bundle = memoryFs.readFileSync(`/build/${config.output.filename}`, 'UTF8');
50
+ const bundleHash = hashBuilder.fromString(bundle);
51
+ const bundleName = 'elm-component';
52
+ const bundlePath = path.join(publishPath, `${bundleName}.js`);
53
+ const wrappedBundle = elmComponentWrapper(bundleHash, bundle);
54
+ fs.outputFileSync(bundlePath, wrappedBundle);
55
+ let css = null;
56
+ if (data.build['main.css']) {
57
+ // This is an awesome hack by KimTaro that will blow your mind.
58
+ // Remove it once this get merged: https://github.com/webpack-contrib/css-loader/pull/523
59
+ css = fontFamilyUnicodeParser(memoryFs.readFileSync(`/build/main.css`, 'UTF8'));
60
+ const cssPath = path.join(publishPath, `styles.css`);
61
+ // We convert single quotes to double quotes in order to
62
+ // support the viewTemplate's string interpolation
63
+ fs.outputFileSync(cssPath, css.replace(/'/g, '"'));
64
+ }
65
+ const elmRoot = `oc-elmRoot-${componentPackage.name}`;
66
+ const templateString = viewTemplate({
67
+ elmRoot,
68
+ css,
69
+ bundleHash,
70
+ bundleName
71
+ });
72
+ const templateStringCompressed = production
73
+ ? templateString.replace(/\s+/g, ' ')
74
+ : templateString;
75
+ const hash = hashBuilder.fromString(templateStringCompressed);
76
+ const view = ocViewWrapper(hash, templateStringCompressed);
77
+ return cb(null, {
78
+ template: { view, hash },
79
+ bundle: { hash: bundleHash }
80
+ });
81
+ });
82
+ };
83
+ async.waterfall([
84
+ (next) => fs.outputFile(elmOCProviderPath, elmOCProviderContent, next),
85
+ (next) => compile({ viewPath: elmOCProviderPath }, next),
86
+ (compiled, next) => fs.remove(elmOCProviderPath, (err) => next(err, compiled)),
87
+ (compiled, next) => fs.ensureDir(publishPath, (err) => next(err, compiled)),
88
+ (compiled, next) => fs.writeFile(path.join(publishPath, publishFileName), compiled.template.view, (err) => next(err, compiled))
89
+ ], (err, compiled) => {
90
+ if (err) {
91
+ return callback(strings.errors.compilationFailed(viewFileName, err));
92
+ }
93
+ callback(null, {
94
+ template: {
95
+ type: options.componentPackage.oc.files.template.type,
96
+ hashKey: compiled.template.hash,
97
+ src: publishFileName
98
+ },
99
+ bundle: { hashKey: compiled.bundle.hash }
100
+ });
101
+ });
102
+ };
@@ -0,0 +1,4 @@
1
+ export default function elmOCProviderTemplate({ jsPath, viewPath }: {
2
+ jsPath?: string;
3
+ viewPath: string;
4
+ }): string;
@@ -1,8 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
1
3
  const extractName = (path) => path.match(/(\w+)\.elm$/)[1];
2
4
  const removeJsExtension = (path) => path.replace(/\.(t|j)s?$/, '');
3
-
4
- const elmOCProviderTemplate = ({ jsPath, viewPath }) => `
5
- import Component from '${viewPath}';
5
+ function elmOCProviderTemplate({ jsPath, viewPath }) {
6
+ return `
7
+ import { Elm } from '${viewPath}';
6
8
  ${jsPath ? `import jsApp from '${removeJsExtension(jsPath)}'` : ''}
7
9
 
8
10
  function getData(providerProps, parameters, cb) {
@@ -20,8 +22,13 @@ const elmOCProviderTemplate = ({ jsPath, viewPath }) => `
20
22
  });
21
23
  }
22
24
 
23
- function init({ node, flags: { _baseUrl, _componentName, _componentVersion, _staticPath, ...flags } }) {
25
+ function renderer(model, node) {
26
+ const { _baseUrl, _componentName, _componentVersion, _staticPath, ...props } = model;
24
27
  const app = Component.Elm["${extractName(viewPath)}"].init({ node, flags });
28
+ const app = Elm["${extractName(viewPath)}"].init({
29
+ node,
30
+ flags: props
31
+ })
25
32
 
26
33
  if (app.ports && app.ports.getData) {
27
34
  app.ports.getData.subscribe(parameters => {
@@ -43,5 +50,5 @@ const elmOCProviderTemplate = ({ jsPath, viewPath }) => `
43
50
 
44
51
  export default init;
45
52
  `;
46
-
47
- module.exports = elmOCProviderTemplate;
53
+ }
54
+ exports.default = elmOCProviderTemplate;
@@ -0,0 +1,7 @@
1
+ declare const removeTsExtension: (path: any) => any;
2
+ declare const higherOrderServerTemplate: ({ serverPath, bundleHashKey, componentName, componentVersion }: {
3
+ serverPath: any;
4
+ bundleHashKey: any;
5
+ componentName: any;
6
+ componentVersion: any;
7
+ }) => string;
@@ -1,11 +1,6 @@
1
+ "use strict";
1
2
  const removeTsExtension = (path) => path.replace(/\.tsx?$/, '');
2
-
3
- const higherOrderServerTemplate = ({
4
- serverPath,
5
- bundleHashKey,
6
- componentName,
7
- componentVersion
8
- }) => `
3
+ const higherOrderServerTemplate = ({ serverPath, bundleHashKey, componentName, componentVersion }) => `
9
4
  import { data as dataProvider } from '${removeTsExtension(serverPath)}';
10
5
 
11
6
  export const data = (context : any, callback : (error: any, data?: any) => void) => {
@@ -31,5 +26,4 @@ export const data = (context : any, callback : (error: any, data?: any) => void)
31
26
  });
32
27
  }
33
28
  `;
34
-
35
29
  module.exports = higherOrderServerTemplate;
@@ -0,0 +1,6 @@
1
+ export default function viewTemplate({ templateId, css, hash, bundleName }: {
2
+ templateId: string;
3
+ css: string;
4
+ hash: string;
5
+ bundleName: string;
6
+ }): string;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const cssesc_1 = __importDefault(require("cssesc"));
7
+ function viewTemplate({ templateId, css, hash, bundleName }) {
8
+ return `function elmViewTemplate(model) {
9
+ var modelHTML = model.__html ? model.__html : '';
10
+ var staticPath = model.component.flags._staticPath;
11
+ var flags = JSON.stringify(model.component.flags);
12
+ window.oc = window.oc || {};
13
+ window.oc.__elmTemplate = window.oc.__elmTemplate || { count: 0 };
14
+ var count = window.oc.__elmTemplate.count;
15
+ var templateId = "${templateId}-" + count;
16
+ window.oc.__elmTemplate.count++;
17
+ return '<div id="' + templateId + '" class="${templateId}">' + modelHTML + '</div>' +
18
+ '${css ? '<style>' + (0, cssesc_1.default)(css) + '</style>' : ''}' +
19
+ '<script>' +
20
+ 'window.oc = window.oc || {};' +
21
+ 'oc.cmd = oc.cmd || [];' +
22
+ 'oc.cmd.push(function(oc){' +
23
+ '${css ? "oc.events.fire(\\'oc:cssDidMount\\', \\'" + css + "\\');" : ''}' +
24
+ 'oc.require(' +
25
+ '["oc", "elmComponents", "${hash}"],' +
26
+ '"' + staticPath + '${bundleName}.js",' +
27
+ 'function(ElmComponent) {' +
28
+ 'var targetNode = document.getElementById("' + templateId + '");' +
29
+ 'targetNode.setAttribute("id","");' +
30
+ 'ElmComponent({ node: targetNode, flags:' + flags + '});' +
31
+ '}' +
32
+ ');' +
33
+ '});' +
34
+ '</script>'
35
+ }`;
36
+ }
37
+ exports.default = viewTemplate;
@@ -0,0 +1,5 @@
1
+ declare const RE_FONT_FAMILY: RegExp;
2
+ declare const RE_FONT_NAMES: RegExp;
3
+ declare const RE_DOUBLE_COUTE: RegExp;
4
+ declare const RE_SPACE: RegExp;
5
+ declare const RE_UNICODE: RegExp;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ const RE_FONT_FAMILY = /font-family\s*:\s*([^;\n]+)[;\n]/g;
3
+ const RE_FONT_NAMES = /(?:(?:'|(?:\\"))?([(?:\\\\)\w\d _-]+)(?:'|(?:\\"))?(,\s*)?)/g;
4
+ const RE_DOUBLE_COUTE = /"/g;
5
+ const RE_SPACE = /[ ]+/g;
6
+ const RE_UNICODE = /\\([\d\w]{4})/g;
7
+ module.exports = function (source) {
8
+ source = source.replace(RE_FONT_FAMILY, (str, fontNames) => {
9
+ const strFontNames = fontNames
10
+ .replace(RE_DOUBLE_COUTE, "'")
11
+ .replace(RE_FONT_NAMES, (match, fontName, sep) => {
12
+ if (match.includes("'")) {
13
+ const replacement = fontName
14
+ .replace(RE_SPACE, ' ')
15
+ .replace(RE_UNICODE, (m, unicode) => String.fromCharCode(parseInt(unicode, 16)));
16
+ return `'${replacement}'${sep || ''}`;
17
+ }
18
+ return match;
19
+ });
20
+ return `font-family: ${strFontNames};`;
21
+ });
22
+ return source;
23
+ };
@@ -0,0 +1,8 @@
1
+ declare const chalk: any;
2
+ declare const fs: any;
3
+ declare const resolve: any;
4
+ declare const path: any;
5
+ declare const os: any;
6
+ declare const immer: any;
7
+ declare function writeJson(fileName: any, object: any): void;
8
+ declare function verifyTypeScriptSetup(componentPath: any): void;
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ const chalk = require('chalk');
3
+ const fs = require('fs');
4
+ const resolve = require('resolve');
5
+ const path = require('path');
6
+ const os = require('os');
7
+ const immer = require('immer').produce;
8
+ function writeJson(fileName, object) {
9
+ fs.writeFileSync(fileName, JSON.stringify(object, null, 2).replace(/\n/g, os.EOL) + os.EOL);
10
+ }
11
+ function verifyTypeScriptSetup(componentPath) {
12
+ const paths = {
13
+ appTsConfig: path.resolve(componentPath, 'tsconfig.json'),
14
+ yarnLockFile: path.resolve(componentPath, 'yarn.lock'),
15
+ appNodeModules: path.resolve(componentPath, 'node_modules')
16
+ };
17
+ let firstTimeSetup = false;
18
+ if (!fs.existsSync(paths.appTsConfig)) {
19
+ writeJson(paths.appTsConfig, {});
20
+ firstTimeSetup = true;
21
+ }
22
+ const isYarn = fs.existsSync(paths.yarnLockFile);
23
+ // Ensure typescript is installed
24
+ let ts;
25
+ try {
26
+ ts = require(resolve.sync('typescript', {
27
+ basedir: paths.appNodeModules
28
+ }));
29
+ }
30
+ catch (_) {
31
+ console.error(chalk.bold.red(`It looks like you're trying to use TypeScript but do not have ${chalk.bold('typescript')} installed.`));
32
+ console.error(chalk.bold('Please install', chalk.cyan.bold('typescript'), 'by running', chalk.cyan.bold(isYarn ? 'yarn add typescript' : 'npm install typescript') + '.'));
33
+ console.error();
34
+ process.exit(1);
35
+ }
36
+ const compilerOptions = {
37
+ // These are suggested values and will be set when not present in the
38
+ // tsconfig.json
39
+ // 'parsedValue' matches the output value from ts.parseJsonConfigFileContent()
40
+ target: {
41
+ parsedValue: ts.ScriptTarget.ES5,
42
+ suggested: 'es5'
43
+ },
44
+ lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
45
+ allowJs: { suggested: true },
46
+ skipLibCheck: { suggested: true },
47
+ esModuleInterop: { suggested: true },
48
+ allowSyntheticDefaultImports: { suggested: true },
49
+ strict: { suggested: true },
50
+ forceConsistentCasingInFileNames: { suggested: true },
51
+ // TODO: Enable for v4.0 (#6936)
52
+ // noFallthroughCasesInSwitch: { suggested: true },
53
+ // These values are required and cannot be changed by the user
54
+ // Keep this in sync with the webpack config
55
+ module: {
56
+ parsedValue: ts.ModuleKind.ESNext,
57
+ value: 'esnext',
58
+ reason: 'for import() and import/export'
59
+ },
60
+ moduleResolution: {
61
+ parsedValue: ts.ModuleResolutionKind.NodeJs,
62
+ value: 'node',
63
+ reason: 'to match webpack resolution'
64
+ },
65
+ resolveJsonModule: { value: true, reason: 'to match webpack loader' },
66
+ isolatedModules: { value: true, reason: 'implementation limitation' },
67
+ paths: { value: undefined, reason: 'aliased imports are not supported' }
68
+ };
69
+ const formatDiagnosticHost = {
70
+ getCanonicalFileName: (fileName) => fileName,
71
+ getCurrentDirectory: ts.sys.getCurrentDirectory,
72
+ getNewLine: () => os.EOL
73
+ };
74
+ const messages = [];
75
+ let appTsConfig;
76
+ let parsedTsConfig;
77
+ let parsedCompilerOptions;
78
+ try {
79
+ const { config: readTsConfig, error } = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile);
80
+ if (error) {
81
+ throw new Error(ts.formatDiagnostic(error, formatDiagnosticHost));
82
+ }
83
+ appTsConfig = readTsConfig;
84
+ // Get TS to parse and resolve any "extends"
85
+ // Calling this function also mutates the tsconfig above,
86
+ // adding in "include" and "exclude", but the compilerOptions remain untouched
87
+ let result;
88
+ parsedTsConfig = immer(readTsConfig, (config) => {
89
+ result = ts.parseJsonConfigFileContent(config, ts.sys, path.dirname(paths.appTsConfig));
90
+ });
91
+ if (result.errors && result.errors.length) {
92
+ throw new Error(ts.formatDiagnostic(result.errors[0], formatDiagnosticHost));
93
+ }
94
+ parsedCompilerOptions = result.options;
95
+ }
96
+ catch (e) {
97
+ if (e && e.name === 'SyntaxError') {
98
+ console.error(chalk.red.bold('Could not parse', chalk.cyan('tsconfig.json') + '.', 'Please make sure it contains syntactically correct JSON.'));
99
+ }
100
+ console.log(e && e.message ? `${e.message}` : '');
101
+ process.exit(1);
102
+ }
103
+ if (appTsConfig.compilerOptions == null) {
104
+ appTsConfig.compilerOptions = {};
105
+ firstTimeSetup = true;
106
+ }
107
+ for (const option of Object.keys(compilerOptions)) {
108
+ const { parsedValue, value, suggested, reason } = compilerOptions[option];
109
+ const valueToCheck = parsedValue === undefined ? value : parsedValue;
110
+ const coloredOption = chalk.cyan('compilerOptions.' + option);
111
+ if (suggested != null) {
112
+ if (parsedCompilerOptions[option] === undefined) {
113
+ appTsConfig.compilerOptions[option] = suggested;
114
+ messages.push(`${coloredOption} to be ${chalk.bold('suggested')} value: ${chalk.cyan.bold(suggested)} (this can be changed)`);
115
+ }
116
+ }
117
+ else if (parsedCompilerOptions[option] !== valueToCheck) {
118
+ appTsConfig.compilerOptions[option] = value;
119
+ messages.push(`${coloredOption} ${chalk.bold(valueToCheck == null ? 'must not' : 'must')} be ${valueToCheck == null ? 'set' : chalk.cyan.bold(value)}` + (reason != null ? ` (${reason})` : ''));
120
+ }
121
+ }
122
+ // tsconfig will have the merged "include" and "exclude" by this point
123
+ if (parsedTsConfig.include == null) {
124
+ appTsConfig.include = ['src'];
125
+ messages.push(`${chalk.cyan('include')} should be ${chalk.cyan.bold('src')}`);
126
+ }
127
+ if (parsedTsConfig.exclude == null) {
128
+ appTsConfig.exclude = ['src/_package'];
129
+ messages.push(`${chalk.cyan('exclude')} should have ${chalk.cyan.bold('src/_package')}`);
130
+ }
131
+ if (messages.length > 0) {
132
+ if (firstTimeSetup) {
133
+ console.log(chalk.bold('Your', chalk.cyan('tsconfig.json'), 'has been populated with default values.'));
134
+ console.log();
135
+ }
136
+ else {
137
+ console.warn(chalk.bold('The following changes are being made to your', chalk.cyan('tsconfig.json'), 'file:'));
138
+ messages.forEach((message) => {
139
+ console.warn(' - ' + message);
140
+ });
141
+ console.warn();
142
+ }
143
+ writeJson(paths.appTsConfig, appTsConfig);
144
+ }
145
+ }
146
+ module.exports = verifyTypeScriptSetup;
@@ -0,0 +1,6 @@
1
+ declare const viewTemplate: ({ elmRoot, css, bundleHash, bundleName }: {
2
+ elmRoot: any;
3
+ css: any;
4
+ bundleHash: any;
5
+ bundleName: any;
6
+ }) => string;
@@ -1,9 +1,5 @@
1
- const viewTemplate = ({
2
- elmRoot,
3
- css,
4
- bundleHash,
5
- bundleName
6
- }) => `function elmViewTemplate(model) {
1
+ "use strict";
2
+ const viewTemplate = ({ elmRoot, css, bundleHash, bundleName }) => `function elmViewTemplate(model) {
7
3
  var modelHTML = model.__html ? model.__html : '';
8
4
  var staticPath = model.elmComponent.flags._staticPath;
9
5
  var flags = JSON.stringify(model.elmComponent.flags);
@@ -31,5 +27,4 @@ const viewTemplate = ({
31
27
  '});' +
32
28
  '</script>'
33
29
  }`;
34
-
35
30
  module.exports = viewTemplate;