Skip to content

Commit abd4cb4

Browse files
committed
allow json5 package files + option to not send signal after npm_install
1 parent 1d7420a commit abd4cb4

File tree

7 files changed

+61
-43
lines changed

7 files changed

+61
-43
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ You can continue to load other resources such as CSS files as before using the `
6363
NPM.JS dependencies
6464
-------------------
6565

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

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

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

npm_mjs/management/commands/create_package_json.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
22
import os
33

4+
import pyjson5
45
from django.apps import apps as django_apps
56
from django.core.management.base import BaseCommand
6-
from json_minify import json_minify
77

88
from npm_mjs.paths import TRANSPILE_CACHE_PATH
99

@@ -34,11 +34,15 @@ def handle(self, *args, **options):
3434
package = {}
3535
configs = django_apps.get_app_configs()
3636
for config in configs:
37-
app_package_path = os.path.join(config.path, "package.json")
38-
try:
39-
with open(app_package_path) as data_file:
40-
data = json.loads(json_minify(data_file.read()))
41-
except OSError:
37+
json5_package_path = os.path.join(config.path, "package.json5")
38+
json_package_path = os.path.join(config.path, "package.json")
39+
if os.path.isfile(json5_package_path):
40+
with open(json5_package_path) as data_file:
41+
data = pyjson5.decode(data_file.read())
42+
elif os.path.isfile(json_package_path):
43+
with open(json_package_path) as data_file:
44+
data = json.loads(data_file.read())
45+
else:
4246
continue
4347
deep_merge_dicts(package, data)
4448
if not os.path.exists(TRANSPILE_CACHE_PATH):

npm_mjs/management/commands/npm_install.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from npm_mjs.tools import set_last_run
1717

1818

19-
def install_npm(force, stdout):
19+
def install_npm(force, stdout, post_npm_signal=True):
2020
change_times = [0]
2121
for path in SETTINGS_PATHS:
2222
change_times.append(os.path.getmtime(path))
@@ -59,7 +59,8 @@ def install_npm(force, stdout):
5959
if node_version > 16:
6060
os.environ["NODE_OPTIONS"] = "--openssl-legacy-provider"
6161
call(["npm", "install"], cwd=TRANSPILE_CACHE_PATH)
62-
signals.post_npm_install.send(sender=None)
62+
if post_npm_signal:
63+
signals.post_npm_install.send(sender=None)
6364
npm_install = True
6465
return npm_install
6566

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

80+
parser.add_argument(
81+
"--nosignal",
82+
action="store_false",
83+
dest="post_npm_signal",
84+
default=True,
85+
help="Send a signal after finishing npm install.",
86+
)
87+
7988
def handle(self, *args, **options):
80-
if options["force"]:
81-
force = True
82-
else:
83-
force = False
84-
install_npm(force, self.stdout)
89+
install_npm(options["force"], self.stdout, options["post_npm_signal"])

npm_mjs/management/commands/webpack.config.template.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,34 @@ const baseRule = {
99
loader: "babel-loader",
1010
options: {
1111
presets: ["@babel/preset-env"],
12-
plugins: ["@babel/plugin-syntax-dynamic-import", "@babel/plugin-proposal-optional-chaining"]
12+
plugins: [
13+
"@babel/plugin-syntax-dynamic-import",
14+
"@babel/plugin-proposal-optional-chaining"
15+
]
1316
}
1417
}
1518
}
1619

1720
const predefinedVariables = {
18-
"transpile_VERSION": transpile.VERSION
21+
transpile_VERSION: transpile.VERSION
1922
}
2023

2124
if (settings.DEBUG) {
2225
baseRule.exclude = /node_modules/
23-
predefinedVariables.staticUrl = `(url => ${JSON.stringify(settings.STATIC_URL)} + url)`
24-
} else if (settings.STATICFILES_STORAGE !== "npm_mjs.storage.ManifestStaticFilesStorage") {
25-
predefinedVariables.staticUrl = `(url => ${JSON.stringify(settings.STATIC_URL)} + url + "?v=" + ${transpile.VERSION})`
26+
predefinedVariables.staticUrl = `(url => ${JSON.stringify(
27+
settings.STATIC_URL
28+
)} + url)`
29+
} else if (
30+
settings.STATICFILES_STORAGE !==
31+
"npm_mjs.storage.ManifestStaticFilesStorage"
32+
) {
33+
predefinedVariables.staticUrl = `(url => ${JSON.stringify(
34+
settings.STATIC_URL
35+
)} + url + "?v=" + ${transpile.VERSION})`
2636
}
2737

28-
module.exports = { // eslint-disable-line no-undef
38+
module.exports = {
39+
// eslint-disable-line no-undef
2940
mode: settings.DEBUG ? "development" : "production",
3041
module: {
3142
rules: [baseRule]
@@ -35,8 +46,6 @@ module.exports = { // eslint-disable-line no-undef
3546
chunkFilename: transpile.VERSION + "-[id].js",
3647
publicPath: transpile.BASE_URL
3748
},
38-
plugins: [
39-
new webpack.DefinePlugin(predefinedVariables)
40-
],
49+
plugins: [new webpack.DefinePlugin(predefinedVariables)],
4150
entry: transpile.ENTRIES
4251
}

npm_mjs/package.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

npm_mjs/package.json5

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Django-npm-mjs will combine this file with package.json files in other installed
2+
// apps before executing npm commands. Different from a regular package.json, comments
3+
// are allowed in this file.
4+
{
5+
description: "Install dependencies for ES6 transpilation",
6+
private: true,
7+
dependencies: {
8+
"@babel/preset-env": "^7.23.2",
9+
"@babel/core": "7.23.2",
10+
"@babel/cli": "7.23.0",
11+
"@babel/plugin-syntax-dynamic-import": "7.8.3",
12+
"@babel/plugin-transform-optional-chaining": "^7.23.0",
13+
"@babel/plugin-transform-template-literals": "^7.18.6",
14+
"babel-loader": "9.1.3",
15+
webpack: "5.89.0",
16+
"webpack-cli": "5.1.4",
17+
},
18+
}

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Django >= 3.2
2-
JSON_minify == 0.3.0
2+
pyjson5 == 1.6.4

0 commit comments

Comments
 (0)