Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: optimize UMD build and remove CJS build steps #3895

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading