Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:support webpack5 #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 47 additions & 36 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path')
const path = require('path');

const presets = {
antd: {
Expand All @@ -14,7 +14,7 @@ const presets = {
'localeData',
'localizedFormat',
],
replaceMoment: true
replaceMoment: true,
},
antdv3: {
plugins: [
Expand All @@ -28,82 +28,93 @@ const presets = {
'isMoment',
'localeData',
'localizedFormat',
'badMutable'
'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;
if (typeof entry === 'object' && !Array.isArray(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") {
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);
};
}
}
};

class WebpackDayjsPlugin {
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;