Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): allow loader bypass, independent tsconfig #7

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 141 additions & 48 deletions DOCS.md

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@

Default webpack development and production configuration.

The goal of `weldable` was to make it easier to install `webpack` build packages and be up and running with a basic
`development` or `production` server with minimal preferences.
The goal of `weldable` is to make it easier to install `webpack` build packages and be up and running with a basic
`development` or `production` build with minimal preferences.

`weldable` is intended...

- To be used for basic webpack development and production builds.
- To be quickly used for basic webpack development and production builds.
- To purposefully not include webpack configurations for linting and styling aspects beyond basic webpack capabilities.
- To be designed with the expectation that you can expand on the `weldable` base using the CLI extend option. `-x ./webpack.exampleConfig.js`.
- To be used as a build resource with exposed webpack plugins/addons. And without the need to reinstall available webpack packages (or at least the ones `weldable` uses).
- To be used as a single build resource with exposed webpack plugins/addons. And without the need to reinstall available webpack packages (or at least the ones `weldable` uses).

[If weldable doesn't work for you, you can always go back to the `webpack` project `init` command](https://webpack.js.org/configuration/#set-up-a-new-webpack-project)

## Requirements
The basic requirements:
Expand All @@ -27,13 +29,13 @@ The basic requirements:
NPM install...

```shell
$ npm i weldable
$ npm i weldable --save-dev
```

or Yarn

```shell
$ yarn add weldable
$ yarn add weldable --dev
```

## How to use
Expand All @@ -50,11 +52,12 @@ For in-depth use of `weldable` see our [DOCS](./DOCS.md).
Options:
-e, --env Use a default configuration type if NODE_ENV is not set to the available choices.
[string] [choices: "development", "production"] [default: "production"]
-l, --lang Codebase language, JS or TS [string] [choices: "js", "ts"] [default: "js"]
-l, --loader Preprocess loader, use the classic JS (babel-loader), TS (ts-loader), or "none" to use webpack defaults, or a
different loader. [string] [choices: "none", "js", "ts"] [default: "js"]
-s, --stats Output JSON webpack bundle stats. Use the default, or a relative project path and filename [./stats.json]
[string]
--tsconfig Generate a base tsconfig from NPM @tsconfig/[base]. An existing tsconfig.json will override this option, see
tsconfig-opt.
tsconfig-opt. This option can be run without running webpack.
[string] [choices: "", "create-react-app", "node18", "node20", "react-native", "recommended", "strictest"]
--tsconfig-opt Regenerate or merge a tsconfig [string] [choices: "merge", "regen"] [default: "regen"]
-x, --extend Extend, or override, the default configs with your own relative path webpack configs using webpack merge.[array]
Expand Down
1 change: 1 addition & 0 deletions __fixtures__/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MOCK_JS_VALUE=lorem ipsum
MOCK_HTML_VALUE=dolor sit
STATIC_DIR=public
8 changes: 0 additions & 8 deletions __fixtures__/public/index.html

This file was deleted.

1 change: 1 addition & 0 deletions __fixtures__/public/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world, this is a static resource');
9 changes: 9 additions & 0 deletions __fixtures__/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fixture app html woot base template, %MOCK_HTML_VALUE%</title>
<script src="/static.js"></script>
</head>
<body></body>
</html>
37 changes: 26 additions & 11 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { setupDotenvFilesForEnv } = require('../src/dotenv');
const {
env: nodeEnv,
extend: extendedConfigs,
lang: language,
loader,
stats: statsFile,
tsconfig: baseTsConfig,
'tsconfig-opt': tsConfigOptions
Expand All @@ -31,10 +31,11 @@ const {
default: 'production'
})
.option('l', {
alias: 'lang',
describe: 'Codebase language, JS or TS',
alias: 'loader',
describe:
'Preprocess loader, use the classic JS (babel-loader), TS (ts-loader), or "none" to use webpack defaults, or a different loader.',
type: 'string',
choices: ['js', 'ts'],
choices: ['none', 'js', 'ts'],
default: 'js'
})
.option('s', {
Expand All @@ -46,15 +47,16 @@ const {
})
.option('tsconfig', {
describe:
'Generate a base tsconfig from NPM @tsconfig/[base]. An existing tsconfig.json will override this option, see tsconfig-opt.',
'Generate a base tsconfig from NPM @tsconfig/[base]. An existing tsconfig.json will override this option, see tsconfig-opt. This option can be run without running webpack.',
type: 'string',
choices: ['', 'create-react-app', 'node18', 'node20', 'react-native', 'recommended', 'strictest']
})
.option('tsconfig-opt', {
describe: 'Regenerate or merge a tsconfig',
type: 'string',
choices: ['merge', 'regen'],
default: 'regen'
implies: 'tsconfig',
coerce: args => (!args && 'regen') || args
})
.option('x', {
alias: 'extend',
Expand All @@ -68,9 +70,9 @@ const {
/**
* Set global OPTIONS
*
* @type {{statsFile: string, dotenv: object, isRegenTsConfig: boolean, baseTsConfig: string,
* extendedConfigs: Array<string>, isMergeTsConfig: boolean, language: string, statsPath: string,
* nodeEnv: string}}
* @type {{statsFile: string, dotenv: object, isRegenTsConfig: boolean, isCreateTsConfig: boolean,
* loader: string, isCreateTsConfigOnly: boolean, baseTsConfig: string, extendedConfigs: Array<string>,
* isMergeTsConfig: boolean, statsPath: string, nodeEnv: string}}
* @private
*/
OPTIONS._set = {
Expand All @@ -86,15 +88,28 @@ OPTIONS._set = {
process.env.NODE_ENV = nodeEnv;
}

return setupDotenvFilesForEnv({ env: process.env.NODE_ENV, relativePath: this.contextPath, isMessaging: true });
return setupDotenvFilesForEnv({
env: process.env.NODE_ENV,
relativePath: this.contextPath,
isMessaging: true,
setExposedParams: true
});
},
isCreateTsConfig: function () {
const isBaseTsConfig = typeof baseTsConfig === 'string';
return (loader === 'ts' && isBaseTsConfig) || isBaseTsConfig;
},
isCreateTsConfigOnly: function () {
const isBaseTsConfig = typeof baseTsConfig === 'string';
return (loader !== 'ts' && isBaseTsConfig) || false;
},
isMergeTsConfig: function () {
return tsConfigOptions === 'merge';
},
isRegenTsConfig: function () {
return tsConfigOptions === 'regen';
},
language,
loader,
statsFile: function () {
return (statsFile && path.basename(statsFile)) || undefined;
},
Expand Down
1 change: 1 addition & 0 deletions cspell.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"fnames",
"nocolor",
"oneline",
"preprocess",
"regen",
"stringifier",
"systemvars"
Expand Down
94 changes: 82 additions & 12 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [~setupWebpackDotenvFile(filePath)](#module_dotenv..setupWebpackDotenvFile) ⇒ <code>undefined</code> \| <code>\*</code>
* [~setupWebpackDotenvFilesForEnv(params)](#module_dotenv..setupWebpackDotenvFilesForEnv) ⇒ <code>Array</code>
* [~setupDotenvFile(filePath)](#module_dotenv..setupDotenvFile) ⇒ <code>void</code>
* [~setDotenvParam(params)](#module_dotenv..setDotenvParam)
* [~setupDotenvFilesForEnv(params)](#module_dotenv..setupDotenvFilesForEnv) ⇒ <code>object</code>

<a name="module_dotenv..dotenv"></a>
Expand Down Expand Up @@ -93,32 +94,54 @@ Set up, and access, a dotenv file and the related set of parameters.
</tr> </tbody>
</table>

<a name="module_dotenv..setDotenvParam"></a>

### dotenv~setDotenvParam(params)
Set an array of dotenv params

**Kind**: inner method of [<code>dotenv</code>](#module_dotenv)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>params</td><td><code>Array.&lt;{param: string, value: string, ignoreIfSet: boolean}&gt;</code></td>
</tr> </tbody>
</table>

<a name="module_dotenv..setupDotenvFilesForEnv"></a>

### dotenv~setupDotenvFilesForEnv(params) ⇒ <code>object</code>
A function for use with non-webpack configurations. Set up and access local and specific dotenv file parameters.
Failed or missing parameters return an empty string.
dotenv parameters are string based, failed or missing dotenv parameters return an empty string.

**Kind**: inner method of [<code>dotenv</code>](#module_dotenv)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
<th>Param</th><th>Type</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>params</td><td><code>object</code></td>
<td>params</td><td><code>object</code></td><td></td>
</tr><tr>
<td>params.env</td><td><code>string</code></td>
<td>params.env</td><td><code>string</code></td><td></td>
</tr><tr>
<td>params.relativePath</td><td><code>string</code></td><td></td>
</tr><tr>
<td>params.relativePath</td><td><code>string</code></td>
<td>params.dotenvNamePrefix</td><td><code>string</code></td><td><p>Add an internal prefix to dotenv parameters used for configuration to avoid overlap.</p>
</td>
</tr><tr>
<td>params.dotenvNamePrefix</td><td><code>string</code></td>
<td>params.setBuildDefaults</td><td><code>boolean</code></td><td></td>
</tr><tr>
<td>params.setBuildDefaults</td><td><code>boolean</code></td>
<td>params.isMessaging</td><td><code>boolean</code></td><td></td>
</tr><tr>
<td>params.isMessaging</td><td><code>boolean</code></td>
<td>params.setExposedParams</td><td><code>boolean</code></td><td><p>Ignore the potential for dotenv parameter overlap and attempt to set non-prefixed configuration parameters if not already set.</p>
</td>
</tr> </tbody>
</table>

Expand Down Expand Up @@ -237,10 +260,24 @@ Start `weldable`

<a name="module_Init..weldable"></a>

### Init~weldable() ⇒ <code>Promise.&lt;void&gt;</code>
### Init~weldable(options) ⇒ <code>Promise.&lt;void&gt;</code>
Organize package functionality.

**Kind**: inner method of [<code>Init</code>](#module_Init)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>options</td><td><code>object</code></td>
</tr><tr>
<td>options.isCreateTsConfigOnly</td><td><code>boolean</code></td>
</tr> </tbody>
</table>

<a name="module_Logger"></a>

## Logger
Expand All @@ -266,7 +303,7 @@ Convenience wrapper for preset console messaging and colors.
## Typescript
<a name="module_Typescript..createTsConfig"></a>

### Typescript~createTsConfig(dotenv, options) ⇒ <code>undefined</code> \| <code>Object</code>
### Typescript~createTsConfig(dotenv, options, settings) ⇒ <code>undefined</code> \| <code>Object</code>
Create, or merge, a tsconfig file.

**Kind**: inner method of [<code>Typescript</code>](#module_Typescript)
Expand All @@ -288,11 +325,15 @@ Create, or merge, a tsconfig file.
</tr><tr>
<td>options.contextPath</td><td><code>string</code></td>
</tr><tr>
<td>options.isCreateTsConfig</td><td><code>boolean</code></td>
</tr><tr>
<td>options.isMergeTsConfig</td><td><code>boolean</code></td>
</tr><tr>
<td>options.isRegenTsConfig</td><td><code>boolean</code></td>
</tr><tr>
<td>options.language</td><td><code>string</code></td>
<td>settings</td><td><code>object</code></td>
</tr><tr>
<td>settings.configFilename</td><td><code>string</code></td>
</tr> </tbody>
</table>

Expand Down Expand Up @@ -411,10 +452,35 @@ Start webpack development or production.
## webpackConfigs

* [webpackConfigs](#module_webpackConfigs)
* [~preprocessLoader(dotenv, options)](#module_webpackConfigs..preprocessLoader) ⇒ <code>Object</code>
* [~common(dotenv, options)](#module_webpackConfigs..common) ⇒ <code>Object</code>
* [~development(dotenv)](#module_webpackConfigs..development) ⇒ <code>Object</code>
* [~production(dotenv)](#module_webpackConfigs..production) ⇒ <code>Object</code>

<a name="module_webpackConfigs..preprocessLoader"></a>

### webpackConfigs~preprocessLoader(dotenv, options) ⇒ <code>Object</code>
Assumption based preprocess loader

**Kind**: inner method of [<code>webpackConfigs</code>](#module_webpackConfigs)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>dotenv</td><td><code>object</code></td>
</tr><tr>
<td>dotenv._BUILD_SRC_DIR</td><td><code>string</code></td>
</tr><tr>
<td>options</td><td><code>object</code></td>
</tr><tr>
<td>options.loader</td><td><code>string</code></td>
</tr> </tbody>
</table>

<a name="module_webpackConfigs..common"></a>

### webpackConfigs~common(dotenv, options) ⇒ <code>Object</code>
Expand All @@ -431,8 +497,12 @@ Common webpack settings between environments.
<tr>
<td>dotenv</td><td><code>object</code></td>
</tr><tr>
<td>dotenv._BUILD_APP_INDEX_PREFIX</td><td><code>string</code></td>
</tr><tr>
<td>dotenv._BUILD_DIST_DIR</td><td><code>string</code></td>
</tr><tr>
<td>dotenv._BUILD_HTML_INDEX_DIR</td><td><code>string</code></td>
</tr><tr>
<td>dotenv._BUILD_PUBLIC_PATH</td><td><code>string</code></td>
</tr><tr>
<td>dotenv._BUILD_RELATIVE_DIRNAME</td><td><code>string</code></td>
Expand All @@ -445,7 +515,7 @@ Common webpack settings between environments.
</tr><tr>
<td>options</td><td><code>object</code></td>
</tr><tr>
<td>options.language</td><td><code>string</code></td>
<td>options.loader</td><td><code>string</code></td>
</tr> </tbody>
</table>

Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/dotenv.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exports[`dotenv should return specific properties: specific properties 1`] = `
"setupDotenvFilesForEnv": [Function],
"setupWebpackDotenvFilesForEnv": [Function],
},
"setDotenvParam": [Function],
"setupDotenvFile": [Function],
"setupDotenvFilesForEnv": [Function],
"setupWebpackDotenvFile": [Function],
Expand Down
Loading