Skip to content

Commit

Permalink
perf(webpack-build): configured webpack to bundle core code into mini…
Browse files Browse the repository at this point in the history
…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
codejedi365 committed Nov 6, 2021
1 parent 1e6f4dc commit 212f3bf
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 10 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "eslint-plugin-testcafe-community",
"version": "0.0.0-semantically-versioned",
"description": "ESLint rules & globals for TestCafe, from the TestCafe community",
"main": "dist/lib/index.js",
"main": "dist/plugin.min.cjs",
"files": [
"dist/**",
"docs/**"
Expand Down Expand Up @@ -31,7 +31,7 @@
"testcafe"
],
"scripts": {
"build": "tsc",
"build": "webpack --mode=production --node-env=production",
"commit": "git commit",
"commit-retry": "CZ_RETRY=true git commit",
"format": "npm run lint -- --fix",
Expand All @@ -41,7 +41,7 @@
"test": "run-s --silent test:*",
"test:unit": "jest --selectProjects UNIT",
"test:plugin": "jest --selectProjects INTEGRATION --coverage=false",
"generate-readme-table": "ts-node build/generate-readme-table.ts",
"generate-readme-table": "ts-node -O '{\"module\":\"commonjs\"}' build/generate-readme-table.ts",
"prepare": "is-ci || husky install",
"watch": "npm run test:unit -- --watch --"
},
Expand Down
23 changes: 16 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// Module value notes:
// - commonjs is incompatible with webpack's tree-shake algorithm
// - amd does not shake out JSON as well and likely other modules, it was also longer (100+ lines) in length than es2015
// - resolveJsonModules flag is only compatible with commonjs, amd, es2015, ESNext
// Chose es2015 for best webpack build
// - SIDE EFFECT: ts-node does not like .ts scripts => es2015, since imports are is not in "module" (.mjs) file
// '–> RESOLUTION: CLI option -O, --compiler-options "<JSON>" to merge override module to commonjs
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */

// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationDir": "dist", /* Configure the root directory for where declaration files are emitted */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
Expand All @@ -40,16 +48,17 @@
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
"resolveJsonModule": true,
"noFallthroughCasesInSwitch": false, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"resolveJsonModule": true, /* Allows importing modules with a '.json' extension, which is common in node projects. TS does NOT resolve JSON files by default. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
Expand Down
83 changes: 83 additions & 0 deletions webpack.config.js
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;
};

0 comments on commit 212f3bf

Please sign in to comment.