Skip to content

Commit

Permalink
fix: added the styletagtransform option when it is a module to `add…
Browse files Browse the repository at this point in the history
…BuildDependency` (#528)
  • Loading branch information
cap-Bernardito committed Jul 20, 2021
1 parent a0ee2e0 commit 270513f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utils.js
Expand Up @@ -314,6 +314,8 @@ function getStyleTagTransformFnCode(
`${options.styleTagTransform}`
);

loaderContext.addBuildDependency(options.styleTagTransform);

return esModule
? `import styleTagTransformFn from ${modulePath};`
: `var styleTagTransformFn = require(${modulePath});`;
Expand Down
26 changes: 26 additions & 0 deletions test/__snapshots__/styleTagTransform-option.test.js.snap
@@ -1,5 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"styleTagTransform" option should "styleTagTransform" function path added to buildDependencies when injectType lazyStyleTag: DOM 1`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
<style id=\\"existing-style\\">.existing { color: yellow }</style>
<style>body {
color: red;
}
.modify{}
</style><style>h1 {
color: blue;
}
.modify{}
</style></head>
<body>
<h1>Body</h1>
<div class=\\"target\\"></div>
<iframe class=\\"iframeTarget\\"></iframe>
</body></html>"
`;
exports[`"styleTagTransform" option should "styleTagTransform" function path added to buildDependencies when injectType lazyStyleTag: errors 1`] = `Array []`;
exports[`"styleTagTransform" option should "styleTagTransform" function path added to buildDependencies when injectType lazyStyleTag: warnings 1`] = `Array []`;
exports[`"styleTagTransform" option should work when the "styleTagTransform" option is not specify and injectType lazyStyleTag: DOM 1`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
Expand Down
19 changes: 19 additions & 0 deletions test/styleTagTransform-option.test.js
Expand Up @@ -99,4 +99,23 @@ describe('"styleTagTransform" option', () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it(`should "styleTagTransform" function path added to buildDependencies when injectType lazyStyleTag`, async () => {
const styleTagTransformFn = require.resolve("./fixtures/styleTagTransform");
const entry = getEntryByInjectType("simple.js", "lazyStyleTag");
const compiler = getCompiler(entry, {
injectType: "lazyStyleTag",
styleTagTransform: styleTagTransformFn,
});
const stats = await compile(compiler);
const { buildDependencies } = stats.compilation;

runInJsDom("main.bundle.js", compiler, stats, (dom) => {
expect(dom.serialize()).toMatchSnapshot("DOM");
});

expect(buildDependencies.has(styleTagTransformFn)).toBe(true);
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});

0 comments on commit 270513f

Please sign in to comment.