Skip to content

Commit

Permalink
feat(nuxt-module)!: extend nuxt config (#1428)
Browse files Browse the repository at this point in the history
* feat(nuxt-module): extend nuxt config method with default config

* refactor(cli): remove unused nuxt.config.js upgrade after init

* docs: update docs with feature info
  • Loading branch information
patzick authored Apr 15, 2021
1 parent eb3b8bf commit 381a32a
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 181 deletions.
3 changes: 1 addition & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ The v0.5.x of shopware-pwa is also available to play on at:

---


<div class="flex-container">

<div class="md-50">
Expand Down Expand Up @@ -54,7 +53,7 @@ Guides for functional concepts like CMS or Checkout

### [Operations](/landing/operations) <Badge text="new" type="info"/>

Best practices, release notes, migration guides, guidelines for setup, hosting and deployment
Best practices, release notes, [migration guides](/landing/operations/migrations/), guidelines for setup, hosting and deployment

### [Resources](/landing/resources/)

Expand Down
6 changes: 4 additions & 2 deletions docs/landing/cookbook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ Let's try to install a [Google Tag Manager for Nuxt.js](https://www.npmjs.com/pa
2. Add @nuxtjs/gtm to the _modules_ section of `nuxt.config.js`
```js
export default {
import { extendNuxtConfig } from '@shopware-pwa/nuxt-module'
export default extendNuxtConfig({
modules: [
...,
"@nuxtjs/gtm"
Expand All @@ -257,7 +259,7 @@ export default {
debug: true, // for dev,
pageTracking: true, // push route change event automatically
},
};
});
```
3. Now the GTM module is enabled and ready to use
25 changes: 24 additions & 1 deletion docs/landing/operations/migrations/0.7.x_to_0.8.x.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Migrate version 0.7.x to 0.8.x <Badge text="canary" type="warning"/>

**BREAKING CHANGE**: Vuex has been turned off and replaced with `useSharedState` composable.
## Vuex replacement <Badge text="BREAKING CHANGE" type="error"/>

Vuex has been turned off and replaced with `useSharedState` composable.
If you want to persist your data between Server and Client, then setting `sharedRef` from `useSharedState` on the server side will cause that this ref is also filled in client side.

`useSharedState` is also adding very helpful method, which is invoked if Ref value is not setted.
Expand Down Expand Up @@ -28,3 +30,24 @@ We can assume this code is in some component on MyAccount page. We have two scen
2. Client goes into Home `/` and clicks MyAccount icon. Then we didn't had the chance to get this data on Server, so we'll call code from `preloadRef`.

This way we always call for API data, but we will not repeat API calls when we already have this value.

## Nuxt config changes <Badge text="BREAKING CHANGE" type="error"/>

In order to provide better experience we want to keep your `nuxt.config.js` file as clean as possible.
You can now safely remove all things you haven't putted there yourself. Newly generated `nuxt.config.js` file can look like this

```js
import { extendNuxtConfig } from "@shopware-pwa/nuxt-module";

export default extendNuxtConfig({
head: {
title: "shopware-pwa-project",
meta: [{ hid: "description", name: "description", content: "" }],
link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }],
},
});
```

::: tip
It will not break your current project, but not using `extendNuxtConfig` method can cause troubles in the future. That's why this is marked as a breaking change
:::
1 change: 0 additions & 1 deletion packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ module.exports = {
await run(`yarn link ${localCoreDevPackages.join(" ")}`);
}

await toolbox.updateNuxtConfigFile();
await run("yarn --check-files");
updateConfigSpinner.succeed();

Expand Down
121 changes: 0 additions & 121 deletions packages/cli/src/extensions/nuxt-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,127 +45,6 @@ module.exports = (toolbox: GluegunToolbox) => {
}
};

/**
* Updates nuxt.config.js with configuration for Shopwre PWA
* TODO: we can remove this method after: https://github.com/vuestorefront/shopware-pwa/issues/1403
*/
toolbox.updateNuxtConfigFile = async () => {
// Add buildModules
const VSFbuildModuleExist = await toolbox.patching.exists(
"nuxt.config.js",
`'@shopware-pwa/nuxt-module'`
);
if (!VSFbuildModuleExist) {
await toolbox.patching.patch("nuxt.config.js", {
insert: `
'@shopware-pwa/nuxt-module',
`,
after: "buildModules: [",
});
}

// Add server to 0.0.0.0
const serverSectionExist = await toolbox.patching.exists(
"nuxt.config.js",
`server: {`
);
if (!serverSectionExist) {
await toolbox.patching.patch("nuxt.config.js", {
insert: `
server: {
port: process.env.PORT || 3000,
host: process.env.HOST || '0.0.0.0'
},`,
after: "export default {",
});
}

// Add shopware-pwa meta tag
const headSectionExist = await toolbox.patching.exists(
"nuxt.config.js",
`head: {`
);
if (headSectionExist) {
await toolbox.patching.patch("nuxt.config.js", {
insert: `\n { hid: 'project-type', name: 'project-type', content: 'shopware-pwa' },`,
after: "meta: [",
});
}

// Add global SCSS file to config
const isGlobalScssFileAdded = await toolbox.patching.exists(
"nuxt.config.js",
`~assets/scss/main.scss`
);
if (!isGlobalScssFileAdded) {
await toolbox.patching.patch("nuxt.config.js", {
insert: `
'~assets/scss/main.scss',`,
after: "css: [",
});
}

// ignore .yalc
const yalcIgnoreExist = await toolbox.patching.exists(
".gitignore",
`.yalc`
);
if (!yalcIgnoreExist) {
await toolbox.patching.append(".gitignore", ".yalc\nyalc.lock\n");
}
const swPluginsIgnoreExist = await toolbox.patching.exists(
".gitignore",
`.shopware-pwa`
);
if (!swPluginsIgnoreExist) {
await toolbox.patching.append(".gitignore", ".shopware-pwa\n");
}

// Ignore rootDirectory static folder: https://github.com/DivanteLtd/shopware-pwa/issues/1047
const rootStaticDirectoryIgnored = await toolbox.patching.exists(
".gitignore",
`/static`
);
if (!rootStaticDirectoryIgnored) {
await toolbox.patching.append(".gitignore", "/static");
}

// Add chokidar flag
const configChokidarFlag = await toolbox.patching.exists(
"nuxt.config.js",
`CHOKIDAR_USEPOLLING`
);
if (!configChokidarFlag) {
const configEnvSection = await toolbox.patching.exists(
"nuxt.config.js",
"env:"
);
await toolbox.patching.patch("nuxt.config.js", {
insert: !configEnvSection
? `
env: {
CHOKIDAR_USEPOLLING: process.env.NODE_ENV == "production" ? 0 : 1,
},`
: `
CHOKIDAR_USEPOLLING: process.env.NODE_ENV == "production" ? 0 : 1,`,
after: !configEnvSection ? "export default {" : "env: {",
});
}

// Add telemetry flag
const configTelemetryFlag = await toolbox.patching.exists(
"nuxt.config.js",
`telemetry:`
);
if (!configTelemetryFlag) {
await toolbox.patching.patch("nuxt.config.js", {
insert: `
telemetry: false,`,
after: "export default {",
});
}
};

/**
* Generates template files using ejs.
*/
Expand Down
58 changes: 5 additions & 53 deletions packages/cli/src/templates/project-template/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
export default {
telemetry: false,
env: {
CHOKIDAR_USEPOLLING: process.env.NODE_ENV == 'production' ? 0 : 1,
},
server: {
port: process.env.PORT || 3000,
host: process.env.HOST || '0.0.0.0',
},
// Global page headers: https://go.nuxtjs.dev/config-head
import { extendNuxtConfig } from '@shopware-pwa/nuxt-module'

export default extendNuxtConfig({
head: {
title: 'shopware-pwa-project',
meta: [
{ hid: 'project-type', name: 'project-type', content: 'shopware-pwa' },
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' },
],
meta: [{ hid: 'description', name: 'description', content: '' }],
link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
},

// Global CSS: https://go.nuxtjs.dev/config-css
css: ['~assets/scss/main.scss'],

// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [],

// Auto import components: https://go.nuxtjs.dev/config-components
components: false,

// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: ['@shopware-pwa/nuxt-module'],

// Modules: https://go.nuxtjs.dev/config-modules
modules: [
// https://go.nuxtjs.dev/axios
'@nuxtjs/axios',
// https://go.nuxtjs.dev/pwa
'@nuxtjs/pwa',
],

// Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: {},

// PWA module configuration: https://go.nuxtjs.dev/pwa
pwa: {
manifest: {
lang: 'en',
},
workbox: {
offlineStrategy: 'StaleWhileRevalidate',
},
},

// Build Configuration: https://go.nuxtjs.dev/config-build
build: {},
}
})
71 changes: 71 additions & 0 deletions packages/nuxt-module/__tests__/__snapshots__/config.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`nuxt-module - extendNuxtConfig should return default config 1`] = `
Object {
"axios": Object {},
"build": Object {},
"buildModules": Array [
"@shopware-pwa/nuxt-module",
],
"components": false,
"css": Array [
"~assets/scss/main.scss",
],
"env": Object {
"CHOKIDAR_USEPOLLING": "1",
"EXPERIMENTAL_IMAGE_PROCESSING_SERVER": "",
},
"head": Object {
"link": Array [
Object {
"href": "/favicon.ico",
"rel": "icon",
"type": "image/x-icon",
},
],
"meta": Array [
Object {
"content": "shopware-pwa",
"hid": "project-type",
"name": "project-type",
},
Object {
"content": "shopware-pwa",
"hid": "project-type",
"name": "project-type",
},
Object {
"charset": "utf-8",
},
Object {
"content": "width=device-width, initial-scale=1",
"name": "viewport",
},
Object {
"content": "",
"hid": "description",
"name": "description",
},
],
"title": "shopware-pwa-project",
},
"modules": Array [
"@nuxtjs/axios",
"@nuxtjs/pwa",
],
"plugins": Array [],
"pwa": Object {
"manifest": Object {
"lang": "en",
},
"workbox": Object {
"offlineStrategy": "StaleWhileRevalidate",
},
},
"server": Object {
"host": "0.0.0.0",
"port": 3000,
},
"telemetry": false,
}
`;
53 changes: 53 additions & 0 deletions packages/nuxt-module/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { extendNuxtConfig } from "../src/index";

describe("nuxt-module - extendNuxtConfig", () => {
it("should return default config", () => {
const result = extendNuxtConfig({});
expect(result).toMatchSnapshot();
});

it("should overwrite server port", () => {
const result = extendNuxtConfig({
server: {
port: 2000,
},
});
expect(result.server?.port).toEqual(2000);
});

it("should add new build module", () => {
const result = extendNuxtConfig({
buildModules: ["some-new-module"],
});
expect(result.buildModules).toEqual([
"@shopware-pwa/nuxt-module",
"some-new-module",
]);
});

describe("env properties", () => {
const ORIGINAL_ENV = process.env;

beforeEach(() => {
jest.resetModules(); // clears the cache
process.env = { ...ORIGINAL_ENV };
});

afterAll(() => {
process.env = ORIGINAL_ENV;
});

it("should pass env properties to config", () => {
process.env.NODE_ENV = "production";
process.env.EXPERIMENTAL_IMAGE_PROCESSING_SERVER = "https://example.com";

const { extendNuxtConfig } = require("../src/index");
const result = extendNuxtConfig({});

expect(result.env?.CHOKIDAR_USEPOLLING).toEqual("0");
expect(result.env?.EXPERIMENTAL_IMAGE_PROCESSING_SERVER).toEqual(
"https://example.com"
);
});
});
});
Loading

1 comment on commit 381a32a

@vercel
Copy link

@vercel vercel bot commented on 381a32a Apr 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.