I'm looking for a way to minify the white space in template literals. Since regular js minification wouldn't remove white space on a template literal I was expecting that maybe polymer-cli had a way to minify those.
An example of the result of minification:
import{PolymerElement,html}from'../node_modules/#polymer/polymer/polymer-element.js';export default class MyApp extends PolymerElement{static get is(){return'my-app'}static get template(){return html`
<style>
:host {
display: block;
height: 100vh;
}
.app {
height: 100vh;
}
</style>
<div class="app">
My App
</div>
`}}customElements.define(MyApp.is,MyApp);
polymer-cli currently doesn't support minification of tagged template strings. Internally, it uses Babel plugins to minify JavaScript, so ideally we'd be able to insert the babel-plugin-minify-template-strings plugin into the pipeline when minification is enabled.
For now, we could use babel-cli along with that plugin to process the build output of polymer-cli:
Start with a Polymer 3 template project, e.g., PolymerLabs/start-polymer3.
git clone https://github.com/PolymerLabs/start-polymer3.git
cd start-polymer3
Install babel-cli and the babel-plugin-minify-template-strings plugin. Your project may need other Babel plugins. In this case, this template project needs babel-plugin-syntax-dynamic-import for Babel to handle the dynamic imports in the code.
yarn add -D babel-cli \
babel-plugin-minify-template-strings \
babel-plugin-syntax-dynamic-import
Add a .babelrc config file with the following file contents:
{
"compact": true,
"ignore": [
"node_modules"
],
"plugins": [
"minify-template-strings",
"syntax-dynamic-import"
]
}
Add a build NPM script to package.json to run babel-cli (with .babelrc above) on the JavaScript output of polymer build:
"scripts": {
"build": "polymer build && $(npm bin)/babel -d . build/**/src/**/*.js"
}
Run npm run build (or yarn build). The command output (running with polymer-cli (1.7.0-pre.13), zsh, macOS High Sierra) should look similar to this:
➜ start-polymer3 git:(master) ✗ yarn build
yarn run v1.3.2
$ polymer build && $(npm bin)/babel -d . build/**/src/**/*.js
info: [cli.command.build] Clearing build/ directory...
info: [cli.build.build] (es6-unbundled) Building...
info: [cli.build.build] (es6-unbundled) Build complete!
build/es6-unbundled/src/lazy-element.js -> build/es6-unbundled/src/lazy-element.js
build/es6-unbundled/src/start-polymer3.js -> build/es6-unbundled/src/start-polymer3.js
✨ Done in 8.66s.
➜ start-polymer3 git:(master) ✗
See GitHub repo with the above changes, and its sample output
Did you try to setup your polymer.json with the following config?:
"builds": [{
"bundle": true,
"js": {"minify": true},
"css": {"minify": true},
"html": {"minify": true}
}],
Related
I created a project using npm scripts in order to avoid the use of gulp. The thing is, my project has two scripts:
prepare.sh (uses wget to download some files and do mkdirs)
process.js (transform a json file into another overriding some keys)
package.json
{
"scripts": {
"process": "./process.js",
"prepare": "./prepare.sh $npm_package_config_source $npm_config_env",
"config": "npm run prepare && npm run process"
},
"config": {
"source": "https://myurl"
},
"devDependencies": {
"fs": "0.0.1-security",
"json-override": "^0.2.0"
}
}
So, if I want to apply the transform in this project I run npm run config, but I want this project to be part of another as a local module of a front-end project.
How can I set up my project? And when I add it as a dependency of my front project, how can I call the config script from the package.json of the front project?
You can add a bin object to your package.json which will result in files installed into the node_modules/.bin folder docs.npmjs.com/files/package.json#bin
example
{
"bin": {
"process": "./process.js",
"prepare": "./prepare.sh"
},
"scripts": {
"config": "prepare && process"
},
"devDependencies": {
"fs": "0.0.1-security",
"json-override": "^0.2.0"
}
}
Also since npm runs scripts with node_modules/.bin as part of the path you can simply call them by name only. Just remember to add #!/usr/bin/env node to the top of process.js
I am using Polymer and reloading changes in files manually. So I tried using using browser-sync and also browser-sync with gulp but not able to succeed.
I tried two following ways :
1) npm scripts in package.json
"scripts": {
"dev": "polymer serve | npm run watch",
"watch": "browser-sync start --proxy localhost:8080 --files 'src/*.html, src/*.js, images/*' "
},
Running it using npm run dev ,it ran but not able to detect the changes in the file.
2) Using gulp with browser-sync
var gulp = require('gulp');
var bs = require('browser-sync').create();
gulp.task('browser-sync', function() {
bs.init({
port : 5000,
proxy: {
target : "localhost:8080"
}
});
});
gulp.task('watch', ['browser-sync'], function () {
gulp.watch("*.html").on('change', bs.reload);
});
It also ran but not able to detect the changes in *.html file which is present in src folder.
Can anybody help me why changes of files not being detected.
I figured it myself, i was doing one small mistake in npm scripts. I have modified as :
"scripts": {
"dev": "polymer serve --port 8081 | npm run watch",
"test": "polymer test",
"watch": "browser-sync start --proxy localhost:8081 --files \"src/**/*.*, index.html, *.js\""
},
Now it is working fine !!
I've got a gulp task which is intended to copy bower dependencies to another folder, but only a subset of the dependencies are being matched.
Here's a simplified version of the task which just prints out the matching paths:
gulp.task('list-dependencies',function() {
gulp.src(mainBowerFiles('**/*'), { base: './bower_components' })
.pipe($.print());
});
Here's the bower.json file contents:
bower.json
{
"name": "app",
"private": true,
"dependencies": {
"bootstrap": "3.0.0",
"jquery": "1.10.2",
"modernizr": "~2.8.3",
"angular": "~1.4.4",
"angular-ui": "~0.4.0",
"angular-route": "~1.4.4"
}
}
Here's a listing of the bower_components folder:
$ ls bower_components/
angular angular-route angular-ui bootstrap jquery modernizr
outut:
[13:30:08] Starting 'list-dependencies'...
[13:30:08] Finished 'list-dependencies' after 27 ms
[gulp] bower_components\jquery\jquery.js
[gulp] bower_components\angular\angular.js
[gulp] bower_components\angular-route\angular-route.js
[gulp] bower_components\bootstrap\dist\js\bootstrap.js
[gulp] bower_components\bootstrap\dist\css\bootstrap.css
As you can see, angular-ui and modernizr aren't listed. Can someone explain why this is happening?
Bower has a concept of defining "main" files within a bower.json. The packages that were missed were due to the fact that these packages didn't contain a bower.json file which defined the main files. To get the main-bower-files plugin to work, you have to define overrides either within your bower file or as a parameter to main-bower-files.
I have two questions. I am completing a project from the book "Learning to Program", and am a little confused as to why npm is not installing, and why I am receiving another error message. I'm using Windows, I have installed node.js (v0.12.2) and have attempted to install grunt: npm install -g grunt-cli
Question 1 - When I try to install npm, it does not list all of the dependencies defined in package.json.
The following shows when I try to install npm in my main directory:
C:\Users\Me\My Documents\kittenbook\npm install
npm WARN package.json kittenbook#0.0.1 No description
npm WARN package.json kittenbook#0.0.1 No repository field
npm WARN package.json kittenbook#0.0.1 No README data
From what I see, it's supposed to display a bunch of lines after those three warnings, such as:
npm http GET https://registry.npmjs.org/grunt-contrib-concat
npm http GET https://registry.npmjs.org/grunt
npm http GET https://registry.npmjs.org/grunt-contrib-copy
and so on.
Question 2:
This is probably because installing npm is not working correctly, but when I try to run "grunt jshint" it gives me this error:
>> Local Npm module "grunt-contrib-.copy" not found. Is it installed?
Running "jshint:files" (jshint) task
>> 2 files lint free
Done, without errors.
Sorry for this very unorganized question, but I am very new to programming and I am very confused as to where the problem is.
Here is some more information.
Gruntfile.js
module.exports = function(grunt) {
// Project configuration
grunt.initConfig({
concat: {
release: {
src: ['js/values.js', 'js/prompt.js'],
dest: 'release/main.js'
}
},
copy: {
release: {
src: 'manifest.json',
dest: 'release/manifest.json'
}
},
jshint: {
files: ['js/values.js', 'js/prompt.js']
}
});
// Load Grunt plugins
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib.copy');
grunt.loadNpmTasks('grunt-contrib-jshint');
// Register Tasks
grunt.registerTask('default', ['jshint', 'concat', 'copy']);
};
package.json
{
"name": "kittenbook",
"version": "0.0.1",
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-copy": "~0.5.0"
}
}
If you need other information I will gladly get it to you. Thank you all so much for your help.
grunt-contrib.copy should be grunt-contrib-copy (use dash, not period)
I have setup Sublime Text 2 to run JSHint when JavaScript files are saved. The easiest way to achieve this seemed to be to use the plugin SublimeOnSaveBuild together with the JSHint plugin. And it works great.
The problem is that I now need to run an additional shell command when JavaScript files are saved. This is project specific, so I added a build system to the .sublime-project file (echo Test is just a placeholder for the real command).
"build_systems": [
{
"name": "RunOnJavaScriptSave",
"selector": "source.js",
"cmd": ["echo Test"],
"windows": {
"shell": true
}
}
]
This is now triggered when JavaScript files are saved, but it stops JSHint from running. It seems the "source.js" selector conflicts with the JSHint plugin.
I located Packages\JSHint\JSHint.sublime-build and indeed it also contains a "source.js" selector:
{
"selector": "source.js",
"cmd": ["jshint", "$file", "--reporter", "$packages/JSHint/reporter.js"],
...
}
While not very elegant, my "solution" is to run jshint in my build system as well:
"build_systems": [
{
"name": "RunOnJavaScriptSave",
"selector": "source.js",
"cmd": "jshint $file --reporter \"$packages/JSHint/reporter.js\" && echo Test",
...
}
]
This works, but surely there must be a more elegant way to have several build systems that use the same selector (source.js in my case) and have them all run on save?
Any help would be much appreciated.