Skip to main content

Overview

@morjs/utils is the shared internal utilities package for MorJS’s build toolchain. It re-exports @morjs/takin (the CLI engine) alongside a curated set of build-time dependencies, so all MorJS compiler plugins can import from a single, version-consistent source.
PropertyValue
Package@morjs/utils
Version1.0.71
LicenseMIT
Node>= 12.13.0
@morjs/utils is an internal package. Its API surface can change between versions. Plugin authors should import from @morjs/utils when writing compiler plugins for MorJS, but should not depend on it for application code. Use @morjs/api or @morjs/core for runtime code.

Installation

npm install @morjs/utils

Key exports

Re-exported from @morjs/takin

Everything from @morjs/takin is available directly from @morjs/utils. This includes Takin, Runner, Plugin, all runner hooks, config utilities, CLI helpers, and logger.
import { Plugin, Runner, Takin, logger, takin } from '@morjs/utils'
See the @morjs/takin reference for the full API.

Webpack utilities

import { webpack, WebpackChain, WebpackWrapper, WebpackPlugin, WebpackUserConfig, WebpackDevServer } from '@morjs/utils'
ExportDescription
webpackThe webpack 5 instance (version 5.76.0)
WebpackChainwebpack-chain-5 for fluent webpack config modification
WebpackWrapperMorJS wrapper around a webpack compiler instance
WebpackPluginBase class for webpack-integrated MorJS plugins
WebpackUserConfigUser config schema additions for webpack-level config
WebpackDevServerwebpack-dev-server (version 4.11.1)

Babel utilities

import { babelDeps } from '@morjs/utils'
All Babel 7 packages are re-exported: @babel/core, @babel/parser, @babel/generator, @babel/traverse, @babel/types, @babel/template, and related transform plugins.

PostCSS and PostHTML

import { postcss, posthtml, cssProcessorFactory } from '@morjs/utils'
ExportDescription
postcssPostCSS 8 instance
posthtmlPostHTML 0.16 — used for template AST processing
cssProcessorFactoryFactory for building CSS processor pipelines

Schema validation (zod)

import { zod as z } from '@morjs/utils'
MorJS uses zod (bundled via @morjs/takin) for all config schema validation. Use the zod re-export to write type-safe config schemas that integrate with MorJS’s registerUserConfig hook.

Glob and path utilities

import { glob, slash, micromatch } from '@morjs/utils'
ExportDescription
globfast-glob instance (fastGlob from takin)
slashNormalize path separators to forward slashes
micromatchGlob matching (version 4.0.5)

TypeScript compiler

import { typescript } from '@morjs/utils'
The TypeScript 4.4.4 compiler instance. Used by the script parser to type-check and transform TypeScript source files.

Minification utilities

import { TerserPlugin, CssMinimizerPlugin, HtmlMinimizerPlugin, CopyWebpackPlugin } from '@morjs/utils'
These are the webpack plugin instances for JS minification (terser), CSS minification (css-minimizer), HTML minification, and asset copying.

Other utilities

import { pRetry, pQueue, cjsToEsmTransformer } from '@morjs/utils'
ExportDescription
pRetryPromise retry with exponential backoff
pQueuePromise queue with concurrency control
cjsToEsmTransformerTransform CommonJS to ESM (used internally for compat)

Compiler hook extensions

@morjs/utils also registers the following additional RunnerHooks that are only available to compiler plugins:
HookTypeDescription
webpackWrapperSyncHook<WebpackWrapper>Tap to receive the webpack wrapper after it’s created. Used to modify webpack configuration.
entryBuilderAsyncSeriesHook<EntryBuilderHelpers>Tap to hook into the entry discovery and building phase.
scriptParserSyncWaterfallHook<[CustomTransformers, FileParserOptions]>Waterfall hook for modifying TypeScript/Babel transformers for script files.
templateParserAsyncSeriesWaterfallHook<...>Waterfall hook for modifying PostHTML AST transformers for template files.
styleParserAsyncSeriesWaterfallHook<...>Waterfall hook for modifying PostCSS processors for style files.
import { Plugin, Runner } from '@morjs/utils'

class MyCompilerPlugin implements Plugin {
  name = 'MyCompilerPlugin'

  apply(runner: Runner) {
    // Tap into webpack config modification
    runner.hooks.webpackWrapper.tap(this.name, (webpackWrapper) => {
      webpackWrapper.chain.resolve.alias
        .set('@utils', path.resolve(__dirname, 'src/utils'))
    })

    // Tap into template AST processing
    runner.hooks.templateParser.tap(this.name, (tree, options) => {
      return tree.walk((node) => {
        // Transform template nodes
        return node
      })
    })
  }
}

Usage in compiler plugins

When writing a MorJS compiler plugin, import from @morjs/utils to get access to the plugin interface, runner hooks, and build utilities:
import { Plugin, Runner, logger, WebpackWrapper } from '@morjs/utils'

export default class MyPlugin implements Plugin {
  name = 'MyPlugin'

  apply(runner: Runner) {
    runner.hooks.webpackWrapper.tap(this.name, (wrapper: WebpackWrapper) => {
      // Modify webpack config using webpack-chain API
      wrapper.chain.module
        .rule('my-loader')
        .test(/\.myext$/)
        .use('my-loader')
        .loader(require.resolve('./my-loader'))
    })
  }
}

Build docs developers (and LLMs) love