66*/
77
88import * as fs from 'fs' ;
9-
9+ import * as fse from 'fs-extra' ;
1010import * as path from 'path' ;
1111
12- const BUILD_DIR = 'build' ;
12+ const NPM_BUILD_DIR = 'build/npm ' ;
1313const ASSETS = [ 'README.md' , 'LICENSE' , 'package.json' , '.npmignore' ] ;
1414const ENTRY_POINTS = [ 'main' , 'module' , 'types' ] ;
1515
1616// check if build dir exists
1717try {
18- if ( ! fs . existsSync ( path . resolve ( BUILD_DIR ) ) ) {
19- console . error ( `Directory ${ BUILD_DIR } DOES NOT exist` ) ;
18+ if ( ! fs . existsSync ( path . resolve ( NPM_BUILD_DIR ) ) ) {
19+ console . error ( `Directory ${ NPM_BUILD_DIR } DOES NOT exist` ) ;
2020 console . error ( "This script should only be executed after you've run `yarn build`." ) ;
2121 process . exit ( 1 ) ;
2222 }
2323} catch ( error ) {
24- console . error ( `Error while looking up directory ${ BUILD_DIR } ` ) ;
24+ console . error ( `Error while looking up directory ${ NPM_BUILD_DIR } ` ) ;
2525}
2626
2727// copy non-code assets to build dir
@@ -32,24 +32,40 @@ ASSETS.forEach(asset => {
3232 console . error ( `Asset ${ asset } does not exist.` ) ;
3333 process . exit ( 1 ) ;
3434 }
35- fs . copyFileSync ( assetPath , path . resolve ( BUILD_DIR , asset ) ) ;
35+ fs . copyFileSync ( assetPath , path . resolve ( NPM_BUILD_DIR , asset ) ) ;
3636 } catch ( error ) {
37- console . error ( `Error while copying ${ asset } to ${ BUILD_DIR } ` ) ;
37+ console . error ( `Error while copying ${ asset } to ${ NPM_BUILD_DIR } ` ) ;
3838 }
3939} ) ;
4040
41+ // TODO remove in v7! Until then:
42+ // copy CDN bundles into npm dir to temporarily keep bundles in npm tarball
43+ // inside the tarball, they are located in `build/`
44+ const npmTmpBundlesPath = path . resolve ( NPM_BUILD_DIR , 'build' ) ;
45+ const cdnBundlesPaht = path . resolve ( 'build' , 'bundles' ) ;
46+ try {
47+ if ( ! fs . existsSync ( npmTmpBundlesPath ) ) {
48+ fs . mkdirSync ( npmTmpBundlesPath ) ;
49+ }
50+ fse . copy ( cdnBundlesPaht , npmTmpBundlesPath ) ;
51+ } catch ( error ) {
52+ console . error ( `Error while tmp copying CDN bundles to ${ NPM_BUILD_DIR } ` ) ;
53+ }
54+ // end remove
55+
4156// package.json modifications
42- const packageJsonPath = path . resolve ( BUILD_DIR , 'package.json' ) ;
57+ const packageJsonPath = path . resolve ( NPM_BUILD_DIR , 'package.json' ) ;
4358const pkgJson : { [ key : string ] : string } = require ( packageJsonPath ) ;
4459
4560// modify entry points to point to correct paths (i.e. strip out the build directory)
4661ENTRY_POINTS . filter ( entryPoint => pkgJson [ entryPoint ] ) . forEach ( entryPoint => {
47- pkgJson [ entryPoint ] = pkgJson [ entryPoint ] . replace ( `${ BUILD_DIR } /` , '' ) ;
62+ pkgJson [ entryPoint ] = pkgJson [ entryPoint ] . replace ( `${ NPM_BUILD_DIR } /` , '' ) ;
4863} ) ;
4964
5065// TODO decide if we want this:
5166delete pkgJson . scripts ;
5267delete pkgJson . volta ;
68+ delete pkgJson . jest ;
5369
5470// write modified package.json to file (pretty-printed with 2 spaces)
5571try {
0 commit comments