最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 开源笔记:动手写静态网页开发框架 - 其二

    正文概述 掘金(Asuka109)   2020-12-28   427

    我希望在整个项目的开发过程中完全使用 TypeScript 来帮助进行类型提示和生成类型定义文件,所以我选择使用 microbundle 来作为构建工具。首先安装 microbundle 的依赖项:

    yarn add -D microbundle
    

    然后修改 package.json,microbundle 会根据其中相关配置进行构建,修改的部分如下:

    {
      "source": "src/index.ts",
      "main": "dist/index.js",
      "umd:main": "dist/index.umd.js",
      "scripts": {
        "build": "microbundle",
        "dev": "microbundle watch"
      }
    }
    

    其意为使用 src/index.ts 作为源文件,构建生成 commonjs 格式的 dist/index.js 模块文件作为包的入口,并同时生成 umd 格式的 dist/index.umd.js 模块文件放在一起备用。

    参考 poi 的命令行参数和配置文件,我们首先编写 cli 命令行工具能够接受的参数:

    src/config.ts

    import postHtml from 'posthtml'
    
    export interface Config {
      input?: {
        /** The directory to page files. */
        pages?: string
        /** The directory to component files. */
        components?: string
        /** The directory to layout files. */
        layouts?: string
      }
      output?: {
        /** The directory to output files. */
        dir?: string
        /** Clean output directory before building. */
        clean?: boolean
        /** Specify the output format. */
        format?: 'minimize' | 'beautify' | 'original'
      },
      devServer?: {
        /** Server host. */
        host?: string,
        /** Server port. */
        port?: string | number
        /** Open the dev server in your browser when building succeeded. */
        open?: boolean
      }
      /** Configure options for postHtml. */
      postHtmlPlugins?: postHtml.Plugin<unknown>[]
    }
    

    然后编写一个函数用于生成默认配置:

    export const defaultConfig = () => ({
      input: {
        pages: 'src/pages',
        components: 'src/components',
        layouts: 'src/layouts'
      },
      output: {
        dir: 'dist',
        clean: true,
        format: 'beautify'
      },
      devServer: {
        host: '0.0.0.0',
        port: '4000',
        open: true
      },
      postHtmlPlugins: []
    } as Config)
    

    以及一个函数用于从不同路径读取配置文件:

    /**
     * Newx will search newx.config.{ts,js} .newxrc.json
     * or newx property in package.json from your project. 
     */
    export const readConfig = () => {
      const configContents: Config[] = [];
      ['newx.config.ts', 'newx.config.js', '.newxrc.json', '.newxrc'].forEach(filename => {
        try {
          if (statSync('newx.config.ts').isFile()) {
            configContents.push(require('./newx.config.ts'))
          }
        } catch (e) {}
      })
      return Object.assign(defaultConfig(), ...configContents)
    }
    

    完成定义之后我们使用 cac 库编写命令行的框架:

    import { cac } from 'cac'
    
    const cli = cac('newx')
    
    cli.command('build', 'Build files.')
      .action((options) => {
        console.log('build: ', options)
      })
    
    cli.command('dev', 'Watch files and running dev-server.')
      .option('-h, --host <host>', 'Server host.')
      .option('-p, --port <port>', 'Server port.')
      .option('--open', 'Open the dev server in your browser when building succeeded.')
      .action((options) => {
        console.log('dev: ', options)
      })
    
    cli
      .option('--pages <path>', 'The directory to page files.')
      .option('--components <path>', 'The directory to page files.')
      .option('--layouts <path>', 'The directory to layout files.')
      .option('--dir <path>', 'The directory to output files.')
      .option('--clean', 'Disable clean output directory before building.')
      .option('-f, --format <type>', 'Specify the output format.')
    
    cli.help()
    
    cli.parse()
    

    运行 yarn dev -f cjs src/cli.ts 来让 microbundle 监控并编译源文件,这时就可以通过路径执行命令行程序:

    ➜  node .\dist\cli.js dev -p 8080     
    dev:  { '--': [], p: 8080, port: 8080 }
    ➜  node .\dist\cli.js build -f beautify --clean false
    build:  { '--': [], f: 'beautify', clean: false, format: 'beautify' }
    

    命令行的框架已经搭建完成,现在就可以正式开始开发了。


    起源地下载网 » 开源笔记:动手写静态网页开发框架 - 其二

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元