Refactoring
This commit is contained in:
5
webpack/module/index.ts
Normal file
5
webpack/module/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import rules from './rules';
|
||||
|
||||
export default (lang, locale) => ({
|
||||
rules: rules(lang, locale)
|
||||
});
|
34
webpack/module/rules/i18n.ts
Normal file
34
webpack/module/rules/i18n.ts
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Replace i18n texts
|
||||
*/
|
||||
|
||||
const StringReplacePlugin = require('string-replace-webpack-plugin');
|
||||
|
||||
export default (lang, locale) => ({
|
||||
enforce: 'pre',
|
||||
test: /\.(tag|js)$/,
|
||||
exclude: /node_modules/,
|
||||
loader: StringReplacePlugin.replace({
|
||||
replacements: [
|
||||
{
|
||||
pattern: /%i18n:(.+?)%/g, replacement: (_, key) => {
|
||||
let text = locale;
|
||||
const error = key.split('.').some(k => {
|
||||
if (text.hasOwnProperty(k)) {
|
||||
text = text[k];
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (error) {
|
||||
console.warn(`key '${key}' not found in '${lang}'`);
|
||||
return key;
|
||||
} else {
|
||||
return text.replace(/'/g, '\\\'').replace(/"/g, '\\"');
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
});
|
11
webpack/module/rules/index.ts
Normal file
11
webpack/module/rules/index.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import i18n from './i18n';
|
||||
import themeColor from './theme-color';
|
||||
import tag from './tag';
|
||||
import stylus from './stylus';
|
||||
|
||||
export default (lang, locale) => [
|
||||
i18n(lang, locale),
|
||||
themeColor(),
|
||||
tag(),
|
||||
stylus()
|
||||
];
|
13
webpack/module/rules/stylus.ts
Normal file
13
webpack/module/rules/stylus.ts
Normal file
@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Stylus support
|
||||
*/
|
||||
|
||||
export default () => ({
|
||||
test: /\.styl$/,
|
||||
exclude: /node_modules/,
|
||||
use: [
|
||||
{ loader: 'style-loader' },
|
||||
{ loader: 'css-loader' },
|
||||
{ loader: 'stylus-loader' }
|
||||
]
|
||||
});
|
20
webpack/module/rules/tag.ts
Normal file
20
webpack/module/rules/tag.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Riot tags
|
||||
*/
|
||||
|
||||
export default () => ({
|
||||
test: /\.tag$/,
|
||||
exclude: /node_modules/,
|
||||
loader: 'riot-tag-loader',
|
||||
query: {
|
||||
hot: false,
|
||||
style: 'stylus',
|
||||
expr: false,
|
||||
compact: true,
|
||||
parserOptions: {
|
||||
style: {
|
||||
compress: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
25
webpack/module/rules/theme-color.ts
Normal file
25
webpack/module/rules/theme-color.ts
Normal file
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Theme color provider
|
||||
*/
|
||||
|
||||
const StringReplacePlugin = require('string-replace-webpack-plugin');
|
||||
|
||||
const constants = require('../../../src/const.json');
|
||||
|
||||
export default () => ({
|
||||
enforce: 'pre',
|
||||
test: /\.tag$/,
|
||||
exclude: /node_modules/,
|
||||
loader: StringReplacePlugin.replace({
|
||||
replacements: [
|
||||
{
|
||||
pattern: /\$theme\-color\-foreground/g,
|
||||
replacement: () => constants.themeColorForeground
|
||||
},
|
||||
{
|
||||
pattern: /\$theme\-color/g,
|
||||
replacement: () => constants.themeColor
|
||||
},
|
||||
]
|
||||
})
|
||||
});
|
Reference in New Issue
Block a user