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

fix: migrate build from rollup to vite #225

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .extfiles
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package.json
LICENSE
icon.png
README.md
dist/**
!dist/yarn.lock
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"engines": {
"podman-desktop": "^0.16.0"
},
"main": "./dist/extension.js",
"main": "./dist/extension.cjs",
"source": "./src/extension.ts",
"contributes": {
"configuration": {
Expand Down Expand Up @@ -58,8 +58,8 @@
}
},
"scripts": {
"build": "rollup --bundleConfigAsCjs --config rollup.config.js --compact --environment BUILD:production",
"watch": "rollup --bundleConfigAsCjs --config rollup.config.js -w",
"build": "vite build && node scripts/build.cjs",
"watch": "vite build -w",
"lint:check": "eslint . --ext js,ts",
"lint:fix": "eslint . --fix --ext js,ts",
"format:check": "prettier --check src/**",
Expand All @@ -69,28 +69,27 @@
"desk:run": "ts-node-esm ./scripts/run.mts run",
"test": "vitest run --coverage --passWithNoTests"
},
"dependencies": {},
"dependencies": {
dgolovin marked this conversation as resolved.
Show resolved Hide resolved
"@podman-desktop/api": "next"
},
"devDependencies": {
"@podman-desktop/api": "next",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^11.0.0",
"@types/node": "^18.14.6",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"@vitest/coverage-v8": "^1.6.0",
"7zip-min": "^1.4.4",
"byline": "^5.0.0",
"compare-versions": "^6.0.0-rc.1",
"copyfiles": "^2.4.1",
"eslint": "^8.36.0",
"got": "^12.5.3",
"hasha": "^5.2.2",
"mkdirp": "^2.1.3",
"prettier": "^2.8.4",
"rollup": "^3.18.0",
"ts-node": "^10.9.1",
"tslib": "^2.5.0",
"typescript": "^4.9.5",
"vite": "^5.2.12",
"vitest": "^1.6.0",
"which": "^3.0.0",
"zip-local": "^0.3.5"
Expand Down
74 changes: 74 additions & 0 deletions scripts/build.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env node
/**********************************************************************
* Copyright (C) 2022 - 2024 Red Hat, 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.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

const zipper = require('zip-local');
const path = require('path');
const packageJson = require('../package.json');
const { mkdirp } = require('mkdirp');
const fs = require('fs');
const byline = require('byline');
const cp = require('copyfiles');
const cproc = require('node:child_process');

const destFile = path.resolve(__dirname, `../${packageJson.name}.cdix`);
const builtinDirectory = path.resolve(__dirname, '../builtin');
const zipDirectory = path.resolve(builtinDirectory, `${packageJson.name}.cdix`);
const extFiles = path.resolve(__dirname, '../.extfiles');
const fileStream = fs.createReadStream(extFiles, { encoding: 'utf8' });

const includedFiles = [];
const excludedFiles = [];

// remove the .cdix file before zipping
if (fs.existsSync(destFile)) {
fs.rmSync(destFile);
}
// remove the builtin folder before zipping
if (fs.existsSync(builtinDirectory)) {
fs.rmSync(builtinDirectory, { recursive: true, force: true });
}

// install external modules into dist folder
cproc.exec('yarn add hasha@^5.2.2 --cwd .', { cwd: './dist' }, (error, stdout, stderr) => {
if (error) {
console.log(stdout);
console.log(stderr);
throw error;
}

byline(fileStream)
.on('data', line => {
line.startsWith('!') ? excludedFiles.push(line.substring(1)) : includedFiles.push(line);
})
.on('error', () => {
throw new Error('Error reading .extfiles');
})
.on('end', () => {
includedFiles.push(zipDirectory); // add destination dir
mkdirp.sync(zipDirectory);
console.log(`Copying files to ${zipDirectory}`);
cp(includedFiles, { exclude: excludedFiles }, error => {
if (error) {
throw new Error('Error copying files', error);
}
console.log(`Zipping files to ${destFile}`);
zipper.sync.zip(zipDirectory).compress().save(destFile);
});
});
});
64 changes: 64 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**********************************************************************
* Copyright (C) 2023 Red Hat, 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.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { join } from 'path';
import { builtinModules } from 'module';

const PACKAGE_ROOT = __dirname;

/**
* @type {import('vite').UserConfig}
* @see https://vitejs.dev/config/
*/
const config = {
mode: process.env.MODE,
root: PACKAGE_ROOT,
envDir: process.cwd(),
resolve: {
alias: {
'/@/': join(PACKAGE_ROOT, 'src') + '/',
},
},
optimizeDeps: {},
build: {
sourcemap: 'inline',
target: 'esnext',
outDir: 'dist',
assetsDir: '.',
minify: process.env.MODE === 'production' ? 'esbuild' : false,
lib: {
entry: 'src/extension.ts',
formats: ['cjs'],
},
rollupOptions: {
external: [
'@podman-desktop/api',
'hasha',
...builtinModules.flatMap(p => [p, `node:${p}`]),
],
output: {
entryFileNames: '[name].cjs',
intro: 'const window = {}',
},
},
emptyOutDir: true,
reportCompressedSize: false,
},
};

export default config;
Loading
Loading