-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(webpack-build): configured webpack to bundle core code into mini…
…fied production build Collapsed core code into single bundle except for core Node.js modules & runtime node_modules dependencies BREAKING CHANGE: Distribution code is now bundled into a single encapsulated module. No exposed libraries available as they were before `createRule()` ex. Only exports eslint required members.Types not yet provided.
- Loading branch information
1 parent
1e6f4dc
commit 212f3bf
Showing
3 changed files
with
102 additions
and
10 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
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,83 @@ | ||
/** | ||
* Webpack@5 build configuration | ||
* @author codejedi365 | ||
*/ | ||
// IMPORTS | ||
const { resolve, dirname, basename } = require("path"); | ||
const nodeExternals = require("webpack-node-externals"); | ||
const thisModule = require("./package.json"); | ||
|
||
// CONSTANTS | ||
const isProduction = process.env.NODE_ENV === "production"; | ||
const pkgDir = __dirname; | ||
const entrypoint = resolve(pkgDir, "lib", "index.ts"); | ||
const outDir = (() => { | ||
const dir = resolve( | ||
__dirname, | ||
/^\.{1,2}|\/$/.test(dirname(thisModule.main)) | ||
? "dist" | ||
: dirname(thisModule.main) | ||
); | ||
if (dir.search(pkgDir)) { | ||
// Prevent malicious cleaning of directories outside of project | ||
throw new Error("WARNING: outDir points outside of project directory"); | ||
} | ||
return dir; | ||
})(); | ||
|
||
/** | ||
* Dynamic configuration function | ||
* @param env webpack environment object | ||
* @returns Webpack configuration object | ||
*/ | ||
function buildConfig() { | ||
return { | ||
entry: entrypoint, | ||
output: { | ||
clean: true, | ||
path: outDir, | ||
filename: basename(thisModule.main), | ||
library: { | ||
type: "commonjs" | ||
} | ||
}, | ||
plugins: [], | ||
module: { | ||
rules: [ | ||
{ | ||
test: /(?<!\.test)\.(ts)$/i, | ||
loader: "ts-loader", | ||
options: { | ||
onlyCompileBundledFiles: true | ||
}, | ||
exclude: ["/node_modules/"] | ||
} | ||
] | ||
}, | ||
optimization: { | ||
// minimize & mangle the output files (TerserPlugin w/ webpack@v5) | ||
minimize: isProduction, | ||
// determine which exports are used by modules and removed unused ones | ||
usedExports: true | ||
}, | ||
resolve: { | ||
extensions: [".ts", ".js", ".json"] | ||
}, | ||
// ignore all modules in node_modules folder (ie. do not bundle runtime dependencies) | ||
externals: [nodeExternals()], | ||
externalsPresets: { | ||
// ignore node built-in modules like path, fs, etc. | ||
node: true | ||
} | ||
}; | ||
} | ||
|
||
module.exports = (env) => { | ||
const config = buildConfig(env); | ||
if (isProduction) { | ||
config.mode = "production"; | ||
} else { | ||
config.mode = "development"; | ||
} | ||
return config; | ||
}; |