Skip to content

Commit

Permalink
feat: executable-file
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Feb 16, 2021
1 parent 48d2c95 commit a855940
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,48 @@ function processResult(loaderContext, result) {
export default function loader(content) {
const options = this.getOptions(schema);
const { executableFile } = options;

let func;
const callback = this.async();

let exports;

if (executableFile) {
// eslint-disable-next-line global-require,import/no-dynamic-require
func = require(executableFile);
try {
// eslint-disable-next-line global-require,import/no-dynamic-require
exports = require(executableFile);
} catch (error) {
callback(new Error(`Unable to require "${executableFile}": ${error}`));
return;
}
} else {
try {
exports = exec(content, this);
} catch (error) {
throw new Error(`Unable to execute "${this.resource}": ${error}`);
callback(new Error(`Unable to execute "${this.resource}": ${error}`));
return;
}

func = exports && exports.default ? exports.default : exports;
}

const func = exports && exports.default ? exports.default : exports;

if (typeof func !== "function") {
throw new Error(
`Module "${this.resource}" does not export a function as default`
callback(
new Error(
`Module "${this.resource}" does not export a function as default`
)
);
return;
}

let result;

try {
result = func(options, this, content);
} catch (error) {
throw new Error(`Module "${this.resource}" throw error: ${error}`);
callback(new Error(`Module "${this.resource}" throw error: ${error}`));
return;
}

if (result && typeof result.then === "function") {
const callback = this.async();

result
.then((res) => processResult(this, res))
.catch((error) => {
Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/executableFile.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`executableFile option should emit error: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Error: Unable to require \\"unresolved-file.js\\": Error: Cannot find module 'unresolved-file.js' from 'src/index.js'",
"ChunkRenderError: Cannot read property 'get' of undefined",
]
`;

exports[`executableFile option should emit error: warnings 1`] = `Array []`;

exports[`executableFile option should work: errors 1`] = `Array []`;

exports[`executableFile option should work: result 1`] = `
Expand Down
37 changes: 37 additions & 0 deletions test/executableFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,41 @@ describe("executableFile option", () => {
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot("errors");
});

it("should emit error", async () => {
const compiler = getCompiler(
"executableFileEntry.js",
{},
{
module: {
rules: [
{
test: /\.(json)$/i,
rules: [
{
loader: require.resolve("./helpers/helperLoader.js"),
},
{
loader: require.resolve("../src"),
options: {
executableFile: "unresolved-file.js",
},
},
],
},
{
test: /\.json$/i,
type: "asset/resource",
},
],
},
}
);
const stats = await compile(compiler);

expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
"warnings"
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot("errors");
});
});

0 comments on commit a855940

Please sign in to comment.