Skip to content

Commit

Permalink
feat: added the buildDependensies option (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Feb 15, 2021
1 parent 10807db commit 04be3eb
Show file tree
Hide file tree
Showing 10 changed files with 16,012 additions and 237 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ next loader uses the same AST.
Type: `Array[String]`
Default: `[]`

An array of absolute, native paths to file dependencies that should be watched
by webpack for changes.
An array of absolute, native paths to file dependencies that should be watched by webpack for changes.

Dependencies can also be added using [`loaderContext.addDependency(file: string)`](https://webpack.js.org/api/loaders/#thisadddependency).

Expand All @@ -116,11 +115,19 @@ Dependencies can also be added using [`loaderContext.addDependency(file: string)
Type: `Array[String]`
Default: `[]`

An array of absolute, native paths to directory dependencies that should be
watched by webpack for changes.
An array of absolute, native paths to directory dependencies that should be watched by webpack for changes.

Context dependencies can also be added using [`loaderContext.addContextDependency(directory: string)`](https://webpack.js.org/api/loaders/#thisaddcontextdependency).

### `buildDependencies`

Type: `Array[String]`
Default: `[]`

An array of absolute, native paths to directory dependencies that should be watched by webpack for changes.

Build dependencies can also be added using `loaderContext.addBuildDependency(file: string)`.

### `cacheable`

Type: `Boolean`
Expand Down
16,143 changes: 15,938 additions & 205 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"standard-version": "^9.0.0",
"webpack": "^5.11.0"
"webpack": "^5.22.0"
},
"keywords": [
"webpack",
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function processResult(loaderContext, result) {
loaderContext.addContextDependency(dep)
);

(result.buildDependencies || []).forEach((dep) =>
loaderContext.addBuildDependency(dep)
);

// Defaults to false which is a good default here because we assume that
// results tend to be not cacheable when this loader is necessary
loaderContext.cacheable(Boolean(result.cacheable));
Expand Down
63 changes: 43 additions & 20 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loader should allow adding dependencies and contextDependencies via loader context: errors 1`] = `Array []`;
exports[`loader should allow adding dependencies, contextDependencies and buildDependencies via loader context: errors 1`] = `Array []`;

exports[`loader should allow adding dependencies and contextDependencies via loader context: result 1`] = `
exports[`loader should allow adding dependencies, contextDependencies and buildDependencies via loader context: result 1`] = `
"{
\\"content\\": \\"module.exports = \\\\\\"hello world\\\\\\";\\",
\\"map\\": null,
Expand All @@ -14,11 +14,16 @@ exports[`loader should allow adding dependencies and contextDependencies via loa
],
\\"contextDependencies\\": [
\\"test/fixtures\\"
],
\\"buildDependencies\\": [
\\"test/fixtures/args.js\\",
\\"test/fixtures/simple.js\\",
\\"test/fixtures\\"
]
}"
`;

exports[`loader should allow adding dependencies and contextDependencies via loader context: warnings 1`] = `Array []`;
exports[`loader should allow adding dependencies, contextDependencies and buildDependencies via loader context: warnings 1`] = `Array []`;

exports[`loader should call the function with the loader options: errors 1`] = `Array []`;

Expand All @@ -38,7 +43,8 @@ exports[`loader should call the function with the loader options: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/args.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -54,7 +60,8 @@ exports[`loader should flag the module as cacheable if requested: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/cacheable.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -74,15 +81,16 @@ exports[`loader should flag the module as not cacheable by default: result 1`] =
\\"dependencies\\": [
\\"test/fixtures/simple.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

exports[`loader should flag the module as not cacheable by default: warnings 1`] = `Array []`;

exports[`loader should handle dependencies and contextDependencies of the module: errors 1`] = `Array []`;
exports[`loader should handle dependencies, contextDependencies and buildDependencies of the module: errors 1`] = `Array []`;

exports[`loader should handle dependencies and contextDependencies of the module: result 1`] = `
exports[`loader should handle dependencies, contextDependencies and buildDependencies of the module: result 1`] = `
"{
\\"content\\": \\"module.exports = \\\\\\"hello world\\\\\\";\\",
\\"map\\": null,
Expand All @@ -94,11 +102,15 @@ exports[`loader should handle dependencies and contextDependencies of the module
],
\\"contextDependencies\\": [
\\"test/fixtures\\"
],
\\"buildDependencies\\": [
\\"test/fixtures/args.js\\",
\\"test/fixtures/simple.js\\"
]
}"
`;

exports[`loader should handle dependencies and contextDependencies of the module: warnings 1`] = `Array []`;
exports[`loader should handle dependencies, contextDependencies and buildDependencies of the module: warnings 1`] = `Array []`;

exports[`loader should has module.parent: errors 1`] = `Array []`;

Expand All @@ -115,7 +127,8 @@ exports[`loader should has module.parent: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/module-parent.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -140,7 +153,8 @@ exports[`loader should keep dependencies if errors are emitted: result 1`] = `
],
\\"contextDependencies\\": [
\\"test/fixtures\\"
]
],
\\"buildDependencies\\": []
}"
`;

Expand Down Expand Up @@ -178,7 +192,8 @@ exports[`loader should pass on the code from the buffer fixture: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/buffer.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -194,7 +209,8 @@ exports[`loader should pass on the code from the presets/figlet fixture: result
\\"dependencies\\": [
\\"test/fixtures/presets/figlet.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -210,7 +226,8 @@ exports[`loader should pass on the code from the presets/modernizr fixture: resu
\\"dependencies\\": [
\\"test/fixtures/presets/modernizr.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -230,7 +247,8 @@ exports[`loader should pass on the code from the simple fixture without options:
\\"dependencies\\": [
\\"test/fixtures/simple.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -250,7 +268,8 @@ exports[`loader should pass on the code from the simple fixture: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/simple.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -266,7 +285,8 @@ exports[`loader should recognize modules produced by babel: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/babel.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand Down Expand Up @@ -340,7 +360,8 @@ exports[`loader should work the same if a promise is returned #2: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/promise-compex.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -360,7 +381,8 @@ exports[`loader should work the same if a promise is returned: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/promise.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand All @@ -380,7 +402,8 @@ exports[`loader should work with async function: result 1`] = `
\\"dependencies\\": [
\\"test/fixtures/async-function.js\\"
],
\\"contextDependencies\\": []
\\"contextDependencies\\": [],
\\"buildDependencies\\": []
}"
`;

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/dependencies-via-context.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
function dependencies(options, loaderContext) {
loaderContext.addDependency(require.resolve('./args.js'));
loaderContext.addDependency(require.resolve('./simple.js'));
loaderContext.addBuildDependency(require.resolve('./args.js'));
loaderContext.addBuildDependency(require.resolve('./simple.js'));
loaderContext.addBuildDependency(__dirname);
loaderContext.addContextDependency(__dirname);

return {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ function dependencies() {
require.resolve('./args.js'),
require.resolve('./simple.js'),
],
buildDependencies: [
require.resolve('./args.js'),
require.resolve('./simple.js'),
],
contextDependencies: [__dirname],
code: 'module.exports = "hello world";',
};
Expand Down
6 changes: 1 addition & 5 deletions test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
const compiler = webpack(fullConfig);

if (!config.outputFileSystem) {
const outputFileSystem = createFsFromVolume(new Volume());
// Todo remove when we drop webpack@4 support
outputFileSystem.join = path.join.bind(path);

compiler.outputFileSystem = outputFileSystem;
compiler.outputFileSystem = createFsFromVolume(new Volume());
}

return compiler;
Expand Down
5 changes: 5 additions & 0 deletions test/helpers/helperLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ export default function helperLoader(content, map, meta) {
const contextDependencies = this.getContextDependencies().map((dependency) =>
rel(dependency).replace(/\\/g, "/")
);

const buildDependencies = Array.from(
this._module.buildInfo.buildDependencies || []
).map((dependency) => rel(dependency).replace(/\\/g, "/"));
const json = JSON.stringify(
{
content,
map,
meta,
dependencies,
contextDependencies,
buildDependencies,
},
null,
" "
Expand Down
4 changes: 2 additions & 2 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe("loader", () => {
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot("errors");
});

it("should handle dependencies and contextDependencies of the module", async () => {
it("should handle dependencies, contextDependencies and buildDependencies of the module", async () => {
const compiler = getCompiler("dependencies.js");
const stats = await compile(compiler);

Expand All @@ -119,7 +119,7 @@ describe("loader", () => {
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot("errors");
});

it("should allow adding dependencies and contextDependencies via loader context", async () => {
it("should allow adding dependencies, contextDependencies and buildDependencies via loader context", async () => {
const compiler = getCompiler("dependencies-via-context.js");
const stats = await compile(compiler);

Expand Down

0 comments on commit 04be3eb

Please sign in to comment.