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

@wjunt/webpack-config

v1.5.3

Published

Presets of webpack config

Downloads

30

Readme

@wjunt/webpack-config

Presets of webpack config.

Install

Install package in project.

yarn add -D @wjunt/webpack-config webpack typescript

Quick start

webpack.config.js

This package uses ts-loader to transpile TypeScript and ES6+.

You can also use babel-loader if you like. It's all up to you.

const { merge, css, ts, vue } = require('@wjunt/webpack-config');

module.exports = merge(
    // Support css bundling.
    css(),
    
    // Support Vue SFC.
    vue(),
    
    // Transpile TypeScript and ES6+.
    ts({
        vue: true,
        exclude: /node_modules/,
    }),
    
    // You can configure webpack here.
    {
        externals: {
            'vue': 'Vue',
            'react': 'React',
            'react-dom': 'ReactDOM',
        },
    },
);

tsconfig.json

If you would like to use TypeScript, there is a common configuration for you to start quickly.

{
  "extends": "@wjunt/webpack-config/conf/tsconfig",
  "compilerOptions": {
    "jsx": "react"
  },
  "include": [
    "source"
  ]
}

Note: { "jsx": "react" } is necessary when you are coding with React.

Usage

Some examples of common projects.

Vue

webpack.config.js

const { merge, css, ts, vue } = require('@wjunt/webpack-config');

module.exports = merge(
    css(),
    vue(),
    ts({
        exclude: /node_modules/,
        feature: ['vue'],
    }),
    {
        externals: {
            'vue': 'Vue',
        },
    },
);

tsconfig.json

{
  "extends": "@wjunt/webpack-config/conf/tsconfig",
  "include": [
    "source"
  ]
}

React

webpack.config.js

const { merge, css, ts } = require('@wjunt/webpack-config');

module.exports = merge(
    css(),
    ts({
        exclude: /node_modules/,
        feature: ['react'],
    }),
    {
        externals: {
            'react': 'React',
            'react-dom': 'ReactDOM',
        },
    },
);

tsconfig.json

{
  "extends": "@wjunt/webpack-config/conf/tsconfig",
  "include": [
    "source"
  ]
}

Tweak React components in real time

To enable React Component hot-reload and keep state unchanged, install react-hot-loader, then mark the root component as hot-updatable.

import { render } from 'react-dom';
import { hot } from 'react-hot-loader/root';

const App = hot(() => (
    <div>
        Your components/routes here.
    </div>
));

render(<App/>, document.querySelector('#app'));

Less

Install less-loader at first.

yarn add -D less-loader less

webpack.config.js

const { merge, less, ts } = require('@wjunt/webpack-config');

module.exports = merge(
    less(), // Test /\.less$/ by default.
    ts(),
);

And you can also simply use css() like this:

webpack.config.js

const { merge, css, ts } = require('@wjunt/webpack-config');

module.exports = merge(
    css({
        test: /\.(less|css)$/,
        'useAppend': ['@wjunt/less-loader'],
    }),
    ts(),
);

Ant Design

Use ts-import-plugin to optimize bundling antd.

yarn add -D ts-import-plugin

webpack.config.js

const { merge, css, ts } = require('@wjunt/webpack-config');

module.exports = merge(
    css(),
    ts({
        exclude: /node_modules/,
        feature: [
            'react',
            'antd',
        ],
    }),
    {
        externals: {
            'react': 'React',
            'react-dom': 'ReactDOM',
        },
    },
);

tsconfig.json is the same as React.

Now it's time to code with antd.

import React from 'react';
import { Button } from 'antd';

export const Component = () => {
    return (
        <div>
            <p>Hello world!</p>
            <Button>Bye~</Button>
        </div>
    );
};

Control Statemens

Use tsx-control-statements to transform jsx control statements.

yarn add -D tsx-control-statements

webpack.config.js

const { merge, css, ts } = require('@wjunt/webpack-config');

module.exports = merge(
    css(),
    ts({
        exclude: /node_modules/,
        feature: [
            'react',
            'control-statements',
        ],
    }),
    {
        externals: {
            'react': 'React',
            'react-dom': 'ReactDOM',
        },
    },
);

tsconfig.json

{
  "extends": "@wjunt/webpack-config/conf/tsconfig",
  "compilerOptions": {
    "jsx": "react"
  },
  "files": [
    "node_modules/tsx-control-statements/index.d.tsx"
  ],
  "include": [
    "source"
  ]
}

Now it's time to code with control statements.

import React from 'react';
import { Button } from 'antd';

export const SongRelatedThingy = ({ songList }: { songList: string[] }) => (
    <p>
        <If condition={songList.includes('Gery-Nikol - Im the Queen')}>
            good taste in music
        </If>
    </p>
);

Enable Hot Reload Manually

You can enable hot reload at specific entry/entries with hotReload() API.

webpack.config.js

const { merge, ts, hotReload } = require('@wjunt/webpack-config');

module.exports = merge(
    ts(),
    {
        entry: {
            app: hotReload('./src/app.ts'),
        },
    },
);

Split chunks

It's recommended to split common chunks when project grows. For example the dependencies in node_modules.

You can simply split all chunks in node_modules with splitChunks() API. All split chunks will be placed in vendors.js by default.

webpack.config.js

const { merge, ts, splitChunks } = require('@wjunt/webpack-config');

module.exports = merge(
    ts(),
    splitChunks(), // With priority -10.
);

If you wanna change the output filename, try splitChunks('lib'), which places all split chunks in a file with the name you give.

It's also possible to set several cache groups. For example:

webpack.config.js

const { merge, ts, splitChunks } = require('@wjunt/webpack-config');

module.exports = merge(
    ts(),
    splitChunks(),
    splitChunks({
        name: 'react',
        chunks: 'initial',
        test: /node_modules\/react/,
    }),
);

This configuration places all chunks related to react in react.js, while others in vendors.js.

Exclude node_modules

Mostly it's unnecessary to transpile files in node_modules.

webpack.config.js

const { merge, ts } = require('@wjunt/webpack-config');

module.exports = merge(
    // Same as webpack rule spec.
    ts({
        exclude: /node_modules/,
    }),
);

To be continued...