I am trying to structure my project based on lerna.js now as per documentation
everything is setup and is working fine when run as development server following is directory
structure.
index.js
packages/
package1
package2
index.js => contains: import Package1 from 'package1' <--- Package1 etc path is managed by lerna.
package3
DEV & BUILD Command
# Build
"build::js": "babel ./packages --out-dir ./build --ignore node_modules",
"build::nonjs": "babel ./package1 --out-dir ./build/package1 --copy-files",
"build": "rm -rf ./build && mkdir ./build && npm run build::js && npm run build::nonjs"
# Dev
"dev:nodemon": "DEBUG=*,-babel,-babel:*,-express:*,-nodemon:*,-nodemon,-snapdragon:*,-finalhandler,-follow-redirects nodemon -L --exec babel-node --inspect index.js",
"dev": "yarn dev:nodemon",
Now runing dev command on my project works fine but when files are build for production than babel doesnt change Package1 path to its absolute location just like regular module.
...
var _Cache = _interopRequireDefault(require("package1")); // <<---- how to get ride of this problem. as. node tires to locate module but there is no package1 in node_modules its just regular modules which is inside packages/ directory. now running it on dev simply works.
Related
I setting up my test framework with TypeScript, Babel and Cucumber. I want to make it without yarn but it doesn't work on NPM. On NPM tests run but don't see scenrios, on Yarn everything is fine.
Yarn setup (works):
"scripts": {
"transpile": "rimraf /dist && babel --extensions .ts --out-dir /dist /src",
"cucumber-compile": "npm run transpile && cucumber-js",
"cucumber": "yarn cucumber-compile"
},
Output after I run:
yarn run cucumber --profile api
OUTPUT here
NPM setup (doesn't work):
"scripts": {
"transpile": "rimraf /dist && babel --extensions .ts --out-dir /dist /src",
"cucumber-compile": "npm run transpile && cucumber-js",
"cucumber": "npm run cucumber-compile"
},
Output after I run:
npm run cucumber --profile api
OUTPUT here
My index.ts:
index.ts
My .babelrc
.babelrc
I have a folder of dynamically loaded asset files I want to include in my parcel output directory. How can I include unreferenced static asset files like .json, .jpeg, .txt, .etc with my parcel build command?
With Parcel v2 there's a different plugin: https://github.com/elwin013/parcel-reporter-static-files-copy
yarn add parcel-reporter-static-files-copy --dev
then you need to create .parcelrc or add to the following to it. (Note: "..." is literal not something you need to fill in):
{
"extends": ["#parcel/config-default"],
"reporters": ["...", "parcel-reporter-static-files-copy"]
}
now any files (and sub-directories) in a directory named static will be automatically copied to the website (typically your dist folder) when you do the regular parcel build.
Note: This answer is for Parcel v1
There is a parcel plugin for that:
https://www.npmjs.com/package/parcel-plugin-static-files-copy
Install it:
yarn add parcel-plugin-static-files-copy --dev
Or
npm install -D parcel-plugin-static-files-copy
Then, in package.json, add:
"staticFiles": {
"staticPath": ["path/to/a/staticFolder"]
}
It should copy your files to the public folder.
Stay safe!
The best way to handle this is having control of the code. npm already provides the tools needed for this job. In the package.json, when running commands with &&, the first command will run, and if it does finish without any error, the second command will also be executed. Running &, however, will run each command in the background independently, regardless of what happens to the other command.
In other words:
Use && for sequential execution.
Use & for parallel execution.
For example:
project/
|dist/
|...
|src/
|assets/
|text.txt
|memos.txt
|info.ini
|css/
|style.css
|img/
|a.png
|b.jpg
|c.jpeg
|data.json
|not-to-copy.json
|not-to-copy.conf
|index.js
|index.html
|package.json
If you have a project structure like this add, some scripts to the package.json
{
...
"source": "src/index.html",
"scripts": {
"clean-dist": "rm -rf dist && mkdir dist",
"copy-img": "cp -vR ./src/img ./dist",
"copy-data": "cp -r src/data.json dist",
"copy-assets": "cp -r src/assets/* dist",
"copy-files": "npm run copy-img & npm run copy-assets & npm run copy-data",
"init": "npm run clean-dist && npm run copy-files",
"start": "npm run init && parcel",
"build": "npm run init && parcel build"
},
...
}
This configuration will sequentially run clean-dist and copy-files. The former will delete the dist directory and make the directory again. Then copy-files will copy src/img -> dist/img, src/assets/* -> dist/* and src/data.json -> dist/data.json in parallel. Finally, parcel will be executed.
You can edit your package.json scripts to copy the files after the build has executed. This is how I had a .htaccess file to the dist folder:
"build": "rm -rf dist && parcel build src/index.html -d dist --public-url ./ '.' cp src/.htaccess dist"
My use case is the following. I make a production build like this:
cross-env API_URL=my_url yarn build
and then run it like this:
yarn start:prod
and it all works ok. But what I want to be able to do is:
yarn build
and then start it like:
cross-env API_URL=my_url yarn start:prod
so that I can test the same build against different backend environments. Is this doable?
Right now, if I try it, the API_URL doesn't get picked up after build and it defaults to the one I have in an .env file (because I support a default case). I'm aware that Webpack needs the env variables during build, but maybe there's a workaround I'm missing.
My build script, inside package.json is:
cross-env NODE_ENV=production env-cmd .env.prod --no-override --config config/webpack.prod.babel.js --color -p --progress --hide-modules --display-optimization-bailout
and my start:prod script is:
cross-env NODE_ENV=production env-cmd .env-prod --no-override node server
I am trying to set up my project based on MonoRepo for that I have used lerna.js now as far as I know lerna.js work as follows.
For Dev
Create lerna.json and update all packages and workspace details to package.json and lerna.json
run yarn or npm client as usual you do.
any folder inside packages/ will be treated as node_module so you can directly call them.
Now running my application as dev works fine but when to build or transpile my es6 code using Babel for production than lerna.json doesn't work right following are problems which are making me confused about how to use it.
Do I have to Publish all my packages to npm for using them in production?
running lerna Bootstrap links packages but when I view my package inside node_modules it still contains es6 code..due to this node application throws an error for using import statement which node didn't understand unless you use experimental flag.
Following is the Example:
lerna.json
{
"packages": [
"packages/*"
],
"version": "independent",
"npmClient": "yarn",
"useWorkspaces": true
}
packages Directory
packages/
context/
dyna_modules/
www/
npm scripts
"clean": "lerna clean --yes && rimraf node_modules && rm -rf package-lock.json yarn.lock",
"build::js": "babel ./packages --out-dir ./build/packages/ --ignore node_modules",
"build::nonjs": "babel package.json prisma.yml Dockerfile docker-compose.yml .env --out-dir ./build --copy-files && babel ./packages/routes --out-dir ./build/packages/routes --copy-files",
"build": "rm -rf ./build && mkdir ./build && npm run build::js && npm run build::nonjs",
"dev:nodemon": "DEBUG=*,-babel,-babel:*,-express:*,-nodemon:*,-nodemon,-snapdragon:*,-finalhandler,-follow-redirects nodemon -L --exec babel-node --inspect index.js",
"dev": "yarn dev:nodemon --ignore-engines",
"prestart": "npm run build && lerna bootstrap",
"start": "cd ./build && node ./packages/www/index.js",
Problem
Running yarn start always fails with error for using the Import statement. During the inspection, I found out that all my packages/modules inside node_module contain es6 syntax instead of a trans-piled code.
I'm experimenting with setting up a dev environment to use NPM only, without the use of grunt.js or bower.js.
I followed this tutorial: http://beletsky.net/2015/04/npm-for-everything.html
I'm using nodemon to watch my .js and .scss files for changes which restarts the node server. So in my package.json file, under scripts I have
scripts:
"watch-js": "nodemon -e js --watch public/js -x \"npm run build-js\"",
"watch-sass": "nodemon -e scss --watch public/sass -x \"npm run build-sass\"",
"watch": "npm run watch-js & npm run watch-sass"
But when I run npm run watch it only watches for the public/js files to change. And it triggers a build accordingly.
But it won't watch for the sass files.
Versions:
node v0.10.36 nodemon v1.4.1
I also include a build script which if I run compiles the sass to css, so my build-sass script should be ok
"build": "npm run build-js & npm run build-sass",
"watch": "npm run watch-js & npm run watch-sass"
If you are using windows, you might be encountering the windows problem
Because npm is reliant on the operating systems shell to run scripts commands, they can quickly become unportable. While Linux, Solaris, BSD and Mac OSX come preinstalled with Bash as the default shell, Windows does not. On Windows, npm will resort to using Windows command prompt for these things
If so, you can try using parallelshell or npm-run-all for better cross platform support.
This works for me in Mac OS
Install nodemon
npm install --save-dev nodemon
Install npm-run-all:
npm install --save-dev npm-run-all
In package.json add two scripts:
"startn": "nodemon ./app.js",
"watch-jscss": "gulp watch"
Where 'app.js' is your server file
"gulp watch" can be replace by whatever task is doing a sass watch
In the console:
npm-run-all --parallel watch-jscss startn