@quatico/websmith-node
v0.8.5
Published
Node library for running websmith with CLI and webpack in Node.js
Readme
@quatico/websmith-node
This package contains a compiler runner for Node.js. It provides a small API for running the websmith compiler and webpack with the websmith-loader from within a Node.js application.
Getting started
Install the package with your favorite package manager. For example with pnpm:
pnpm add @quatico/websmith-nodeUsage
The library exports a two main functions. The first one is compile compiles one or more files with the websmith compiler. The second one is webpack to bundle one or more files with webpack.
Compile files with the websmith compiler
You can compile a single file or a whole directory with the websmith compiler:
import { compile } from "@quatico/websmith-node";
const logs = await compile(["./src/index.ts"]);
console.log(logs);The compile function returns a string with the logs of the compilation. It also accepts a second argument with the following options:
tsConfig: A typescript compiler options object. See tsconfig.json for more information.websmith: A websmith options object. See websmith.config.ts for more information.
Run your profile or addons with the websmith compiler:
import { compile } from "@quatico/websmith-node";
const logs = await compile(["./src/index.ts"], {
tsConfig: {
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.Node10,
outDir: "./lib",
noEmit: false,
},
websmith: {
config: {
addonsDir: "./example-addons",
addons: ["export-yaml-generator"],
profiles: {
"my-profile": {
addons: ["foobar-replace-transformer"],
},
},
profile: "my-profile",
},
},
});Bundle files with webpack
The webpack function bundles one or more files with webpack. It accepts a single argument with the files to bundle, and an optional second argument with the webpack configuration.
import { webpack } from "@quatico/websmith-node";
const logs = await webpack(["./src/index.ts"]);
console.log(logs);The webpack function returns a string with the logs of the webpack build. It also accepts a second argument with the following options:
webpack: A webpack configuration object. See webpack.config.ts for more information.websmith: A websmith-loader options object. See WebpackLoaderOptions.ts for more information.
Run your profile or addons with the webpack build:
import { webpack } from "@quatico/websmith-node";
const logs = await webpack(["./src/index.ts"], {
webpack: {
output: {
path: "./lib",
},
module: {
rules: [
{
test: /\.[jt]s?$/,
use: [
{
loader: require.resolve("websmith-loader"),
options: {
transpileOnly: true,
tsConfigFile: path.join(__dirname, "..", "tsconfig.json"),
},
},
],
},
],
},
},
websmith: {
config: {
profile: "my-profile",
},
},
});See the websmith github repository for more information and examples.
