Skip to content

Commit

Permalink
Lint files and improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Oct 31, 2022
1 parent 38a365f commit 54bfefc
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/webpack-config/src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import getWebpackConfig from './webpack.prod.config.js';
/**
* Build a given Webpack config.
* @param {WebpackConfig} config The Weback configuration object.
* @param {String} name The name of the build.
* @param {string} name The name of the build.
*/
async function build(config, name) {
console.log(`Building ${name} bundle in ${config.output.path.replace(cwd(), '.')}...`);
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-config/src/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ export default async function dev(options = {}) {
};

server.instance.init(browserSyncConfig);
};
}
13 changes: 5 additions & 8 deletions packages/webpack-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import getWebpackDevConfig from './webpack.dev.config.js';
* Create a configuration.
*
* @param {MetaConfig} config
* @return {MetaConfig}
* @returns {MetaConfig}
*/
export function createConfig(config) {
return config;
Expand All @@ -28,15 +28,12 @@ export function defineConfig(config) {
* @param {Object} options
* @param {'production'|'development'} [options.mode]
* @param {'modern'|'legacy'} [options.target]
*
* @return {import('webpack').Configuration}
* @returns {import('webpack').Configuration}
*/
export function getWebpackConfig(
{ mode, target } = { mode: process.env.NODE_ENV, target: 'legacy' }
) {
const config = getMetaConfig();
export function getWebpackConfig({ mode = process.env.NODE_ENV, target = 'legacy' } = {}) {
const config = getMetaConfig({ target: [target] });
const options = { isModern: target === 'modern', isLegacy: target === 'legacy' };
return mode === 'production'
? getWebpackProdConfig(config, options)
: getWebpackDevConfig(config, options);
: getWebpackDevConfig(config);
}
2 changes: 1 addition & 1 deletion packages/webpack-config/src/presets/prototyping.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default function prototyping(options) {
// Add debug comments
Twig.Templates.registerParser('twig', (params) => {
if (params.id) {
const namespace = Object.entries(params.options.namespaces).find(([key, value]) =>
const namespace = Object.entries(params.options.namespaces).find(([, value]) =>
params.id.startsWith(value)
);
let tpl = params.id;
Expand Down
10 changes: 5 additions & 5 deletions packages/webpack-config/src/utils/Html.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class Html {
/**
* Render classes.
* @param {Classes} classes
* @return {string}
* @returns {string}
*/
static renderClass(classes) {
if (!classes) {
Expand Down Expand Up @@ -65,7 +65,7 @@ export default class Html {
/**
* Render a style attribute.
* @param {Record<string, string|number>} styles
* @return {string}
* @returns {string}
*/
static renderStyleAttribute(styles) {
if (!styles) {
Expand All @@ -87,7 +87,7 @@ export default class Html {
* Render attributes.
*
* @param {Record<string, any>} attributes
* @return {string}
* @returns {string}
*/
static renderAttributes(attributes) {
if (!attributes) {
Expand Down Expand Up @@ -132,7 +132,7 @@ export default class Html {
* Convert a map to an object.
*
* @param {Map} map
* @return {Record<string, any>}
* @returns {Record<string, any>}
*/
static mapToObject(map) {
const obj = {};
Expand All @@ -152,7 +152,7 @@ export default class Html {
* @param {string} name
* @param {Record<string, any>} attributes
* @param {string} content
* @return {string}
* @returns {string}
*/
static renderTag(name, attributes, content = '') {
const formattedAttributes = Html.renderAttributes(attributes);
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-config/src/utils/extend-webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export default async function extendWebpackConfig(config, fn) {
await oldWebpackConfig(webpackConfig, isDev);
await fn(webpackConfig, isDev);
};
};
}
18 changes: 12 additions & 6 deletions packages/webpack-config/src/utils/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { findUp } from 'find-up';
import extendBrowsersync from './extend-browsersync-config.js';
import extendWebpack from './extend-webpack-config.js';

export default async (options = { analyze: false, target: [] }) => {
/**
* Get config from meta.config.js file.
*
* @param {{ analyze: boolean, target: Array<'modern'|'legacy'> }} [options] CLI Options.
* @returns {import('../index').MetaConfig}
*/
export default async function getConfig({ analyze = false, target = [] } = {}) {
const configPath = await findUp(['meta.config.js', 'meta.config.mjs']);

if (!configPath) {
Expand All @@ -16,7 +22,7 @@ export default async (options = { analyze: false, target: [] }) => {

const { default: config } = await import(configPath);

if (options.analyze) {
if (analyze) {
config.analyze = true;
}

Expand All @@ -43,9 +49,9 @@ export default async (options = { analyze: false, target: [] }) => {
}

// Read from command line args first, then meta.config.js, then set default
if (Array.isArray(options.target) && options.target.length) {
config.modern = options.target.includes('modern');
config.legacy = options.target.includes('legacy');
if (Array.isArray(target) && target.length) {
config.modern = target.includes('modern');
config.legacy = target.includes('legacy');
} else if (config.target) {
const targetConfig = Array.isArray(config.target) ? config.target : [config.target];
config.modern = targetConfig.includes('modern');
Expand All @@ -71,4 +77,4 @@ export default async (options = { analyze: false, target: [] }) => {
}

return config;
};
}
12 changes: 6 additions & 6 deletions packages/webpack-config/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Test if the given input has a trailing slash.
* @param {string = ''} input The string to test.
* @return {boolean}
* @returns {boolean}
*/
export function hasTrailingSlash(input = '') {
return input.endsWith('/');
Expand All @@ -10,7 +10,7 @@ export function hasTrailingSlash(input = '') {
/**
* Make sure that a string does not end with a trailing slash.
* @param {string = ''} input The string to test.
* @return {string} The string without trailing slash.
* @returns {string} The string without trailing slash.
*/
export function withoutTrailingSlash(input = '') {
return (hasTrailingSlash(input) ? input.slice(0, -1) : input) || '/';
Expand All @@ -19,7 +19,7 @@ export function withoutTrailingSlash(input = '') {
/**
* Make sure the string given ends with a trailing slash.
* @param {string = ''} input The string to test.
* @return {string} The string with a trailing slash.
* @returns {string} The string with a trailing slash.
*/
export function withTrailingSlash(input = '') {
return input.endsWith('/') ? input : `${input}/`;
Expand All @@ -28,7 +28,7 @@ export function withTrailingSlash(input = '') {
/**
* Test if the given input has a leading slash.
* @param {string = ''} input The string to test.
* @return {boolean}
* @returns {boolean}
*/
export function hasLeadingSlash(input = '') {
return input.startsWith('/');
Expand All @@ -37,10 +37,10 @@ export function hasLeadingSlash(input = '') {
/**
* Make sure the given string does not have a leading slash.
* @param {string = ''} input The string to test.
* @return {string} The string without leading slash.
* @returns {string} The string without leading slash.
*/
export function withoutLeadingSlash(input = '') {
return (hasLeadingSlash(input) ? input.substr(1) : input) || '/';
return (hasLeadingSlash(input) ? input.slice(1) : input) || '/';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-config/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getWebpackConfig from './webpack.prod.config.js';
/**
* Build a given Webpack config.
* @param {WebpackConfig} config The Weback configuration object.
* @param {String} name The name of the build.
* @param {string} name The name of the build.
*/
async function build(config, name) {
console.log(`Building ${name} bundle in ${config.output.path.replace(cwd(), '.')}...`);
Expand Down
16 changes: 9 additions & 7 deletions packages/webpack-config/src/webpack.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ const { BundleAnalyzerPlugin } = BundleAnalyzerPluginImport;

dotenv.config();

export default async (config, options = {}) => {
/**
* Get Webpack base config.
* @param {import('./index').MetaConfig} config
* @param {{ isModern?: boolean, isLegacy?: boolean }} [options]
* @returns {import('webpack').Configuration}
*/
export default async function getWebpackBaseConfig(config, options = {}) {
const isDev = process.env.NODE_ENV !== 'production';
const { isModern, isLegacy } = { isModern: false, isLegacy: false, ...options };
const src = commonDir(config.src);
Expand Down Expand Up @@ -122,11 +128,7 @@ export default async (config, options = {}) => {
exclude: [/node_modules[\\/](?!@studiometa[\\/]).*/],
type: 'javascript/auto',
// eslint-disable-next-line no-nested-ternary
use: isDev
? [esbuild]
: isModern
? [esbuild]
: [babel, esbuild],
use: isDev || isModern ? [esbuild] : [babel, esbuild],
},
{
test: /\.(png|jpe?g|gif|webp)$/i,
Expand Down Expand Up @@ -308,4 +310,4 @@ export default async (config, options = {}) => {
}

return webpackBaseConfig;
};
}
11 changes: 8 additions & 3 deletions packages/webpack-config/src/webpack.dev.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import webpackMerge from 'webpack-merge';
import getWebpackConfig from './webpack.base.config.js';

export default async (config, options) => {
const baseConfig = await getWebpackConfig(config, options);
/**
* Get Webpack dev config.
* @param {import('./index').MetaConfig} config Meta config.
* @returns {import('webpack').Configuration}
*/
export default async function getWebpackDevConfig(config) {
const baseConfig = await getWebpackConfig(config);
const devConfig = webpackMerge.merge(baseConfig, {
mode: 'development',
devtool: 'cheap-source-map',
Expand All @@ -29,4 +34,4 @@ export default async (config, options) => {
}

return devConfig;
};
}
10 changes: 8 additions & 2 deletions packages/webpack-config/src/webpack.prod.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import webpackMerge from 'webpack-merge';
import getWebpackConfig from './webpack.base.config.js';

export default async (config, options) => {
/**
* Get Webpack prod config.
* @param {import('./index').MetaConfig} config
* @param {{ isModern?: boolean, isLegacy?: boolean }} [options]
* @returns {import('webpack').Configuration}
*/
export default async function getWebpackProdConfig(config, options = {}) {
const baseConfig = await getWebpackConfig(config, options);

const prodConfig = webpackMerge.merge(baseConfig, {
Expand All @@ -13,4 +19,4 @@ export default async (config, options) => {
}

return prodConfig;
};
}

0 comments on commit 54bfefc

Please sign in to comment.