Skip to content

Commit

Permalink
build: optimize UMD build and remove CJS build steps
Browse files Browse the repository at this point in the history
  • Loading branch information
jikkai committed Oct 30, 2024
1 parent f007660 commit 89823b2
Show file tree
Hide file tree
Showing 75 changed files with 3,113 additions and 3,363 deletions.
13 changes: 7 additions & 6 deletions common/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
"./vite": "./vite/index.js"
},
"dependencies": {
"@typescript-eslint/parser": "^8.8.1",
"@vitejs/plugin-react": "^4.3.2",
"@vitest/coverage-istanbul": "^2.1.2",
"@typescript-eslint/parser": "^8.12.2",
"@vitejs/plugin-react": "^4.3.3",
"@vitest/coverage-istanbul": "^2.1.4",
"fs-extra": "^11.2.0",
"happy-dom": "15.0.0",
"javascript-obfuscator": "^4.1.1",
"vite": "^5.4.8",
"vite-plugin-dts": "^4.2.3",
"vite": "^5.4.10",
"vite-plugin-dts": "^4.3.0",
"vite-plugin-external": "^4.3.1",
"vitest": "^2.1.2"
"vitest": "^2.1.4"
}
}
103 changes: 51 additions & 52 deletions common/shared/vite/build-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
);
},
};
Expand Down
126 changes: 126 additions & 0 deletions common/shared/vite/build-umd.js
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));
}
}
},
};
};
Loading

0 comments on commit 89823b2

Please sign in to comment.