Updated commerce config

This commit is contained in:
LFades 2022-01-11 13:15:02 -05:00
parent 1735b9e5ab
commit 32c60e9248
5 changed files with 48 additions and 46 deletions

View File

@ -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"
]
}
},

View File

@ -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 }

View File

@ -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 }

View File

@ -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'

View File

@ -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"]
}