webpack-transform-path-resolve-plugin
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Modify webpack resolve path by function

中文文档

This is a webpack resolve plugin, for transform resolve path by function.

Example

Read different language config file for different version.

Directories as follow

- languages
    - en
        - a.js
        - b.js
    - cn
        - a.js

For example, if current lang is Chinese, then read file from directory languages/cn, if the file not exist, then read it from languages/en

a.js is exist, then import Lang from @lang/a.js, will resolve to languages/cn/a.js b.js is not exist is Chinese, then import Lang from @lang/b.js, will resolve to languages/en/b.js

const fs = require('fs')
const TransformPathResolvePlugin = require('webpack-transform-path-resolve-plugin');

const webpackConfig = {
    resolve: {
        plugins: [
          new TransformPathResolvePlugin({
            pathReg: /^@versions/,
            transform(filePath) {
                if (filePath) {
                const realPath = filePath.replace('@lang', path.resolve(__dirname, './__mocks__/languages/cn'));
                if (fs.existsSync(realPath)) {
                    return realPath;
                };
                return filePath.replace('@lang', path.resolve(__dirname, './__mocks__/languages/en'));
                }
            },
          }),
        ],
    }
}    

Package Sidebar

Install

npm i webpack-transform-path-resolve-plugin

Weekly Downloads

2

Version

1.0.1

License

ISC

Unpacked Size

8.55 kB

Total Files

13

Last publish

Collaborators

  • xu3927