From 085ac938c2641cbb31ec6bf50f0617eafe543ba4 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 16 Feb 2018 02:53:54 +0900 Subject: [PATCH] wip --- package.json | 1 + webpack/loaders/replace.js | 15 +++++++++++++++ webpack/module/rules/fa.ts | 6 +++--- webpack/module/rules/i18n.ts | 6 +++--- webpack/module/rules/index.ts | 5 +++-- webpack/webpack.config.ts | 3 +++ 6 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 webpack/loaders/replace.js diff --git a/package.json b/package.json index 06e517a0d1..6df445f290 100644 --- a/package.json +++ b/package.json @@ -125,6 +125,7 @@ "is-url": "1.2.2", "js-yaml": "3.10.0", "license-checker": "16.0.0", + "loader-utils": "^1.1.0", "mecab-async": "0.1.2", "mkdirp": "0.5.1", "mocha": "5.0.0", diff --git a/webpack/loaders/replace.js b/webpack/loaders/replace.js new file mode 100644 index 0000000000..41c33ce8d6 --- /dev/null +++ b/webpack/loaders/replace.js @@ -0,0 +1,15 @@ +const loaderUtils = require('loader-utils'); + +function trim(text) { + return text.substring(1, text.length - 2); +} + +module.exports = function(src) { + this.cacheable(); + const options = loaderUtils.getOptions(this); + if (typeof options.search != 'string' || options.search.length == 0) console.error('invalid search'); + if (typeof options.replace != 'function') console.error('invalid replacer'); + src = src.replace(new RegExp(trim(options.search), 'g'), options.replace); + this.callback(null, src); + return src; +}; diff --git a/webpack/module/rules/fa.ts b/webpack/module/rules/fa.ts index 2ac89ce4f8..a31bf1bee2 100644 --- a/webpack/module/rules/fa.ts +++ b/webpack/module/rules/fa.ts @@ -5,12 +5,12 @@ import { pattern, replacement } from '../../../src/common/build/fa'; export default () => ({ - enforce: 'pre', + //enforce: 'pre', test: /\.(vue|js|ts)$/, exclude: /node_modules/, - loader: 'string-replace-loader', + loader: 'replace', query: { - search: pattern, + search: pattern.toString(), replace: replacement } }); diff --git a/webpack/module/rules/i18n.ts b/webpack/module/rules/i18n.ts index 2352a42be0..f3270b4df7 100644 --- a/webpack/module/rules/i18n.ts +++ b/webpack/module/rules/i18n.ts @@ -8,12 +8,12 @@ export default lang => { const replacer = new Replacer(lang); return { - enforce: 'pre', + //enforce: 'post', test: /\.(vue|js|ts)$/, exclude: /node_modules/, - loader: 'string-replace-loader', + loader: 'replace', query: { - search: replacer.pattern, + search: replacer.pattern.toString(), replace: replacer.replacement } }; diff --git a/webpack/module/rules/index.ts b/webpack/module/rules/index.ts index 1ddebacffa..d97614ad29 100644 --- a/webpack/module/rules/index.ts +++ b/webpack/module/rules/index.ts @@ -8,10 +8,11 @@ import collapseSpaces from './collapse-spaces'; export default lang => [ //collapseSpaces(), - //i18n(lang), - //fa(), + //base64(), vue(), + i18n(lang), + fa(), stylus(), typescript() ]; diff --git a/webpack/webpack.config.ts b/webpack/webpack.config.ts index f4b9247e63..dd00acaae6 100644 --- a/webpack/webpack.config.ts +++ b/webpack/webpack.config.ts @@ -40,6 +40,9 @@ module.exports = Object.keys(langs).map(lang => { '.js', '.ts' ] }, + resolveLoader: { + modules: ['node_modules', './webpack/loaders'] + }, cache: true, devtool: 'eval', stats: true,