Skip to content

Commit

Permalink
Webpack 5 (#1)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Rewrite to webpack 5 support
  • Loading branch information
zainulbr authored Oct 22, 2020
1 parent 930100a commit 423f45b
Show file tree
Hide file tree
Showing 8 changed files with 7,674 additions and 103 deletions.
36 changes: 8 additions & 28 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
{
"presets": [
[
"env",
"@babel/preset-env",
{
"useBuiltIns": true,
"targets": {
"node": "4.3"
},
"exclude": [
"transform-async-to-generator",
"transform-regenerator"
]
"debug": false,
"targets": {
"browsers": [
"last 3 versions"
]
}
}
]
],
"plugins": [
[
"transform-object-rest-spread",
{
"useBuiltIns": true
}
]
],
"env": {
"test": {
"presets": [
"env"
],
"plugins": [
"transform-object-rest-spread"
]
}
}
]
}
8 changes: 5 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"import/no-unresolved": 0,
"import/extensions": 0,
"no-plusplus": 1,
"consistent-return": 1,
"consistent-return": 0,
"no-multi-assign": 1,
"no-param-reassign": 1,
"no-param-reassign": 0,
"prefer-destructuring": 1,
"no-nested-ternary": 1,
"prefer-rest-params": 1
"prefer-rest-params": 1,
"global-require":0,
"import/no-dynamic-require":0
}
}
46 changes: 29 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,39 @@
"webpack-defaults": "webpack-defaults"
},
"devDependencies": {
"babel-cli": "^6.24.0",
"babel-jest": "^19.0.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.5.1",
"cross-env": "^5.0.1",
"del-cli": "^1.0.0",
"eslint": "^3.19.0",
"eslint-config-webpack": "^1.2.1",
"eslint-plugin-import": "^2.2.0",
"jest": "^19.0.2",
"lint-staged": "^3.6.0",
"nsp": "^2.6.3",
"@babel/cli": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"babel-jest": "^26.6.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.26.0",
"cross-env": "^7.0.2",
"del-cli": "^3.0.1",
"eslint": "^7.11.0",
"eslint-config-webpack": "^1.2.5",
"eslint-plugin-import": "^2.22.1",
"jest": "^26.6.0",
"lint-staged": "^10.4.2",
"nsp": "^3.2.1",
"pre-commit": "^1.2.2",
"standard-version": "^4.0.0",
"webpack-defaults": "^1.4.0"
"standard-version": "^9.0.0",
"webpack": "^5.1.3",
"webpack-defaults": "^4.0.0"
},
"peerDependencies": {
"webpack": "^2.0.0 || >= 3.0.0-rc.0 || ^3.0.0"
"webpack": "*"
},
"jest": {
"roots": [
"./"
],
"moduleDirectories": [
"node_modules",
"src"
]
},
"engines": {
"node": ">= 4.3 < 5.0.0 || >= 5.10"
"node": "12"
},
"homepage": "https://webpack.js.org",
"repository": {
Expand All @@ -69,3 +80,4 @@
]
}
}

8 changes: 4 additions & 4 deletions src/MakeLocalizeFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ function makeLocalizeFunction(localization, nested) {
*
* @returns {*}
*/
function byString(localization, nestedKey) {
function byString(plocalization, nestedKey) {
// remove a leading dot and split by dot
const keys = nestedKey.replace(/^\./, '').split('.');

let localization = plocalization;
// loop through the keys to find the nested value
for (let i = 0, length = keys.length; i < length; ++i) {
for (let i = 0, { length } = keys; i < length; ++i) {
const key = keys[i];

if (!(key in localization)) { return; }
if (!(key in localization)) { return undefined; }

localization = localization[key];
}
Expand Down
104 changes: 56 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import makeLocalizeFunction from './MakeLocalizeFunction';
* @constructor
*/
class I18nPlugin {
constructor(localization, options, failOnMissing) {
constructor(localization, poptions, failOnMissing) {
let options = poptions;
// Backward-compatiblility
if (typeof options === 'string') {
options = {
Expand All @@ -27,69 +28,76 @@ class I18nPlugin {
}

this.options = options || {};
this.localization = localization ? (typeof localization === 'function' ? localization : makeLocalizeFunction(localization, !!this.options.nested)) : null;
const plocalization = (typeof localization === 'function' ? localization : makeLocalizeFunction(localization, !!this.options.nested));
this.localization = localization ? plocalization : {};
this.functionName = this.options.functionName || '__';
this.failOnMissing = !!this.options.failOnMissing;
this.hideMessage = this.options.hideMessage || false;
}

apply(compiler) {
const PLUGIN_NAME = 'I18nPlugin';
const { localization, failOnMissing, hideMessage } = this; // eslint-disable-line no-unused-vars
const name = this.functionName;

compiler.plugin('compilation', (compilation, params) => { // eslint-disable-line no-unused-vars
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation, { normalModuleFactory }) => {
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
});

compiler.plugin('compilation', (compilation, data) => {
data.normalModuleFactory.plugin('parser', (parser, options) => { // eslint-disable-line no-unused-vars
const handler = (parser) => { // eslint-disable-line no-unused-vars
// should use function here instead of arrow function due to save the Tapable's context
parser.plugin(`call ${name}`, function i18nPlugin(expr) {
let param;
let defaultValue;
switch (expr.arguments.length) {
case 2:
param = this.evaluateExpression(expr.arguments[1]);
if (!param.isString()) return;
param = param.string;
defaultValue = this.evaluateExpression(expr.arguments[0]);
if (!defaultValue.isString()) return;
defaultValue = defaultValue.string;
break;
case 1:
param = this.evaluateExpression(expr.arguments[0]);
if (!param.isString()) return;
defaultValue = param = param.string;
break;
default:
return;
}
let result = localization ? localization(param) : defaultValue;

if (typeof result === 'undefined') {
let error = this.state.module[__dirname];
if (!error) {
error = new MissingLocalizationError(this.state.module, param, defaultValue);
this.state.module[__dirname] = error;
parser.hooks.call
.for(name)
.tap(`call ${name}`, (expr) => {
let param;
let defaultValue;
switch (expr.arguments.length) {
case 2:
param = expr.arguments[1].value;
if (typeof param !== 'string') return;
defaultValue = expr.arguments[0].value;
if (typeof defaultValue !== 'string') return;
break;
case 1:
param = expr.arguments[0].value;
if (typeof param !== 'string') return;
defaultValue = param;
break;
default:
return;
}
let result = localization ? localization(param) : defaultValue;
if (typeof result === 'undefined') {
let error = parser.state.module[__dirname];
if (!error) {
error = new MissingLocalizationError(parser.state.module, param, defaultValue);
parser.state.module[__dirname] = error;

if (failOnMissing) {
this.state.module.errors.push(error);
} else if (!hideMessage) {
this.state.module.warnings.push(error);
if (failOnMissing) {
parser.state.module.errors.push(error);
} else if (!hideMessage) {
parser.state.module.warnings.push(error);
}
} else if (!error.requests.includes(param)) {
error.add(param, defaultValue);
}
} else if (!error.requests.includes(param)) {
error.add(param, defaultValue);
result = defaultValue;
}
result = defaultValue;
}

const dep = new ConstDependency(JSON.stringify(result), expr.range);
dep.loc = expr.loc;
this.state.current.addDependency(dep);
return true;
});
});
const dep = new ConstDependency(JSON.stringify(result), expr.range);
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
return true;
});
};
normalModuleFactory.hooks.parser
.for('javascript/auto')
.tap(PLUGIN_NAME, handler);
normalModuleFactory.hooks.parser
.for('javascript/dynamic')
.tap(PLUGIN_NAME, handler);
normalModuleFactory.hooks.parser
.for('javascript/esm')
.tap(PLUGIN_NAME, handler);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/cases/apply-translations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('apply-translations', () => {

return processFile('apply-translations.code.js', translations)
.then(({ file }) => {
translated = require.requireActual(file);
translated = require(file);
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/cases/opts-function-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('options.functionName', () => {

return processFile('opts-function-name.code.js', translations, options)
.then(({ file }) => {
translated = require.requireActual(file);
translated = require(file);
});
});

Expand All @@ -34,7 +34,7 @@ describe('options.functionName', () => {

return processFile('opts-function-name.code.js', translations, options)
.then(({ file }) => {
translated = require.requireActual(file);
translated = require(file);
});
});

Expand Down
Loading

0 comments on commit 423f45b

Please sign in to comment.