Better build process with Gulp for general Node.js projects written in TypeScript. To embrace native ES modules in Node.js,
@messageflow/build
allows you to build with compiling to any other module system such asCommonJS
and also allows you to output files in the format of.mjs
. See the Using .mjs in Node.js section to learn how to use.mjs
in Node.js.
# Install via NPM as one of the `devDependencies`
$ npm install --save-dev @messageflow/build
const gulp = require('gulp');
const { builder } = require('@messageflow/build');
const build = builder();
/** `cleanGlobs` can be helpful when the destination directory is not the `dist` directory. */
// const build = builder({
// dist: '.',
// cleanGlobs: [
// './*.js',
// './*.d.ts',
// '!./gulpfile.js',
// '!./json.d.ts',
// ],
// });
gulp.task('clean', build.clean);
gulp.task('lint', build.lint);
gulp.task('copy', build.copy);
gulp.task('ts', build.ts);
gulp.task('watch', build.watch);
gulp.task('default', build.default); // or gulp.task('default', build.all);
// gulpfile.js
const gulp = require('gulp');
const builder = require('@messageflow/build').builder({ foss: true });
/** This is equivalent to */
// const builder = require('@messageflow/build').builder({
// dist: '.',
// cleanGlobs: [
// './*@(mj|j)s',
// './*.d.ts',
// '!./gulpfile.js',
// '!./json.d.ts',
// ],
// })
gulp.task('lint', builder.lint);
gulp.task('default', builder.all);
Node.js will import the correct dependencies based on the file extension you use. .mjs
file will only import .mjs
files.
// main.mjs
import otherMod from './other-mod'; // This will import 'other-mod.mjs` but not 'other-mod.js'
// main.js
const otherMod = require('./other-mod'); // This will import 'other-mod.js' but not 'other-mod.mjs'
[
'./*.@(mj|j)s',
'./*.d.ts',
'!./gulpfile.js',
'!./json.d.ts',
]
[
'!**/demo*/**/*.ts*',
'!**/test*/**/*.ts*',
]
{
presets: [
['@babel/preset-env', {
targets: { node: 'current' },
spec: true,
modules: false,
useBuiltIns: 'usage',
shippedProposals: true,
}],
['minify', {
replace: false,
mangle: { keepFnName: true },
removeConsole: false,
removeDebugger: true,
}],
],
}
When foss
is set to true, the following flags will be set to use its default values:
dist
-.
cleanGlobs
- DEFAULT_FOSS_CLEAN_GLOBS
src
<?string> Optional source directory. Defaults tosrc
.dist
<?string> Optional destination directory. Defaults todist
.cleanGlobs
<?string|string[]> Optional glob patterns to clean files/ directories up before every build process initiates. This is required only when the destination directory is not thedist
directory. Defaults to the value ofdist
if unspecified.copyGlobs
<?string|string[]> Optional glob patterns to copy files/ directories to destination build directory. Defaults to['<SRC>/**/*.*', '!<SRC>/**/*.ts*', '<SRC>/**/*.d.ts']
.ignoreGlobs
<?string|string[]> Optional glob patterns to ignore files/ directories. Defaults to DEFAULT_IGNORE_GLOBS. This only works whenisProd
is set to true.isProd
<?boolean> Optional production flage. Set totrue
if the build process is meant for production. Defaults toprocess.env.NODE_ENV === 'production'
.rootPath
<?string> Optional path to current working directory. Defaults to.
.babelConfig
<?Object> Optional configuration for Babel. This is only needed whenisProd
is set to true. Defaults to DEFAULT_BABEL_CONFIG.tsConfig
<?string> Optional path totsconfig.json
. Defaults to./tsconfig.json
.tslintConfig
<?string> Optional path totslint.json
. Defaults to./tslint.json
. This defaults to./tslint.prod.json
whenisProd
is set to true.esModules
<?boolean> Optional ES modules flag to run compilation with native ES Modules. Defaults totrue
.mjs
<?boolean> Optional flag to run compilation with native ES modules and output files in the format of.mjs
. Defaults tofalse
. Native ES modules will be used and will ignore any value set byesModules
if this is set to truefoss
<?boolean> Optional flag to run compilation for FOSS. Defaults tofalse
. See Default FOSS configuration to see what is being used under the hood.
-
options
<?BuilderParams> Optional configuration for the build process. -
returns: <Object> An object of build tasks to be assigned as Gulp task, e.g.
gulp.task('<TASK_NAME>', <GULP_TASK_FUNCTION>)
. It comprises of a list of tasks fo a common build process with Gulp for most of the projects:clean
- Always remove old files from previous build.lint
- Always lint all.ts
files with giventslint.json
.ts
- Compile all.ts
files with giventsconfig.json
.copy
- Copy all asset files such asimages
,json
,md
, etc.watch
- Run the build process by watching for flle changes.default
- Default build process that comprises all the above.all
- Another default build process to compile and output in both.js
and.mjs
. See the How to build for FOSS section to learn how to use@messageflow/build
to build your open source Node.js package using the{ foss: true }
shorthand.
MIT License © Rong Sen Ng