Skip to content

Commit

Permalink
lint js files, cleanup scripts (coralproject#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
cvle authored and wyattjoh committed Jun 29, 2018
1 parent 65c8da0 commit 986716b
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ yarn.lock
coverage

.idea/
.vs
.docz
*.swp
*.DS_STORE
Expand Down
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
".vscode": true,
"package-lock.json": true
},
"tslint.autoFixOnSave": true
}
"tslint.autoFixOnSave": true,
"tslint.jsEnable": true
}
21 changes: 9 additions & 12 deletions config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ const paths = require("./paths");
module.exports = {
rootDir: "../",
roots: ["<rootDir>/src", "<rootDir>/scripts"],
collectCoverageFrom: [
"src/**/*.{js,jsx,mjs,ts,tsx}"
],
collectCoverageFrom: ["src/**/*.{js,jsx,mjs,ts,tsx}"],
coveragePathIgnorePatterns: ["/node_modules/"],
setupFiles: [
"<rootDir>/config/polyfills.js"
],
setupFiles: ["<rootDir>/config/polyfills.js"],
testMatch: [
"**/__tests__/**/*.{js,jsx,mjs,ts,tsx}",
"**/*.(spec|test).{js,jsx,mjs,ts,tsx}"
"**/*.(spec|test).{js,jsx,mjs,ts,tsx}",
],
testEnvironment: "node",
testURL: "http://localhost",
transform: {
"^.+\\.(js|jsx|mjs|ts|tsx)$": "<rootDir>/node_modules/ts-jest",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|mjs|css|json|ftl)$)": "<rootDir>/config/jest/fileTransform.js"
"^(?!.*\\.(js|jsx|mjs|css|json|ftl)$)":
"<rootDir>/config/jest/fileTransform.js",
},
transformIgnorePatterns: [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|ts|tsx)$"
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|ts|tsx)$",
],
moduleNameMapper: {
"^react-native$": "react-native-web"
"^react-native$": "react-native-web",
},
moduleFileExtensions: [
"web.js",
Expand All @@ -36,6 +33,6 @@ module.exports = {
"node",
"mjs",
"ts",
"tsx"
"tsx",
],
}
};
6 changes: 3 additions & 3 deletions config/jest/cssTransform.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';
"use strict";

// This is a custom Jest transformer turning style imports into empty objects.
// http://facebook.github.io/jest/docs/en/webpack.html

module.exports = {
process() {
return 'module.exports = {};';
return "module.exports = {};";
},
getCacheKey() {
// The output is always the same.
return 'cssTransform';
return "cssTransform";
},
};
4 changes: 2 additions & 2 deletions config/jest/fileTransform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
"use strict";

const path = require('path');
const path = require("path");

// This is a custom Jest transformer turning file imports into filenames.
// http://facebook.github.io/jest/docs/en/webpack.html
Expand Down
19 changes: 11 additions & 8 deletions config/postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ const kebabCase = require("lodash/kebabCase");
const mapKeys = require("lodash/mapKeys");
const flat = require("flat");
const flexbugsFixes = require("postcss-flexbugs-fixes");
const paths = require('./paths');
const paths = require("./paths");

delete require.cache[paths.appThemeVariables];
const variables = require(paths.appThemeVariables);
const flatKebabVariables = mapKeys(flat(variables, {delimiter: "-"}), (_, k) => kebabCase(k));
const flatKebabVariables = mapKeys(
flat(variables, { delimiter: "-" }),
(_, k) => kebabCase(k)
);

module.exports = {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
ident: "postcss",
plugins: [
precss({ variables: flatKebabVariables }),
fontMagician(),
flexbugsFixes,
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
">1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9", // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
flexbox: "no-2009",
}),
],
};
4 changes: 3 additions & 1 deletion doczrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export default {
},
],
});
config.resolve.plugins = [new TsconfigPathsPlugin({ extensions, configFile: paths.appTsconfig })];
config.resolve.plugins = [
new TsconfigPathsPlugin({ extensions, configFile: paths.appTsconfig }),
];
// fs.writeFileSync(path.resolve(__dirname, "tmp"), stringify(config, null, 2));
return config;
},
Expand Down
110 changes: 81 additions & 29 deletions loaders/locales-loader.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const loaderUtils = require('loader-utils');
const fs = require('fs');
const path = require('path');
const camelCase = require('lodash/camelCase');
const upperFirst = require('lodash/upperFirst');
const memoize = require('lodash/memoize');
const loaderUtils = require("loader-utils");
const fs = require("fs");
const path = require("path");
const camelCase = require("lodash/camelCase");
const upperFirst = require("lodash/upperFirst");
const memoize = require("lodash/memoize");

const pascalCase = (x) => upperFirst(camelCase(x));
const pascalCase = x => upperFirst(camelCase(x));

/**
* Default values for every param that can be passed in the loader query.
Expand All @@ -15,12 +15,12 @@ const DEFAULT_QUERY_VALUES = {
pathToLocales: null,

// Default locale if non could be negotiated.
defaultLocale: 'en-US',
defaultLocale: "en-US",

// Fallback locale if a translation was not found.
// If not set, will use the text that is already
// in the code base.
fallbackLocale: '',
fallbackLocale: "",

// If set, restrict to this list of available locales.
availableLocales: null,
Expand All @@ -33,11 +33,11 @@ const DEFAULT_QUERY_VALUES = {

// Target specifies the prefix for fluent files to be loaded. ${target}-xyz.ftl and ${†arget}.ftl are
// loaded into the locales.
target: '',
target: "",
};

function getFiles(target, pathToLocale, context) {
const {pathToLocales, commonFiles} = context;
const { pathToLocales, commonFiles } = context;

const common = [];
const suffixes = [];
Expand All @@ -55,13 +55,22 @@ function getFiles(target, pathToLocale, context) {
}
});

return {common, suffixes};
return { common, suffixes };
}

function generateTarget(target, context) {
const {defaultLocale, fallbackLocale, pathToLocales, locales, commonFiles, bundled} = context;
const {
defaultLocale,
fallbackLocale,
pathToLocales,
locales,
commonFiles,
bundled,
} = context;
const getLocalePath = locale => path.join(pathToLocales, locale);
const getLocaleFiles = memoize(locale => getFiles(target, getLocalePath(locale), context));
const getLocaleFiles = memoize(locale =>
getFiles(target, getLocalePath(locale), context)
);
const loadables = locales.filter(local => !bundled.includes(local));

return `
Expand All @@ -74,49 +83,83 @@ function generateTarget(target, context) {
};
// Bundled locales are directly available in the main bundle.
${bundled.map(locale => `
${bundled
.map(
locale => `
{
var suffixes = ${JSON.stringify(getLocaleFiles(locale).suffixes)};
var contents = [];
${getLocaleFiles(locale).common.map(file => `
${getLocaleFiles(locale)
.common.map(
file => `
contents.push(require('${getLocalePath(locale)}/${file}'));
`).join("\n")}
contents = contents.concat(suffixes.map(function(suffix) { return require(\`${getLocalePath(locale)}/${target}\${suffix}\`); }));
`
)
.join("\n")}
contents = contents.concat(suffixes.map(function(suffix) { return require(\`${getLocalePath(
locale
)}/${target}\${suffix}\`); }));
ret.bundled[${JSON.stringify(locale)}] = contents.join("\\n");
}
`).join("\n")}
`
)
.join("\n")}
// Loadables are in a separate bundle, that can be easily loaded.
${loadables.map(locale => `
${loadables
.map(
locale => `
ret.loadables[${JSON.stringify(locale)}] = function() {
var suffixes = ${JSON.stringify(getLocaleFiles(locale).suffixes)};
var promises = [];
${getLocaleFiles(locale).common.map(file => `
${getLocaleFiles(locale)
.common.map(
file => `
promises.push(
import(
/* webpackChunkName: ${JSON.stringify(`${target}-locale-${locale}`)}, webpackMode: "lazy" */
/* webpackChunkName: ${JSON.stringify(
`${target}-locale-${locale}`
)}, webpackMode: "lazy" */
'${getLocalePath(locale)}/${file}'
)
);
`).join("\n")}
`
)
.join("\n")}
promises = promises.concat(suffixes.map(function(suffix) {
return import(
/* webpackChunkName: ${JSON.stringify(`${target}-locale-${locale}`)}, webpackMode: "lazy-once" */
/* webpackChunkName: ${JSON.stringify(
`${target}-locale-${locale}`
)}, webpackMode: "lazy-once" */
\`${getLocalePath(locale)}/${target}\${suffix}\`
)
}));
return Promise.all(promises).then(function(modules) {
return modules.map(function(m){return m.default}).join("\\n");
});
};
`).join("\n")}
`
)
.join("\n")}
module.exports = ret;
`;
}

module.exports = function(source) {
const options = Object.assign({}, DEFAULT_QUERY_VALUES, loaderUtils.getOptions(this));
const {pathToLocales, defaultLocale, fallbackLocale, availableLocales, target, bundled, commonFiles} = options;
const options = Object.assign(
{},
DEFAULT_QUERY_VALUES,
loaderUtils.getOptions(this)
);
const {
pathToLocales,
defaultLocale,
fallbackLocale,
availableLocales,
target,
bundled,
commonFiles,
} = options;

let locales = fs.readdirSync(pathToLocales);
if (availableLocales) {
Expand All @@ -129,7 +172,9 @@ module.exports = function(source) {
}

if (fallbackLocale && !locales.includes(fallbackLocale)) {
throw new Error(`fallbackLocale ${fallbackLocale} not in available locales`);
throw new Error(
`fallbackLocale ${fallbackLocale} not in available locales`
);
}
if (!pathToLocales) {
throw new Error(`pathToLocales is required`);
Expand All @@ -142,7 +187,14 @@ module.exports = function(source) {
throw new Error(`defaultLocale ${defaultLocale} not in available locales`);
}

const context = {pathToLocales, defaultLocale, fallbackLocale, commonFiles, locales, bundled};
const context = {
pathToLocales,
defaultLocale,
fallbackLocale,
commonFiles,
locales,
bundled,
};

this.cacheable();
return generateTarget(target, context);
Expand Down
Loading

0 comments on commit 986716b

Please sign in to comment.