Enable ESLint on webpack files

This commit is contained in:
the1812 2022-06-15 20:44:46 +08:00
parent 4cb75cd8ce
commit 432fdc0289
14 changed files with 91 additions and 94 deletions

View File

@ -1,7 +1,7 @@
webpack/
packages/ packages/
typings/ typings/
dist/ dist/
dev/ dev/
node_modules/
*.config.* *.config.*
.eslintrc.* .eslintrc.*

View File

@ -18,10 +18,7 @@ module.exports = {
parser: '@typescript-eslint/parser', parser: '@typescript-eslint/parser',
sourceType: 'module', sourceType: 'module',
}, },
plugins: [ plugins: ['vue', '@typescript-eslint'],
'vue',
'@typescript-eslint',
],
rules: { rules: {
'import/no-unresolved': 'off', 'import/no-unresolved': 'off',
'import/extensions': 'off', 'import/extensions': 'off',
@ -38,7 +35,8 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-use-before-define': ['error'], '@typescript-eslint/no-use-before-define': ['error'],
'@typescript-eslint/no-redeclare': 'error', '@typescript-eslint/no-redeclare': 'error',
'@typescript-eslint/naming-convention': ['error', '@typescript-eslint/naming-convention': [
'error',
{ {
selector: 'enumMember', selector: 'enumMember',
format: ['PascalCase'], format: ['PascalCase'],
@ -65,7 +63,7 @@ module.exports = {
'no-await-in-loop': 'off', 'no-await-in-loop': 'off',
'no-restricted-syntax': 'off', 'no-restricted-syntax': 'off',
'no-useless-escape': 'off', 'no-useless-escape': 'off',
'no-empty-function': ['error', { 'allow': ['constructors'] }], 'no-empty-function': ['error', { allow: ['constructors'] }],
'no-return-assign': ['error', 'except-parens'], 'no-return-assign': ['error', 'except-parens'],
'no-redeclare': 'off', 'no-redeclare': 'off',
'no-script-url': 'off', 'no-script-url': 'off',
@ -76,33 +74,34 @@ module.exports = {
'arrow-parens': ['error', 'as-needed'], 'arrow-parens': ['error', 'as-needed'],
'object-curly-newline': 'off', 'object-curly-newline': 'off',
'semi': ['error', 'never'], semi: ['error', 'never'],
'linebreak-style': 'off', 'linebreak-style': 'off',
'camelcase': 'off', camelcase: 'off',
'lines-between-class-members': 'off', 'lines-between-class-members': 'off',
'radix': ['error', 'as-needed'], radix: ['error', 'as-needed'],
'max-len': 'error', 'max-len': 'error',
'max-classes-per-file': 'off', 'max-classes-per-file': 'off',
'prefer-destructuring': ['error', 'prefer-destructuring': [
'error',
{ {
'VariableDeclarator': { VariableDeclarator: {
'array': false, array: false,
'object': true object: true,
},
AssignmentExpression: {
array: false,
object: false,
}, },
'AssignmentExpression': {
'array': false,
'object': false
}
}, },
], ],
'curly': ['error', 'all'], curly: ['error', 'all'],
}, },
overrides: [ overrides: [
{ {
files: ['*.vue', 'shims.d.ts'], files: ['*.vue', 'shims.d.ts', 'webpack/**/*.ts', 'registry/webpack/**/*.ts'],
rules: { rules: {
'import/no-default-export': 'off', 'import/no-default-export': 'off',
} },
} },
] ],
} }

View File

@ -6,10 +6,10 @@
"author": "Grant Howard, Coulomb-G", "author": "Grant Howard, Coulomb-G",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"build-core": "webpack --config ./webpack/webpack.prod.js --bail", "build-core": "webpack --config ./webpack/webpack.prod.ts --bail",
"build-features": "webpack --config ./registry/webpack/all.js --bail", "build-features": "webpack --config ./registry/webpack/all.ts --bail",
"lint": "eslint --config ./.eslintrc.js --quiet --fix \"src/**/*.@(js|ts|vue)\" \"registry/lib/**/*.@(js|ts|vue)\"", "lint": "eslint --config ./.eslintrc.js --quiet --fix \"./**/*.@(js|ts|vue)\"",
"lint-check": "eslint --config ./.eslintrc.js \"src/**/*.@(js|ts|vue)\" \"registry/lib/**/*.@(js|ts|vue)\"", "lint-check": "eslint --config ./.eslintrc.js \"./**/*.@(js|ts|vue)\"",
"type": "tsc -p tsconfig.type-check.json --noEmit" "type": "tsc -p tsconfig.type-check.json --noEmit"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,8 +1,6 @@
import { builders } from './build' import { builders } from './build'
export default async () => { export default async () => [
return [ ...(await builders.component({ buildAll: true })),
...(await builders.component({ buildAll: true })), ...(await builders.plugin({ buildAll: true })),
...(await builders.plugin({ buildAll: true })), ]
]
}

View File

@ -1,10 +1,10 @@
import { buildByEntry } from './config'
import glob from 'glob' import glob from 'glob'
import { buildByEntry } from './config'
export const builders = Object.fromEntries(['component', 'plugin', 'doc'].map(type => { export const builders = Object.fromEntries(['component', 'plugin', 'doc'].map(type => {
const src = `./registry/lib/${type}s/` const src = `./registry/lib/${type}s/`
return [type, async ({ buildAll = false } = {}) => { return [type, async ({ buildAll = false } = {}) => {
const entries = glob.sync(src + '**/index.ts') const entries = glob.sync(`${src}**/index.ts`)
if (buildAll) { if (buildAll) {
console.log(`[build all] discovered ${entries.length} ${type}s`) console.log(`[build all] discovered ${entries.length} ${type}s`)
@ -13,7 +13,7 @@ export const builders = Object.fromEntries(['component', 'plugin', 'doc'].map(ty
let entry: string let entry: string
if (entries.length > 1) { if (entries.length > 1) {
const { AutoComplete } = require('enquirer') const { AutoComplete } = await import('enquirer')
const prompt = new AutoComplete({ const prompt = new AutoComplete({
name: 'path', name: 'path',
message: 'Select build target', message: 'Select build target',

View File

@ -1,13 +1,17 @@
import { getDefaultConfig } from '../../webpack/webpack.config'
import path from 'path' import path from 'path'
import { getId } from '../lib/id' import lodash from 'lodash'
import { Configuration } from 'webpack' import { Configuration } from 'webpack'
import { getDefaultConfig } from '../../webpack/webpack.config'
import { getId } from '../lib/id'
export const buildByEntry = ({ src, type, entry }: { src: string; type: string; entry: string }) => { export const buildByEntry = (
params: { src: string; type: string; entry: string },
) => {
// const match = entry.match(/\/?registry\/dist\/([^\/]+)\/(.+)\/index\.ts/) // const match = entry.match(/\/?registry\/dist\/([^\/]+)\/(.+)\/index\.ts/)
// if (!match) { // if (!match) {
// throw new Error(`Invalid entry path: ${entry}`) // throw new Error(`Invalid entry path: ${entry}`)
// } // }
const { src, type, entry } = params
const id = getId(src, entry) const id = getId(src, entry)
const defaultConfig = getDefaultConfig(path.resolve('./registry/lib/')) const defaultConfig = getDefaultConfig(path.resolve('./registry/lib/'))
const config: Configuration = { const config: Configuration = {
@ -29,7 +33,6 @@ export const buildByEntry = ({ src, type, entry }: { src: string; type: string;
externals: [ externals: [
...(defaultConfig.externals as any[]), ...(defaultConfig.externals as any[]),
({ request }, callback) => { ({ request }, callback) => {
const lodash = require('lodash')
const regexMatch = (regex: RegExp, base: string[]) => { const regexMatch = (regex: RegExp, base: string[]) => {
const match = request.match(regex) const match = request.match(regex)
if (match) { if (match) {
@ -68,7 +71,7 @@ export const buildByEntry = ({ src, type, entry }: { src: string; type: string;
} }
} }
return callback() return callback()
} },
], ],
} }
return config return config

View File

@ -1,10 +1,8 @@
import { builders } from './build'
import path from 'path' import path from 'path'
import { builders } from './build'
export default builders.doc().then(configs => { export default builders.doc().then(configs => configs.map(config => {
return configs.map(config => { config.output.path = path.resolve('./registry/dist/')
config.output.path = path.resolve(`./registry/dist/`) config.output.filename = 'doc.js'
config.output.filename = 'doc.js' return config
return config }))
})
})

3
src/shims.d.ts vendored
View File

@ -23,3 +23,6 @@ declare module '*.svg' {
const svg: string const svg: string
export default svg export default svg
} }
declare module 'enquirer' {
export const AutoComplete: any
}

View File

@ -1,10 +1,10 @@
import { runtimeInfo } from '../compilation-info/runtime'
import { commitHash } from '../compilation-info/git'
import { import {
objectProperty, objectProperty,
identifier, identifier,
stringLiteral, stringLiteral,
} from '@babel/types' } from '@babel/types'
import { runtimeInfo } from '../compilation-info/runtime'
import { commitHash } from '../compilation-info/git'
import { InjectMetadataAction } from './types' import { InjectMetadataAction } from './types'
export const injectCoreInfo: InjectMetadataAction = () => [ export const injectCoreInfo: InjectMetadataAction = () => [

View File

@ -1,6 +1,5 @@
import { existsSync, readFileSync } from 'fs' import { existsSync, readFileSync } from 'fs'
import { basename, dirname, join } from 'path' import { basename, dirname, join } from 'path'
import { InjectMetadataAction } from './types'
import { glob } from 'glob' import { glob } from 'glob'
import { import {
objectProperty, objectProperty,
@ -8,6 +7,7 @@ import {
identifier, identifier,
stringLiteral, stringLiteral,
} from '@babel/types' } from '@babel/types'
import { InjectMetadataAction } from './types'
export const injectDescription: InjectMetadataAction = ({ filename }) => { export const injectDescription: InjectMetadataAction = ({ filename }) => {
const folder = dirname(filename) const folder = dirname(filename)

View File

@ -1,5 +1,4 @@
import { basename, dirname } from 'path' import { basename, dirname } from 'path'
import { InjectMetadataAction } from './types'
import { glob } from 'glob' import { glob } from 'glob'
import { import {
ObjectProperty, ObjectProperty,
@ -11,6 +10,7 @@ import {
arrowFunctionExpression, arrowFunctionExpression,
stringLiteral, stringLiteral,
} from '@babel/types' } from '@babel/types'
import { InjectMetadataAction } from './types'
export const injectI18n: InjectMetadataAction = ({ filename }) => { export const injectI18n: InjectMetadataAction = ({ filename }) => {
const folder = dirname(filename) const folder = dirname(filename)

View File

@ -11,45 +11,42 @@ const injectActions: InjectMetadataAction[] = [
injectI18n, injectI18n,
] ]
export const injectMetadata = (): PluginObj => { export const injectMetadata = (): PluginObj => ({
return { visitor: {
visitor: { ExportNamedDeclaration(path, state) {
ExportNamedDeclaration(path, state) { const { filename } = state.file.opts
const { filename } = state.file.opts const isFromRegistry = filename.startsWith(
const isFromRegistry = filename.startsWith( nodePath.resolve('./registry'),
nodePath.resolve('./registry'), )
) const isFromCore = filename.startsWith(
const isFromCore = filename.startsWith( nodePath.resolve('./src/components'),
nodePath.resolve('./src/components'), ) || filename.startsWith(
) || filename.startsWith( nodePath.resolve('./src/plugins'),
nodePath.resolve('./src/plugins'), )
) const isEntryFile = nodePath.basename(filename) === 'index.ts'
const isEntryFile = nodePath.basename(filename) === 'index.ts' if (!((isFromRegistry || isFromCore) && isEntryFile)) {
if (!((isFromRegistry || isFromCore) && isEntryFile)) { return
}
const { node } = path
if (node.declaration?.type !== 'VariableDeclaration') {
return
}
node.declaration.declarations?.forEach(d => {
const isNameValid = d.id?.type === 'Identifier' && ['component', 'plugin'].includes(d.id.name)
if (!isNameValid) {
return return
} }
const { node } = path const targetExpression = d.init.type === 'CallExpression' ? d.init.arguments[0] : d.init
if (node.declaration?.type !== 'VariableDeclaration') { if (targetExpression.type !== 'ObjectExpression') {
return return
} }
node.declaration.declarations?.forEach(d => { targetExpression.properties.push(
const isNameValid = d.id?.type === 'Identifier' && ['component', 'plugin'].includes(d.id.name) ...injectActions.flatMap(action => action({
if (!isNameValid) { expression: targetExpression,
return filename,
} })),
const targetExpression = )
d.init.type === 'CallExpression' ? d.init.arguments[0] : d.init })
if (targetExpression.type !== 'ObjectExpression') {
return
}
targetExpression.properties.push(
...injectActions.flatMap(action => action({
expression: targetExpression,
filename,
})),
)
})
},
}, },
} },
} })

View File

@ -26,5 +26,5 @@ export const sassLoader: RuleSetUseItem = {
}, },
} }
export const cssStyleLoaders = [ cssLoader, postCssLoader ] export const cssStyleLoaders = [cssLoader, postCssLoader]
export const sassStyleLoaders = [ cssLoader, postCssLoader, sassLoader ] export const sassStyleLoaders = [cssLoader, postCssLoader, sassLoader]

View File

@ -1,7 +1,6 @@
import webpack from 'webpack' import webpack, { Configuration } from 'webpack'
import { getBanner, getDefaultConfig } from './webpack.config' import { getBanner, getDefaultConfig } from './webpack.config'
import previewMeta from '../src/client/bilibili-evolved.preview.meta.json' import previewMeta from '../src/client/bilibili-evolved.preview.meta.json'
import { Configuration } from 'webpack'
const previewConfig = Object.assign(getDefaultConfig(), { const previewConfig = Object.assign(getDefaultConfig(), {
entry: './src/client/bilibili-evolved.ts', entry: './src/client/bilibili-evolved.ts',