Generate mmlpx Store
name automatically, suitable for mobx actions as well.
Compatible with ts-loader(^2.2.0) and awesome-typescript-loader(^3.1.3).
import { action } from 'mobx';
import { Store } from 'mmlpx';
@Store
export default class UserStore {
@action updateUser() {}
}
import { action } from 'mobx';
import { Store } from 'mmlpx';
@Store('${filePath}/UserStore')
export default class UserStore {
@action updateUser() {}
}
If we had configured mobx action transpilation, the action name would be generated automatically.
import { action } from 'mobx';
import { Store } from 'mmlpx';
@Store('${filePath}/UserStore')
export default class UserStore {
@action('UserStore/updateUser') updateUser() {}
}
const createMmlpxTransformer = require('ts-plugin-mmlpx').default;
var config = {
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
transpileOnly: true,
compilerOptions: {
module: 'es2015'
},
getCustomTransformers: () => ({
before: [
// transform Store/ViewModel of mmlpx by default
createMmlpxTransformer(),
// manual config mobx action
// createMmlpxTransformer([
// { libraryName: 'mobx', bindings: ['action'] }
// ]),
]
})
}
}
]
}
}
const defaultOptions = {
libraryName: 'mmlpx',
bindings: ['Store', 'ViewModel'],
};
function createTransformer(options: Array<Partial<Options>> = [defaultOptions]): ts.TransformerFactory<ts.SourceFile>
type Options = {
libraryName?: string;
bindings?: string[];
};
As we use ${fileName}/${decoratedClassName}
as the id of configured bindings, we should name our Store/ViewModel and file name more descriptive, such as UserStore.ts/UserStore.
Avoid naming file name as index.ts and Store/ViewModel as Index class like index.ts/Index after transpilation, make sure they're unique.