Skip to content

Commit

Permalink
fix-options
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraKumo001 committed Aug 14, 2024
1 parent 5cedd64 commit bee6bff
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ If include is omitted, all modules are covered.
{
name: 'storybook-addon-module-mock',
options: {
include: [/message/,"**/action.*"], // RegExp or glob pattern
include: ["**/action.*"], // glob pattern
exclude: ["**/node_modules/**"],
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storybook-addon-module-mock",
"version": "1.3.1",
"version": "1.3.3",
"main": "./dist/cjs/index.js",
"types": "./dist/cjs/index.d.ts",
"exports": {
Expand All @@ -27,6 +27,7 @@
},
"dependencies": {
"@storybook/test": "^8.2.8",
"minimatch": "^10.0.1",
"react-json-tree": "^0.19.0"
},
"devDependencies": {
Expand Down
33 changes: 32 additions & 1 deletion src/plugins/webpack-import-writer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
import { Compiler } from 'webpack';
import { AddonOptions } from '../types';

export class ImportWriterPlugin {
constructor(private options?: AddonOptions) {}
apply(compiler: Compiler) {
compiler.hooks.compilation.tap('ImportWriter', (compilation) => {
compilation.mainTemplate.hooks.require.tap('ImportWriter', (source: string) => {
const s = source.replace(
/return module\.exports;/g,
`
if (Object.prototype.toString.call(module.exports) === '[object Module]') {
function minimatch(str,pattern) {
function escapeRegExp(string) {
return string.replace(/[.*+?^\${}()|[\\]\\\\]/g, '\\\\$$&');
}
let regexPattern = pattern
.split('**').map(part => part.split('*').map(escapeRegExp).join('[^/]*')).join('.*');
let regex = new RegExp('^' + regexPattern + '$$');
return regex.test(str);
}
const isTarget = (fileName, options) => {
if (!options) return true;
const { include, exclude } = options;
if (!fileName) return true;
if (
include &&
include.some((i) => (i instanceof RegExp ? i.test(fileName) : minimatch(fileName, i)))
)
return true;
if (
exclude &&
exclude.some((i) => (i instanceof RegExp ? i.test(fileName) : minimatch(fileName, i)))
)
return false;
return true;
};
if (Object.prototype.toString.call(module.exports) === '[object Module]' &&
isTarget(moduleId, ${JSON.stringify(this.options)})
) {
class Module {
[Symbol.toStringTag] = 'Module';
}
Expand Down
7 changes: 5 additions & 2 deletions src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Options } from '@storybook/types';
import { ImportWriterPlugin } from './plugins/webpack-import-writer.js';
import { AddonOptions } from './types.js';
import type { Configuration } from 'webpack';

export const managerEntries = (entry: string[] = []): string[] => [
...entry,
require.resolve('./manager'),
];

export async function webpack(config: Configuration) {
export async function webpack(config: Configuration, options: Options & AddonOptions) {
config.optimization = {
...config.optimization,
concatenateModules: false,
};
config.plugins = [...(config.plugins ?? []), new ImportWriterPlugin()];
const { include, exclude } = options;
config.plugins = [...(config.plugins ?? []), new ImportWriterPlugin({ include, exclude })];
return config;
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4055,6 +4055,13 @@ min-indent@^1.0.0:
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==

minimatch@^10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-10.0.1.tgz#ce0521856b453c86e25f2c4c0d03e6ff7ddc440b"
integrity sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ==
dependencies:
brace-expansion "^2.0.1"

minimatch@^3.0.2, minimatch@^3.1.1, minimatch@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
Expand Down

0 comments on commit bee6bff

Please sign in to comment.