diff --git a/examples/kitchensink-ts/tsconfig-server.json b/examples/kitchensink-ts/tsconfig-server.json index 898f9bc323..71f5756632 100644 --- a/examples/kitchensink-ts/tsconfig-server.json +++ b/examples/kitchensink-ts/tsconfig-server.json @@ -17,5 +17,5 @@ }, // TODO remove the include when we fix the pluginsPath "include": ["src/plugins/**/*.json", "./"], - "exclude": ["node_modules/", "dist/", "src/admin", ".cache/", ".tmp/"] + "exclude": ["node_modules/", "build/", "dist/", "src/admin", ".cache/", ".tmp/"] } diff --git a/packages/core/strapi/lib/commands/develop.js b/packages/core/strapi/lib/commands/develop.js index a699a9662a..347fa51fb1 100644 --- a/packages/core/strapi/lib/commands/develop.js +++ b/packages/core/strapi/lib/commands/develop.js @@ -18,17 +18,16 @@ const { buildTypeScript, buildAdmin } = require('./builders'); * */ module.exports = async function({ build, watchAdmin, polling, browser }) { - const currentDirectory = process.cwd(); + const appDir = process.cwd(); - const isTSProject = await tsUtils.isTypeScriptProject(currentDirectory); - const buildDestDir = isTSProject ? path.join(currentDirectory, 'dist') : currentDirectory; + const isTSProject = await tsUtils.isTypeScriptProject(appDir); + const distDir = isTSProject ? path.join(appDir, 'dist') : appDir; try { if (cluster.isMaster || cluster.isPrimary) { return primaryProcess({ - buildDestDir, - currentDirectory, - dir: buildDestDir, + distDir, + appDir, build, browser, isTSProject, @@ -37,7 +36,7 @@ module.exports = async function({ build, watchAdmin, polling, browser }) { } if (cluster.isWorker) { - return workerProcess({ dir: buildDestDir, watchAdmin, polling }); + return workerProcess({ appDir, distDir, watchAdmin, polling, isTSProject }); } } catch (e) { console.error(e); @@ -45,32 +44,25 @@ module.exports = async function({ build, watchAdmin, polling, browser }) { } }; -const primaryProcess = async ({ - buildDestDir, - currentDirectory, - build, - isTSProject, - watchAdmin, - browser, -}) => { +const primaryProcess = async ({ distDir, appDir, build, isTSProject, watchAdmin, browser }) => { if (isTSProject) { - await buildTypeScript({ srcDir: currentDirectory, watch: true }); + await buildTypeScript({ srcDir: appDir, watch: false }); } - const config = loadConfiguration(buildDestDir); + const config = loadConfiguration(distDir); const serveAdminPanel = getOr(true, 'admin.serveAdminPanel')(config); - const buildExists = fs.existsSync(path.join(buildDestDir, 'build')); + const buildExists = fs.existsSync(path.join(distDir, 'build')); // Don't run the build process if the admin is in watch mode if (build && !watchAdmin && serveAdminPanel && !buildExists) { try { await buildAdmin({ - buildDestDir, + buildDestDir: distDir, forceBuild: false, isTSProject, optimization: false, - srcDir: currentDirectory, + srcDir: appDir, }); } catch (err) { process.exit(1); @@ -106,19 +98,20 @@ const primaryProcess = async ({ cluster.fork(); }; -const workerProcess = ({ dir, watchAdmin, polling }) => { +const workerProcess = ({ appDir, distDir, watchAdmin, polling, isTSProject }) => { const strapiInstance = strapi({ - distDir: dir, + distDir, autoReload: true, serveAdminPanel: watchAdmin ? false : true, }); const adminWatchIgnoreFiles = strapiInstance.config.get('admin.watchIgnoreFiles', []); watchFileChanges({ - dir, + appDir, strapiInstance, watchIgnoreFiles: adminWatchIgnoreFiles, polling, + isTSProject, }); process.on('message', async message => { @@ -138,19 +131,24 @@ const workerProcess = ({ dir, watchAdmin, polling }) => { /** * Init file watching to auto restart strapi app * @param {Object} options - Options object - * @param {string} options.dir - This is the path where the app is located, the watcher will watch the files under this folder + * @param {string} options.appDir - This is the path where the app is located, the watcher will watch the files under this folder * @param {Strapi} options.strapi - Strapi instance * @param {array} options.watchIgnoreFiles - Array of custom file paths that should not be watched */ -function watchFileChanges({ dir, strapiInstance, watchIgnoreFiles, polling }) { - const restart = () => { +function watchFileChanges({ appDir, strapiInstance, watchIgnoreFiles, polling, isTSProject }) { + const restart = async () => { if (strapiInstance.reload.isWatching && !strapiInstance.reload.isReloading) { strapiInstance.reload.isReloading = true; + + if (isTSProject) { + await tsUtils.compile(appDir); + } + strapiInstance.reload(); } }; - const watcher = chokidar.watch(dir, { + const watcher = chokidar.watch(appDir, { ignoreInitial: true, usePolling: polling, ignored: [ @@ -175,6 +173,7 @@ function watchFileChanges({ dir, strapiInstance, watchIgnoreFiles, polling }) { joinBy('/', strapiInstance.dirs.public, '**'), '**/*.db*', '**/exports/**', + '**/dist/**', ...watchIgnoreFiles, ], }); diff --git a/packages/generators/app/lib/resources/json/ts/tsconfig-server.json.js b/packages/generators/app/lib/resources/json/ts/tsconfig-server.json.js index 34a19db241..00a2890044 100644 --- a/packages/generators/app/lib/resources/json/ts/tsconfig-server.json.js +++ b/packages/generators/app/lib/resources/json/ts/tsconfig-server.json.js @@ -16,5 +16,5 @@ module.exports = () => ({ noEmitOnError: true, }, - exclude: ['node_modules/', 'dist/', 'src/admin', 'src/plugins/**/admin'], + exclude: ['node_modules/', 'build/', 'dist/', 'src/admin', 'src/plugins/**/admin'], }); diff --git a/packages/utils/typescript/lib/compile.js b/packages/utils/typescript/lib/compile.js index e573833dc8..ef9a441ea3 100644 --- a/packages/utils/typescript/lib/compile.js +++ b/packages/utils/typescript/lib/compile.js @@ -4,7 +4,7 @@ const compilers = require('./compilers'); const getConfigPath = require('./utils/get-config-path'); const copyResources = require('./utils/copy-resources'); -module.exports = async (srcDir, { watch = false }) => { +module.exports = async (srcDir, { watch = false } = {}) => { // TODO: Use the Strapi debug logger instead or don't log at all console.log(`Starting the compilation for TypeScript files in ${srcDir}`);