Skip to content

Commit

Permalink
Merge pull request #1848 from gregmartyn/module-config
Browse files Browse the repository at this point in the history
add support for type:module razzle.config.js
  • Loading branch information
fivethreeo authored Oct 25, 2022
2 parents dd97fca + 41df166 commit 719e257
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/razzle/config/loadRazzleConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,39 @@ const defaultRazzleOptions = require('./defaultOptions');
const setupEnvironment = require('./env').setupEnvironment;
const loadPlugins = require('./loadPlugins');

const getModuleFormat = packageJson => {
// See https://nodejs.org/api/packages.html#type for more info on "type"
return packageJson.type || "commonjs";
};

module.exports = (webpackObject, razzleConfig, packageJsonIn) => {
return new Promise(async resolve => {

let razzle = razzleConfig || {};
let packageJson = packageJsonIn || {};
let paths = Object.assign({}, defaultPaths);
// Check for razzle.config.js file
if (fs.existsSync(paths.appRazzleConfig)) {

if (fs.existsSync(paths.appPackageJson)) {
try {
razzle = require(paths.appRazzleConfig);
packageJson = require(paths.appPackageJson);
} catch (e) {
clearConsole();
logger.error('Invalid razzle.config.js file.', e);
logger.error('Invalid package.json.', e);
process.exit(1);
}
}
if (fs.existsSync(paths.appPackageJson)) {

// Check for razzle.config.js file
if (fs.existsSync(paths.appRazzleConfig)) {
try {
packageJson = require(paths.appPackageJson);
if (getModuleFormat(packageJson) === 'module') {
razzle = await import(paths.appRazzleConfig);
}
else {
razzle = require(paths.appRazzleConfig);
}
} catch (e) {
clearConsole();
logger.error('Invalid package.json.', e);
logger.error('Invalid razzle.config.js file.', e);
process.exit(1);
}
}
Expand Down

0 comments on commit 719e257

Please sign in to comment.