Skip to content

Commit

Permalink
refactor: optimize code for next version
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Jan 29, 2024
1 parent c91de3a commit c403e94
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 55 deletions.
File renamed without changes.
56 changes: 56 additions & 0 deletions src/Common/VMScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const vm = require('vm');
const { transformToCommonJS } = require('./Utils');
const { executeFunctionException } = require('../Plugin/Messages/Exception');

class VMScript {
context = null;

/**
*
* @param {Object} context The context object.
* @param {string} name The function name compiled in the VM context.
*/
constructor(context, name = '') {
this.context = vm.createContext(context);
this.name = name;
}

/**
* Execute the source code.
*
* @param {Buffer | string} code The source code.
* @param {string} filename The filename is used in stack traces produced by this script.
* @param {{}?} data The data passed in the compiled function.
* @param {boolean?} esModule Whether the source code is an ES module.
* @return {string}
*/
exec(code, { filename, data = {}, esModule = false }) {
if (Buffer.isBuffer(code)) {
code = code.toString();
}

// transform the code to CommonJS
if (esModule === true) {
code = transformToCommonJS(code);
}

try {
const script = new vm.Script(code, { filename });
let result = script.runInContext(this.context);

// if the code returns nothing but creates a named definition in the context
if (!result && this.name && this.name in this.context) {
result = this.context[this.name];
//console.log('*** VM EXEC 1: ', { code, result });
} else {
//console.log('*** VM EXEC 2: ', { code, result });
}

return typeof result === 'function' ? result(data) : result || '';
} catch (error) {
executeFunctionException(error, filename);
}
}
}

module.exports = VMScript;
3 changes: 2 additions & 1 deletion src/Loader/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class Option {
const queryData = parseQuery(resourceQuery);
let options = PluginService.getLoaderCache(loaderId);

this.fileSystem = loaderContext.fs.fileSystem;
this.#pluginOption = PluginService.getOptions();
this.#watch = PluginService.isWatchMode();
this.fileSystem = loaderContext.fs.fileSystem;
this.#webpackOptions = loaderContext._compiler.options || {};
this.#rootContext = rootContext;
this.#resourcePath = resourcePath;
Expand All @@ -59,6 +59,7 @@ class Option {

PluginService.setLoaderCache(loaderId, options);
}

this.#options = options;

// if the data option is a string, it must be an absolute or relative filename of an existing file that exports the data
Expand Down
1 change: 1 addition & 0 deletions src/Loader/Preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Preprocessor {
handlebars: 'Handlebars',
nunjucks: 'Nunjucks',
twig: 'Twig',
pug: 'Pug',
};

// disabled preprocessor
Expand Down
1 change: 0 additions & 1 deletion src/Loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const {
const loader = function (content, map, meta) {
/** @type {BundlerPluginLoaderContext} */
const loaderContext = this;

const loaderCallback = loaderContext.async();
const { rootContext, resource, resourcePath, entryId } = loaderContext;
const hooks = PluginService.getHooks();
Expand Down
10 changes: 4 additions & 6 deletions src/Plugin/AssetCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const AssetEntry = require('./AssetEntry');
const AssetResource = require('./AssetResource');
const AssetInline = require('./AssetInline');
const AssetTrash = require('./AssetTrash');
const VMScript = require('./VMScript');
const VMScript = require('../Common/VMScript');
const Integrity = require('./Extras/Integrity');

const { compilationName, verbose } = require('./Messages/Info');
Expand Down Expand Up @@ -788,10 +788,8 @@ class AssetCompiler {

const entry = AssetEntry.getByChunk(chunk);

if (!entry) return;

// process only entries supported by this plugin
if (!entry.isTemplate && !entry.isStyle) return;
if (!entry || (!entry.isTemplate && !entry.isStyle)) return;

Collection.addEntry(entry);

Expand Down Expand Up @@ -1198,7 +1196,7 @@ class AssetCompiler {
* Render the module source code generated by a loader.
*
* @param {string} type The type of module, one of the values: template, style.
* @param {Object} source The Webpack source.
* @param {RawSource} source The Webpack source.
* @param {string} resource The full path of source file, including a query.
* @param {string} sourceFile The full path of source file w/o a query.
* @param {string} assetFile
Expand All @@ -1223,7 +1221,7 @@ class AssetCompiler {
// the css-loader defaults generate ESM code, which must be transformed into CommonJS to compile the code
// the template loader generates CommonJS code, no need to transform
const esModule = type === 'style';
let result = vmScript.compile(source, sourceFile, esModule);
let result = vmScript.exec(source.source(), { filename: sourceFile, esModule });

if (type === 'style') {
result = CssExtractModule.apply(result);
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/PluginService.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class PluginService {
/**
* Returns plugin options instance.
*
* TODO: rename to getOptionInstance()
* TODO: rename to getPluginOptionInstance()
*
* @return {OptionPluginInterface}
*/
Expand Down
46 changes: 0 additions & 46 deletions src/Plugin/VMScript.js

This file was deleted.

0 comments on commit c403e94

Please sign in to comment.