Skip to content

Commit

Permalink
feat(core): add support for netlify-cms extensions
Browse files Browse the repository at this point in the history
Add `extensionsDir` option
Convert import calls to require in webpack config
Update test snapshots
  • Loading branch information
medfreeman committed Sep 6, 2017
1 parent 0e7d7bb commit f670cf3
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 108 deletions.
16 changes: 13 additions & 3 deletions lib/entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import CMS from "netlify-cms";
import CSS from "netlify-cms/dist/cms.css";
/* global REQUIRE_EXTENSIONS */
/* eslint-disable import/order */
function requireAll(r) {
r.keys().forEach(r);
}

export default {
const CMS = require("netlify-cms");

if (REQUIRE_EXTENSIONS) {
requireAll(require.context("extensions/", true, /\.js$/));
}
const CSS = require("netlify-cms/dist/cms.css");

module.exports = {
CMS,
CSS
};
15 changes: 12 additions & 3 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const WEBPACK_NETLIFY_COMPILER_NAME = "netlify-cms";
const DEFAULTS = {
adminPath: "admin",
adminTitle: "Content Manager",
extensionsDir: "netlify-cms",
cmsConfig: {
media_folder: "static/uploads"
}
Expand All @@ -48,12 +49,18 @@ export default function NetlifyCmsModule(moduleOptions) {
);
};

const getWebpackNetlifyConfig = function(builder, adminPath, adminTitle) {
const getWebpackNetlifyConfig = function(
builder,
adminPath,
adminTitle,
extensionsDir
) {
return webpackNetlifyConfig.call(
builder,
WEBPACK_NETLIFY_COMPILER_NAME,
adminPath,
adminTitle
adminTitle,
extensionsDir
);
};

Expand All @@ -78,6 +85,7 @@ export default function NetlifyCmsModule(moduleOptions) {
const options = getOptions();
const ADMIN_PATH = options.adminPath.replace(/\/?$/, "/");
const ADMIN_TITLE = options.adminTitle;
const EXTENSIONS_DIR = options.extensionsDir;
const DIST_DIR = getDistDir(ADMIN_PATH);

// This will be called once when builder started
Expand All @@ -87,7 +95,8 @@ export default function NetlifyCmsModule(moduleOptions) {
const webpackConfig = getWebpackNetlifyConfig(
builder,
ADMIN_PATH,
ADMIN_TITLE
ADMIN_TITLE,
EXTENSIONS_DIR
);

webpackConfig.plugins.push({
Expand Down
19 changes: 17 additions & 2 deletions src/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resolve } from "path";
import { existsSync } from "fs";
import { join, resolve } from "path";

import { Utils } from "nuxt";
/* eslint-disable import/no-extraneous-dependencies */
Expand All @@ -7,7 +8,13 @@ import webpack from "webpack";
import HTMLPlugin from "html-webpack-plugin";
import ExtractTextPlugin from "extract-text-webpack-plugin";

export default function webpackNetlifyCmsConfig(name, urlPath, pageTitle) {
export default function webpackNetlifyCmsConfig(
name,
urlPath,
pageTitle,
extensionsDir
) {
const EXTENSIONS_DIR = join(this.options.srcDir, extensionsDir);
const config = {
name,
entry: resolve(__dirname, "../lib/entry.js"),
Expand All @@ -30,13 +37,21 @@ export default function webpackNetlifyCmsConfig(name, urlPath, pageTitle) {
}
]
},
resolve: {
alias: {
extensions: EXTENSIONS_DIR
}
},
plugins: [
new HTMLPlugin({
title: pageTitle,
filename: "index.html",
template: resolve(__dirname, "../lib/template", "index.html"),
inject: true,
chunksSortMode: "dependency"
}),
new webpack.DefinePlugin({
REQUIRE_EXTENSIONS: existsSync(EXTENSIONS_DIR) ? true : false
})
]
};
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/dev.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[`module dev mode admin 1`] = `
<title>Content Manager</title>
</head>
<body>
<script type=\\"text/javascript\\" src=\\"/admin/bundle.b3269f80518b70bb145e.js\\"></script></body>
<script type=\\"text/javascript\\" src=\\"/admin/bundle.a47fc32f4be2190c8f24.js\\"></script></body>
</html>
"
`;
2 changes: 1 addition & 1 deletion test/__snapshots__/module.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[`module admin 1`] = `
<title>Content Manager</title>
</head>
<body>
<script type=\\"text/javascript\\" src=\\"/admin/bundle.4853fc2928590d1bc9cb.js\\"></script></body>
<script type=\\"text/javascript\\" src=\\"/admin/bundle.a408a93c790a7fcc10d1.js\\"></script></body>
</html>
"
`;
Loading

0 comments on commit f670cf3

Please sign in to comment.