-
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: optimize UMD build and remove CJS build steps (#3895)
- Loading branch information
Showing
75 changed files
with
3,113 additions
and
3,363 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 |
---|---|---|
|
@@ -14,118 +14,117 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const process = require('node:process'); | ||
const esbuild = require('esbuild'); | ||
const fs = require('fs-extra'); | ||
|
||
exports.buildPkg = function buildPkg() { | ||
return { | ||
name: 'build-pkg', | ||
enforce: 'pre', | ||
generateBundle() { | ||
const srcDir = path.resolve(process.cwd(), 'src/locale'); | ||
const outputDir = path.resolve(process.cwd(), 'lib/locale'); | ||
apply: 'build', | ||
enforce: 'post', | ||
buildEnd() { | ||
const pkgPath = path.resolve(process.cwd(), 'package.json'); | ||
|
||
if (!fs.existsSync(pkgPath)) { | ||
return; | ||
} | ||
|
||
const pkg = require(pkgPath); | ||
const pkg = fs.readJSONSync(pkgPath); | ||
|
||
// author | ||
pkg.author = 'DreamNum <[email protected]>'; | ||
// pkg.license = 'Apache-2.0'; | ||
|
||
// license | ||
if (pkg.name.startsWith('@univer/')) { | ||
pkg.license = 'Apache-2.0'; | ||
} | ||
|
||
// funding | ||
pkg.funding = { | ||
type: 'opencollective', | ||
url: 'https://opencollective.com/univer', | ||
}; | ||
|
||
// homepage | ||
pkg.homepage = 'https://univer.ai'; | ||
|
||
// repository | ||
pkg.repository = { | ||
type: 'git', | ||
url: 'https://github.com/dream-num/univer', | ||
}; | ||
|
||
// bugs | ||
pkg.bugs = { | ||
url: 'https://github.com/dream-num/univer/issues', | ||
}; | ||
|
||
// keywords | ||
pkg.keywords = pkg.keywords || [ | ||
'univer', | ||
]; | ||
|
||
// exports | ||
pkg.exports = Object.assign({ | ||
'.': './src/index.ts', | ||
'./*': './src/*', | ||
}, pkg.exports); | ||
pkg.main = './lib/cjs/index.js'; | ||
pkg.module = './lib/es/index.js'; | ||
pkg.types = './lib/types/index.d.ts'; | ||
|
||
const facadeEntry = path.resolve(process.cwd(), 'src/facade/index.ts'); | ||
if (fs.existsSync(facadeEntry)) { | ||
pkg.publishConfig.exports['./facade'] = { | ||
import: './lib/es/facade.js', | ||
types: './lib/types/facade/index.d.ts', | ||
}; | ||
} | ||
|
||
// main | ||
pkg.main = './src/index.ts'; | ||
|
||
// publishConfig | ||
pkg.publishConfig = { | ||
access: 'public', | ||
main: './lib/cjs/index.js', | ||
main: './lib/es/index.js', | ||
module: './lib/es/index.js', | ||
exports: { | ||
'.': { | ||
import: './lib/es/index.js', | ||
require: './lib/cjs/index.js', | ||
types: './lib/types/index.d.ts', | ||
}, | ||
'./*': { | ||
import: './lib/es/*', | ||
require: './lib/cjs/*', | ||
types: './lib/types/index.d.ts', | ||
}, | ||
'./lib/*': './lib/*', | ||
}, | ||
}; | ||
|
||
// locale | ||
const localeDir = path.resolve(process.cwd(), 'src/locale'); | ||
if (fs.existsSync(localeDir)) { | ||
pkg.exports['./locale/*'] = './src/locale/*.ts'; | ||
pkg.publishConfig.exports['./locale/*'] = { | ||
import: './lib/locale/*.js', | ||
types: './lib/types/locale/*.d.ts', | ||
}; | ||
} | ||
|
||
// directories | ||
pkg.directories = { | ||
lib: 'lib', | ||
}; | ||
// files | ||
pkg.files = [ | ||
'lib', | ||
]; | ||
|
||
if (fs.existsSync(srcDir)) { | ||
fs.readdirSync(srcDir) | ||
.filter((file) => file.includes('-') && fs.statSync(path.resolve(srcDir, file)).isFile()) | ||
.forEach((file) => { | ||
const fullPath = path.join(srcDir, file); | ||
|
||
const syncResult = esbuild.buildSync({ | ||
entryPoints: [fullPath], | ||
bundle: true, | ||
platform: 'node', | ||
format: 'cjs', | ||
write: false, | ||
}); | ||
|
||
// eslint-disable-next-line no-new-func | ||
const module = new Function('module', `${syncResult.outputFiles[0].text};return module;`)({}); | ||
const result = module.exports.default; | ||
const data = JSON.stringify(result, null, 2); | ||
|
||
if (!fs.existsSync(outputDir)) { | ||
fs.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
|
||
const outputPath = path.resolve(outputDir, file.replace('.ts', '.json')); | ||
|
||
fs.writeFileSync(outputPath, data); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(`[vite:build-pkg] ${outputPath} generated`); | ||
|
||
// exports | ||
pkg.exports['./locale/*'] = './src/locale/*.ts'; | ||
pkg.publishConfig.exports['./locale/*'] = './lib/locale/*.json'; | ||
}); | ||
} | ||
|
||
pkg.univerSpace = pkg.publishConfig.exports; | ||
|
||
fs.writeFileSync( | ||
fs.writeJSONSync( | ||
`${process.cwd()}/package.json`, | ||
`${JSON.stringify(pkg, null, 4)}\n` | ||
pkg, | ||
{ spaces: 4, EOL: '\n' } | ||
); | ||
}, | ||
}; | ||
|
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,126 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const path = require('node:path'); | ||
const process = require('node:process'); | ||
const react = require('@vitejs/plugin-react'); | ||
const vue = require('@vitejs/plugin-vue'); | ||
const fs = require('fs-extra'); | ||
const { build, mergeConfig } = require('vite'); | ||
const vitePluginExternal = require('vite-plugin-external'); | ||
|
||
const { autoExternalizeDependency } = require('./auto-externalize-dependency-plugin'); | ||
const { obfuscator } = require('./obfuscator'); | ||
const { convertLibNameFromPackageName } = require('./utils'); | ||
|
||
exports.buildUMD = function buildUMD() { | ||
return { | ||
name: 'build-umd', | ||
apply: 'build', | ||
enforce: 'post', | ||
async buildEnd() { | ||
const pkgPath = path.resolve(process.cwd(), 'package.json'); | ||
|
||
if (!fs.existsSync(pkgPath)) { | ||
return; | ||
} | ||
|
||
const pkg = fs.readJSONSync(pkgPath); | ||
|
||
const basicConfig = { | ||
configFile: false, | ||
build: { | ||
emptyOutDir: false, | ||
target: 'chrome70', | ||
lib: { | ||
formats: ['umd'], | ||
}, | ||
}, | ||
plugins: [ | ||
autoExternalizeDependency(), | ||
vitePluginExternal({ | ||
nodeBuiltins: true, | ||
}), | ||
react(), | ||
vue(), | ||
], | ||
define: { | ||
'process.env.NODE_ENV': JSON.stringify('production'), | ||
'process.env.BUILD_TIMESTAMP': JSON.stringify(Number.parseInt(Date.now() / 1000)), | ||
}, | ||
css: { | ||
modules: { | ||
localsConvention: 'camelCaseOnly', | ||
generateScopedName: 'univer-[local]', | ||
}, | ||
}, | ||
}; | ||
|
||
if (!pkg.name.startsWith('@univerjs/')) { | ||
basicConfig.plugins.push(obfuscator()); | ||
} | ||
|
||
await build(mergeConfig({ | ||
build: { | ||
outDir: 'lib', | ||
lib: { | ||
entry: path.resolve(process.cwd(), 'src/index.ts'), | ||
name: convertLibNameFromPackageName(pkg.name), | ||
fileName: () => 'umd/index.js', | ||
}, | ||
}, | ||
}, basicConfig)); | ||
|
||
const facadeEntry = path.resolve(process.cwd(), 'src/facade/index.ts'); | ||
if (fs.existsSync(facadeEntry)) { | ||
await build(mergeConfig({ | ||
build: { | ||
outDir: 'lib', | ||
lib: { | ||
entry: path.resolve(process.cwd(), 'src/facade/index.ts'), | ||
name: convertLibNameFromPackageName(`${pkg.name}Facade`), | ||
fileName: () => 'umd/facade.js', | ||
}, | ||
}, | ||
}, basicConfig)); | ||
} | ||
|
||
const localeDir = path.resolve(process.cwd(), 'src/locale'); | ||
if (fs.existsSync(localeDir)) { | ||
const locales = fs.readdirSync(localeDir); | ||
|
||
for (const file of locales) { | ||
if (fs.statSync(path.resolve(localeDir, file)).isDirectory()) { | ||
continue; | ||
} | ||
|
||
const localeValue = file.replace('.ts', ''); | ||
|
||
await build(mergeConfig({ | ||
build: { | ||
outDir: 'lib/umd', | ||
lib: { | ||
entry: path.resolve(process.cwd(), path.resolve(localeDir, file)), | ||
name: convertLibNameFromPackageName(`${pkg.name}-${localeValue}`), | ||
fileName: () => `locale/${localeValue}.js`, | ||
}, | ||
}, | ||
}, basicConfig)); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.