Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent npm preload module to break dev bundles #1003

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ test/transform_export/out.js
test/exports_basics/out.js
test/circular/export/
test/serviceworker/service-worker.js
!test/dev_bundles_minify/node_modules
19 changes: 9 additions & 10 deletions lib/bundle/add_npm_packages_node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var prettier = require("prettier");

/**
* Adds node to bundle to preload npm packages for dev bundles
Expand All @@ -6,11 +7,8 @@
* @param {{}} npmContext The context containing the npm packages loaded
*/
module.exports = function(bundle, npmContext) {
var unshift = [].unshift;

unshift.apply(bundle.nodes, [
makeAddNpmPackagesNode(npmContext)
]);
var push = [].push;
push.apply(bundle.nodes, [makeAddNpmPackagesNode(npmContext)]);
};

function makeAddNpmPackagesNode(npmContext) {
Expand All @@ -23,11 +21,12 @@ function makeAddNpmPackagesNode(npmContext) {
metadata: {
format: "global"
},
source: [
"if (steal && typeof steal.addNpmPackages === 'function') {",
"steal.addNpmPackages(" + JSON.stringify(packages) + ");",
"}"
].join(" ")
source: prettier.format(
`if (steal && typeof steal.addNpmPackages === "function") {
steal.addNpmPackages(${JSON.stringify(packages)});
}`,
{ tabWidth: 4 }
)
}
};
}
34 changes: 34 additions & 0 deletions test/dev_bundle_build_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,38 @@ describe("dev bundle build", function() {
return rmdir(devBundlePath);
});
});

it("minified dev bundles work", function(done) {
var dir = path.join(__dirname, "dev_bundles_minify");
var devBundlePath = path.join(dir, "dev-bundle.js");

var config = {
config: path.join(dir, "package.json!npm")
};

var options = assign({}, baseOptions, {
minify: true,
filter: "node_modules/**/*" // only bundle npm deps
});

var clean = function(err) {
rmdir(devBundlePath).then(function() {
done(err);
});
};
devBundleBuild(config, options)
.then(function() {
var exists = fs.existsSync(devBundlePath);
assert(exists, "dev bundle should be created");
})
.then(function() {
return open("test/dev_bundles_minify/dev.html");
})
.then(function(p) {
p.close();
p.browser.assert.element("h1");
})
.then(clean)
.catch(clean);
});
});
16 changes: 16 additions & 0 deletions test/dev_bundles_minify/dev.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Minified development bundles</title>
</head>
<body>
<script src="../../node_modules/steal/steal-sans-promises.js"
base-url="."
config-main="package.json!npm"
deps-bundle="dev-bundle"
></script>
</body>
</html>
6 changes: 6 additions & 0 deletions test/dev_bundles_minify/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var dummy = require("dummy");
dummy();

var h1 = document.createElement("h1");
h1.innerHTML = "worked!";
document.body.appendChild(h1);
7 changes: 7 additions & 0 deletions test/dev_bundles_minify/node_modules/dummy/main.js

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

8 changes: 8 additions & 0 deletions test/dev_bundles_minify/node_modules/dummy/package.json

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

8 changes: 8 additions & 0 deletions test/dev_bundles_minify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "app",
"main": "main.js",
"version": "1.0.0",
"dependencies": {
"dummy": "1.0.0"
}
}