-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
loader-generator.ts
55 lines (52 loc) · 1.5 KB
/
loader-generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import path from 'path';
import addonGenerator from './addon-generator';
import { toKebabCase } from './utils/helpers';
/**
* Formats a string into webpack loader format
* (eg: 'style-loader', 'raw-loader')
*
* @param {string} name A loader name to be formatted
* @returns {string} The formatted string
*/
export function makeLoaderName(name: string): string {
name = toKebabCase(name);
if (!/loader$/.test(name)) {
name += '-loader';
}
return name;
}
/**
* A yeoman generator class for creating a webpack
* loader project. It adds some starter loader code
* and runs `webpack-defaults`.
*
* @class LoaderGenerator
* @extends {Generator}
*/
const LoaderGenerator = addonGenerator(
[
{
default: 'my-loader',
filter: makeLoaderName,
message: 'Loader name',
name: 'name',
type: 'input',
validate: (str: string): boolean => str.length > 0,
},
],
path.resolve(__dirname, '../../generate-loader/templates'),
[
'src/cjs.js.tpl',
'test/test-utils.js.tpl',
'test/unit.test.js.tpl',
'test/functional.test.js.tpl',
'test/fixtures/simple-file.js.tpl',
'examples/simple/webpack.config.js.tpl',
'examples/simple/src/index.js.tpl',
'examples/simple/src/lazy-module.js.tpl',
'examples/simple/src/static-esm-module.js.tpl',
],
['src/_index.js.tpl'],
(gen): object => ({ name: gen.props.name }),
);
export default LoaderGenerator;