Skip to content

Commit

Permalink
support uncompiled ESM projects (no NPM or r.js)
Browse files Browse the repository at this point in the history
(see #111)
  • Loading branch information
sheppard committed Sep 29, 2020
1 parent 392587e commit 58290a7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
30 changes: 11 additions & 19 deletions build/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,24 @@ def build(ctx, config, version):
\b
Runs the following in order:
wq init (if using wq optimize)
wq init (if using r.js)
wq setversion
wq collectjson (if configured)
wq scss (if configured)
wq mustache (if configured)
\b
Followed by either:
wq optimize + wq babel + wq appcache
Or:
npm build
npm build (if using npm)
wq optimize (if using r.js)
wq babel (if configured & using r.js)
wq appcache (if configured & using r.js)
"""

has_package_json = os.path.exists(
os.path.join(os.path.dirname(config.filename), 'package.json')
)
if has_package_json:
if 'optimize' in config:
raise click.UsageError(
"optimize section is not used for npm build"
)
else:
if 'optimize' not in config:
raise click.UsageError(
"optimize section not found in %s" % config.filename
)
if has_package_json and 'optimize' in config:
raise click.UsageError(
"optimize section is not used for npm build"
)

def run(command, **kwargs):
confs = config.get(command.name, {})
Expand All @@ -55,7 +47,7 @@ def run(command, **kwargs):
conf.update(kwargs)
ctx.invoke(command, **conf)

if not has_package_json:
if 'optimize' in config:
run(init)

# Save version information
Expand All @@ -70,7 +62,7 @@ def run(command, **kwargs):
['npm', 'build'],
cwd=os.path.abspath(os.path.dirname(config.filename))
)
else:
elif 'optimize' in config:
# Compile Javascript / CSS (using r.js)
ctx.invoke(optimize)

Expand Down
32 changes: 24 additions & 8 deletions build/setversion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from wq.core import wq
import click
import re


@wq.command()
Expand All @@ -9,6 +10,8 @@
help="Name of text file (default is version.txt)"
)
@click.option('--jsout', help="Name of an AMD module (e.g. myapp/version.js)")
@click.option('--esm', help="Name of an ESM module (e.g. myapp/version.js)")
@click.option('--package', help="Path to package.json")
@click.argument('version')
def setversion(**conf):
"""
Expand All @@ -27,16 +30,29 @@ def setversion(**conf):
vtxt.write(version)
vtxt.close()

if conf['jsout']:
if conf['esm'] or conf['jsout']:
# Update version.js
vjs = open(conf['jsout'], 'w')
vjs.write(VERSIONJS_TMPL % version)
vjs.close()
click.echo('%s: %s' % (conf['jsout'], version))
if conf['esm']:
js_file = conf['esm']
js_tmpl = """export default "%s";"""
else:
js_file = conf['jsout']
js_tmpl = """define(function(){return "%s";});"""
with open(js_file, 'w') as f:
f.write(js_tmpl % version)
click.echo('%s: %s' % (js_file, version))
else:
click.echo('Application version: %s' % version)

return version

if conf['package']:
with open(conf['package']) as f:
content = f.read()
content = re.sub(
'"version": "[^"]+"',
'"version": "%s"' % version,
content
)
with open(conf['package'], 'w') as f:
f.write(content)

VERSIONJS_TMPL = """define(function(){return "%s";});"""
return version

0 comments on commit 58290a7

Please sign in to comment.