npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-build-tool

v0.0.26

Published

Commands for building JS/TS/CSS/SCSS files.

Downloads

420

Readme

Webpack Build Tool

This package is used to build Typescript, Javascript and CSS/SCSS files and bundle them using webpack. Uses https://swc.rs/docs/usage/swc-loader for js/ts files.

Installation

npm install webpack-build-tool -D

Usage

Create a webpack-build-tool-config.js or webpack-build-tool-config.ts in the same directory as the package.json.

// webpack-build-tool-config.ts

import { Configuration } from 'webpack-build-tool'

const config: Configuration = {
  entryFiles: {
    'folder/main': './src/main.ts',
  },
  // outDir: 'dist', // to specify a different output directory
  // manifest: true, // to generate a minifest.json file
  // webpack: (config) => config, // to enhance/change webpack config
  // browserslist: { // if you want to override the default browserlists
  //   production: ['>0.2%', 'not dead', 'not op_mini all', 'ie >= 11'],
  //   development: ['last 1 chrome version', 'last 1 firefox version', 'last 1 safari version']
  // },
  // jest: {
  //   testPathIgnorePatterns: ['/node_modules/']
  // },
  // licenseChecker: {
  //   outputLicenceFile: true
  //   excludedLicences: []
  //   allowedLicences: ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'MIT']
  //   ignoredPackages: []
  // },
  // jsLoader: 'swc' // 'swc' | 'babel'
}

export default config
// webpack-build-tool-config.js

/** @type { import('webpack-build-tool').Configuration } */
module.exports = {
  entryFiles: {
    'folder/main': './src/main.ts',
  },
  // outDir: 'dist', // to specify a different output directory
  // manifest: true, // to generate a minifest.json file
  // webpack: (config) => config, // to enhance/change webpack config
  // browserslist: { // if you want to override the default browserlists
  //   production: ['>0.2%', 'not dead', 'not op_mini all', 'ie >= 11'],
  //   development: ['last 1 chrome version', 'last 1 firefox version', 'last 1 safari version']
  // },
  // jest: {
  //   testPathIgnorePatterns: ['/node_modules/']
  // },
  // licenseChecker: {
  //   outputLicenceFile: true
  //   excludedLicences: []
  //   allowedLicences: ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'MIT']
  //   ignoredPackages: []
  // },
  // jsLoader: 'swc', // 'swc' | 'babel'
  // jsTestLoader: 'swc', // 'swc' | 'babel'
  // importSource: 'react' // 'react' | 'preact'
}

Then update your package.json and add the package and new scripts:

// package.json

{
  ...,
  "scripts": {
    "start": "webpack-build-tool start",
    "serve": "webpack-build-tool serve",
    "build": "webpack-build-tool build",
    "build-watch": "webpack-build-tool build --watch",
    "build-with-analyze": "webpack-build-tool build --analyze",
    "build-no-lint": "webpack-build-tool build --no-lint",
    "lint": "webpack-build-tool lint",
    "lint-with-fix": "webpack-build-tool lint --fix",
    "test": "webpack-build-tool test",
    "typecheck": "webpack-build-tool typecheck",
    "browserslist": "webpack-build-tool browserslist"
  }
}

Using --no-lint will improve build time.

Using webpack-dev-server

When running the webpack-build-tool serve command a webserver will be started at http://localhost:4000 and a file dev-server-running in the root directory will be created. This file can be used to dynamically load the files from the localhost server.

Using Typescript

Create tsconfig.json file:

{
  "compilerOptions": {
    "module": "esnext",
    "types": [
      "jest",
      "node"
    ],
    "target": "es2015",
    "moduleResolution": "node",
    "noEmit": true,
    "strictNullChecks": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "noImplicitAny": true
  }
}

Using SWC and React/Preact

Create .swcrc file:

{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": false,
      "dynamicImport": true
    },
    "transform": {
      "react": {
        "runtime": "automatic",
        "importSource": "react"
      }
    }
  }
}

Building SASS/SCSS/CSS Files

No additional configuration needed, just add them to your entryFiles.

// webpack-build-tool-config.js

/** @type { import('webpack-build-tool').Configuration } */
module.exports = {
  // ...
  entryFiles: {
    'folder/main.min': './src/main.ts',
    'folder/styles.min': './src/styles.scss',
  }
}

Inline Import for Styles

If you need to import styles inline you may use the ?inline query params on imports.

import styles from './styles.css?inline'

Using ESLint

Create the following files in the root of your project.

The following config includes eslint-config-wuif which can be installed using npm i eslint-config-wuif -D, but you can also use your own eslint config.

// .eslintignore

dist
// .eslintrc

{
  "root": true,
  "extends": [
    "eslint-config-wuif"
  ],
  "settings": {
    "react": {
      "version": "18"
    }
  }
}

Using Jest

Just create test files using following naming: ... .(spec|test).(ts|tsx|js|jsx)

// src/demo.spec.ts

import { testFunction } from './main'

describe('testFunction', () => {
  test('should return value', async () => {
    expect(testFunction()).toBe('testValue')
  })
})

Using .env

You can create a .env file in your project root and this can be used in any file

// .env

API_ROOT=https://myurl.com
// src/index.ts

console.log(process.env.API_ROOT)

Using @testing-library/preact

// webpack-build-tool-config.js

/** @type { import('webpack-build-tool').Configuration } */
module.exports = {
  // ...
  jest: {
    transformIgnorePatterns: ['<rootDir>/node_modules/(?!(preact|@testing-library)/)'],
  }
}

Using jest in IDE

You need to add the following two files.

// jest.config.js

/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
  testEnvironment: 'jsdom',
  transform: {
    '^.+\\.(js|jsx|ts|tsx|mjs)$': ['@swc/jest'] // or ['babel-jest'] if using babel
  },
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': 'webpack-build-tool/__mocks__/file-mock.js',
    '\\.(css|sass|scss)$': 'webpack-build-tool/__mocks__/style-mock.js',
    '\\.(css|sass|scss)\\?inline$': 'webpack-build-tool/__mocks__/inline-style-mock.js'
  }
}

module.exports = config

Running Type Checks for Typescript Files without Building

If you want to run type checks but not build files you can use the webpack-build-tool typecheck command. This is faster than doing so during build.

Using Babel instead of SWC

// webpack-build-tool-config.js

/** @type { import('webpack-build-tool').Configuration } */
module.exports = {
  // ...
  jsLoader: 'babel'
}

For your IDE you may need to add a .babelrc:

// .babelrc

{
  "presets": ["@babel/preset-react", "@babel/preset-typescript"]
}

Show Timings of Webpack and ESLint

If you want to inspect what slows down your webpack build you activate the Speed Measure Webpack Plugin by adding --timings flag.

// package.json

{
  ...,
  "scripts": {
    "start": "webpack-build-tool build --timings",
  }
}

For ESLint timing you need to add TIMING=1 environment variable. (https://eslint.org/docs/latest/extend/custom-rules#profile-rule-performance)