Skip to content

Commit

Permalink
feat: applicable less and scss to vite (#1187)
Browse files Browse the repository at this point in the history
  • Loading branch information
atzcl authored Mar 30, 2021
1 parent 874f6a2 commit fb01176
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@testing-library/react": "^11.2.3",
"@testing-library/vue": "^5.6.1",
"@types/hoist-non-react-statics": "^3.3.1",
"@types/fs-extra": "^9.0.9",
"@types/jest": "^24.0.18",
"@types/node": "^12.6.8",
"@types/react": "^17.0.0",
Expand Down
62 changes: 54 additions & 8 deletions packages/antd/copy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
import { copy } from 'fs-extra'
import { copy, readFile, writeFile, existsSync } from 'fs-extra'
import glob from 'glob'

glob(`./src/**/*.{less,scss}`, (err, files) => {
if (err) {
throw err
export const runCopy = ({
esStr,
libStr,
}: Record<'esStr' | 'libStr', string>) => {
const replaceOperation = async (filename: string) => {
if (!existsSync(filename)) {
return Promise.resolve()
}

const fileContent: string = (await readFile(filename)).toString()

return writeFile(
filename,
fileContent.replace(new RegExp(libStr, 'g'), esStr)
)
}
files.forEach((filename) => {
copy(filename, filename.replace(/src\//, 'esm/'))
copy(filename, filename.replace(/src\//, 'lib/'))

return new Promise((resolve, reject) => {
glob(`./src/**/*`, (err, files) => {
if (err) {
return reject(err)
}

const all = [] as Promise<unknown>[]

for (let i = 0; i < files.length; i += 1) {
const filename = files[i]

if (/\.(less|scss)$/.test(filename)) {
all.push(copy(filename, filename.replace(/src\//, 'esm/')))
all.push(copy(filename, filename.replace(/src\//, 'lib/')))

continue
}

if (/\/style.ts$/.test(filename)) {
replaceOperation(
filename.replace(/src\//, 'esm/').replace(/\.ts$/, '.js')
)

continue
}
}

return Promise.all(all).then(resolve).catch(reject)
})
})
}

if (Boolean(process.env.INIT_RUN)) {
runCopy({
esStr: 'antd/es/',
libStr: 'antd/lib/',
})
})
}
4 changes: 2 additions & 2 deletions packages/antd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"start": "dumi dev",
"build": "rimraf -rf lib && npm run build:cjs && npm run build:esm && npm run copy:style",
"copy:style": "ts-node copy",
"copy:style": "cross-env INIT_RUN=true ts-node copy",
"build:cjs": "tsc --declaration",
"build:esm": "tsc --declaration --module es2015 --outDir esm",
"build:docs": "dumi build"
Expand Down Expand Up @@ -52,4 +52,4 @@
"access": "public"
},
"gitHead": "4d068dad6183e8da294a4c899a158326c0b0b050"
}
}
14 changes: 4 additions & 10 deletions packages/next/copy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { copy } from 'fs-extra'
import glob from 'glob'
import { runCopy } from '../antd/copy'

glob(`./src/**/*.{less,scss}`, (err, files) => {
if (err) {
throw err
}
files.forEach((filename) => {
copy(filename, filename.replace(/src\//, 'esm/'))
copy(filename, filename.replace(/src\//, 'lib/'))
})
runCopy({
esStr: '@alifd/next/es/',
libStr: '@alifd/next/lib/',
})
3 changes: 2 additions & 1 deletion packages/react/src/shared/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import { createPortal } from 'react-dom'
import { globalThisPolyfill } from '@formily/shared'

const env = {
Expand All @@ -9,7 +10,7 @@ export const render = (element: React.ReactElement) => {
if (globalThisPolyfill['document']) {
env.portalDOM =
env.portalDOM || globalThisPolyfill['document'].createElement('div')
return require('react-dom').createPortal(element, env.portalDOM)
return createPortal(element, env.portalDOM)
} else {
return React.createElement('template', {}, element)
}
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,13 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/fs-extra@^9.0.9":
version "9.0.9"
resolved "https://registry.npm.taobao.org/@types/fs-extra/download/@types/fs-extra-9.0.9.tgz#11ed43b3f3c6b3490f1ef9bd17f58da896e2d861"
integrity sha1-Ee1Ds/PGs0kPHvm9F/WNqJbi2GE=
dependencies:
"@types/node" "*"

"@types/glob@^7.1.1":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183"
Expand Down

0 comments on commit fb01176

Please sign in to comment.