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

@cs-magic/webpack-config

v0.5.0

Published

Shared Webpack configurations for CS Magic projects

Downloads

105

Readme

@cs-magic/webpack-config

CS Magic 项目的共享 Webpack 配置。

安装

npm install --save-dev @cs-magic/webpack-config

使用

基础配置

// webpack.config.js
const getWebpackConfig = require('@cs-magic/webpack-config')

module.exports = getWebpackConfig({
  development: process.env.NODE_ENV === 'development',
  production: process.env.NODE_ENV === 'production',
})

React + TypeScript 项目

// webpack.config.js
const getWebpackConfig = require('@cs-magic/webpack-config')

module.exports = getWebpackConfig({
  development: process.env.NODE_ENV === 'development',
  production: process.env.NODE_ENV === 'production',
  react: true,
  typescript: true,
})

自定义配置

// webpack.config.js
const { merge } = require('webpack-merge')
const getWebpackConfig = require('@cs-magic/webpack-config')

const customConfig = {
  // 你的自定义配置
}

module.exports = env => merge(getWebpackConfig(env), customConfig)

特性

  • 开发环境优化

    • 热模块替换
    • Source Maps
    • 开发服务器配置
  • 生产环境优化

    • 代码分割
    • 压缩和混淆
    • CSS 提取和优化
    • Bundle 分析
  • 资源处理

    • JavaScript/TypeScript
    • CSS/SASS
    • 图片和字体
    • CSS Modules
  • React 支持

    • 热重载
    • JSX/TSX 支持
  • TypeScript 支持

    • 类型检查
    • 编译优化

命令

{
  "scripts": {
    "start": "webpack serve --env development",
    "build": "webpack --env production",
    "analyze": "ANALYZE=true webpack --env production"
  }
}

环境变量

  • NODE_ENV: 决定构建模式
  • ANALYZE: 开启 bundle 分析

扩展配置

添加新的 loader

const config = getWebpackConfig({ development: true })

config.module.rules.push({
  test: /\.mdx?$/,
  use: ['babel-loader', '@mdx-js/loader'],
})

添加新的插件

const config = getWebpackConfig({ production: true })
const CompressionPlugin = require('compression-webpack-plugin')

config.plugins.push(new CompressionPlugin())

Next.js Integration

For Next.js projects, we provide a webpack config modifier that works with Next.js's webpack configuration:

// next.config.js
const { withCustomWebpack } = require('@cs-magic/webpack-config/presets/webpack.nextjs')

const nextConfig = {
  reactStrictMode: true,
  // Your other Next.js config
}

module.exports = withCustomWebpack(nextConfig)

Features

  • Custom aliases (@, @components, etc.)
  • SVG support via @svgr/webpack
  • Optimized chunk splitting
  • Worker loader support
  • Development environment variable
  • Module transpilation support
  • Production optimizations

Environment Variables

The config automatically adds:

  • __DEV__: boolean indicating development mode

Custom Webpack Configuration

You can still add your own webpack configuration:

const nextConfig = {
  webpack: (config, options) => {
    // Your custom webpack modifications
    return config
  }
}

module.exports = withCustomWebpack(nextConfig)

Chrome Extension Configuration

为 Chrome 扩展项目提供特定的 webpack 配置:

// webpack.config.js
const getWebpackConfig = require('@cs-magic/webpack-config/presets/webpack.chrome')

module.exports = (env) => {
  return getWebpackConfig({
    development: process.env.NODE_ENV === 'development'
  })
}

项目结构

推荐的项目结构:

src/
├── manifest.json
├── background/
│   └── index.ts
├── content/
│   └── index.ts
├── popup/
│   ├── index.tsx
│   └── popup.html
├── options/
│   ├── index.tsx
│   └── options.html
├── components/
│   └── shared/
├── assets/
│   └── icons/
└── utils/

特性

  • Manifest V3 支持
  • TypeScript 支持
  • React 支持
  • 样式处理 (CSS/SCSS)
  • 资源处理
  • 代码分割
  • 热重载
  • 开发环境优化

构建命令

package.json 中添加:

{
  "scripts": {
    "dev": "webpack --watch --mode development --env development",
    "build": "webpack --mode production --env production",
    "clean": "rimraf dist"
  }
}

开发流程

  1. 构建扩展:
npm run dev
  1. 在 Chrome 中加载扩展:
  • 打开 chrome://extensions/
  • 启用"开发者模式"
  • 点击"加载已解压的扩展程序"
  • 选择 dist 目录

发布准备

构建生产版本:

npm run build

生成的 dist 目录包含可以上传到 Chrome Web Store 的内容。