Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!packageManager) {
packageManager = await determinePackageManager(filepath);
}
let commandToUse = packageManager === 'npm' ? 'install' : 'add';
let args = [commandToUse, ...modules];
if (saveDev) {
args.push('-D');
} else if (packageManager === 'npm') {
args.push('--save');
}
// npm doesn't auto-create a package.json when installing,
// so create an empty one if needed.
if (packageManager === 'npm' && !packageLocation) {
await fs.writeFile(path.join(cwd, 'package.json'), '{}');
}
try {
await pipeSpawn(packageManager, args, {cwd});
} catch (err) {
throw new Error(`Failed to install ${modules.join(', ')}.`);
}
if (installPeers) {
await Promise.all(
modules.map(m => installPeerDependencies(filepath, m, options)),
);
}
}
async addAsset(asset) {
let contents = asset.generated[this.bundle.type];
if (!contents || (contents && contents.path)) {
contents = await fs.readFile(contents ? contents.path : asset.name);
}
// Create sub-directories if needed
if (this.bundle.name.includes(path.sep)) {
await fs.mkdirp(path.dirname(this.bundle.name));
}
this.size = contents.length;
await fs.writeFile(this.bundle.name, contents);
}
async cargoBuild(cargoConfig, cargoDir) {
// Ensure the cargo config has cdylib as the crate-type
if (!cargoConfig.lib) {
cargoConfig.lib = {};
}
if (!Array.isArray(cargoConfig.lib['crate-type'])) {
cargoConfig.lib['crate-type'] = [];
}
if (!cargoConfig.lib['crate-type'].includes('cdylib')) {
cargoConfig.lib['crate-type'].push('cdylib');
await fs.writeFile(
path.join(cargoDir, 'Cargo.toml'),
toml.stringify(cargoConfig),
);
}
// Run cargo
let args = ['+nightly', 'build', '--target', RUST_TARGET, '--release'];
await exec('cargo', args, {cwd: cargoDir});
// Get output file paths
let [stdout] = await exec('cargo', ['metadata', '--format-version', '1'], {
cwd: cargoDir,
});
const cargoMetadata = JSON.parse(stdout);
const cargoTargetDir = cargoMetadata.target_directory;
let outDir = path.join(cargoTargetDir, RUST_TARGET, 'release');
export async function bundle(assets, options) {
// if (fsNative.data.src) delete fsNative.data.src;
// if (fsNative.data.dist) delete fsNative.data.dist;
fsNative.data = {};
const startTime = performance.now();
process.env = {};
await fs.mkdirp('/src/');
if (options.browserslist && !hasBrowserslist(assets)) {
await fs.writeFile(`/src/.browserslistrc`, options.browserslist);
}
for (let f of assets) {
const p = `/src/${f.name}`;
await fs.mkdirp(path.dirname(p));
await fs.writeFile(p, f.content || ' ');
}
const entryPoints = assets.filter(v => v.isEntry).map(v => `/src/${v.name}`);
if (!entryPoints.length) throw new Error('No asset marked as entrypoint');
let entryPointsOutput;
try {
const bundler = new Bundler(entryPoints, {
outDir: '/dist',
// if (fsNative.data.src) delete fsNative.data.src;
// if (fsNative.data.dist) delete fsNative.data.dist;
fsNative.data = {};
const startTime = performance.now();
process.env = {};
await fs.mkdirp('/src/');
if (options.browserslist && !hasBrowserslist(assets)) {
await fs.writeFile(`/src/.browserslistrc`, options.browserslist);
}
for (let f of assets) {
const p = `/src/${f.name}`;
await fs.mkdirp(path.dirname(p));
await fs.writeFile(p, f.content || ' ');
}
const entryPoints = assets.filter(v => v.isEntry).map(v => `/src/${v.name}`);
if (!entryPoints.length) throw new Error('No asset marked as entrypoint');
let entryPointsOutput;
try {
const bundler = new Bundler(entryPoints, {
outDir: '/dist',
autoinstall: false,
watch: false,
cache: false,
hmr: false,
logLevel: 0,
minify: options.minify,
async write(filename, data) {
try {
await this.ensureDirExists();
await this.writeDepMtimes(data);
await fs.writeFile(this.getCacheFile(filename), JSON.stringify(data));
this.invalidated.delete(filename);
} catch (err) {
logger.error(`Error writing to cache: ${err.message}`);
}
}
async write(filename, data) {
try {
await this.ensureDirExists();
await this.writeDepMtimes(data);
await fs.writeFile(this.getCacheFile(filename), JSON.stringify(data));
this.invalidated.delete(filename);
} catch (err) {
logger.error(`Error writing to cache: ${err.message}`);
}
}