Using node modules with Rollup to build web client - javascript

I'm trying to build a react application using rollup instead of browserify and babel. I realize I need to use the rollup-plugin-babel to transpile jsx, but when I tell rollup the format is iife, the final page loads with an error:
Uncaught ReferenceError: React is not defined
What do I need to add to the rollup.config.js to include the node modules I've installed in package.json in my final build?

Two options:
Include React as a separate <script> tag before your app bundle
Include rollup-plugin-node-resolve in your config file, to pull in dependencies from your node_modules folder.
If you take the second route you'll also need rollup-plugin-commonjs (to convert the CommonJS module into an ES module). I think you would also need to add import * as React from 'react' to each module that contained JSX, otherwise you'll continue to get the ReferenceError.
Note: you might be able to use rollup-plugin-buble to transpile JSX. It's similar to the Babel plugin but much faster (though it doesn't transpile every ES2015 feature)

Related

Vite build: Don't perform typecheck on modules that have been included via the "module" package.json param

I am transitioning my application from Webpack to Vite and I have this custom component library that uses typescript and includes many #types/library-name libraries.
I also have the main frontend repository which I am currently migrating to Vite. It uses this component library.
The problem is that Vite couldn't import this component library at first since there was no "module" property defined in the package.json file of my library. I defined it and pointed it to build/index.ts.
Now, when I include this library from my frontend and perform typecheck using tsc --noEmit, it gives a lot of errors that should've been thrown during the library build, i.e. errors about some modules not being defined, like for example library imports pluralize and during the build of the frontend it throws the following error:
frontend-repo/node_modules/pluralize/pluralize.js' implicitly has an 'any' type.
Try `npm i --save-dev #types/pluralize` if it exists or add a new declaration (.d.ts) file containing `declare module 'pluralize';
How do I make it so that the type resolution for my component library stays local to the library build?

Webpack 2 - import throws error on postcss.config.js and other js files

I have a project with Webpack 2, PostCSS, ES2015 (Babel) and Jest.
Right now, all ES2015 is working correctly on src/index.js and files directly connect to src/index.js.
But in some files it breaks the code. Example, on mixins/index.js, I have this:
const postcss = require('postcss');
And when I change it to this:
import postcss from 'postcss';
It throws this error:
Module build failed: SyntaxError: Unexpected reserved word
The same happen in postcss.config.js.
What am I missing on Webpack configuration to make it work?
You can check the repository here. These are the files I talked about: postcss.config.js and mixins/index.js
PostCSS requires your files to be in dedicated CSS files instead of *.js files like they are in your project.
If you want to use PostCSS with JavaScript (like with ReactJS inline-styles) you need the postcss-js parser which requires an additional loader and a naming style like *.style.js so that webpack doesn't confuse the JavaScript styles with regular JavaScript files which are processed via Babel.

Which tools do I need in order to use ES6 style imports for my project using Gulp?

I have a legacy project which uses Gulp to compile SASS and minify JS etc. (based on Drupal).
I'd like to use ES6 style imports and also be able to import modules that are located in node_modules without referencing the full path (for example import the lodash.debounce module as
import throttle from 'lodash.throttle'
What do I need to achieve that? (besides Gulp and NPM). Babel? Browserify? Lost with all the tooling.
I'd like to avoid webpack as it would increase the complexity of my project (and I don't know if I can use it in parallel with the way Drupal 7 is doing things)
Yep, you want babel. The documentation to set it up in your gulpfile is here: http://babeljs.io/docs/setup/#installation
You'll want to use the es2015 preset to enable the import syntax.
var gulp = require("gulp");
var babel = require("gulp-babel");
gulp.task("default", function () {
return gulp.src("src/app.js")
.pipe(babel({
presets: ['es2015']
}))
.pipe(gulp.dest("dist"));
});
To use import you need a transpiler, for example babel.
Assuming you are building code for the browser then to use import (transpiled to require) you need webpack or browserify which will also allow you to import from node_modules (there are other module builders too, jspm and systemjs, however I know next-to-nothing about them).
For either of these you will also need something to enable babel to be used in the build process. I use webpack with babel-loader.
I found a useful reference here specifically on using ES6 modules with webpack.

import requirejs amd module with webpack

I'm using Converse.js and it's prebuilt into the RequireJS/AMD syntax. Including the file from a CDN you could use it like require(['converse'], function (converse) { /* .. */ }). How is it possible to use this with webpack? I would like to bundle converse.js with my webpack output.
I have the file on disk, and want to import it like
import converse from './converse.js';
converse.initialize({ .. });
Webpack picks up the file and bundles it correctly, although it's not useable yet as it throws 'initialize is not a function'. What am I missing?
I suspect the way their bundle is built will not work correctly with how Webpack evaluates modules in a limited context.
From their builds, taking the built AMD module via NPM without dependencies should be parsable by Webpack and it will enable you to provide the dependencies to avoid dupes in the final output.
If all else fails, using the script-loader will evaluate the script in the global context and you'd get the same experience as if you'd have followed their usage guidelines to reference it from a CDN, just don't forget to configure the globals for your linter.

EcmaScript 6 module requiring, how does it work?

So I know how to require and export modules in ES6. But for frameworks like Aurelia, the docs say that you require aurelia like so:
import {LogManager} from 'aurelia-framework';
Do I have to place a JS file named aurelia-framework in the folder where the JS file I'm executing it from resides, or does the import function work similiar to the require function in NodeJS/CommonJS?
According to this article ES6 modules spec only deals with loading modules that are present in the file path. Downloading these files (via NPM or by other means) is outside of scope of ECMAScript 6 modules spec. Nothing is said in the spec about supporting npm package includes (traversing the directory structure down to the /, one directory at a time, looking for a package.json file and then searching within the node_modules directory where package.json file is found). So while the import syntax is similar to commonJS style, the whole magic of looking for modules in the node_modules directory is not included.
So for your example to work, aurelia-framework must be a javascript file somewhere in your file system and it should contain an exports statement.
import {LogManager} from 'aurelia-framework'; // ./aurelia-framework.js
import {LogManager} from '../libs/aurelia-framework'; // ../libs/aurelia-framework.js
with Aurelia, you can install dependent libraries using jspm. you can see an example of that here. jspm will get the packages for you and bring them into subfolders in your project. jspm uses an index (stored in config.js) to know where to locate the files (similar to how requirejs, but works for amd, commonjs, and es6 modules).
there is also an example of using the aurelia libraries with requirejs amd loader. this example uses a bundle of aurelia libraries generated by r.js as shown here

Categories

Resources