diff --git a/packages/strapi-admin/index.js b/packages/strapi-admin/index.js index 68d874116a..8a44825b05 100644 --- a/packages/strapi-admin/index.js +++ b/packages/strapi-admin/index.js @@ -73,8 +73,7 @@ async function copyAdmin(dest) { ); } -async function build({ dir, env }) { - console.log('Building your app'); +async function build({ dir, env, options }) { const cacheDir = path.resolve(dir, '.cache'); const pkgJSON = require(path.join(dir, 'package.json')); @@ -97,7 +96,7 @@ async function build({ dir, env }) { const entry = path.resolve(cacheDir, 'admin', 'src', 'app.js'); const dest = path.resolve(dir, 'build'); - const config = getWebpackConfig({ entry, dest, env }); + const config = getWebpackConfig({ entry, dest, env, options }); const compiler = webpack(config); diff --git a/packages/strapi-admin/webpack.config.js b/packages/strapi-admin/webpack.config.js index 9620c48e7c..b61e8c96ff 100644 --- a/packages/strapi-admin/webpack.config.js +++ b/packages/strapi-admin/webpack.config.js @@ -13,12 +13,18 @@ const alias = require('./webpack.alias.js'); // TODO: parametrize const URLs = { - host: '/admin/', - backend: 'http://localhost:1337', mode: 'host', }; -module.exports = ({ publicPath = '/admin/', entry, dest, env }) => { +module.exports = ({ + entry, + dest, + env, + options = { + backend: 'http://localhost:1337', + publicPath: '/admin/', + }, +}) => { const isProduction = env === 'production'; const webpackPlugins = isProduction @@ -45,7 +51,7 @@ module.exports = ({ publicPath = '/admin/', entry, dest, env }) => { loader: MiniCssExtractPlugin.loader, options: { fallback: require.resolve('style-loader'), - publicPath: publicPath, + publicPath: options.publicPath, }, }, ] @@ -58,7 +64,7 @@ module.exports = ({ publicPath = '/admin/', entry, dest, env }) => { entry, output: { path: dest, - publicPath: publicPath, + publicPath: options.publicPath, // Utilize long-term caching by adding content hashes (not compilation hashes) // to compiled assets for production filename: isProduction ? '[name].js' : '[name].[chunkhash].js', @@ -239,12 +245,14 @@ module.exports = ({ publicPath = '/admin/', entry, dest, env }) => { new SimpleProgressWebpackPlugin(), new webpack.DefinePlugin({ - 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV), - NODE_ENV: JSON.stringify(process.env.NODE_ENV), - REMOTE_URL: JSON.stringify(URLs.host), - BACKEND_URL: JSON.stringify(URLs.backend), + 'process.env.NODE_ENV': JSON.stringify( + isProduction ? 'production' : 'development' + ), + NODE_ENV: JSON.stringify(isProduction ? 'production' : 'development'), + REMOTE_URL: JSON.stringify(options.publicPath), + BACKEND_URL: JSON.stringify(options.backend), MODE: JSON.stringify(URLs.mode), // Allow us to define the public path for the plugins assets. - PUBLIC_PATH: JSON.stringify(publicPath), + PUBLIC_PATH: JSON.stringify(options.publicPath), }), ...webpackPlugins, diff --git a/packages/strapi-generate-new/files/server.js b/packages/strapi-generate-new/files/server.js deleted file mode 100755 index e76ad3bdec..0000000000 --- a/packages/strapi-generate-new/files/server.js +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env node -'use strict'; - -/** - * Use `server.js` to run your application without `$ strapi start`. - * To start the server, run: `$ npm start`. - * - * This is handy in situations where the Strapi CLI is not relevant or useful. - */ -const strapi = require('strapi'); - -strapi({ - appPath: __dirname, -}).start(); diff --git a/packages/strapi/bin/strapi.js b/packages/strapi/bin/strapi.js index 8efd21a245..43cccb936a 100755 --- a/packages/strapi/bin/strapi.js +++ b/packages/strapi/bin/strapi.js @@ -145,9 +145,13 @@ program .action(getScript('generate')); program - .command('build') + .command('build [dir]') + .option('--env [env]', 'Build for which env') .description('Builds the strapi admin app') - .action(getScript('build')); + .action((dir, options) => { + const env = options.env || 'production'; + getScript('build')({ dir, env }); + }); /** * Normalize help argument diff --git a/packages/strapi/lib/commands/build.js b/packages/strapi/lib/commands/build.js index 99f3121d14..862903d3da 100644 --- a/packages/strapi/lib/commands/build.js +++ b/packages/strapi/lib/commands/build.js @@ -1,9 +1,35 @@ 'use strict'; +const path = require('path'); +const { green } = require('chalk'); const strapiAdmin = require('strapi-admin'); +const { cli } = require('strapi-utils'); -// build script shoul only run in production mode process.env.NODE_ENV = 'production'; -module.exports = () => { - return strapiAdmin.build({ dir: process.cwd(), env: 'production' }); +// build script shoul only run in production mode +module.exports = ({ dir = '', env }) => { + // Check that we're in a valid Strapi project. + if (!cli.isStrapiApp()) { + return console.log( + `⛔️ ${cyan('strapi start')} can only be used inside a Strapi project.` + ); + } + + const appPath = path.join(process.cwd(), dir); + + console.log(`Building your app with ${green(env)} configuration`); + + // Require server configurations + const server = require(path.resolve( + appPath, + 'config', + 'environments', + env, + 'server.json' + )); + + return strapiAdmin.build({ + dir: process.cwd(), + env: 'production', + }); }; diff --git a/packages/strapi/lib/commands/dev.js b/packages/strapi/lib/commands/dev.js index eb46a6e78f..75fbe7b115 100644 --- a/packages/strapi/lib/commands/dev.js +++ b/packages/strapi/lib/commands/dev.js @@ -16,7 +16,7 @@ const _ = require('lodash'); const fs = require('fs-extra'); const { cyan } = require('chalk'); const chokidar = require('chokidar'); -const buildApp = require('./build'); +const strapiAdmin = require('strapi-admin'); // Logger. const { cli, logger } = require('strapi-utils'); @@ -28,7 +28,9 @@ const { cli, logger } = require('strapi-utils'); * (fire up the application in our working directory). */ -module.exports = async function(appPath = '') { +module.exports = async function(dir = '.') { + process.env.NODE_ENV = 'development'; + // Check that we're in a valid Strapi project. if (!cli.isStrapiApp()) { return console.log( @@ -36,33 +38,29 @@ module.exports = async function(appPath = '') { ); } - appPath = path.join(process.cwd(), appPath); + const appPath = path.join(process.cwd(), dir); + + // Require server configurations + const server = require(path.resolve( + appPath, + 'config', + 'environments', + 'development', + 'server.json' + )); if (!fs.existsSync(path.resolve(appPath, 'build'))) { - await buildApp(); + await strapiAdmin.build({ + dir: process.cwd(), + env: 'production', + }); + } else { } try { const strapiInstance = strapi({ appPath }); - // Set NODE_ENV - if (_.isEmpty(process.env.NODE_ENV)) { - process.env.NODE_ENV = 'development'; - } - - // Require server configurations - const server = require(path.resolve( - appPath, - 'config', - 'environments', - 'development', - 'server.json' - )); - - if ( - process.env.NODE_ENV === 'development' && - _.get(server, 'autoReload.enabled') === true - ) { + if (_.get(server, 'autoReload.enabled') === true) { if (cluster.isMaster) { cluster.on('message', (worker, message) => { switch (message) { @@ -101,27 +99,19 @@ module.exports = async function(appPath = '') { } }); - return strapiInstance.start(afterwards); + return strapiInstance.start(); } else { return; } } - strapiInstance.start(afterwards); + strapiInstance.start(); } catch (e) { logger.error(e); process.exit(1); } }; -function afterwards(err, strapiInstance) { - if (err) { - logger.error(err.stack ? err.stack : err); - - strapiInstance ? strapiInstance.stop() : process.exit(1); - } -} - /** * Init file watching to auto restart strapi app * @param {Object} options - Options object