Gulp + Sourcemaps + PostCSS(Autoprefixer + Minification via CSSnano) - build

Basically, I want sourcemaps available to my unminified and minifies flavors of my site.css file. I'd like my end result to be:
site.css
site.min.css
site.css.map
site.css.min.map
Currently, I only get:
site.css
site.min.css
site.css.min.map
I know my gulp script is wrong, but I don't know how to fix it. I need sourcemaps to write a sourcemap to site.css before site.min.css gets created. HALP!
and Thank you
gulp.task('scss', gulp.series('bootstrap:scss', function compileScss() {
return gulp.src(['./site/assets/scss/*.scss'])
.pipe(sourcemaps.init())
.pipe(sass.sync({
outputStyle: 'expanded'
}).on('error', sass.logError))
.pipe(gulp.dest('./site/dist/css')) // outputs site.css
.pipe(postcss([autoprefixer(), cssnano()
]))
.pipe(sourcemaps.write('.'))
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./site/dist/css')) //outputs site.min.css
}));

You only need sourcemaps for your unminified version, i would introduce a NODE_ENV for doing minification and sourcemaps, then use gulp-if to see if you're in development or production environment
Alternatively you could have separate build and dev tasks.
Using process.env.NODE_ENV means you can use them in postcss.config.js files etc. too
I found this really good because it shows you how you can use a gulp.babel.js file with Gulp 4. I was using 3.9.1 being reluctant to upgrade until this week but this helped immensely with understanding the changes from v3>4.

Related

gulp-jshint task is taking over 5 minutes to complete

I'm working with gulp and am fairly new, my gulp jshint task is as follow:
gulp.task('jshint', ()=>{
return gulp.src([`${root}/**/*.js`])
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('gulp-jshint-html-reporter', { filename: 'jshint-output.html' }))
.pipe(jshint.reporter('fail'));
});
The task is taking 5mins every time I run my build. Is there anyway to speed this up?
$root is src, it is not running through node_modules.
Thanks in advance!
Figured it out, there were some lib files being picked up that shouldn't have and as they were huge they caused jshint to take longer. flips desk

Babel breaks other javascript plugins/frameworks

I am using the Zurb Fonudation framework. When I place a JavaScript framework such as snap.svg in the src/assets/js folder it will automatically get compiled into the app.js file. So far I've had one jQuery plugin that I've tried to use that is broken, and also snap.svg that gets broken. I'm assuming this has something to do with babel. For example with snap.svg I get the following error..
snap.svg.js:420 Uncaught TypeError: Cannot set property 'eve' of undefined
I've tried placing the path to snap.svg in the config.yml file but that doesn't seem to make any difference other than where snap.svg is located within app.js
I'm assuming I'm just not doing something right. Any ideas?
You can tell babel to not transpile particular pieces of code by passing the 'ignore' flag to it within the build process. E.g.:
function javascript() {
return gulp.src(PATHS.javascript)
.pipe($.sourcemaps.init())
.pipe($.babel({ignore: ['src/assets/js/snap.svg']}))
.pipe($.concat('app.js'))
.pipe($.if(PRODUCTION, $.uglify()
.on('error', e => { console.log(e); })
))
.pipe($.if(!PRODUCTION, $.sourcemaps.write()))
.pipe(gulp.dest(PATHS.dist + '/assets/js'));
}
You can see more about customizing the build process in this forum post: http://foundation.zurb.com/forum/posts/36974-enhancing-foundation-with-bower-components

ember test to no run jshint

I would like to run "ember test" without JShint. My ultimate goal is to set run jshint in development environment and not run it in production build.
I first started to turn off the option in Brocfile.js
http://discuss.emberjs.com/t/disable-ember-cli-hinting/6731/2
var app = new EmberApp({
hinting: false
});
It worked, so I decided to try
var EmberApp = require('ember-cli/lib/broccoli/ember-app'),
isProduction = ( process.env.EMBER_ENV || 'development' ) === 'production';
if( isProduction ){
app.hinting = false;
}
Then I realize the process.env.EMBER_ENV, doesn't seem to work. But little did I know I was probably running the wrong command.
ember test
The command didn't specify any environment, so I tried
ember test --environment=production
ember test --environment production
which result in an exception:
Build failed.
Path or pattern "test-loader.js" did not match any files
Error: Path or pattern "test-loader.js" did not match any files
at Object.multiGlob (.../node_modules/ember-cli/node_modules/broccoli-kitchen-sink-helpers/index.js:202:13)
Next, I try to read node_modules/ember-cli/lib/broccoli/ember-app.js, I see:
var isProduction = this.env === 'production';
this.tests = options.hasOwnProperty('tests') ? options.tests : !isProduction;
this.hinting = options.hasOwnProperty('hinting') ? options.hinting : !isProduction;
But I don't really know how this.env get pass in the ember test and I can't get the environment within the Brocfil.js. I am assuming by default, hinting would respect the isProduction value if it isn't defined.
And searching further more, got me to https://github.com/rwjblue/ember-cli-test-loader, which seems to be related.
My questions are:
1. Is there a way to run ember test without jshint via CLI?
2. Can this be set using config/environment.js?
3. Can I set a breakpoint to debug the Brocfile.js file? I tried with chrome:localhost:4200, I don't any node_modules file being loaded.
Thanks in advance. I am an extreme newbie to javascript and ember..
try
ember test --query=nolint
the latest ember-cli-qunit uses this query param to disable lint checks in tests
here is the commit where it was added if you are interested

Jest React Example

I am trying to run the React Example from the Jest React tutorial but I am receiving errors
λ npm test
> ...
> jest
Found 1 matching tests...
FAIL __tests__\CheckboxWithLabel-test.js (0.551s)
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
I have pretty much copied the code directly from the example. The package.json is as follows:
{
"dependencies": {
"react": "*",
"react-tools": "*"
},
"scripts":{
"test": "jest"
},
"jest": {
"scriptPreprocessor": "<rootDir>/preprocessor.js",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react"
]
},
"devDependencies": {
"jest-cli": "~0.1.17"
}
}
Any thoughts on what I can do to resolve these errors and run the example test successfully? It's very possible I'm missing an important detail (or details) but not entirely sure what. Oh and for what it's worth, I'm running this on Windows if that impacts this. I would really like to get some tests on my react components (was having some trouble there too so started with the basic examples) -- any help would be appreciated :)
I created an issue on their github page. Waiting to find out if it is actually a windows related issue
In the meantime, eventually got it working by just specifying the name of the module rather than the relative path
"unmockedModulePathPatterns": ["react"]
To anybody who ends up here on a search about this, #ron's github issue was ultimately resolved, and the conclusion was that unmockedModulePathPatterns expects an array of regex statments matching file paths, not necessarily the file paths themselves. This is why using the "relative" paths worked. From the Jest API docs:
An array of regexp pattern strings that are matched against all
modules before the module loader will automatically return a mock for
them. If a module's path matches any of the patterns in this list, it
will not be automatically mocked by the module loader.
This is useful for some commonly used 'utility' modules that are
almost always used as implementation details almost all the time (like
underscore/lo-dash, etc). It's generally a best practice to keep this
list as small as possible and always use explicit
jest.mock()/jest.dontMock() calls in individual tests. Explicit
per-test setup is far easier for other readers of the test to reason
about the environment the test will run in.
It is possible to override this setting in individual tests by
explicitly calling jest.mock() at the top of the test file.
(https://facebook.github.io/jest/docs/api.html#config-unmockedmodulepathpatterns-array-string)
and from the issue itself:
unmockedModulePathPatterns are used internally by Jest to create a
RegExp against which all required modules will be tested. As such, you
need to provide a valid regex pattern. For example, this worked nicely
for me :
> unmockedModulePathPatterns: [
> "node_modules\\" + path.sep + "react",
> "node_modules\\" + path.sep + "reflux",
> "node_modules\\" + path.sep + "react-router"
> ],
"mlarcher", (https://github.com/facebook/jest/issues/100)
<rootDir> should be replaced with the actual path. It looks like you don't have a subdirectory you want to start from, whereas in some cases you might only want to run tests in a src/ path so your package.json would look more like this:
{
...
"jest": {
"rootDir": "src",
"scriptPreprocessor": "../jest/preprocessor.js" // Note: relative to src
}
...
}

ember-cli fails on --environment=production (Uncaught Error: Could not find module)

I am using ember-cli and have a problem with selecting the production environment. Specifically, everything works when I run ember serve --environment=development and I get a blank page when I run ember serve --environment=production. In the console, I see:
Uncaught TypeError: undefined is not a function
Uncaught Error: Could not find module simple-auth/authenticators/base
All other things are equal, and all dependencies are up to date. I'm a total noob so I don't even know where to begin on how to debug: is it ember? ember-cli? broccoli? Any help would be appreciated.
I had exact the same problem, and James_1x0 is correct, it is an broccoli issue.
After debugging it occurs, that the "undefined" error apears on "Ember.handlebars.compile" which lead to other research.
It seems, that in production envrironment "handlebars.js" is replaced by "handlebars.runtime.js" in the ember-cli build process, which seem to be a problem for broccoli at this time.
Other devs had the same problem but with other libraries as well:
https://github.com/stefanpenner/ember-cli/pull/675#issuecomment-47431195
Here the solution was to add:
var index = app.legacyFilesToAppend.indexOf('bower_components/handlebars/handlebars.runtime.js');
if(index) {
app.legacyFilesToAppend[index] = 'bower_components/handlebars/handlebars.js';
}
into your Brocfile.js to replace the "handlebars.runtime.js" with the "handlebars.js".
This also fixed the problem for me.
It sure has the drawback that the whole handlebars file is deployed but its a workarround for now, until the problem is fixed.
Solution is mentioned on Ember CLI website:
This is somewhat non-standard and discouraged, but suppose that due to a requirement in your application that you need to use the full version of Handlebars even in the production environment.
Basically, you can pass vendorFiles option to your EmberApp instance which will force CLI to include full version of Handlebars.
Example of explicitly requiring handlebars.js , in Brocfile.js:
var app = new EmberApp({
vendorFiles: {
'handlebars.js': {
production: 'bower_components/handlebars/handlebars.js'
}
}
});
This is recommended way of solving this issue(discussion on GitHub).