-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: eliminate TS6053 error * chore: add deps * feat: add build style * refactor(antd): use build style * refactor(next): use build style * fix: eliminate ts error in rollup-plugin-postcss * feat: add build all styles output option
- Loading branch information
atzcl
authored
Apr 9, 2021
1 parent
1da31ca
commit 3ceedb1
Showing
17 changed files
with
349 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { build } from '../../scripts/build-style' | ||
|
||
build({ | ||
esStr: 'antd/es/', | ||
libStr: 'antd/lib/', | ||
styleEntry: 'style.less', | ||
allStylesOutputFile: 'dist/antd.css', | ||
// antd/es/button/style/index ===> antd/es/button/style/css | ||
importCssCompilerToCssTransform: (v) => | ||
v.replace(/'antd\/(es|lib)\/(.*)'/, (subStr) => | ||
subStr.replace(/\/style\/index'$/, `/style/css'`) | ||
), | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import './style.less' | ||
import './style.less' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import './style.less' | ||
import './style.less' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { build } from '../../scripts/build-style' | ||
|
||
build({ | ||
esStr: '@alifd/next/es/', | ||
libStr: '@alifd/next/lib/', | ||
styleEntry: 'main.scss', | ||
allStylesOutputFile: 'dist/next.css', | ||
// antd/es/button/style/index ===> antd/es/button/style/css | ||
importCssCompilerToCssTransform: (v) => | ||
v.replace(/'@alifd\/next\/(es|lib)\/(.*)'/g, (subStr) => | ||
subStr.replace(/\/style'$/, `/index.css'`) | ||
), | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import './main.scss' | ||
import './main.scss' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import './main.scss' | ||
import './main.scss' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import fs from 'fs-extra' | ||
import { join } from 'path' | ||
import { identifier } from 'safe-identifier' | ||
|
||
import typescript from 'rollup-plugin-typescript2' | ||
|
||
import { build, getRollupBasePlugin } from './helper' | ||
|
||
/** | ||
* @ref https://github.com/egoist/rollup-plugin-postcss/blob/master/src/postcss-loader.js | ||
*/ | ||
const styleInjectPath = require | ||
.resolve('style-inject/dist/style-inject.es') | ||
.replace(/[\\/]+/g, '/') | ||
|
||
let styleInjectText = '' | ||
const generateCssStyleInject = async (cssFilePath: string) => { | ||
if (!styleInjectText) { | ||
styleInjectText = (await fs.readFile(styleInjectPath)).toString() | ||
styleInjectText = styleInjectText.replace('export default styleInject;', '') | ||
} | ||
|
||
let cssContent = (await fs.readFile(cssFilePath)).toString() | ||
|
||
// 删除可能存在的 sourceMap 注释 | ||
cssContent = cssContent.replace(/\n\/\*#(.*)css.map\s\*\//g, '') | ||
|
||
const cssVariableName = identifier('css', true) | ||
|
||
return fs.outputFile( | ||
cssFilePath.replace('.css', '.js'), | ||
styleInjectText + | ||
`\nvar ${cssVariableName} = "${cssContent}";\n\nstyleInject(${cssVariableName})` | ||
) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const buildAllStyles = async (outputFile: string) => { | ||
const outputFilePath = 'dist/formily.css' | ||
|
||
await build({ | ||
input: 'src/style.ts', | ||
output: { | ||
file: outputFile, | ||
}, | ||
plugins: [ | ||
typescript({ | ||
tsconfig: './tsconfig.json', | ||
tsconfigOverride: { | ||
compilerOptions: { | ||
module: 'ESNext', | ||
declaration: false, | ||
}, | ||
}, | ||
}), | ||
...getRollupBasePlugin(), | ||
], | ||
}) | ||
|
||
return generateCssStyleInject(join(process.cwd(), outputFile)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { copy, readFile, outputFile, existsSync } from 'fs-extra' | ||
|
||
import { getRollupBasePlugin, build } from './helper' | ||
|
||
export type BuildStyleOptions = { | ||
filename: string | ||
styleEntry: string | ||
importCssCompilerToCssTransform?: (fileContent: string) => string | ||
} | ||
|
||
const importCssCompilerToCss = async ({ | ||
fileName, | ||
outputFileName, | ||
styleEntry, | ||
transform, | ||
}: { | ||
outputFileName: string | ||
fileName: string | ||
styleEntry: string | ||
transform?: BuildStyleOptions['importCssCompilerToCssTransform'] | ||
}) => { | ||
let styleFileContent = (await readFile(fileName)).toString() | ||
|
||
if (!styleFileContent) { | ||
return | ||
} | ||
|
||
styleFileContent = styleFileContent.replace( | ||
new RegExp(`\.\/${styleEntry}`), | ||
'./css.css' | ||
) | ||
|
||
return outputFile( | ||
outputFileName, | ||
transform?.(styleFileContent) || styleFileContent | ||
) | ||
} | ||
|
||
const getPaths = (filename: string, moduleType: 'esm' | 'lib') => { | ||
const styleFilePath = filename | ||
.replace(/src\//, `${moduleType}/`) | ||
.replace(/\.ts$/, '.js') | ||
const cssFilePath = styleFilePath.replace(/\/\w+\.js$/, '/css.css') | ||
const cssSourceMapFilePath = `${cssFilePath}.map` | ||
|
||
return { | ||
// esm/array-base/style.js | ||
styleFilePath, | ||
// esm/array-base/css.css | ||
cssFilePath, | ||
// esm/array-base/css.css.map | ||
cssSourceMapFilePath, | ||
} | ||
} | ||
|
||
const buildCss = async ({ | ||
filename, | ||
esmPaths, | ||
libPaths, | ||
styleEntry, | ||
}: Pick<BuildStyleOptions, 'filename' | 'styleEntry'> & | ||
Record<'esmPaths' | 'libPaths', ReturnType<typeof getPaths>>) => { | ||
// src/array-base/style.ts ===> src/array-base/style.less | ||
const input = filename.replace(/style\.ts$/, styleEntry) | ||
|
||
if (!existsSync(input)) { | ||
return | ||
} | ||
|
||
await build({ | ||
input, | ||
output: { | ||
file: esmPaths.cssFilePath, | ||
}, | ||
plugins: getRollupBasePlugin(), | ||
}) | ||
|
||
return Promise.all([ | ||
copy(esmPaths.cssFilePath, libPaths.cssFilePath), | ||
existsSync(esmPaths.cssSourceMapFilePath) && | ||
copy(esmPaths.cssSourceMapFilePath, libPaths.cssSourceMapFilePath), | ||
]) | ||
} | ||
|
||
export const buildStyle = async ({ | ||
// xxxx/style.ts | ||
filename, | ||
// example: style.less/main.scss | ||
styleEntry, | ||
importCssCompilerToCssTransform, | ||
}: BuildStyleOptions): Promise<unknown> => { | ||
const esmPaths = getPaths(filename, 'esm') | ||
const libPaths = getPaths(filename, 'lib') | ||
|
||
await buildCss({ | ||
filename, | ||
esmPaths, | ||
libPaths, | ||
styleEntry, | ||
}) | ||
|
||
return Promise.all([ | ||
importCssCompilerToCss({ | ||
fileName: esmPaths.styleFilePath, | ||
outputFileName: esmPaths.cssFilePath.replace(/\.css$/, '.js'), | ||
styleEntry, | ||
transform: importCssCompilerToCssTransform, | ||
}), | ||
importCssCompilerToCss({ | ||
fileName: libPaths.styleFilePath, | ||
outputFileName: libPaths.cssFilePath.replace(/\.css$/, '.js'), | ||
styleEntry, | ||
transform: importCssCompilerToCssTransform, | ||
}), | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import { copy, readFile, writeFile, existsSync } from 'fs-extra' | ||
import glob from 'glob' | ||
|
||
export type CopyBaseOptions = Record<'esStr' | 'libStr', string> | ||
|
||
const importLibToEs = async ({ | ||
libStr, | ||
esStr, | ||
filename, | ||
}: CopyBaseOptions & { 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) | ||
) | ||
} | ||
|
||
export const runCopy = ({ | ||
resolveForItem, | ||
...lastOpts | ||
}: CopyBaseOptions & { resolveForItem?: (filename: string) => unknown }) => { | ||
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] | ||
|
||
resolveForItem?.(filename) | ||
|
||
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)) { | ||
importLibToEs({ | ||
...lastOpts, | ||
filename: filename.replace(/src\//, 'esm/').replace(/\.ts$/, '.js'), | ||
}) | ||
|
||
continue | ||
} | ||
} | ||
}) | ||
}) | ||
} |
Oops, something went wrong.