@ima/cli
Version:
IMA.js CLI tool to build, develop and work with IMA.js applications.
53 lines (52 loc) • 2.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const generator_1 = __importDefault(require("@babel/generator"));
const parser_1 = require("@babel/parser");
const serverControllerProcessor_1 = require("./processors/serverControllerProcessor");
const stubProcessor_1 = require("./processors/stubProcessor");
/**
* The default processors that are run on the AST.
* IMPORTANT: the order of the processors is important.
*/
const defaultProcessors = [
stubProcessor_1.stubProcessor,
serverControllerProcessor_1.serverControllerProcessor,
];
/**
* This generic webpack loader is triggered by the 'use server' directive.
* It parses a file into an AST and then runs a series of processors against it,
* which can modify the AST. Finally, it generates code from the transformed AST.
*
* @param {string} source The source code of the module.
* @returns {string} The transformed source code.
*/
const UseServerLoader = function (source, inputMap) {
const options = this.getOptions();
const done = this.async();
// Only run on the client and if 'use server' is present
if (options.environment !== 'client' ||
(!source.trimStart().startsWith(`"use server"`) &&
!source.trimStart().startsWith(`'use server'`))) {
return done(null, source, inputMap);
}
// Parse source into AST
let ast = (0, parser_1.parse)(source, {
sourceType: 'module',
plugins: ['typescript'],
sourceFilename: this.resourcePath,
});
// Run AST through all configured processors one by one
for (const processor of defaultProcessors) {
ast = processor(ast);
}
// Generate code from the final AST
const output = (0, generator_1.default)(ast, {
sourceMaps: !!this.sourceMap,
sourceFileName: this.resourcePath,
});
done(null, output.code, output.map || undefined);
};
exports.default = UseServerLoader;