|
| 1 | +const path = require( 'path' ) |
| 2 | +const base = require( './base' ) |
| 3 | +const dev = require( './dev' ) |
| 4 | +const prod = require( './prod' ) |
| 5 | + |
| 6 | +const DEFAULTS = { |
| 7 | + browserslist: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9' ] |
| 8 | +} |
| 9 | + |
| 10 | +function tryRequirePackage( context ) { |
| 11 | + let pkg |
| 12 | + |
| 13 | + try { |
| 14 | + pkg = require( path.join( context, 'package.json' ) ) |
| 15 | + } catch ( e ) {} |
| 16 | + |
| 17 | + return pkg |
| 18 | +} |
| 19 | + |
| 20 | +const ID = 'driver-pages:webpack' |
| 21 | + |
| 22 | +module.exports = function ( api ) { |
| 23 | + api.hooks.chainWebpack.tapPromise( ID, async () => { |
| 24 | + const pkg = tryRequirePackage( process.cwd() ) |
| 25 | + |
| 26 | + const browserslist = api.config.browserslist || |
| 27 | + ( pkg && pkg.browserslist ) || |
| 28 | + DEFAULTS.browserslist |
| 29 | + |
| 30 | + let transpileModules = ( |
| 31 | + api.config.babel && api.config.babel.transpileModules |
| 32 | + ) || [] |
| 33 | + |
| 34 | + // clone |
| 35 | + transpileModules = transpileModules.slice() |
| 36 | + |
| 37 | + if ( api.config && api.config.plugins ) { |
| 38 | + api.config.plugins.forEach( plugin => { |
| 39 | + const pkg = tryRequirePackage( plugin.context ) |
| 40 | + |
| 41 | + if ( pkg ) { |
| 42 | + transpileModules.push( pkg.name ) |
| 43 | + } else { |
| 44 | + transpileModules.push( function ( filepath ) { |
| 45 | + return filepath.includes( plugin.context ) |
| 46 | + } ) |
| 47 | + } |
| 48 | + } ) |
| 49 | + } |
| 50 | + |
| 51 | + const exclude = [ |
| 52 | + /\.nut\/pages\.js/, |
| 53 | + /\.nut\/routes\.js/, |
| 54 | + /\.nut\/route-components\//, |
| 55 | + ] |
| 56 | + |
| 57 | + const includeCaches = {} |
| 58 | + |
| 59 | + // from egoist/poi |
| 60 | + function include( filepath ) { |
| 61 | + filepath = filepath.replace( /\\/g, '/' ) |
| 62 | + |
| 63 | + // use cache |
| 64 | + if ( typeof includeCaches[ filepath ] === 'boolean' ) { |
| 65 | + return includeCaches[ filepath ] |
| 66 | + } |
| 67 | + |
| 68 | + if ( !filepath.includes( 'node_modules' ) ) { |
| 69 | + includeCaches[ filepath ] = true |
| 70 | + return true |
| 71 | + } |
| 72 | + |
| 73 | + if ( transpileModules ) { |
| 74 | + const shouldTranspile = transpileModules.some( m => { |
| 75 | + if ( typeof m === 'string' ) { |
| 76 | + return filepath.includes( `/node_modules/${ m }/` ) || |
| 77 | + filepath.includes( `/node_modules/_${ m.replace( /\//g, '_' ) }` ) |
| 78 | + } |
| 79 | + |
| 80 | + if ( typeof m === 'function' ) { |
| 81 | + return m( filepath ) |
| 82 | + } |
| 83 | + |
| 84 | + if ( m && m.test ) { |
| 85 | + return m.test( filepath ) |
| 86 | + } |
| 87 | + |
| 88 | + return false |
| 89 | + } ) |
| 90 | + |
| 91 | + includeCaches[ filepath ] = shouldTranspile |
| 92 | + return shouldTranspile |
| 93 | + } |
| 94 | + |
| 95 | + includeCaches[ filepath ] = false |
| 96 | + return false |
| 97 | + } |
| 98 | + |
| 99 | + const options = { |
| 100 | + browserslist, |
| 101 | + include, |
| 102 | + exclude, |
| 103 | + clean: api.config.output && api.config.output.clean === true, |
| 104 | + publicPath: api.config.output && api.config.output.publicPath, |
| 105 | + } |
| 106 | + |
| 107 | + base( api.webpack, options ) |
| 108 | + |
| 109 | + if ( api.env === 'production' ) { |
| 110 | + prod( api.webpack, options ) |
| 111 | + } else { |
| 112 | + dev( api.webpack, options ) |
| 113 | + } |
| 114 | + } ) |
| 115 | +} |
0 commit comments