Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browser: support rewriting URLs #8

Open
dominicbarnes opened this issue Jul 1, 2017 · 2 comments
Open

browser: support rewriting URLs #8

dominicbarnes opened this issue Jul 1, 2017 · 2 comments

Comments

@dominicbarnes
Copy link
Member

From @dominicbarnes on September 14, 2016 3:37

I can easily forsee an HTML file that references a file that gets transpiled, so then the extension changes. There should be an option to rewrite those refs during prewrite.

Copied from original issue: makojs/html#8

@dominicbarnes
Copy link
Member Author

From @matthewmueller on November 18, 2016 7:39

+1, I needed this for doing static builds, I ended up doing it outside but it'd be better if it was an option inside here, so there's no need to parse twice.

FWIW, this is what I'm using. I guess the configuration part would be the pagedir stuff. But a lot of this could be simplified if it was inside the plugin. Oh also using [email protected], 2.x API doesn't make any sense to me :-|

mako.dependencies('html', function (file, build) {
  if (!file.dependencies().length) return

  const ast = htmldeps.parse(file.contents.toString())
  const deps = htmldeps.extract(ast)
  const pages = params.pages

  deps.forEach(function (dep) {
    if (!dep.path) return
    const abs = path.resolve(file.dirname, dep.path)
    const asset = build.tree.findFile(abs)
    if (!asset) return

    const pagedir = path.relative(pages, asset.dirname)
    const parts = pagedir.split('/')
    const dirname = parts.pop()
    const remaining = parts.join('/')
    const relative = dirname === 'index' ? remaining : pagedir
    const script = path.join('/', relative, asset.basename)

    if (dep.attr && dep.attr.value) {
      dep.attr.value = script
    }
  })

  const html = htmldeps.stringify(ast)
  file.contents = new Buffer(html)
})

@dominicbarnes
Copy link
Member Author

From @matthewmueller on November 18, 2016 9:49

Actually re-writing this plugin now, should be much more powerful. Also reducing it's dependency tree a bit :-)

@dominicbarnes dominicbarnes changed the title Support rewriting URLs browser: support rewriting URLs Jul 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant