Skip to content

Commit

Permalink
fix: migrate build from rollup to vite
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Golovin <[email protected]>
  • Loading branch information
dgolovin committed Jun 6, 2024
1 parent 4b322d6 commit 000bf0a
Show file tree
Hide file tree
Showing 5 changed files with 732 additions and 566 deletions.
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": {
"@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

0 comments on commit 000bf0a

Please sign in to comment.