Skip to content

Commit

Permalink
allow json5 package files + option to not send signal after npm_install
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Nov 5, 2023
1 parent 1d7420a commit abd4cb4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 43 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ You can continue to load other resources such as CSS files as before using the `
NPM.JS dependencies
-------------------

1. Add package.json files into one or more of your apps. All package.json files will be merged.
1. Add package.json or package.json5 files into one or more of your apps. All package files will be merged.

2. Import in your JS files from any of the npm modules specified in your package.json files.
2. Import in your JS files from any of the npm modules specified in your package files.

3. Run `./manage.py transpile`.

Expand Down
16 changes: 10 additions & 6 deletions npm_mjs/management/commands/create_package_json.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
import os

import pyjson5
from django.apps import apps as django_apps
from django.core.management.base import BaseCommand
from json_minify import json_minify

from npm_mjs.paths import TRANSPILE_CACHE_PATH

Expand Down Expand Up @@ -34,11 +34,15 @@ def handle(self, *args, **options):
package = {}
configs = django_apps.get_app_configs()
for config in configs:
app_package_path = os.path.join(config.path, "package.json")
try:
with open(app_package_path) as data_file:
data = json.loads(json_minify(data_file.read()))
except OSError:
json5_package_path = os.path.join(config.path, "package.json5")
json_package_path = os.path.join(config.path, "package.json")
if os.path.isfile(json5_package_path):
with open(json5_package_path) as data_file:
data = pyjson5.decode(data_file.read())
elif os.path.isfile(json_package_path):
with open(json_package_path) as data_file:
data = json.loads(data_file.read())
else:
continue
deep_merge_dicts(package, data)
if not os.path.exists(TRANSPILE_CACHE_PATH):
Expand Down
19 changes: 12 additions & 7 deletions npm_mjs/management/commands/npm_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from npm_mjs.tools import set_last_run


def install_npm(force, stdout):
def install_npm(force, stdout, post_npm_signal=True):
change_times = [0]
for path in SETTINGS_PATHS:
change_times.append(os.path.getmtime(path))
Expand Down Expand Up @@ -59,7 +59,8 @@ def install_npm(force, stdout):
if node_version > 16:
os.environ["NODE_OPTIONS"] = "--openssl-legacy-provider"
call(["npm", "install"], cwd=TRANSPILE_CACHE_PATH)
signals.post_npm_install.send(sender=None)
if post_npm_signal:
signals.post_npm_install.send(sender=None)
npm_install = True
return npm_install

Expand All @@ -76,9 +77,13 @@ def add_arguments(self, parser):
help="Force npm install even if no change is detected.",
)

parser.add_argument(
"--nosignal",
action="store_false",
dest="post_npm_signal",
default=True,
help="Send a signal after finishing npm install.",
)

def handle(self, *args, **options):
if options["force"]:
force = True
else:
force = False
install_npm(force, self.stdout)
install_npm(options["force"], self.stdout, options["post_npm_signal"])
27 changes: 18 additions & 9 deletions npm_mjs/management/commands/webpack.config.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,34 @@ const baseRule = {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
plugins: ["@babel/plugin-syntax-dynamic-import", "@babel/plugin-proposal-optional-chaining"]
plugins: [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-optional-chaining"
]
}
}
}

const predefinedVariables = {
"transpile_VERSION": transpile.VERSION
transpile_VERSION: transpile.VERSION
}

if (settings.DEBUG) {
baseRule.exclude = /node_modules/
predefinedVariables.staticUrl = `(url => ${JSON.stringify(settings.STATIC_URL)} + url)`
} else if (settings.STATICFILES_STORAGE !== "npm_mjs.storage.ManifestStaticFilesStorage") {
predefinedVariables.staticUrl = `(url => ${JSON.stringify(settings.STATIC_URL)} + url + "?v=" + ${transpile.VERSION})`
predefinedVariables.staticUrl = `(url => ${JSON.stringify(
settings.STATIC_URL
)} + url)`
} else if (
settings.STATICFILES_STORAGE !==
"npm_mjs.storage.ManifestStaticFilesStorage"
) {
predefinedVariables.staticUrl = `(url => ${JSON.stringify(
settings.STATIC_URL
)} + url + "?v=" + ${transpile.VERSION})`
}

module.exports = { // eslint-disable-line no-undef
module.exports = {
// eslint-disable-line no-undef
mode: settings.DEBUG ? "development" : "production",
module: {
rules: [baseRule]
Expand All @@ -35,8 +46,6 @@ module.exports = { // eslint-disable-line no-undef
chunkFilename: transpile.VERSION + "-[id].js",
publicPath: transpile.BASE_URL
},
plugins: [
new webpack.DefinePlugin(predefinedVariables)
],
plugins: [new webpack.DefinePlugin(predefinedVariables)],
entry: transpile.ENTRIES
}
18 changes: 0 additions & 18 deletions npm_mjs/package.json

This file was deleted.

18 changes: 18 additions & 0 deletions npm_mjs/package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Django-npm-mjs will combine this file with package.json files in other installed
// apps before executing npm commands. Different from a regular package.json, comments
// are allowed in this file.
{
description: "Install dependencies for ES6 transpilation",
private: true,
dependencies: {
"@babel/preset-env": "^7.23.2",
"@babel/core": "7.23.2",
"@babel/cli": "7.23.0",
"@babel/plugin-syntax-dynamic-import": "7.8.3",
"@babel/plugin-transform-optional-chaining": "^7.23.0",
"@babel/plugin-transform-template-literals": "^7.18.6",
"babel-loader": "9.1.3",
webpack: "5.89.0",
"webpack-cli": "5.1.4",
},
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Django >= 3.2
JSON_minify == 0.3.0
pyjson5 == 1.6.4

0 comments on commit abd4cb4

Please sign in to comment.