bufferoverflow76
bufferoverflow76

Reputation: 797

babel . --copy-files incorrectly copy node_modules

I have a project structure like the following:

-rw-r--r--    1 chung2014  staff    2774 Nov  7 19:13 README.md
-rw-r--r--    1 chung2014  staff      75 Nov 26 23:27 babel.config.js
drwxr-xr-x  588 chung2014  staff   18816 Nov 26 23:01 node_modules
-rw-r--r--    1 chung2014  staff     781 Nov 26 22:25 nodemon.json
-rw-r--r--    1 chung2014  staff  377691 Nov 26 22:08 package-lock.json
-rw-r--r--    1 chung2014  staff    1551 Nov 26 23:27 package.json
-rw-r--r--    1 chung2014  staff    2941 Nov 26 23:29 server.js
drwxr-xr-x   11 chung2014  staff     352 Nov 26 23:03 src
drwxr-xr-x    5 chung2014  staff     160 Nov 26 21:55 test

if I have all the source code inside the src directory, (e.g put server.js into src as well), I can have a script babel src --out-dir dist/ --copy-files in my package.json to compile the all the source code in src to dist/ directory.

However, due to some restriction, I cannot put my server.js inside src directory. So when I try to have a script babel . --out-dir dist/ --copy-files in my package.json, I let babel incorrectly copy files in node_modules to dist, which is not what I want.

So my question is how I can just only compile and copy files from both server.js and src/ to the destination directory dist/ without copying files in node_modules/?

$ cat babel.config.js 

const presets = [
  "@babel/preset-env",
];

module.exports = { presets };

Upvotes: 4

Views: 3528

Answers (3)

Lucio
Lucio

Reputation: 5428

New solution available

--no-copy-ignored is a new argument which allows the value of --ignore to be respected when copying files.

Usage

Example:

babel src -d dist --ignore 'src/**/*.spec.js' --copy-files --no-copy-ignored

The spec files are not going to be present on the output directory.


Source: https://github.com/babel/babel/issues/6226#issuecomment-590283042

Upvotes: 3

ysong
ysong

Reputation: 1

create a script like:

require('fs-extra').copy(
  process.argv.slice(-2).shift(), 
  process.argv.slice(-2).pop(), 
  { filter: (src,dist)=>{ return (src.match(/\.js|\.jsx|stories|test/)===null)} },
  err => { if (err) return console.error (err); console.log ('Copy success!'); 
});

and append this to your build command

&& ./scripts/--copy-files src dist/commonjs

the gist:

https://gist.github.com/kmrk/bbc52a4d54b407398aff1695e5b710b7

Upvotes: 0

loganfsmyth
loganfsmyth

Reputation: 161517

The only way to do this would be to drop --copy-files and do --ignore node_modules, e.g.

babel . --out-dir dist/ --ignore node_modules

and you'll also want to ignore dist/ and babel.config.js and anything else in the root that might contain JS files.

babel . --out-dir dist/ --ignore node_modules,dist,babel.config.js

Realistically, the better option would be for server.js to just proxy through to dist instead through, so you could do

babel src --out-dir dist/

and move server.js to src/server.js. If having a server.js is 100% necessary, then have it do require("./dist/server");.

Upvotes: 2

Related Questions