UNPKG

@nx/vite

Version:

The Nx Plugin for building and testing applications using Vite

67 lines (66 loc) 3.24 kB
"use strict"; Object.defineProperty(exports, "addFileReplacements", { enumerable: true, get: function() { return addFileReplacements; } }); const _devkit = require("@nx/devkit"); const _tsquery = require("@phenomnomnominal/tsquery"); const _updateviteconfig = require("../update-vite-config"); function addFileReplacements(configContents, fileReplacements, configPath) { var _tsquery_query, _tsquery_query1, _tsquery_query2; const configNode = (0, _updateviteconfig.getConfigNode)(configContents); if (!configNode) { (0, _updateviteconfig.notFoundWarning)(configPath); return configContents; } const pluginsObject = (_tsquery_query = _tsquery.tsquery.query(configNode, `PropertyAssignment:has(Identifier[name="plugins"])`)) == null ? void 0 : _tsquery_query[0]; const replaceFilesPlugin = (_tsquery_query1 = _tsquery.tsquery.query(configNode, `PropertyAssignment:has(Identifier[name="plugins"]) CallExpression:has(Identifier[name="replaceFiles"])`)) == null ? void 0 : _tsquery_query1[0]; const firstImportDeclaration = (_tsquery_query2 = _tsquery.tsquery.query(configContents, 'ImportDeclaration')) == null ? void 0 : _tsquery_query2[0]; if (pluginsObject) { if (replaceFilesPlugin) { return configContents; } else { return (0, _devkit.applyChangesToString)(configContents, [ { type: _devkit.ChangeType.Insert, index: pluginsObject.getStart() + `plugins: [`.length + 1, text: `replaceFiles(${JSON.stringify(fileReplacements)}),` }, firstImportDeclaration ? { type: _devkit.ChangeType.Insert, index: firstImportDeclaration.getStart(), text: `import replaceFiles from '@nx/vite/plugins/rollup-replace-files.plugin';\n` } : { type: _devkit.ChangeType.Insert, index: 0, text: `import replaceFiles from '@nx/vite/plugins/rollup-replace-files.plugin';\n` } ]); } } else { var _tsquery_query3; const foundDefineConfig = (_tsquery_query3 = _tsquery.tsquery.query(configContents, 'CallExpression:has(Identifier[name="defineConfig"])')) == null ? void 0 : _tsquery_query3[0]; if (!foundDefineConfig) { return; } return (0, _devkit.applyChangesToString)(configContents, [ { type: _devkit.ChangeType.Insert, index: configNode.getStart() + 1, text: `plugins: [replaceFiles(${JSON.stringify(fileReplacements)})],` }, firstImportDeclaration ? { type: _devkit.ChangeType.Insert, index: firstImportDeclaration.getStart(), text: `import replaceFiles from '@nx/vite/plugins/rollup-replace-files.plugin';` } : { type: _devkit.ChangeType.Insert, index: 0, text: `import replaceFiles from '@nx/vite/plugins/rollup-replace-files.plugin';` } ]); } } //# sourceMappingURL=add-file-replacements.js.map