From 32c60e92484468fe6f1ef368fc22a3b4309e5303 Mon Sep 17 00:00:00 2001 From: LFades Date: Tue, 11 Jan 2022 13:15:02 -0500 Subject: [PATCH] Updated commerce config --- packages/commerce/package.json | 10 ++++-- packages/commerce/src/config.js | 30 +++++++++++++++++ .../config.js => site/commerce-config.js | 33 ++++--------------- site/next.config.js | 5 +-- site/tsconfig.json | 16 ++------- 5 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 packages/commerce/src/config.js rename packages/commerce/config.js => site/commerce-config.js (63%) diff --git a/packages/commerce/package.json b/packages/commerce/package.json index 334f57ce6..c87874587 100644 --- a/packages/commerce/package.json +++ b/packages/commerce/package.json @@ -9,13 +9,19 @@ "sideEffects": false, "type": "module", "exports": { - "*": "./dist/*" + "*": "./dist/*", + "config": "./config.js" }, "typesVersions": { "*": { "*": [ - "src/*", + "src/*" + ], + "api": [ "src/api/index.ts" + ], + "config": [ + "dist/config.d.ts" ] } }, diff --git a/packages/commerce/src/config.js b/packages/commerce/src/config.js new file mode 100644 index 000000000..a7ef7070a --- /dev/null +++ b/packages/commerce/src/config.js @@ -0,0 +1,30 @@ +/** + * This file is expected to be used in next.config.js only + */ + +const path = require('path') +const merge = require('deepmerge') + +function withCommerceConfig(nextConfig = {}) { + const commerce = nextConfig.commerce || {} + const { provider } = commerce + + if (!provider) { + throw new Error( + `The commerce provider is missing, please add a valid provider name` + ) + } + + const commerceNextConfig = require(path.join(provider, 'next.config')) + const config = merge(nextConfig, commerceNextConfig) + + config.env = config.env || {} + + Object.entries(config.commerce.features).forEach(([k, v]) => { + if (v) config.env[`COMMERCE_${k.toUpperCase()}_ENABLED`] = true + }) + + return config +} + +module.exports = { withCommerceConfig } diff --git a/packages/commerce/config.js b/site/commerce-config.js similarity index 63% rename from packages/commerce/config.js rename to site/commerce-config.js index 1a48dc456..cc261f483 100644 --- a/packages/commerce/config.js +++ b/site/commerce-config.js @@ -4,8 +4,8 @@ const path = require('path') const fs = require('fs') -const merge = require('deepmerge') const prettier = require('prettier') +const core = require('@vercel/commerce/config') const PROVIDERS = [ 'local', @@ -50,43 +50,24 @@ function withCommerceConfig(nextConfig = {}) { ) } - const commerceNextConfig = require(path.join('../', name, 'next.config')) - const config = merge(nextConfig, commerceNextConfig) - - config.env = config.env || {} - - Object.entries(config.commerce.features).forEach(([k, v]) => { - if (v) config.env[`COMMERCE_${k.toUpperCase()}_ENABLED`] = true - }) - // Update paths in `tsconfig.json` to point to the selected provider - if (config.commerce.updateTSConfig !== false) { - const tsconfigPath = path.join(process.cwd(), 'tsconfig.json') + if (nextConfig.commerce.updateTSConfig !== false) { + const tsconfigPath = path.join( + process.cwd(), + commerce.tsconfigPath || 'tsconfig.json' + ) const tsconfig = require(tsconfigPath) tsconfig.compilerOptions.paths['@framework'] = [`framework/${name}`] tsconfig.compilerOptions.paths['@framework/*'] = [`framework/${name}/*`] - // When running for production it may be useful to exclude the other providers - // from TS checking - if (process.env.VERCEL) { - const exclude = tsconfig.exclude.filter( - (item) => !item.startsWith('framework/') - ) - - tsconfig.exclude = PROVIDERS.reduce((exclude, current) => { - if (current !== name) exclude.push(`framework/${current}`) - return exclude - }, exclude) - } - fs.writeFileSync( tsconfigPath, prettier.format(JSON.stringify(tsconfig), { parser: 'json' }) ) } - return config + return core.withCommerceConfig(nextConfig) } module.exports = { withCommerceConfig, getProviderName } diff --git a/site/next.config.js b/site/next.config.js index 4349ba05a..b81aa3e4e 100644 --- a/site/next.config.js +++ b/site/next.config.js @@ -1,8 +1,5 @@ const commerce = require('./commerce.config.json') -const { - withCommerceConfig, - getProviderName, -} = require('./framework/commerce/config') +const { withCommerceConfig, getProviderName } = require('./commerce-config') const provider = commerce.provider || getProviderName() const isBC = provider === 'bigcommerce' diff --git a/site/tsconfig.json b/site/tsconfig.json index bb7de96f8..f9727d6a5 100644 --- a/site/tsconfig.json +++ b/site/tsconfig.json @@ -27,18 +27,6 @@ "@framework/*": ["framework/local/*"] } }, - "include": ["site/next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"], - "exclude": [ - "node_modules", - "./framework/local", - "./framework/bigcommerce", - "./framework/shopify", - "./framework/swell", - "./framework/vendure", - "./framework/saleor", - "./framework/spree", - "./framework/ordercloud", - "./framework/kibocommerce", - "./framework/commercejs" - ] + "include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"], + "exclude": ["node_modules"] }