From 928479a9bf394065e60185a563530b6f47aeca5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=99=A8=E6=9D=B0?= <309272985@qq.com> Date: Fri, 25 Jun 2021 13:22:41 +0800 Subject: [PATCH 1/2] feat:support webpack5 --- src/index.js | 119 ++++++++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 54 deletions(-) diff --git a/src/index.js b/src/index.js index 10ad27c..2c97141 100644 --- a/src/index.js +++ b/src/index.js @@ -1,45 +1,54 @@ -const path = require('path') +const path = require("path"); const presets = { antd: { plugins: [ - 'isSameOrBefore', - 'isSameOrAfter', - 'advancedFormat', - 'customParseFormat', - 'weekday', - 'weekYear', - 'weekOfYear', - 'isMoment', - 'localeData', - 'localizedFormat', + "isSameOrBefore", + "isSameOrAfter", + "advancedFormat", + "customParseFormat", + "weekday", + "weekYear", + "weekOfYear", + "isMoment", + "localeData", + "localizedFormat", ], - replaceMoment: true + replaceMoment: true, }, antdv3: { plugins: [ - 'isSameOrBefore', - 'isSameOrAfter', - 'advancedFormat', - 'customParseFormat', - 'weekday', - 'weekYear', - 'weekOfYear', - 'isMoment', - 'localeData', - 'localizedFormat', - 'badMutable' + "isSameOrBefore", + "isSameOrAfter", + "advancedFormat", + "customParseFormat", + "weekday", + "weekYear", + "weekOfYear", + "isMoment", + "localeData", + "localizedFormat", + "badMutable", ], - replaceMoment: true - } -} + replaceMoment: true, + }, +}; const makeEntry = (entry, initEntry) => { if (typeof entry === "object" && !Array.isArray(entry)) { - Object.keys(entry).forEach(e => { - entry[e] = makeEntry(entry[e], initEntry); - }); - return entry; + return Object.entries(entry).reduce( + (acc, [curKey, curEntry]) => ({ + ...acc, + [curKey]: + typeof curEntry === "object" && curEntry.import + ? { + ...curEntry, + import: makeEntry(curEntry.import, initEntry), + } + : makeEntry(curEntry, initEntry), + }), + {} + ); } if (typeof entry === "string") { return [initEntry, entry]; @@ -53,57 +62,59 @@ const makeEntry = (entry, initEntry) => { return makeEntry(originalEntry, initEntry); }; } -} +}; class WebpackDayjsPlugin { - constructor(options = { preset: 'antd' }) { + constructor(options = { preset: "antd" }) { const { preset, plugins, replaceMoment } = options; if (preset && presets[preset]) { - this.plugins = presets[preset].plugins - this.replaceMoment = presets[preset].replaceMoment + this.plugins = presets[preset].plugins; + this.replaceMoment = presets[preset].replaceMoment; } - if (plugins) this.plugins = plugins - if (replaceMoment !== undefined) this.replaceMoment = replaceMoment + if (plugins) this.plugins = plugins; + if (replaceMoment !== undefined) this.replaceMoment = replaceMoment; } apply(compiler) { // add init dayjs entry and loader if (this.plugins) { - const { entry, module } = compiler.options + const { entry, module } = compiler.options; const initLoaderRule = { test: /init-dayjs-webpack-plugin-entry\.js$/, - use: [{ - loader: path.resolve(__dirname, './init-loader.js'), - options: { - plugins: this.plugins - } - }] - } + use: [ + { + loader: path.resolve(__dirname, "./init-loader.js"), + options: { + plugins: this.plugins, + }, + }, + ], + }; if (module.rules) { - module.rules.push(initLoaderRule) + module.rules.push(initLoaderRule); } else { - compiler.options.module.rules = [initLoaderRule] + compiler.options.module.rules = [initLoaderRule]; } - const initFilePath = path.resolve(__dirname, 'init-dayjs-webpack-plugin-entry.js') - const initEntry = require.resolve(initFilePath) + const initFilePath = path.resolve(__dirname, "init-dayjs-webpack-plugin-entry.js"); + const initEntry = require.resolve(initFilePath); - compiler.options.entry = makeEntry(entry, initEntry) + compiler.options.entry = makeEntry(entry, initEntry); } // set dayjs alias if (this.replaceMoment) { - const { alias } = compiler.options.resolve + const { alias } = compiler.options.resolve; if (alias) { - alias.moment = 'dayjs' + alias.moment = "dayjs"; } else { compiler.options.resolve.alias = { - moment: 'dayjs' - } + moment: "dayjs", + }; } } } } -module.exports = WebpackDayjsPlugin +module.exports = WebpackDayjsPlugin; From a4f7dfb9e3e5f8ff117b8eb4413e8fdfcb9c6521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=99=A8=E6=9D=B0?= <309272985@qq.com> Date: Fri, 25 Jun 2021 13:25:14 +0800 Subject: [PATCH 2/2] chore:prettier singleQuote --- src/index.js | 62 ++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/index.js b/src/index.js index 2c97141..51bd8dc 100644 --- a/src/index.js +++ b/src/index.js @@ -1,46 +1,46 @@ -const path = require("path"); +const path = require('path'); const presets = { antd: { plugins: [ - "isSameOrBefore", - "isSameOrAfter", - "advancedFormat", - "customParseFormat", - "weekday", - "weekYear", - "weekOfYear", - "isMoment", - "localeData", - "localizedFormat", + 'isSameOrBefore', + 'isSameOrAfter', + 'advancedFormat', + 'customParseFormat', + 'weekday', + 'weekYear', + 'weekOfYear', + 'isMoment', + 'localeData', + 'localizedFormat', ], replaceMoment: true, }, antdv3: { plugins: [ - "isSameOrBefore", - "isSameOrAfter", - "advancedFormat", - "customParseFormat", - "weekday", - "weekYear", - "weekOfYear", - "isMoment", - "localeData", - "localizedFormat", - "badMutable", + 'isSameOrBefore', + 'isSameOrAfter', + 'advancedFormat', + 'customParseFormat', + 'weekday', + 'weekYear', + 'weekOfYear', + 'isMoment', + 'localeData', + 'localizedFormat', + 'badMutable', ], replaceMoment: true, }, }; const makeEntry = (entry, initEntry) => { - if (typeof entry === "object" && !Array.isArray(entry)) { + if (typeof entry === 'object' && !Array.isArray(entry)) { return Object.entries(entry).reduce( (acc, [curKey, curEntry]) => ({ ...acc, [curKey]: - typeof curEntry === "object" && curEntry.import + typeof curEntry === 'object' && curEntry.import ? { ...curEntry, import: makeEntry(curEntry.import, initEntry), @@ -50,13 +50,13 @@ const makeEntry = (entry, initEntry) => { {} ); } - if (typeof entry === "string") { + if (typeof entry === 'string') { return [initEntry, entry]; } if (Array.isArray(entry)) { return [initEntry].concat(entry); } - if (typeof entry === "function") { + if (typeof entry === 'function') { return async () => { const originalEntry = await entry(); return makeEntry(originalEntry, initEntry); @@ -65,7 +65,7 @@ const makeEntry = (entry, initEntry) => { }; class WebpackDayjsPlugin { - constructor(options = { preset: "antd" }) { + constructor(options = { preset: 'antd' }) { const { preset, plugins, replaceMoment } = options; if (preset && presets[preset]) { this.plugins = presets[preset].plugins; @@ -84,7 +84,7 @@ class WebpackDayjsPlugin { test: /init-dayjs-webpack-plugin-entry\.js$/, use: [ { - loader: path.resolve(__dirname, "./init-loader.js"), + loader: path.resolve(__dirname, './init-loader.js'), options: { plugins: this.plugins, }, @@ -98,7 +98,7 @@ class WebpackDayjsPlugin { compiler.options.module.rules = [initLoaderRule]; } - const initFilePath = path.resolve(__dirname, "init-dayjs-webpack-plugin-entry.js"); + const initFilePath = path.resolve(__dirname, 'init-dayjs-webpack-plugin-entry.js'); const initEntry = require.resolve(initFilePath); compiler.options.entry = makeEntry(entry, initEntry); @@ -107,10 +107,10 @@ class WebpackDayjsPlugin { if (this.replaceMoment) { const { alias } = compiler.options.resolve; if (alias) { - alias.moment = "dayjs"; + alias.moment = 'dayjs'; } else { compiler.options.resolve.alias = { - moment: "dayjs", + moment: 'dayjs', }; } }