-
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
5 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/project-utils/bundling/function/bundlers/BaseFunctionBundler.js
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class BaseFunctionBundler { | ||
bundlerConfig = {}; | ||
|
||
build() { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
watch() { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
setBundlerConfig(config) { | ||
if (typeof config === 'function') { | ||
this.bundlerConfig = config(this.bundlerConfig); | ||
} | ||
|
||
this.bundlerConfig = config; | ||
} | ||
|
||
getBundlerConfig() { | ||
return this.bundlerConfig | ||
} | ||
} | ||
|
||
module.exports = { BaseFunctionBundler }; |
87 changes: 87 additions & 0 deletions
87
packages/project-utils/bundling/function/bundlers/RspackBundler.js
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages"); | ||
const { BaseFunctionBundler } = require("./BaseFunctionBundler"); | ||
|
||
class RspackBundler extends BaseFunctionBundler { | ||
constructor({ cwd, overrides }) { | ||
super(); | ||
this.cwd = cwd; | ||
this.overrides = overrides; | ||
} | ||
|
||
build() { | ||
return new Promise(async (resolve, reject) => { | ||
const bundlerConfig = require("./rspack/rspack.config.js")({ | ||
cwd: this.cwd, | ||
overrides: this.overrides, | ||
production: true | ||
}); | ||
|
||
require("@rspack/core")(bundlerConfig, 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; | ||
} | ||
|
||
console.log(`Compiled successfully.`); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
watch() { | ||
return new Promise(async (resolve, reject) => { | ||
console.log("Compiling..."); | ||
|
||
const bundlerConfig = require("./rspack/rspack.config.js")({ | ||
cwd: this.cwd, | ||
overrides: this.overrides, | ||
production: false | ||
}); | ||
|
||
return require("@rspack/core")(bundlerConfig).watch({}, async (err, stats) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
if (!stats.hasErrors()) { | ||
console.log("Compiled successfully."); | ||
} else { | ||
console.log(stats.toString("errors-warnings")); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = {RspackBundler}; |
87 changes: 87 additions & 0 deletions
87
packages/project-utils/bundling/function/bundlers/WebpackBundler.js
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages"); | ||
const { BaseFunctionBundler } = require("./BaseFunctionBundler"); | ||
|
||
class WebpackBundler extends BaseFunctionBundler { | ||
constructor({ cwd, overrides }) { | ||
super(); | ||
this.cwd = cwd; | ||
this.overrides = overrides; | ||
} | ||
|
||
build() { | ||
return new Promise(async (resolve, reject) => { | ||
const bundlerConfig = require("./webpack/webpack.config.js")({ | ||
cwd: this.cwd, | ||
overrides: this.overrides, | ||
production: true | ||
}); | ||
|
||
require("webpack")(bundlerConfig, 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; | ||
} | ||
|
||
console.log(`Compiled successfully.`); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
watch() { | ||
return new Promise(async (resolve, reject) => { | ||
console.log("Compiling..."); | ||
|
||
const bundlerConfig = require("./webpack/webpack.config.js")({ | ||
cwd: this.cwd, | ||
overrides: this.overrides, | ||
production: false | ||
}); | ||
|
||
return require("webpack")(bundlerConfig).watch({}, async (err, stats) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
if (!stats.hasErrors()) { | ||
console.log("Compiled successfully."); | ||
} else { | ||
console.log(stats.toString("errors-warnings")); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { WebpackBundler }; |
115 changes: 115 additions & 0 deletions
115
packages/project-utils/bundling/function/bundlers/rspack/rspack.config.js
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
const path = require("path"); | ||
const rspack = require("@rspack/core"); | ||
const { version } = require("@webiny/project-utils/package.json"); | ||
const { getOutput, getEntry } = require("../../utils"); | ||
const { TsCheckerRspackPlugin } = require("ts-checker-rspack-plugin"); | ||
|
||
module.exports = options => { | ||
const output = getOutput(options); | ||
const entry = getEntry(options); | ||
|
||
const { cwd, overrides, production, watch } = options; | ||
|
||
let swcOptions = require("./swcrc"); | ||
// Customize Babel options. | ||
if (typeof overrides.swc === "function") { | ||
swcOptions = overrides.swc(swcOptions); | ||
} | ||
|
||
const sourceMaps = options.sourceMaps !== false; | ||
|
||
const definitions = overrides.define ? JSON.parse(overrides.define) : {}; | ||
const tsChecksEnabled = process.env.WEBINY_ENABLE_TS_CHECKS === "true"; | ||
|
||
return { | ||
watch, | ||
entry: [ | ||
sourceMaps && require.resolve("source-map-support/register"), | ||
path.resolve(entry) | ||
].filter(Boolean), | ||
target: "node", | ||
output: { | ||
libraryTarget: "commonjs", | ||
path: output.path, | ||
filename: output.filename, | ||
chunkFilename: `[name].[contenthash:8].chunk.js` | ||
}, | ||
devtool: sourceMaps ? "source-map" : false, | ||
externals: [/^@aws-sdk/, /^sharp$/], | ||
mode: production ? "production" : "development", | ||
optimization: { | ||
minimize: production | ||
}, | ||
performance: { | ||
// Turn off size warnings for entry points | ||
hints: false | ||
}, | ||
plugins: [ | ||
new rspack.DefinePlugin({ | ||
"process.env.WEBINY_VERSION": JSON.stringify(process.env.WEBINY_VERSION || version), | ||
...definitions | ||
}), | ||
/** | ||
* This is necessary to enable JSDOM usage in Lambda. | ||
*/ | ||
new rspack.IgnorePlugin({ | ||
resourceRegExp: /canvas/, | ||
contextRegExp: /jsdom$/ | ||
}), | ||
new rspack.ProgressPlugin(), | ||
// new TsCheckerRspackPlugin() | ||
].filter(Boolean), | ||
|
||
module: { | ||
rules: [ | ||
{ | ||
oneOf: [ | ||
sourceMaps && { | ||
test: /\.js$/, | ||
enforce: "pre", | ||
use: [require.resolve("source-map-loader")] | ||
}, | ||
{ | ||
test: /\.mjs$/, | ||
include: /node_modules/, | ||
type: "javascript/auto", | ||
resolve: { | ||
fullySpecified: false | ||
} | ||
}, | ||
{ | ||
test: /\.(js|ts)$/, | ||
loader: "builtin:swc-loader", | ||
exclude: /node_modules/, | ||
options: { | ||
jsc: { | ||
baseUrl: cwd, | ||
paths: { | ||
"~/*": ["src/*"], | ||
"~": ["src"] | ||
} | ||
} | ||
} | ||
} | ||
].filter(Boolean) | ||
}, | ||
/** | ||
* Some NPM libraries import CSS automatically, and that breaks the build. | ||
* To eliminate the problem, we use the `null-loader` to ignore CSS. | ||
*/ | ||
{ | ||
test: /\.css$/, | ||
loader: require.resolve("null-loader") | ||
} | ||
] | ||
}, | ||
resolve: { | ||
alias: { | ||
// Force `lexical` to use the CJS export. | ||
lexical: require.resolve("lexical") | ||
}, | ||
modules: [path.resolve(path.join(cwd, "node_modules")), "node_modules"], | ||
extensions: [".ts", ".mjs", ".js", ".json", ".css"] | ||
} | ||
}; | ||
}; |
Empty file.