import fs from 'fs-extra' ; import { BuildOptions } from '../utils/options' ; import { join } from 'path' ; import rollupCommonjs from '@rollup/plugin-commonjs' ; import rollupResolve from '@rollup/plugin-node-resolve' ; import { aliasPlugin } from './plugins/alias-plugin' ; import { parse5Plugin } from './plugins/parse5-plugin' ; import { replacePlugin } from './plugins/replace-plugin' ; import { RollupOptions , OutputOptions } from 'rollup' ; import { sizzlePlugin } from './plugins/sizzle-plugin' ; import { writePkgJson } from '../utils/write-pkg-json' ; import { getBanner } from '../utils/banner' ; export async function mockDoc ( opts ) { const inputDir = join ( opts . buildDir , 'mock-doc' ) ; const outputDir = opts . output . mockDocDir ; const bundleDtsPromise = bundleMockDocDts ( inputDir , outputDir ) ; writePkgJson ( opts , outputDir , { name : '@stencil/core/mock-doc' , description : 'Mock window, document and DOM outside of a browser environment.' , main : 'index.cjs' , module : 'index.js' , types : 'index.d.ts' , sideEffects : false , } ) ; const esOutput = { format : 'es' , file : join ( outputDir , 'index.js' ) , preferConst : true , banner : getBanner ( opts , `Stencil Mock Doc` , true ) , } ; const cjsOutput = { format : 'cjs' , file : join ( outputDir , 'index.cjs' ) , intro : CJS_INTRO , outro : CJS_OUTRO , strict : false , esModule : false , banner : getBanner ( opts , `Stencil Mock Doc (CommonJS)` , true ) , } ; const mockDocBundle = { input : join ( inputDir , 'index.js' ) , output : [ esOutput , cjsOutput ] , plugins : [ parse5Plugin ( opts ) , sizzlePlugin ( opts ) , aliasPlugin ( opts ) , replacePlugin ( opts ) , rollupResolve ( ) , rollupCommonjs ( ) , ] , } ; await bundleDtsPromise ; return [ mockDocBundle ] ; } const CJS_INTRO = `
var mockDoc = (function(exports) {
'use strict';
` . trim ( ) ; const CJS_OUTRO = `
if (typeof module !== "undefined" && module.exports) {
  module.exports = exports;
}
return exports;
})({});
` . trim ( ) ; async function bundleMockDocDts ( inputDir , outputDir ) { const srcDtsFiles = ( await fs . readdir ( inputDir ) ) . filter ( ( f ) => { return f . endsWith ( '.d.ts' ) && ! f . endsWith ( 'index.d.ts' ) && ! f . endsWith ( 'index.d.ts-bundled.d.ts' ) ; } ) ; const output = await Promise . all ( srcDtsFiles . map ( ( inputDtsFile ) => { return getDtsContent ( inputDir , inputDtsFile ) ; } ) ) ; const srcIndexDts = await fs . readFile ( join ( inputDir , 'index.d.ts' ) , 'utf8' ) ; output . push ( getMockDocExports ( srcIndexDts ) ) ; await fs . writeFile ( join ( outputDir , 'index.d.ts' ) , output . join ( '\n' ) + '\n' ) ; } async function getDtsContent ( inputDir , inputDtsFile ) { const srcDtsText = await fs . readFile ( join ( inputDir , inputDtsFile ) , 'utf8' ) ; const allLines = srcDtsText . split ( '\n' ) ; const filteredLines = allLines . filter ( ( ln ) => { if ( ln . trim ( ) . startsWith ( '///' ) ) { return false ; } if ( ln . trim ( ) . startsWith ( 'import ' ) ) { return false ; } if ( ln . trim ( ) . startsWith ( '__' ) ) { return false ; } if ( ln . trim ( ) . startsWith ( 'private' ) ) { return false ; } if ( ln . replace ( / /g , '' ) . startsWith ( 'export{}' ) ) { return false ; } return true ; } ) ; let dtsContent = filteredLines . map ( ( ln ) => { if ( ln . trim ( ) . startsWith ( 'export ' ) ) { ln = ln . replace ( 'export ' , '' ) ; } return ln ; } ) . join ( '\n' ) . trim ( ) ; dtsContent = dtsContent . replace ( /    /g , '  ' ) ; return dtsContent ; } function getMockDocExports ( srcIndexDts ) { const exportLines = srcIndexDts . split ( '\n' ) . filter ( ( ln ) => ln . trim ( ) . startsWith ( 'export {' ) ) ; const dtsExports = [ ] ; exportLines . forEach ( ( ln ) => { const splt = ln . split ( '{' ) [ 1 ] . split ( '}' ) [ 0 ] . trim ( ) ; const exportNames = splt . split ( ',' ) . map ( ( n ) => n . trim ( ) ) . filter ( ( n ) => n . length > 0 ) ; dtsExports . push ( ... exportNames ) ; } ) ; return `export { ${ dtsExports . sort ( ) . join ( ', ' ) } }` ; }