-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
355 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,13 @@ | ||
const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages"); | ||
const { getProjectApplication } = require("@webiny/cli/utils"); | ||
const { RspackBundler } = require("./bundlers/RspackBundler"); | ||
const { WebpackBundler } = require("./bundlers/WebpackBundler"); | ||
|
||
module.exports = async options => { | ||
const { overrides, logs, cwd, debug } = options; | ||
const { overrides, cwd } = options; | ||
|
||
let projectApplication; | ||
try { | ||
projectApplication = getProjectApplication({ cwd }); | ||
} catch { | ||
// No need to do anything. | ||
} | ||
const { featureFlags } = require("@webiny/feature-flags"); | ||
const bundler = featureFlags.rspack | ||
? new RspackBundler({ cwd, overrides }) | ||
: new WebpackBundler({ cwd, overrides }); | ||
|
||
let webpackConfig = require("./webpack.config")({ | ||
production: !debug, | ||
projectApplication, | ||
...options | ||
}); | ||
|
||
// Customize Webpack config. | ||
if (typeof overrides.webpack === "function") { | ||
webpackConfig = overrides.webpack(webpackConfig); | ||
} | ||
|
||
const webpack = require("webpack"); | ||
|
||
return new Promise(async (resolve, reject) => { | ||
webpack(webpackConfig).run(async (err, stats) => { | ||
let messages = {}; | ||
|
||
if (err) { | ||
messages = formatWebpackMessages({ | ||
errors: [err.message], | ||
warnings: [] | ||
}); | ||
|
||
const errorMessages = messages.errors.join("\n\n"); | ||
console.error(errorMessages); | ||
return reject(new Error(errorMessages)); | ||
} | ||
|
||
if (stats.hasErrors()) { | ||
messages = formatWebpackMessages( | ||
stats.toJson({ | ||
all: false, | ||
warnings: true, | ||
errors: true | ||
}) | ||
); | ||
} | ||
|
||
if (Array.isArray(messages.errors) && messages.errors.length) { | ||
// Only keep the first error. Others are often indicative | ||
// of the same problem, but confuse the reader with noise. | ||
if (messages.errors.length > 1) { | ||
messages.errors.length = 1; | ||
} | ||
|
||
const errorMessages = messages.errors.join("\n\n"); | ||
console.error(errorMessages); | ||
reject(new Error(errorMessages)); | ||
return; | ||
} | ||
|
||
logs && console.log(`Compiled successfully.`); | ||
resolve(); | ||
}); | ||
}); | ||
return bundler.build(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,13 @@ | ||
const { getProjectApplication } = require("@webiny/cli/utils"); | ||
const { RspackBundler } = require("./bundlers/RspackBundler"); | ||
const { WebpackBundler } = require("./bundlers/WebpackBundler"); | ||
|
||
module.exports = async options => { | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (!options.cwd) { | ||
options.cwd = process.cwd(); | ||
} | ||
const webpack = require("webpack"); | ||
|
||
const { overrides, cwd } = options; | ||
|
||
let projectApplication; | ||
try { | ||
projectApplication = getProjectApplication({ cwd }); | ||
} catch { | ||
// No need to do anything. | ||
} | ||
|
||
// Load base webpack config | ||
let webpackConfig = require("./webpack.config")({ | ||
production: false, | ||
projectApplication, | ||
...options | ||
}); | ||
|
||
// Customize Webpack config. | ||
if (typeof overrides.webpack === "function") { | ||
webpackConfig = overrides.webpack(webpackConfig); | ||
} | ||
|
||
return new Promise(async (resolve, reject) => { | ||
let initialCompilation = true; | ||
if (options.logs) { | ||
const message = initialCompilation ? "Initial compilation started..." : "Compiling..."; | ||
console.log(message); | ||
} | ||
|
||
return webpack(webpackConfig).watch({}, async (err, stats) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
if (!options.logs) { | ||
return; | ||
} | ||
|
||
if (stats.hasErrors()) { | ||
console.log(stats.toString("errors-warnings")); | ||
return; | ||
} | ||
const { featureFlags } = require("@webiny/feature-flags"); | ||
const bundler = featureFlags.rspack | ||
? new RspackBundler({ cwd, overrides }) | ||
: new WebpackBundler({ cwd, overrides }); | ||
|
||
if (initialCompilation) { | ||
initialCompilation = false; | ||
console.log("Initial compilation completed. Watching for changes..."); | ||
} | ||
}); | ||
}); | ||
return bundler.watch(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.