Skip to content

Commit

Permalink
feat: emliminate output js when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
mlrawlings committed Apr 5, 2021
1 parent 7b996ce commit 00ac6e9
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const http = require("http");
const test = require("./test.marko");

http
.createServer((req, res) => {
test.render({}, res);
})
.listen(0);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello World</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as path from "path";
import * as webpack from "webpack";
import MarkoPlugin from "../../../plugin";

const markoPlugin = new MarkoPlugin();

export default [
{
name: "server",
target: "async-node",
mode: "production",
entry: path.join(__dirname, "server.js"),
module: {
rules: [
{
test: /\.marko$/,
loader: "@marko/webpack/loader"
}
]
},
plugins: [
new webpack.DefinePlugin({
"process.env.BUNDLE": true
}),
markoPlugin.server
]
},
{
name: "browser",
target: "web",
mode: "production",
module: {
rules: [
{
test: /\.marko$/,
loader: "@marko/webpack/loader"
}
]
},
plugins: [markoPlugin.browser]
}
];
28 changes: 26 additions & 2 deletions src/__tests__/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ for (const [version, webpack] of Object.entries({ webpack4, webpack5 })) {
test(`${name}`, async () => {
const fixtureDir = path.join(fixturesDir, name);
const snapshotDir = path.join(fixtureDir, "__snapshots__", version);
const remainingSnapshots = fs.readdirSync(snapshotDir);
const configPath = path.join(fixtureDir, "webpack.config.ts");
const { outputPath, outputFS, stats } = await compilation(
webpack as typeof webpack4,
Expand All @@ -42,9 +43,32 @@ for (const [version, webpack] of Object.entries({ webpack4, webpack5 })) {
path.join(outputPath, assetName),
"utf-8"
);
const snapshotName = prefixName + assetName;
const snapshotIndex = remainingSnapshots.indexOf(snapshotName);

expect(source).toMatchFile(
path.join(snapshotDir, prefixName + assetName)
if (
snapshotIndex == -1 &&
expect.getState().snapshotState._updateSnapshot !== "all"
) {
throw new Error(`Missing snapshot: ${version}/${snapshotName}`);
} else {
// eslint-disable-next-line jest/no-conditional-expect
expect(source).toMatchFile(path.join(snapshotDir, snapshotName));
remainingSnapshots.splice(snapshotIndex, 1);
}
}
}

if (remainingSnapshots.length) {
if (expect.getState().snapshotState._updateSnapshot === "all") {
for (const snapshotName of remainingSnapshots) {
fs.unlinkSync(path.join(snapshotDir, snapshotName));
}
} else {
throw new Error(
`Unexpected snapshots for ${version}: ${remainingSnapshots.join(
", "
)}`
);
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,25 @@ export default class MarkoWebpackPlugin {
});
}
);

compiler.hooks.afterCompile.tap(
"MarkWebpackBrowser:afterCompile",
compilation => {
for (const chunk of compilation.chunks) {
for (const assetName of chunk.files) {
const asset = compilation.assets[assetName];
if (asset.size() === 0) {
delete compilation.assets[assetName];
if (Array.isArray(chunk.files)) {
chunk.files.splice(chunk.files.indexOf(assetName), 1);
} else {
chunk.files.delete(assetName);
}
}
}
}
}
);
};
}
}
Expand Down

0 comments on commit 00ac6e9

Please sign in to comment.