enables build w/ typescript, refines the way configs are found

This commit is contained in:
James
2020-11-25 18:57:17 -05:00
parent 56072e952d
commit 0e2fbe0e8f
7 changed files with 71 additions and 31 deletions

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
@@ -5,14 +6,31 @@ import webpack from 'webpack';
import getWebpackProdConfig from '../webpack/getWebpackProdConfig';
import findConfig from '../config/find';
import loadConfig from '../config/load';
import { buildConfig } from '../config/build';
import getBabelConfig from '../babel.config';
const babelConfig = getBabelConfig({
env: () => false,
});
const configPath = findConfig();
const build = () => {
const build = (): void => {
try {
const unsanitizedConfig = loadConfig();
const config = buildConfig(unsanitizedConfig);
require('@babel/register')({
...babelConfig,
extensions: ['.js', '.jsx', '.ts', '.tsx'],
plugins: [
[
require('babel-plugin-ignore-html-and-css-imports'),
{
removeExtensions: ['.svg', '.css', '.scss', '.png', '.jpg'],
},
],
...babelConfig.plugins,
],
});
const config = loadConfig();
const webpackProdConfig = getWebpackProdConfig({
...config,

View File

@@ -1,5 +1,5 @@
import path from 'path';
import fs from 'fs';
import findUp from 'find-up';
const findConfig = (): string => {
// If the developer has specified a config path,
@@ -12,30 +12,21 @@ const findConfig = (): string => {
return path.resolve(process.cwd(), process.env.PAYLOAD_CONFIG_PATH);
}
// By default, Payload is installed as a node_module.
// Traverse up three levels and check for config
const defaultPath = path.resolve(__dirname, '../../../payload.config.js');
const configPath = findUp.sync((dir) => {
const tsPath = path.join(dir, 'payload.config.ts');
const hasTS = findUp.sync.exists(tsPath);
if (fs.existsSync(defaultPath)) {
return defaultPath;
}
if (hasTS) return tsPath;
const defaultTSPath = path.resolve(__dirname, '../../../payload.config.ts');
const jsPath = path.join(dir, 'payload.config.js');
const hasJS = findUp.sync.exists(jsPath);
if (fs.existsSync(defaultTSPath)) {
return defaultTSPath;
}
if (hasJS) return jsPath;
// Check for config in current working directory
const cwdJSPath = path.resolve(process.cwd(), 'payload.config.js');
if (fs.existsSync(cwdJSPath)) {
return cwdJSPath;
}
return undefined;
});
const cwdTSPath = path.resolve(process.cwd(), 'payload.config.ts');
if (fs.existsSync(cwdTSPath)) {
return cwdTSPath;
}
if (configPath) return configPath;
throw new Error('Error: cannot find Payload config. Please create a configuration file located at the root of your current working directory called "payload.config.js" or "payload.config.ts".');
};

View File

@@ -8,7 +8,7 @@ import webpack, { Configuration } from 'webpack';
import { Config } from '../config/types';
import babelConfig from '../babel.config';
const mockModulePath = path.resolve(__dirname, '../mocks/emptyModule.js');
const mockModulePath = path.resolve(__dirname, './mocks/emptyModule.js');
export default (config: Config): Configuration => {
let webpackConfig: Configuration = {
@@ -41,7 +41,7 @@ export default (config: Config): Configuration => {
module: {
rules: [
{
test: /\.js$/,
test: /\.(t|j|)sx?$/,
exclude: /node_modules[\\/](?!(@payloadcms[\\/]payload)[\\/]).*/,
use: {
loader: require.resolve('babel-loader'),
@@ -92,8 +92,12 @@ export default (config: Config): Configuration => {
'payload/config': config.paths.config,
'@payloadcms/payload$': mockModulePath,
},
extensions: ['.ts', '.tsx', '.js', '.json'],
},
plugins: [
new webpack.ProvidePlugin(
{ process: 'process/browser' },
),
new HtmlWebpackPlugin({
template: config.admin && config.admin.indexHTML
? path.join(config.paths.configDir, config.admin.indexHTML)