From 0e2fbe0e8fe96ed92b009cf9c03f9f10261a13ec Mon Sep 17 00:00:00 2001 From: James Date: Wed, 25 Nov 2020 18:57:17 -0500 Subject: [PATCH] enables build w/ typescript, refines the way configs are found --- bin.js | 1 - package.json | 4 +++- src/bin/build.ts | 26 ++++++++++++++++++++---- src/config/find.ts | 31 ++++++++++------------------- src/webpack/getWebpackProdConfig.ts | 8 ++++++-- tsconfig.json | 3 --- yarn.lock | 29 +++++++++++++++++++++++++++ 7 files changed, 71 insertions(+), 31 deletions(-) diff --git a/bin.js b/bin.js index 53f489704..317b4712e 100755 --- a/bin.js +++ b/bin.js @@ -1,3 +1,2 @@ #!/usr/bin/env node - require('./dist/src/bin'); diff --git a/package.json b/package.json index c4ab1c64f..e5f49a5b1 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "scripts": { "copyfiles": "copyfiles src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png} dist/", "build:components": "webpack --config src/webpack/components.config.js", - "build": "cross-env PAYLOAD_CONFIG_PATH=demo/payload.config.js node src/bin/build", + "build": "cross-env PAYLOAD_CONFIG_PATH=demo/payload.config.js node dist/src/bin/build", "build:tsc": "tsc", "build:analyze": "cross-env PAYLOAD_CONFIG_PATH=demo/payload.config.js PAYLOAD_ANALYZE_BUNDLE=true node src/bin/build", "cov": "npm run core:build && node ./node_modules/jest/bin/jest.js src/tests --coverage", @@ -62,6 +62,7 @@ "express-rate-limit": "^5.1.3", "falsey": "^1.0.0", "file-loader": "^1.1.11", + "find-up": "^5.0.0", "flatley": "^5.2.0", "graphql": "15.4.0", "graphql-playground-middleware-express": "^1.7.14", @@ -156,6 +157,7 @@ "@types/express-rate-limit": "^5.1.0", "@types/extract-text-webpack-plugin": "^3.0.4", "@types/file-loader": "^4.2.0", + "@types/find-up": "^4.0.0", "@types/html-webpack-plugin": "^3.2.4", "@types/ignore-styles": "^5.0.0", "@types/is-hotkey": "^0.1.2", diff --git a/src/bin/build.ts b/src/bin/build.ts index bdf46f9e4..4b4d0a741 100755 --- a/src/bin/build.ts +++ b/src/bin/build.ts @@ -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, diff --git a/src/config/find.ts b/src/config/find.ts index 7369ca618..da6f2f0a3 100644 --- a/src/config/find.ts +++ b/src/config/find.ts @@ -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".'); }; diff --git a/src/webpack/getWebpackProdConfig.ts b/src/webpack/getWebpackProdConfig.ts index f72ccd146..93757c134 100644 --- a/src/webpack/getWebpackProdConfig.ts +++ b/src/webpack/getWebpackProdConfig.ts @@ -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) diff --git a/tsconfig.json b/tsconfig.json index d932b841f..791acc1fe 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,9 +15,6 @@ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ "baseUrl": "", "paths": { - "payload/config": [ - "src/admin/types/config" // Webpack alias to user config - ], "@payloadcms/payload/config": [ "src/config/types.ts" ], diff --git a/yarn.lock b/yarn.lock index b7e2e09e6..e67742e36 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1644,6 +1644,13 @@ dependencies: "@types/webpack" "*" +"@types/find-up@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@types/find-up/-/find-up-4.0.0.tgz#6b74a76670477a23f0793cfaf2dafc86df59723a" + integrity sha512-QlRNKeOPFWKisbNtKVOOGXw3AeLbkw8UmT/EyEGM6brfqpYffKBcch7f1y40NYN9O90aK2+K0xBMDJfOAsg2qg== + dependencies: + find-up "*" + "@types/graceful-fs@^4.1.2": version "4.1.4" resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.4.tgz#4ff9f641a7c6d1a3508ff88bc3141b152772e753" @@ -5404,6 +5411,14 @@ find-root@^1.1.0: resolved "https://registry.yarnpkg.com/find-root/-/find-root-1.1.0.tgz#abcfc8ba76f708c42a97b3d685b7e9450bfb9ce4" integrity sha512-NKfW6bec6GfKc0SGx1e07QZY9PE99u0Bft/0rzSD5k3sO/vwkVUpDUKVm5Gpp5Ue3YfShPFTX2070tDs5kB9Ng== +find-up@*, find-up@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/find-up/-/find-up-5.0.0.tgz#4c92819ecb7083561e4f4a240a86be5198f536fc" + integrity sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng== + dependencies: + locate-path "^6.0.0" + path-exists "^4.0.0" + find-up@^1.0.0: version "1.1.2" resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f" @@ -7430,6 +7445,13 @@ locate-path@^5.0.0: dependencies: p-locate "^4.1.0" +locate-path@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-6.0.0.tgz#55321eb309febbc59c4801d931a72452a681d286" + integrity sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw== + dependencies: + p-locate "^5.0.0" + lockfile@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/lockfile/-/lockfile-1.0.4.tgz#07f819d25ae48f87e538e6578b6964a4981a5609" @@ -8531,6 +8553,13 @@ p-locate@^4.1.0: dependencies: p-limit "^2.2.0" +p-locate@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834" + integrity sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw== + dependencies: + p-limit "^3.0.2" + p-map@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/p-map/-/p-map-4.0.0.tgz#bb2f95a5eda2ec168ec9274e06a747c3e2904d2b"